2013-11-08 19:55:02 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-11-09 14:47:32 -05:00
|
|
|
"fmt"
|
2015-05-29 19:24:29 -04:00
|
|
|
"os"
|
2015-05-27 15:55:36 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-11-08 19:55:02 -05:00
|
|
|
"github.com/mitchellh/packer/common"
|
2015-06-15 01:09:38 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2015-05-27 15:55:36 -04:00
|
|
|
"github.com/mitchellh/packer/helper/config"
|
2013-11-08 19:55:02 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-05-27 15:55:36 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-11-08 19:55:02 -05:00
|
|
|
)
|
|
|
|
|
2015-08-18 17:38:32 -04:00
|
|
|
var (
|
2015-08-19 16:12:16 -04:00
|
|
|
errArtifactNotUsed = fmt.Errorf("No instructions given for handling the artifact; expected commit, discard, or export_path")
|
|
|
|
errArtifactUseConflict = fmt.Errorf("Cannot specify more than one of commit, discard, and export_path")
|
|
|
|
errExportPathNotFile = fmt.Errorf("export_path must be a file, not a directory")
|
|
|
|
errImageNotSpecified = fmt.Errorf("Image must be specified")
|
2015-08-18 17:38:32 -04:00
|
|
|
)
|
|
|
|
|
2013-11-08 19:55:02 -05:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-06-15 01:09:38 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2013-11-08 19:55:02 -05:00
|
|
|
|
2014-09-04 21:03:15 -04:00
|
|
|
Commit bool
|
2015-08-18 17:38:32 -04:00
|
|
|
Discard bool
|
2013-11-09 12:48:36 -05:00
|
|
|
ExportPath string `mapstructure:"export_path"`
|
|
|
|
Image string
|
2015-08-18 17:38:32 -04:00
|
|
|
Pty bool
|
2013-11-09 20:21:24 -05:00
|
|
|
Pull bool
|
2013-12-27 12:17:45 -05:00
|
|
|
RunCommand []string `mapstructure:"run_command"`
|
2014-09-05 18:48:42 -04:00
|
|
|
Volumes map[string]string
|
2016-04-29 22:12:20 -04:00
|
|
|
Privileged bool `mapstructure:"privileged"`
|
2013-11-09 01:00:57 -05:00
|
|
|
|
2015-08-18 19:55:29 -04:00
|
|
|
// This is used to login to dockerhub to pull a private base container. For
|
|
|
|
// pushing to dockerhub, see the docker post-processors
|
2014-09-05 18:24:12 -04:00
|
|
|
Login bool
|
|
|
|
LoginEmail string `mapstructure:"login_email"`
|
|
|
|
LoginPassword string `mapstructure:"login_password"`
|
|
|
|
LoginServer string `mapstructure:"login_server"`
|
2015-08-18 17:38:32 -04:00
|
|
|
LoginUsername string `mapstructure:"login_username"`
|
2015-07-16 22:34:36 -04:00
|
|
|
|
2015-05-27 16:03:47 -04:00
|
|
|
ctx interpolate.Context
|
2013-11-08 19:55:02 -05:00
|
|
|
}
|
2013-11-09 14:47:32 -05:00
|
|
|
|
2013-11-09 20:07:14 -05:00
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
2015-06-12 17:02:09 -04:00
|
|
|
c := new(Config)
|
2013-11-09 20:07:14 -05:00
|
|
|
|
2015-05-27 15:55:36 -04:00
|
|
|
var md mapstructure.Metadata
|
2015-06-12 17:02:09 -04:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-05-27 15:55:36 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-11-09 20:07:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-27 12:17:45 -05:00
|
|
|
// Defaults
|
|
|
|
if len(c.RunCommand) == 0 {
|
2015-08-18 19:47:12 -04:00
|
|
|
c.RunCommand = []string{"-d", "-i", "-t", "{{.Image}}", "/bin/bash"}
|
2013-12-27 12:17:45 -05:00
|
|
|
}
|
|
|
|
|
2013-11-09 20:21:24 -05:00
|
|
|
// Default Pull if it wasn't set
|
|
|
|
hasPull := false
|
|
|
|
for _, k := range md.Keys {
|
|
|
|
if k == "Pull" {
|
|
|
|
hasPull = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasPull {
|
|
|
|
c.Pull = true
|
|
|
|
}
|
|
|
|
|
2015-06-15 01:09:38 -04:00
|
|
|
// Default to the normal Docker type
|
|
|
|
if c.Comm.Type == "" {
|
|
|
|
c.Comm.Type = "docker"
|
|
|
|
}
|
|
|
|
|
2015-05-27 15:55:36 -04:00
|
|
|
var errs *packer.MultiError
|
2015-06-15 01:09:38 -04:00
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
2013-11-09 14:47:32 -05:00
|
|
|
if c.Image == "" {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errImageNotSpecified)
|
2013-11-09 20:07:14 -05:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:38:32 -04:00
|
|
|
if (c.ExportPath != "" && c.Commit) || (c.ExportPath != "" && c.Discard) || (c.Commit && c.Discard) {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errArtifactUseConflict)
|
2015-08-18 17:38:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.ExportPath == "" && !c.Commit && !c.Discard {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errArtifactNotUsed)
|
2014-09-04 21:03:15 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:24:29 -04:00
|
|
|
if c.ExportPath != "" {
|
|
|
|
if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() {
|
2015-08-19 16:12:16 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, errExportPathNotFile)
|
2015-05-29 19:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-09 20:07:14 -05:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, nil, errs
|
2013-11-09 14:47:32 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 17:02:09 -04:00
|
|
|
return c, nil, nil
|
2013-11-09 14:47:32 -05:00
|
|
|
}
|