github.com/rothwerx/packer@v0.9.0/post-processor/docker-push/post-processor.go (about) 1 package dockerpush 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/mitchellh/packer/builder/docker" 8 "github.com/mitchellh/packer/common" 9 "github.com/mitchellh/packer/helper/config" 10 "github.com/mitchellh/packer/packer" 11 "github.com/mitchellh/packer/post-processor/docker-import" 12 "github.com/mitchellh/packer/post-processor/docker-tag" 13 "github.com/mitchellh/packer/template/interpolate" 14 ) 15 16 type Config struct { 17 common.PackerConfig `mapstructure:",squash"` 18 19 Login bool 20 LoginEmail string `mapstructure:"login_email"` 21 LoginUsername string `mapstructure:"login_username"` 22 LoginPassword string `mapstructure:"login_password"` 23 LoginServer string `mapstructure:"login_server"` 24 25 ctx interpolate.Context 26 } 27 28 type PostProcessor struct { 29 Driver docker.Driver 30 31 config Config 32 } 33 34 func (p *PostProcessor) Configure(raws ...interface{}) error { 35 err := config.Decode(&p.config, &config.DecodeOpts{ 36 Interpolate: true, 37 InterpolateContext: &p.config.ctx, 38 InterpolateFilter: &interpolate.RenderFilter{ 39 Exclude: []string{}, 40 }, 41 }, raws...) 42 if err != nil { 43 return err 44 } 45 46 return nil 47 } 48 49 func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { 50 if artifact.BuilderId() != dockerimport.BuilderId && 51 artifact.BuilderId() != dockertag.BuilderId { 52 err := fmt.Errorf( 53 "Unknown artifact type: %s\nCan only import from docker-import and docker-tag artifacts.", 54 artifact.BuilderId()) 55 return nil, false, err 56 } 57 58 driver := p.Driver 59 if driver == nil { 60 // If no driver is set, then we use the real driver 61 driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui} 62 } 63 64 if p.config.Login { 65 ui.Message("Logging in...") 66 err := driver.Login( 67 p.config.LoginServer, 68 p.config.LoginEmail, 69 p.config.LoginUsername, 70 p.config.LoginPassword) 71 if err != nil { 72 return nil, false, fmt.Errorf( 73 "Error logging in to Docker: %s", err) 74 } 75 76 defer func() { 77 ui.Message("Logging out...") 78 if err := driver.Logout(p.config.LoginServer); err != nil { 79 ui.Error(fmt.Sprintf("Error logging out: %s", err)) 80 } 81 }() 82 } 83 84 // Get the name. We strip off any tags from the name because the 85 // push doesn't use those. 86 name := artifact.Id() 87 88 if i := strings.Index(name, "/"); i >= 0 { 89 // This should always be true because the / is required. But we have 90 // to get the index to this so we don't accidentally strip off the port 91 if j := strings.Index(name[i:], ":"); j >= 0 { 92 name = name[:i+j] 93 } 94 } 95 96 ui.Message("Pushing: " + name) 97 if err := driver.Push(name); err != nil { 98 return nil, false, err 99 } 100 101 return nil, false, nil 102 }