From 0ba6b22fee91c50a5f6445482688f73aee5a7acd Mon Sep 17 00:00:00 2001 From: nywilken Date: Tue, 17 Dec 2019 17:48:01 -0500 Subject: [PATCH 1/2] builder/docker: Update type of config stored in state bag --- builder/docker/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/docker/builder.go b/builder/docker/builder.go index 470fa0f6e..bbd92af1b 100644 --- a/builder/docker/builder.go +++ b/builder/docker/builder.go @@ -77,7 +77,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) - state.Put("config", b.config) + state.Put("config", &b.config) state.Put("hook", hook) state.Put("ui", ui) From 9403d555491ce67fda686021b61b91028448266e Mon Sep 17 00:00:00 2001 From: nywilken Date: Tue, 17 Dec 2019 17:34:58 -0500 Subject: [PATCH 2/2] builder/docker: Add type assertion check when reading config from state bag --- builder/docker/step_commit.go | 12 +++++++++--- builder/docker/step_connect_docker.go | 8 +++++++- builder/docker/step_export.go | 14 ++++++++++---- builder/docker/step_pull.go | 10 ++++++++-- builder/docker/step_run.go | 13 ++++++++++--- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/builder/docker/step_commit.go b/builder/docker/step_commit.go index d8281bcee..33ccb090b 100644 --- a/builder/docker/step_commit.go +++ b/builder/docker/step_commit.go @@ -14,11 +14,17 @@ type StepCommit struct { } func (s *StepCommit) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - driver := state.Get("driver").(Driver) - containerId := state.Get("container_id").(string) - config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + driver := state.Get("driver").(Driver) + containerId := state.Get("container_id").(string) if config.WindowsContainer { // docker can't commit a running Windows container err := driver.StopContainer(containerId) diff --git a/builder/docker/step_connect_docker.go b/builder/docker/step_connect_docker.go index a907a3ea2..0d064b269 100644 --- a/builder/docker/step_connect_docker.go +++ b/builder/docker/step_connect_docker.go @@ -12,7 +12,13 @@ import ( type StepConnectDocker struct{} func (s *StepConnectDocker) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + return multistep.ActionHalt + } + containerId := state.Get("container_id").(string) driver := state.Get("driver").(Driver) tempDir := state.Get("temp_dir").(string) diff --git a/builder/docker/step_export.go b/builder/docker/step_export.go index b02228036..99a8a1d11 100644 --- a/builder/docker/step_export.go +++ b/builder/docker/step_export.go @@ -14,11 +14,14 @@ import ( type StepExport struct{} func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) - - driver := state.Get("driver").(Driver) - containerId := state.Get("container_id").(string) ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } // We should catch this in validation, but guard anyway if config.ExportPath == "" { @@ -44,6 +47,9 @@ func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multiste return multistep.ActionHalt } + driver := state.Get("driver").(Driver) + containerId := state.Get("container_id").(string) + ui.Say("Exporting the container") if err := driver.Export(containerId, f); err != nil { f.Close() diff --git a/builder/docker/step_pull.go b/builder/docker/step_pull.go index 712ef70c7..d2cf3cdda 100644 --- a/builder/docker/step_pull.go +++ b/builder/docker/step_pull.go @@ -12,9 +12,14 @@ import ( type StepPull struct{} func (s *StepPull) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) - driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } if !config.Pull { log.Println("Pull disabled, won't docker pull") @@ -38,6 +43,7 @@ func (s *StepPull) Run(ctx context.Context, state multistep.StateBag) multistep. config.LoginPassword = password } + driver := state.Get("driver").(Driver) if config.Login || config.EcrLogin { ui.Message("Logging in...") err := driver.Login( diff --git a/builder/docker/step_run.go b/builder/docker/step_run.go index 281505104..c5a0c83f2 100644 --- a/builder/docker/step_run.go +++ b/builder/docker/step_run.go @@ -13,10 +13,14 @@ type StepRun struct { } func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) - driver := state.Get("driver").(Driver) - tempDir := state.Get("temp_dir").(string) ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } runConfig := ContainerConfig{ Image: config.Image, @@ -28,8 +32,11 @@ func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.S for host, container := range config.Volumes { runConfig.Volumes[host] = container } + + tempDir := state.Get("temp_dir").(string) runConfig.Volumes[tempDir] = config.ContainerDir + driver := state.Get("driver").(Driver) ui.Say("Starting docker container...") containerId, err := driver.StartContainer(&runConfig) if err != nil {