github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/docker/config.go (about) 1 package docker 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/mitchellh/mapstructure" 8 "github.com/mitchellh/packer/common" 9 "github.com/mitchellh/packer/helper/communicator" 10 "github.com/mitchellh/packer/helper/config" 11 "github.com/mitchellh/packer/packer" 12 "github.com/mitchellh/packer/template/interpolate" 13 ) 14 15 var ( 16 errArtifactNotUsed = fmt.Errorf("No instructions given for handling the artifact; expected commit, discard, or export_path") 17 errArtifactUseConflict = fmt.Errorf("Cannot specify more than one of commit, discard, and export_path") 18 errExportPathNotFile = fmt.Errorf("export_path must be a file, not a directory") 19 errImageNotSpecified = fmt.Errorf("Image must be specified") 20 ) 21 22 type Config struct { 23 common.PackerConfig `mapstructure:",squash"` 24 Comm communicator.Config `mapstructure:",squash"` 25 26 Commit bool 27 Discard bool 28 ExportPath string `mapstructure:"export_path"` 29 Image string 30 Pty bool 31 Pull bool 32 RunCommand []string `mapstructure:"run_command"` 33 Volumes map[string]string 34 Privileged bool `mapstructure:"privileged"` 35 Author string 36 Changes []string 37 Message string 38 39 // This is used to login to dockerhub to pull a private base container. For 40 // pushing to dockerhub, see the docker post-processors 41 Login bool 42 LoginEmail string `mapstructure:"login_email"` 43 LoginPassword string `mapstructure:"login_password"` 44 LoginServer string `mapstructure:"login_server"` 45 LoginUsername string `mapstructure:"login_username"` 46 EcrLogin bool `mapstructure:"ecr_login"` 47 AwsAccessConfig `mapstructure:",squash"` 48 49 ctx interpolate.Context 50 } 51 52 func NewConfig(raws ...interface{}) (*Config, []string, error) { 53 c := new(Config) 54 55 var md mapstructure.Metadata 56 err := config.Decode(c, &config.DecodeOpts{ 57 Metadata: &md, 58 Interpolate: true, 59 InterpolateContext: &c.ctx, 60 InterpolateFilter: &interpolate.RenderFilter{ 61 Exclude: []string{ 62 "run_command", 63 }, 64 }, 65 }, raws...) 66 if err != nil { 67 return nil, nil, err 68 } 69 70 // Defaults 71 if len(c.RunCommand) == 0 { 72 c.RunCommand = []string{"-d", "-i", "-t", "{{.Image}}", "/bin/bash"} 73 } 74 75 // Default Pull if it wasn't set 76 hasPull := false 77 for _, k := range md.Keys { 78 if k == "Pull" { 79 hasPull = true 80 break 81 } 82 } 83 84 if !hasPull { 85 c.Pull = true 86 } 87 88 // Default to the normal Docker type 89 if c.Comm.Type == "" { 90 c.Comm.Type = "docker" 91 } 92 93 var errs *packer.MultiError 94 if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { 95 errs = packer.MultiErrorAppend(errs, es...) 96 } 97 if c.Image == "" { 98 errs = packer.MultiErrorAppend(errs, errImageNotSpecified) 99 } 100 101 if (c.ExportPath != "" && c.Commit) || (c.ExportPath != "" && c.Discard) || (c.Commit && c.Discard) { 102 errs = packer.MultiErrorAppend(errs, errArtifactUseConflict) 103 } 104 105 if c.ExportPath == "" && !c.Commit && !c.Discard { 106 errs = packer.MultiErrorAppend(errs, errArtifactNotUsed) 107 } 108 109 if c.ExportPath != "" { 110 if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() { 111 errs = packer.MultiErrorAppend(errs, errExportPathNotFile) 112 } 113 } 114 115 if c.EcrLogin && c.LoginServer == "" { 116 errs = packer.MultiErrorAppend(errs, fmt.Errorf("ECR login requires login server to be provided.")) 117 } 118 119 if errs != nil && len(errs.Errors) > 0 { 120 return nil, nil, errs 121 } 122 123 return c, nil, nil 124 }