builder/docker: can login to pull

This commit is contained in:
Mitchell Hashimoto 2014-09-05 15:24:12 -07:00
parent 622b9f459a
commit 2e4882f0c3
5 changed files with 77 additions and 2 deletions

View File

@ -15,6 +15,7 @@ FEATURES:
Packer will look in the PWD and the directory with `packer` for Packer will look in the PWD and the directory with `packer` for
binaries named `packer-TYPE-NAME`. binaries named `packer-TYPE-NAME`.
* builder/docker: Images can now be committed instead of exported. [GH-1198] * builder/docker: Images can now be committed instead of exported. [GH-1198]
* builder/docker: Can now specify login credentials to pull images.
* builder/virtualbox-ovf: New `import_flags` setting can be used to add * builder/virtualbox-ovf: New `import_flags` setting can be used to add
new command line flags to `VBoxManage import` to allow things such new command line flags to `VBoxManage import` to allow things such
as EULAs to be accepted. [GH-1383] as EULAs to be accepted. [GH-1383]

View File

@ -15,6 +15,12 @@ type Config struct {
Pull bool Pull bool
RunCommand []string `mapstructure:"run_command"` RunCommand []string `mapstructure:"run_command"`
Login bool
LoginEmail string `mapstructure:"login_email"`
LoginUsername string `mapstructure:"login_username"`
LoginPassword string `mapstructure:"login_password"`
LoginServer string `mapstructure:"login_server"`
tpl *packer.ConfigTemplate tpl *packer.ConfigTemplate
} }
@ -59,8 +65,12 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
templates := map[string]*string{ templates := map[string]*string{
"export_path": &c.ExportPath, "export_path": &c.ExportPath,
"image": &c.Image, "image": &c.Image,
"login_email": &c.LoginEmail,
"login_username": &c.LoginUsername,
"login_password": &c.LoginPassword,
"login_server": &c.LoginServer,
} }
for n, ptr := range templates { for n, ptr := range templates {

View File

@ -20,6 +20,29 @@ func (s *StepPull) Run(state multistep.StateBag) multistep.StepAction {
} }
ui.Say(fmt.Sprintf("Pulling Docker image: %s", config.Image)) ui.Say(fmt.Sprintf("Pulling Docker image: %s", config.Image))
if config.Login {
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 { if err := driver.Pull(config.Image); err != nil {
err := fmt.Errorf("Error pulling Docker image: %s", err) err := fmt.Errorf("Error pulling Docker image: %s", err)
state.Put("error", err) state.Put("error", err)

View File

@ -51,6 +51,35 @@ func TestStepPull_error(t *testing.T) {
} }
} }
func TestStepPull_login(t *testing.T) {
state := testState(t)
step := new(StepPull)
defer step.Cleanup(state)
config := state.Get("config").(*Config)
driver := state.Get("driver").(*MockDriver)
config.Login = true
// run the step
if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
// verify we pulled
if !driver.PullCalled {
t.Fatal("should've pulled")
}
// verify we logged in
if !driver.LoginCalled {
t.Fatal("should've logged in")
}
if !driver.LogoutCalled {
t.Fatal("should've logged out")
}
}
func TestStepPull_noPull(t *testing.T) { func TestStepPull_noPull(t *testing.T) {
state := testState(t) state := testState(t)
step := new(StepPull) step := new(StepPull)

View File

@ -74,6 +74,18 @@ described.
### Optional: ### Optional:
* `login` (boolean) - Defaults to false. If true, the builder will
login in order to pull the image. The builder only logs in for the
duration of the pull. It always logs out afterwards.
* `login_email` (string) - The email to use to authenticate to login.
* `login_username` (string) - The username to use to authenticate to login.
* `login_password` (string) - The password to use to authenticate to login.
* `login_server` (string) - The server address to login to.
* `pull` (boolean) - If true, the configured image will be pulled using * `pull` (boolean) - If true, the configured image will be pulled using
`docker pull` prior to use. Otherwise, it is assumed the image already `docker pull` prior to use. Otherwise, it is assumed the image already
exists and can be used. This defaults to true if not set. exists and can be used. This defaults to true if not set.