From 5d935767f04c7374eb0fbc833e6b332f6bc4ba43 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Wed, 28 Dec 2016 08:45:19 -0600 Subject: [PATCH] provisioner(converge): add bootstrap_command --- provisioner/converge/provisioner.go | 36 +++++++++---------- provisioner/converge/provisioner_test.go | 15 ++++++++ .../source/docs/provisioners/converge.html.md | 18 ++++++++++ 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/provisioner/converge/provisioner.go b/provisioner/converge/provisioner.go index 55233b504..daaf484d2 100644 --- a/provisioner/converge/provisioner.go +++ b/provisioner/converge/provisioner.go @@ -7,7 +7,6 @@ import ( "bytes" "errors" "fmt" - "net/http" "strings" @@ -28,8 +27,9 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` // Bootstrapping - Bootstrap bool `mapstructure:"bootstrap"` - Version string `mapstructure:"version"` + Bootstrap bool `mapstructure:"bootstrap"` + Version string `mapstructure:"version"` + BootstrapCommand string `mapstructure:"bootstrap_command"` // Modules ModuleDirs []ModuleDir `mapstructure:"module_dirs"` @@ -64,7 +64,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { Interpolate: true, InterpolateContext: &p.config.ctx, InterpolateFilter: &interpolate.RenderFilter{ - Exclude: []string{"execute_command"}, + Exclude: []string{ + "execute_command", + "bootstrap_command", + }, }, }, 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}}" } + if p.config.BootstrapCommand == "" { + p.config.BootstrapCommand = "curl -s https://get.converge.sh | sh {{if ne .Version \"\"}}-s -- -v {{.Version}}{{end}}" + } + // validate 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) @@ -133,21 +140,14 @@ func (p *Provisioner) maybeBootstrap(ui packer.Ui, comm packer.Communicator) err } 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 { - return fmt.Errorf("Error downloading bootstrap script: %s", err) // TODO: is github.com/pkg/error allowed? - } - 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 + return fmt.Errorf("Could not interpolate bootstrap command: %s", err) } var out bytes.Buffer diff --git a/provisioner/converge/provisioner_test.go b/provisioner/converge/provisioner_test.go index 7a7e5ac09..7c97982ae 100644 --- a/provisioner/converge/provisioner_test.go +++ b/provisioner/converge/provisioner_test.go @@ -60,6 +60,21 @@ func TestProvisionerPrepare(t *testing.T) { 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) { diff --git a/website/source/docs/provisioners/converge.html.md b/website/source/docs/provisioners/converge.html.md index 057604324..cdef6b5a4 100644 --- a/website/source/docs/provisioners/converge.html.md +++ b/website/source/docs/provisioners/converge.html.md @@ -60,6 +60,10 @@ Optional parameters: - `prevent_sudo` (bool) - stop Converge from running with adminstrator 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 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. - `ParamsJSON` - The unquoted JSONified form of `params` 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.