github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/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 35 // This is used to login to dockerhub to pull a private base container. For 36 // pushing to dockerhub, see the docker post-processors 37 Login bool 38 LoginEmail string `mapstructure:"login_email"` 39 LoginPassword string `mapstructure:"login_password"` 40 LoginServer string `mapstructure:"login_server"` 41 LoginUsername string `mapstructure:"login_username"` 42 43 ctx interpolate.Context 44 } 45 46 func NewConfig(raws ...interface{}) (*Config, []string, error) { 47 c := new(Config) 48 49 var md mapstructure.Metadata 50 err := config.Decode(c, &config.DecodeOpts{ 51 Metadata: &md, 52 Interpolate: true, 53 InterpolateContext: &c.ctx, 54 InterpolateFilter: &interpolate.RenderFilter{ 55 Exclude: []string{ 56 "run_command", 57 }, 58 }, 59 }, raws...) 60 if err != nil { 61 return nil, nil, err 62 } 63 64 // Defaults 65 if len(c.RunCommand) == 0 { 66 c.RunCommand = []string{"-d", "-i", "-t", "{{.Image}}", "/bin/bash"} 67 } 68 69 // Default Pull if it wasn't set 70 hasPull := false 71 for _, k := range md.Keys { 72 if k == "Pull" { 73 hasPull = true 74 break 75 } 76 } 77 78 if !hasPull { 79 c.Pull = true 80 } 81 82 // Default to the normal Docker type 83 if c.Comm.Type == "" { 84 c.Comm.Type = "docker" 85 } 86 87 var errs *packer.MultiError 88 if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { 89 errs = packer.MultiErrorAppend(errs, es...) 90 } 91 if c.Image == "" { 92 errs = packer.MultiErrorAppend(errs, errImageNotSpecified) 93 } 94 95 if (c.ExportPath != "" && c.Commit) || (c.ExportPath != "" && c.Discard) || (c.Commit && c.Discard) { 96 errs = packer.MultiErrorAppend(errs, errArtifactUseConflict) 97 } 98 99 if c.ExportPath == "" && !c.Commit && !c.Discard { 100 errs = packer.MultiErrorAppend(errs, errArtifactNotUsed) 101 } 102 103 if c.ExportPath != "" { 104 if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() { 105 errs = packer.MultiErrorAppend(errs, errExportPathNotFile) 106 } 107 } 108 109 if errs != nil && len(errs.Errors) > 0 { 110 return nil, nil, errs 111 } 112 113 return c, nil, nil 114 }