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