builder/docker: Add type assertion check when reading config from state bag

This commit is contained in:
nywilken 2019-12-17 17:34:58 -05:00
parent 0ba6b22fee
commit 9403d55549
5 changed files with 44 additions and 13 deletions

View File

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

View File

@ -12,7 +12,13 @@ import (
type StepConnectDocker struct{} type StepConnectDocker struct{}
func (s *StepConnectDocker) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { 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) containerId := state.Get("container_id").(string)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
tempDir := state.Get("temp_dir").(string) tempDir := state.Get("temp_dir").(string)

View File

@ -14,11 +14,14 @@ import (
type StepExport struct{} type StepExport struct{}
func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { 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) 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 // We should catch this in validation, but guard anyway
if config.ExportPath == "" { if config.ExportPath == "" {
@ -44,6 +47,9 @@ func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multiste
return multistep.ActionHalt return multistep.ActionHalt
} }
driver := state.Get("driver").(Driver)
containerId := state.Get("container_id").(string)
ui.Say("Exporting the container") ui.Say("Exporting the container")
if err := driver.Export(containerId, f); err != nil { if err := driver.Export(containerId, f); err != nil {
f.Close() f.Close()

View File

@ -12,9 +12,14 @@ import (
type StepPull struct{} type StepPull struct{}
func (s *StepPull) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { 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) 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 { if !config.Pull {
log.Println("Pull disabled, won't docker 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 config.LoginPassword = password
} }
driver := state.Get("driver").(Driver)
if config.Login || config.EcrLogin { if config.Login || config.EcrLogin {
ui.Message("Logging in...") ui.Message("Logging in...")
err := driver.Login( err := driver.Login(

View File

@ -13,10 +13,14 @@ type StepRun struct {
} }
func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { 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) 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{ runConfig := ContainerConfig{
Image: config.Image, 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 { for host, container := range config.Volumes {
runConfig.Volumes[host] = container runConfig.Volumes[host] = container
} }
tempDir := state.Get("temp_dir").(string)
runConfig.Volumes[tempDir] = config.ContainerDir runConfig.Volumes[tempDir] = config.ContainerDir
driver := state.Get("driver").(Driver)
ui.Say("Starting docker container...") ui.Say("Starting docker container...")
containerId, err := driver.StartContainer(&runConfig) containerId, err := driver.StartContainer(&runConfig)
if err != nil { if err != nil {