provisioner(converge): add bootstrap_command

This commit is contained in:
Brian Hicks 2016-12-28 08:45:19 -06:00
parent 843731d98d
commit 5d935767f0
No known key found for this signature in database
GPG Key ID: FF1F407C0D3C2430
3 changed files with 51 additions and 18 deletions

View File

@ -7,7 +7,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"net/http"
"strings" "strings"
@ -28,8 +27,9 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
// Bootstrapping // Bootstrapping
Bootstrap bool `mapstructure:"bootstrap"` Bootstrap bool `mapstructure:"bootstrap"`
Version string `mapstructure:"version"` Version string `mapstructure:"version"`
BootstrapCommand string `mapstructure:"bootstrap_command"`
// Modules // Modules
ModuleDirs []ModuleDir `mapstructure:"module_dirs"` ModuleDirs []ModuleDir `mapstructure:"module_dirs"`
@ -64,7 +64,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
Interpolate: true, Interpolate: true,
InterpolateContext: &p.config.ctx, InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{ InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{"execute_command"}, Exclude: []string{
"execute_command",
"bootstrap_command",
},
}, },
}, },
raws..., raws...,
@ -87,6 +90,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
p.config.ExecuteCommand = "cd {{.WorkingDirectory}} && {{if .Sudo}}sudo {{end}}converge apply --local --log-level=WARNING --paramsJSON '{{.ParamsJSON}}' {{.Module}}" p.config.ExecuteCommand = "cd {{.WorkingDirectory}} && {{if .Sudo}}sudo {{end}}converge apply --local --log-level=WARNING --paramsJSON '{{.ParamsJSON}}' {{.Module}}"
} }
if p.config.BootstrapCommand == "" {
p.config.BootstrapCommand = "curl -s https://get.converge.sh | sh {{if ne .Version \"\"}}-s -- -v {{.Version}}{{end}}"
}
// validate version // validate version
if !versionRegex.Match([]byte(p.config.Version)) { if !versionRegex.Match([]byte(p.config.Version)) {
return fmt.Errorf("Invalid Converge version %q specified. Valid versions include only letters, numbers, dots, and dashes", p.config.Version) return fmt.Errorf("Invalid Converge version %q specified. Valid versions include only letters, numbers, dots, and dashes", p.config.Version)
@ -133,21 +140,14 @@ func (p *Provisioner) maybeBootstrap(ui packer.Ui, comm packer.Communicator) err
} }
ui.Message("bootstrapping converge") ui.Message("bootstrapping converge")
bootstrap, err := http.Get("https://get.converge.sh") p.config.ctx.Data = struct {
Version string
}{
Version: p.config.Version,
}
command, err := interpolate.Render(p.config.BootstrapCommand, &p.config.ctx)
if err != nil { if err != nil {
return fmt.Errorf("Error downloading bootstrap script: %s", err) // TODO: is github.com/pkg/error allowed? return fmt.Errorf("Could not interpolate bootstrap command: %s", err)
}
if err := comm.Upload("/tmp/install-converge.sh", bootstrap.Body, nil); err != nil {
return fmt.Errorf("Error uploading script: %s", err)
}
if err := bootstrap.Body.Close(); err != nil {
return fmt.Errorf("Error getting bootstrap script: %s", err)
}
// construct command
command := "/bin/sh /tmp/install-converge.sh"
if p.config.Version != "" {
command += " -v " + p.config.Version
} }
var out bytes.Buffer var out bytes.Buffer

View File

@ -60,6 +60,21 @@ func TestProvisionerPrepare(t *testing.T) {
t.Fatal("execute command unexpectedly blank") t.Fatal("execute command unexpectedly blank")
} }
}) })
t.Run("bootstrap_command", func(t *testing.T) {
var p Provisioner
config := testConfig()
delete(config, "bootstrap_command")
if err := p.Prepare(config); err != nil {
t.Fatalf("err: %s", err)
}
if p.config.BootstrapCommand == "" {
t.Fatal("bootstrap command unexpectedly blank")
}
})
}) })
t.Run("validate", func(t *testing.T) { t.Run("validate", func(t *testing.T) {

View File

@ -60,6 +60,10 @@ Optional parameters:
- `prevent_sudo` (bool) - stop Converge from running with adminstrator - `prevent_sudo` (bool) - stop Converge from running with adminstrator
privileges via sudo privileges via sudo
- `bootstrap_command` (string) - the command used to bootstrap Converge. This
has various
[configuration template variables](/docs/templates/configuration-templates.html) available.
### Module Directories ### Module Directories
The provisioner can transfer module directories to the remote host for The provisioner can transfer module directories to the remote host for
@ -94,3 +98,17 @@ contain various template variables:
- `Sudo` - the opposite of `prevent_sudo` from the configuration. - `Sudo` - the opposite of `prevent_sudo` from the configuration.
- `ParamsJSON` - The unquoted JSONified form of `params` from the configuration. - `ParamsJSON` - The unquoted JSONified form of `params` from the configuration.
- `Module` - `module` from the configuration. - `Module` - `module` from the configuration.
### Bootstrap Command
By default, Packer uses the following command to bootstrap Converge:
``` {.liquid}
curl -s https://get.converge.sh | sh {{if ne .Version ""}}-s -- -v {{.Version}}{{end}}
```
This command can be customized using the `bootstrap_command` configuration. As you
can see from the default values above, the value of this configuration can
contain various template variables:
- `Version` - `version` from the configuration.