packer-cn/builder/docker/step_pull.go

73 lines
1.6 KiB
Go
Raw Normal View History

2013-11-09 01:00:57 -05:00
package docker
import (
"fmt"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
2013-11-09 01:00:57 -05:00
"github.com/mitchellh/multistep"
"log"
2013-11-09 01:00:57 -05:00
)
type StepPull struct{}
func (s *StepPull) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver)
2013-11-09 01:00:57 -05:00
ui := state.Get("ui").(packer.Ui)
if !config.Pull {
log.Println("Pull disabled, won't docker pull")
return multistep.ActionContinue
}
2013-11-09 01:00:57 -05:00
ui.Say(fmt.Sprintf("Pulling Docker image: %s", config.Image))
2014-09-05 18:24:12 -04:00
if config.EcrLogin {
ui.Message("Fetching ECR credentials...")
username, password, err := config.EcrGetLogin(config.LoginServer)
if err != nil {
err := fmt.Errorf("Error fetching ECR credentials: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
config.LoginUsername = username
config.LoginPassword = password
}
if config.Login || config.EcrLogin {
2014-09-05 18:24:12 -04:00
ui.Message("Logging in...")
err := driver.Login(
config.LoginServer,
config.LoginEmail,
config.LoginUsername,
config.LoginPassword)
if err != nil {
err := fmt.Errorf("Error logging in: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
defer func() {
ui.Message("Logging out...")
if err := driver.Logout(config.LoginServer); err != nil {
ui.Error(fmt.Sprintf("Error logging out: %s", err))
}
}()
}
if err := driver.Pull(config.Image); err != nil {
2013-11-09 01:00:57 -05:00
err := fmt.Errorf("Error pulling Docker image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *StepPull) Cleanup(state multistep.StateBag) {
}