From 56d22ac99e18e687fda072806b1c240bb1d5aa8e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Sep 2014 14:43:15 -0700 Subject: [PATCH] post-processor/docker-push: can login [GH-1243] --- CHANGELOG.md | 1 + Vagrantfile | 4 +- post-processor/docker-push/post-processor.go | 44 +++++++++++++++++++ .../post-processors/docker-push.html.markdown | 14 +++++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb7fc3894..e91c39212 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ FEATURES: * builder/vmware: VMware Player 6 is now supported. [GH-1168] * builder/vmware-vmx: Boot commands and the HTTP server are supported. [GH-1169] + * post-processor/docker-push: Can now specify login credentials. [GH-1243] IMPROVEMENTS: diff --git a/Vagrantfile b/Vagrantfile index 97f90085b..f8dcb1197 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -32,10 +32,12 @@ sudo apt-get install -y curl git-core zip SCRIPT Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - config.vm.box = "chef/ubuntu-14.04" + config.vm.box = "chef/ubuntu-12.04" config.vm.provision "shell", inline: $script + config.vm.synced_folder ".", "/vagrant", disabled: true + ["vmware_fusion", "vmware_workstation"].each do |p| config.vm.provider "p" do |v| v.vmx["memsize"] = "2048" diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index 9007b303f..dab7ba835 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -12,6 +12,12 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` + 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 } @@ -35,6 +41,24 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { // Accumulate any errors errs := new(packer.MultiError) + + // Process templates + templates := map[string]*string{ + "login_email": &p.config.LoginEmail, + "login_username": &p.config.LoginUsername, + "login_password": &p.config.LoginPassword, + "login_server": &p.config.LoginServer, + } + + for n, ptr := range templates { + var err error + *ptr, err = p.config.tpl.Process(*ptr, nil) + if err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error processing %s: %s", n, err)) + } + } + if len(errs.Errors) > 0 { return errs } @@ -56,6 +80,26 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac driver = &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui} } + if p.config.Login { + ui.Message("Logging in...") + err := driver.Login( + p.config.LoginServer, + p.config.LoginEmail, + p.config.LoginUsername, + p.config.LoginPassword) + if err != nil { + return nil, false, fmt.Errorf( + "Error logging in to Docker: %s", err) + } + + defer func() { + ui.Message("Logging out...") + if err := driver.Logout(p.config.LoginServer); err != nil { + ui.Error(fmt.Sprintf("Error logging out: %s", err)) + } + }() + } + // Get the name. We strip off any tags from the name because the // push doesn't use those. name := artifact.Id() diff --git a/website/source/docs/post-processors/docker-push.html.markdown b/website/source/docs/post-processors/docker-push.html.markdown index 602e126e0..4533f83a2 100644 --- a/website/source/docs/post-processors/docker-push.html.markdown +++ b/website/source/docs/post-processors/docker-push.html.markdown @@ -19,8 +19,18 @@ for you, but for now you must manually do this. ## Configuration -This post-processor has no configuration! Simply add it to your chain -of post-processors and the image will be uploaded. +This post-processor has only optional configuration: + +* `login` (boolean) - Defaults to false. If true, the post-processor will + login prior to pushing. + +* `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. ## Example