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