Merge pull request #8498 from hashicorp/f-builder_docker-typeassertion-check

builder/docker: Fix interface conversion issue when getting config from state bag
This commit is contained in:
Wilken Rivera 2019-12-17 18:08:00 -05:00 committed by GitHub
commit a72242cd70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 14 deletions

View File

@ -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)

View File

@ -14,11 +14,17 @@ type StepCommit struct {
}
func (s *StepCommit) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
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)
config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui)
if config.WindowsContainer {
// docker can't commit a running Windows container
err := driver.StopContainer(containerId)

View File

@ -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)

View File

@ -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()

View File

@ -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(

View File

@ -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 {