2013-11-09 01:00:57 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-11-09 01:00:57 -05:00
|
|
|
"fmt"
|
2017-10-25 12:53:51 -04:00
|
|
|
"log"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2013-11-09 01:00:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepPull struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepPull) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-12-17 17:34:58 -05:00
|
|
|
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
|
|
|
|
}
|
2013-11-09 01:00:57 -05:00
|
|
|
|
2013-11-09 20:21:24 -05:00
|
|
|
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
|
|
|
|
2016-09-03 10:45:52 -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
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:34:58 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2016-09-03 10:45:52 -04:00
|
|
|
if config.Login || config.EcrLogin {
|
2014-09-05 18:24:12 -04:00
|
|
|
ui.Message("Logging in...")
|
|
|
|
err := driver.Login(
|
|
|
|
config.LoginServer,
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-11-09 15:12:23 -05:00
|
|
|
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) {
|
|
|
|
}
|