From 39fd462b561f2058d83acfc1d52d6894dbb28fee Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 12 Dec 2019 10:59:44 -0800 Subject: [PATCH] change all provision func signatures to use map[string]interface{} --- common/shell-local/config.go | 6 ++-- common/shell-local/run.go | 30 +++++++++---------- common/step_provision.go | 5 ++-- packer/plugin/provisioner.go | 2 +- packer/provisioner.go | 27 ++++++++++++++--- packer/provisioner_mock.go | 2 +- packer/provisioner_test.go | 10 +++---- packer/provisioner_timeout.go | 2 +- packer/rpc/provisioner.go | 8 ++--- packer/rpc/provisioner_test.go | 2 +- post-processor/shell-local/post-processor.go | 2 +- provisioner/ansible-local/provisioner.go | 2 +- provisioner/ansible-local/provisioner_test.go | 4 +-- provisioner/ansible/provisioner.go | 2 +- provisioner/ansible/provisioner_test.go | 2 +- provisioner/breakpoint/provisioner.go | 2 +- provisioner/chef-client/provisioner.go | 2 +- provisioner/chef-solo/provisioner.go | 2 +- provisioner/converge/provisioner.go | 2 +- provisioner/file/provisioner.go | 2 +- provisioner/file/provisioner_test.go | 2 +- provisioner/inspec/provisioner.go | 2 +- provisioner/powershell/provisioner.go | 22 +++++--------- provisioner/powershell/provisioner_test.go | 14 ++++----- provisioner/puppet-masterless/provisioner.go | 2 +- .../puppet-masterless/provisioner_test.go | 4 +-- provisioner/puppet-server/provisioner.go | 2 +- provisioner/salt-masterless/provisioner.go | 2 +- provisioner/shell-local/provisioner.go | 4 +-- provisioner/shell/provisioner.go | 2 +- provisioner/sleep/provisioner.go | 2 +- provisioner/windows-restart/provisioner.go | 2 +- .../windows-restart/provisioner_test.go | 12 ++++---- provisioner/windows-shell/provisioner.go | 2 +- provisioner/windows-shell/provisioner_test.go | 8 ++--- template/interpolate/funcs.go | 2 -- 36 files changed, 103 insertions(+), 97 deletions(-) diff --git a/common/shell-local/config.go b/common/shell-local/config.go index 99070f295..bdf3dfbd7 100644 --- a/common/shell-local/config.go +++ b/common/shell-local/config.go @@ -10,6 +10,7 @@ import ( "runtime" "strings" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common/shell" configHelper "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" @@ -44,13 +45,14 @@ type Config struct { // used to track the data sent to shell-local from the builder // GeneratedData - ctx interpolate.Context + ctx interpolate.Context + generatedData map[string]interface{} } func Decode(config *Config, raws ...interface{}) error { // Create passthrough for build-generated data so we can fill it in once we know // it - // config.ctx.Data = common.PlaceholderData() + config.ctx.Data = common.PlaceholderData() err := configHelper.Decode(&config, &configHelper.DecodeOpts{ Interpolate: true, diff --git a/common/shell-local/run.go b/common/shell-local/run.go index 06b8c78d3..8fb58adf2 100644 --- a/common/shell-local/run.go +++ b/common/shell-local/run.go @@ -28,7 +28,15 @@ type EnvVarsTemplate struct { WinRMPassword string } -func Run(ctx context.Context, ui packer.Ui, config *Config) (bool, error) { +func Run(ctx context.Context, ui packer.Ui, config *Config, generatedData map[string]interface{}) (bool, error) { + if generatedData != nil { + config.generatedData = generatedData + } else { + // No fear; probably just in the post-processor, not provisioner. + // Make sure it's not a nil map so we can assign to it later. + config.generatedData = make(map[string]interface{}) + } + config.ctx.Data = generatedData // Check if shell-local can even execute against this runtime OS if len(config.OnlyOn) > 0 { runCommand := false @@ -120,11 +128,6 @@ func createInlineScriptFile(config *Config) (string, error) { writer.WriteString(shebang) } - // generate context so you can interpolate the command - config.ctx.Data = &EnvVarsTemplate{ - WinRMPassword: getWinRMPassword(config.PackerBuildName), - } - for _, command := range config.Inline { // interpolate command to check for template variables. command, err := interpolate.Render(command, &config.ctx) @@ -152,12 +155,11 @@ func createInlineScriptFile(config *Config) (string, error) { // user-provided ExecuteCommand or defaulting to something that makes sense for // the host OS func createInterpolatedCommands(config *Config, script string, flattenedEnvVars string) ([]string, error) { - config.ctx.Data = &ExecuteCommandTemplate{ - Vars: flattenedEnvVars, - Script: script, - Command: script, - WinRMPassword: getWinRMPassword(config.PackerBuildName), - } + config.generatedData["Vars"] = flattenedEnvVars + config.generatedData["Script"] = script + config.generatedData["Command"] = script + + config.ctx.Data = config.generatedData interpolatedCmds := make([]string, len(config.ExecuteCommand)) for i, cmd := range config.ExecuteCommand { @@ -192,10 +194,6 @@ func createFlattenedEnvVars(config *Config) (string, error) { envVars["PACKER_HTTP_PORT"] = httpPort } - // interpolate environment variables - config.ctx.Data = &EnvVarsTemplate{ - WinRMPassword: getWinRMPassword(config.PackerBuildName), - } // Split vars into key/value components for _, envVar := range config.Vars { envVar, err := interpolate.Render(envVar, &config.ctx) diff --git a/common/step_provision.go b/common/step_provision.go index 60f7bbffd..f7a6d19a1 100644 --- a/common/step_provision.go +++ b/common/step_provision.go @@ -75,10 +75,9 @@ func PopulateProvisionHookData(state multistep.StateBag) map[string]interface{} if fieldName == "ID" { continue } + fVal := v.FieldByName(fieldName) - if fVal.IsZero() { - log.Printf("Megan NONINTERFACABLE fVal is %#v", fVal) - } else { + if !fVal.IsZero() { hookData[fieldName] = fVal.Interface() } } diff --git a/packer/plugin/provisioner.go b/packer/plugin/provisioner.go index 14bc2a65e..ac9f1f89d 100644 --- a/packer/plugin/provisioner.go +++ b/packer/plugin/provisioner.go @@ -21,7 +21,7 @@ func (c *cmdProvisioner) Prepare(configs ...interface{}) error { return c.p.Prepare(configs...) } -func (c *cmdProvisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData interface{}) error { +func (c *cmdProvisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error { defer func() { r := recover() c.checkExit(r, nil) diff --git a/packer/provisioner.go b/packer/provisioner.go index a317b157f..6e29e2e61 100644 --- a/packer/provisioner.go +++ b/packer/provisioner.go @@ -20,7 +20,7 @@ type Provisioner interface { // given for cancellation, a UI is given to communicate with the user, and // a communicator is given that is guaranteed to be connected to some // machine so that provisioning can be done. - Provision(context.Context, Ui, Communicator, interface{}) error + Provision(context.Context, Ui, Communicator, map[string]interface{}) error } // A HookedProvisioner represents a provisioner and information describing it @@ -53,7 +53,26 @@ func (h *ProvisionHook) Run(ctx context.Context, name string, ui Ui, comm Commun for _, p := range h.Provisioners { ts := CheckpointReporter.AddSpan(p.TypeName, "provisioner", p.Config) - err := p.Provisioner.Provision(ctx, ui, comm, data) + // Provisioners expect a map[string]interface{} in their data field, but + // it gets converted into a map[interface]interface on the way over the + // RPC. Check that data can be cast into such a form, and cast it. + cast := make(map[string]interface{}) + interMap, ok := data.(map[interface{}]interface{}) + if !ok { + log.Printf("Unable to read map[string]interface out of data." + + "Using empty interface.") + } else { + for key, val := range interMap { + keyString, ok := key.(string) + if ok { + cast[keyString] = val + } else { + log.Printf("Error casting generated data key to a string.") + } + } + } + + err := p.Provisioner.Provision(ctx, ui, comm, cast) ts.End(err) if err != nil { @@ -75,7 +94,7 @@ func (p *PausedProvisioner) Prepare(raws ...interface{}) error { return p.Provisioner.Prepare(raws...) } -func (p *PausedProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData interface{}) error { +func (p *PausedProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData map[string]interface{}) error { // Use a select to determine if we get cancelled during the wait ui.Say(fmt.Sprintf("Pausing %s before the next provisioner...", p.PauseBefore)) @@ -102,7 +121,7 @@ func (p *DebuggedProvisioner) Prepare(raws ...interface{}) error { return p.Provisioner.Prepare(raws...) } -func (p *DebuggedProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData interface{}) error { +func (p *DebuggedProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData map[string]interface{}) error { // Use a select to determine if we get cancelled during the wait message := "Pausing before the next provisioner . Press enter to continue." diff --git a/packer/provisioner_mock.go b/packer/provisioner_mock.go index 2ae7d68b6..f94b62047 100644 --- a/packer/provisioner_mock.go +++ b/packer/provisioner_mock.go @@ -22,7 +22,7 @@ func (t *MockProvisioner) Prepare(configs ...interface{}) error { return nil } -func (t *MockProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData interface{}) error { +func (t *MockProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData map[string]interface{}) error { t.ProvCalled = true t.ProvCommunicator = comm t.ProvUi = ui diff --git a/packer/provisioner_test.go b/packer/provisioner_test.go index 10e546704..108dac90f 100644 --- a/packer/provisioner_test.go +++ b/packer/provisioner_test.go @@ -114,7 +114,7 @@ func TestPausedProvisionerProvision(t *testing.T) { ui := testUi() comm := new(MockCommunicator) - prov.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + prov.Provision(context.Background(), ui, comm, make(map[string]interface{})) if !mock.ProvCalled { t.Fatal("prov should be called") } @@ -143,7 +143,7 @@ func TestPausedProvisionerProvision_waits(t *testing.T) { }, } - err := prov.Provision(context.Background(), testUi(), new(MockCommunicator), make(map[interface{}]interface{})) + err := prov.Provision(context.Background(), testUi(), new(MockCommunicator), make(map[string]interface{})) if err != nil { t.Fatalf("prov failed: %v", err) @@ -164,7 +164,7 @@ func TestPausedProvisionerCancel(t *testing.T) { return ctx.Err() } - err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[interface{}]interface{})) + err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[string]interface{})) if err == nil { t.Fatal("should have err") } @@ -198,7 +198,7 @@ func TestDebuggedProvisionerProvision(t *testing.T) { ui := testUi() comm := new(MockCommunicator) writeReader(ui, "\n") - prov.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + prov.Provision(context.Background(), ui, comm, make(map[string]interface{})) if !mock.ProvCalled { t.Fatal("prov should be called") } @@ -224,7 +224,7 @@ func TestDebuggedProvisionerCancel(t *testing.T) { return ctx.Err() } - err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[interface{}]interface{})) + err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[string]interface{})) if err == nil { t.Fatal("should have error") } diff --git a/packer/provisioner_timeout.go b/packer/provisioner_timeout.go index f7bd01894..a3104815c 100644 --- a/packer/provisioner_timeout.go +++ b/packer/provisioner_timeout.go @@ -13,7 +13,7 @@ type TimeoutProvisioner struct { Timeout time.Duration } -func (p *TimeoutProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData interface{}) error { +func (p *TimeoutProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator, generatedData map[string]interface{}) error { ctx, cancel := context.WithTimeout(ctx, p.Timeout) defer cancel() diff --git a/packer/rpc/provisioner.go b/packer/rpc/provisioner.go index dfcea7da1..5acca71ad 100644 --- a/packer/rpc/provisioner.go +++ b/packer/rpc/provisioner.go @@ -39,11 +39,11 @@ func (p *provisioner) Prepare(configs ...interface{}) (err error) { } type ProvisionerProvisionArgs struct { - GeneratedData interface{} + GeneratedData map[string]interface{} StreamID uint32 } -func (p *provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData interface{}) error { +func (p *provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error { nextId := p.mux.NextId() server := newServerWithMux(p.mux, nextId) server.RegisterCommunicator(comm) @@ -74,7 +74,6 @@ func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *interfa func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *interface{}) error { streamId := args.StreamID - generatedData := args.GeneratedData client, err := newClientWithMux(p.mux, streamId) if err != nil { return NewBasicError(err) @@ -84,8 +83,7 @@ func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *int if p.context == nil { p.context, p.contextCancel = context.WithCancel(context.Background()) } - - if err := p.p.Provision(p.context, client.Ui(), client.Communicator(), generatedData); err != nil { + if err := p.p.Provision(p.context, client.Ui(), client.Communicator(), args.GeneratedData); err != nil { return NewBasicError(err) } diff --git a/packer/rpc/provisioner_test.go b/packer/rpc/provisioner_test.go index 61e6ade18..f70e09dad 100644 --- a/packer/rpc/provisioner_test.go +++ b/packer/rpc/provisioner_test.go @@ -39,7 +39,7 @@ func TestProvisionerRPC(t *testing.T) { // Test Provision ui := &testUi{} comm := &packer.MockCommunicator{} - if err := pClient.Provision(topCtx, ui, comm, make(map[interface{}]interface{})); err == nil { + if err := pClient.Provision(topCtx, ui, comm, make(map[string]interface{})); err == nil { t.Fatalf("Provison should have err") } if !p.ProvCalled { diff --git a/post-processor/shell-local/post-processor.go b/post-processor/shell-local/post-processor.go index e23d31648..ef5cf0849 100644 --- a/post-processor/shell-local/post-processor.go +++ b/post-processor/shell-local/post-processor.go @@ -41,7 +41,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact // this particular post-processor doesn't do anything with the artifact // except to return it. - success, retErr := sl.Run(ctx, ui, &p.config) + success, retErr := sl.Run(ctx, ui, &p.config, map[string]interface{}{}) if !success { return nil, false, false, retErr } diff --git a/provisioner/ansible-local/provisioner.go b/provisioner/ansible-local/provisioner.go index 70bb1e5cc..eab8d2115 100644 --- a/provisioner/ansible-local/provisioner.go +++ b/provisioner/ansible-local/provisioner.go @@ -188,7 +188,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say("Provisioning with Ansible...") if len(p.config.PlaybookDir) > 0 { diff --git a/provisioner/ansible-local/provisioner_test.go b/provisioner/ansible-local/provisioner_test.go index 412541a29..180bd0dc9 100644 --- a/provisioner/ansible-local/provisioner_test.go +++ b/provisioner/ansible-local/provisioner_test.go @@ -134,7 +134,7 @@ func TestProvisionerProvision_PlaybookFiles(t *testing.T) { } comm := &communicatorMock{} - if err := p.Provision(context.Background(), new(packer.NoopUi), comm, make(map[interface{}]interface{})); err != nil { + if err := p.Provision(context.Background(), new(packer.NoopUi), comm, make(map[string]interface{})); err != nil { t.Fatalf("err: %s", err) } @@ -168,7 +168,7 @@ func TestProvisionerProvision_PlaybookFilesWithPlaybookDir(t *testing.T) { } comm := &communicatorMock{} - if err := p.Provision(context.Background(), new(packer.NoopUi), comm, make(map[interface{}]interface{})); err != nil { + if err := p.Provision(context.Background(), new(packer.NoopUi), comm, make(map[string]interface{})); err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index 79238d655..8adf04175 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -214,7 +214,7 @@ func (p *Provisioner) getVersion() error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say("Provisioning with Ansible...") // Interpolate env vars to check for .WinRMPassword p.config.ctx.Data = &PassthroughTemplate{ diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index 6310fbcba..39035d4ed 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -349,7 +349,7 @@ func TestAnsibleLongMessages(t *testing.T) { Writer: new(bytes.Buffer), } - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/breakpoint/provisioner.go b/provisioner/breakpoint/provisioner.go index 0eed2f7a7..49f122aa4 100644 --- a/provisioner/breakpoint/provisioner.go +++ b/provisioner/breakpoint/provisioner.go @@ -42,7 +42,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { if p.config.Disable { if p.config.Note != "" { ui.Say(fmt.Sprintf( diff --git a/provisioner/chef-client/provisioner.go b/provisioner/chef-client/provisioner.go index b2d6e21d2..208c1744e 100644 --- a/provisioner/chef-client/provisioner.go +++ b/provisioner/chef-client/provisioner.go @@ -244,7 +244,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { p.communicator = comm diff --git a/provisioner/chef-solo/provisioner.go b/provisioner/chef-solo/provisioner.go index 1e3e20801..3c9be9778 100644 --- a/provisioner/chef-solo/provisioner.go +++ b/provisioner/chef-solo/provisioner.go @@ -238,7 +238,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say("Provisioning with chef-solo") if !p.config.SkipInstall { diff --git a/provisioner/converge/provisioner.go b/provisioner/converge/provisioner.go index b5953b3ad..563823336 100644 --- a/provisioner/converge/provisioner.go +++ b/provisioner/converge/provisioner.go @@ -108,7 +108,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } // Provision node somehow. TODO: actual docs -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say("Provisioning with Converge") // bootstrapping diff --git a/provisioner/file/provisioner.go b/provisioner/file/provisioner.go index db1780c7b..f95d069f0 100644 --- a/provisioner/file/provisioner.go +++ b/provisioner/file/provisioner.go @@ -92,7 +92,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { if p.config.Direction == "download" { return p.ProvisionDownload(ui, comm) } else { diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 6f229d512..d27dfaaa7 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -127,7 +127,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { Writer: b, } comm := &packer.MockCommunicator{} - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("should successfully provision: %s", err) } diff --git a/provisioner/inspec/provisioner.go b/provisioner/inspec/provisioner.go index 549a5c774..c6d92ef6d 100644 --- a/provisioner/inspec/provisioner.go +++ b/provisioner/inspec/provisioner.go @@ -186,7 +186,7 @@ func (p *Provisioner) getVersion() error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say("Provisioning with Inspec...") for i, envVar := range p.config.InspecEnvVars { diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index 88c07702f..2d86d5a8b 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -220,26 +220,20 @@ func extractScript(p *Provisioner) (string, error) { return temp.Name(), nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error { ui.Say(fmt.Sprintf("Provisioning with Powershell...")) p.communicator = comm p.generatedData = make(map[string]interface{}) - // test that generatedData is a map, not an empty interface. - dataMap, ok := generatedData.(map[interface{}]interface{}) - if ok { - for key, val := range dataMap { - keyString, ok := key.(string) - if ok { - p.generatedData[keyString] = val - } else { - log.Printf("Error casting generated data key to a string.") - } - } + + if generatedData != nil { + log.Printf("Gen data isn't nil.") + p.generatedData = generatedData if winRMPass, ok := p.generatedData["WinRMPassword"]; ok { + log.Printf("Found winrm pass") packer.LogSecretFilter.Set(winRMPass.(string)) } } else { - log.Printf("error reading generated data from builder") + log.Printf("generatedData passed to Provision method is nil!") } scripts := make([]string, len(p.config.Scripts)) @@ -445,7 +439,6 @@ func (p *Provisioner) createCommandTextPrivileged() (command string, err error) if err != nil { return "", err } - ctxData := p.generatedData ctxData["Path"] = p.config.RemotePath ctxData["Vars"] = p.config.RemoteEnvVarPath @@ -475,7 +468,6 @@ func (p *Provisioner) ElevatedUser() string { func (p *Provisioner) ElevatedPassword() string { // Replace ElevatedPassword for winrm users who used this feature p.config.ctx.Data = p.generatedData - elevatedPassword, _ := interpolate.Render(p.config.ElevatedPassword, &p.config.ctx) return elevatedPassword diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index 07bbd8815..152905bb8 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -355,7 +355,7 @@ func TestProvisionerProvision_ValidExitCodes(t *testing.T) { comm := new(packer.MockCommunicator) comm.StartExitStatus = 200 p.Prepare(config) - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -378,7 +378,7 @@ func TestProvisionerProvision_InvalidExitCodes(t *testing.T) { comm := new(packer.MockCommunicator) comm.StartExitStatus = 201 // Invalid! p.Prepare(config) - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err == nil { t.Fatal("should have error") } @@ -399,7 +399,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { p.config.PackerBuilderType = "iso" comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -419,7 +419,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { config["remote_path"] = "c:/Windows/Temp/inlineScript.ps1" p.Prepare(config) - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -448,7 +448,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -484,7 +484,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -511,7 +511,7 @@ func TestProvisionerProvision_UploadFails(t *testing.T) { comm := new(packer.ScriptUploadErrorMockCommunicator) p.Prepare(config) p.config.StartRetryTimeout = 1 * time.Second - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if !strings.Contains(err.Error(), packer.ScriptUploadErrorMockCommunicatorError.Error()) { t.Fatalf("expected Provision() error %q to contain %q", err.Error(), diff --git a/provisioner/puppet-masterless/provisioner.go b/provisioner/puppet-masterless/provisioner.go index b8413c41e..a570c69b9 100644 --- a/provisioner/puppet-masterless/provisioner.go +++ b/provisioner/puppet-masterless/provisioner.go @@ -259,7 +259,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say("Provisioning with Puppet...") p.communicator = comm ui.Message("Creating Puppet staging directory...") diff --git a/provisioner/puppet-masterless/provisioner_test.go b/provisioner/puppet-masterless/provisioner_test.go index cd01cec32..582b8d311 100644 --- a/provisioner/puppet-masterless/provisioner_test.go +++ b/provisioner/puppet-masterless/provisioner_test.go @@ -494,7 +494,7 @@ func TestProvisionerProvision_extraArguments(t *testing.T) { t.Fatalf("err: %s", err) } - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("err: %s", err) } @@ -514,7 +514,7 @@ func TestProvisionerProvision_extraArguments(t *testing.T) { t.Fatalf("err: %s", err) } - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/puppet-server/provisioner.go b/provisioner/puppet-server/provisioner.go index d8f52ab50..a82f6098e 100644 --- a/provisioner/puppet-server/provisioner.go +++ b/provisioner/puppet-server/provisioner.go @@ -229,7 +229,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say("Provisioning with Puppet...") p.communicator = comm ui.Message("Creating Puppet staging directory...") diff --git a/provisioner/salt-masterless/provisioner.go b/provisioner/salt-masterless/provisioner.go index 6963424f9..a25fae4f1 100644 --- a/provisioner/salt-masterless/provisioner.go +++ b/provisioner/salt-masterless/provisioner.go @@ -222,7 +222,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { var err error var src, dst string diff --git a/provisioner/shell-local/provisioner.go b/provisioner/shell-local/provisioner.go index aac65f57b..b51b8278c 100644 --- a/provisioner/shell-local/provisioner.go +++ b/provisioner/shell-local/provisioner.go @@ -25,8 +25,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, _ packer.Communicator, _ interface{}) error { - _, retErr := sl.Run(ctx, ui, &p.config) +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, _ packer.Communicator, generatedData map[string]interface{}) error { + _, retErr := sl.Run(ctx, ui, &p.config, generatedData) return retErr } diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index a03c185fa..71e209e44 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -179,7 +179,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { scripts := make([]string, len(p.config.Scripts)) copy(scripts, p.config.Scripts) diff --git a/provisioner/sleep/provisioner.go b/provisioner/sleep/provisioner.go index 8d4bfe0a0..46acc1806 100644 --- a/provisioner/sleep/provisioner.go +++ b/provisioner/sleep/provisioner.go @@ -20,7 +20,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return config.Decode(&p, &config.DecodeOpts{}, raws...) } -func (p *Provisioner) Provision(ctx context.Context, _ packer.Ui, _ packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, _ packer.Ui, _ packer.Communicator, _ map[string]interface{}) error { select { case <-ctx.Done(): return ctx.Err() diff --git a/provisioner/windows-restart/provisioner.go b/provisioner/windows-restart/provisioner.go index a02cf1c9b..8e1d9713e 100644 --- a/provisioner/windows-restart/provisioner.go +++ b/provisioner/windows-restart/provisioner.go @@ -96,7 +96,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { p.cancelLock.Lock() p.cancel = make(chan struct{}) p.cancelLock.Unlock() diff --git a/provisioner/windows-restart/provisioner_test.go b/provisioner/windows-restart/provisioner_test.go index 0a2eda530..391068457 100644 --- a/provisioner/windows-restart/provisioner_test.go +++ b/provisioner/windows-restart/provisioner_test.go @@ -104,7 +104,7 @@ func TestProvisionerProvision_Success(t *testing.T) { waitForRestart = func(context.Context, *Provisioner, packer.Communicator) error { return nil } - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -140,7 +140,7 @@ func TestProvisionerProvision_CustomCommand(t *testing.T) { waitForRestart = func(context.Context, *Provisioner, packer.Communicator) error { return nil } - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -163,7 +163,7 @@ func TestProvisionerProvision_RestartCommandFail(t *testing.T) { comm.StartExitStatus = 1 p.Prepare(config) - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err == nil { t.Fatal("should have error") } @@ -182,7 +182,7 @@ func TestProvisionerProvision_WaitForRestartFail(t *testing.T) { waitForCommunicator = func(context.Context, *Provisioner) error { return fmt.Errorf("Machine did not restart properly") } - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err == nil { t.Fatal("should have error") } @@ -216,7 +216,7 @@ func TestProvision_waitForRestartTimeout(t *testing.T) { } go func() { - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) waitDone <- true }() <-waitContinue @@ -327,7 +327,7 @@ func TestProvision_Cancel(t *testing.T) { // Create two go routines to provision and cancel in parallel // Provision will block until cancel happens go func() { - done <- p.Provision(topCtx, ui, comm, make(map[interface{}]interface{})) + done <- p.Provision(topCtx, ui, comm, make(map[string]interface{})) }() // Expect interrupt error diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index f4121f274..366fd2f80 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -156,7 +156,7 @@ func extractScript(p *Provisioner) (string, error) { return temp.Name(), nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { ui.Say(fmt.Sprintf("Provisioning with windows-shell...")) scripts := make([]string, len(p.config.Scripts)) copy(scripts, p.config.Scripts) diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index b9ce1cf05..4a5663bdb 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -292,7 +292,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { p.config.PackerBuilderType = "iso" comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err := p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -311,7 +311,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { config["remote_path"] = "c:/Windows/Temp/inlineScript.bat" p.Prepare(config) - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -342,7 +342,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } @@ -381,7 +381,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) if err != nil { t.Fatal("should not have error") } diff --git a/template/interpolate/funcs.go b/template/interpolate/funcs.go index a3a8e36ee..2373ded1b 100644 --- a/template/interpolate/funcs.go +++ b/template/interpolate/funcs.go @@ -3,7 +3,6 @@ package interpolate import ( "errors" "fmt" - "log" "os" "path/filepath" "strconv" @@ -166,7 +165,6 @@ func funcGenTemplateDir(ctx *Context) interface{} { func funcGenGenerated(ctx *Context) interface{} { return func(s string) (string, error) { - log.Printf("Megan context data is %#v", ctx.Data) if data, ok := ctx.Data.(map[string]string); ok { // PlaceholderData has been passed into generator, and we can check // the value against the placeholder data to make sure that it is