github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/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  	EcrLogin        bool   `mapstructure:"ecr_login"`
    44  	AwsAccessConfig `mapstructure:",squash"`
    45  
    46  	ctx interpolate.Context
    47  }
    48  
    49  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    50  	c := new(Config)
    51  
    52  	var md mapstructure.Metadata
    53  	err := config.Decode(c, &config.DecodeOpts{
    54  		Metadata:           &md,
    55  		Interpolate:        true,
    56  		InterpolateContext: &c.ctx,
    57  		InterpolateFilter: &interpolate.RenderFilter{
    58  			Exclude: []string{
    59  				"run_command",
    60  			},
    61  		},
    62  	}, raws...)
    63  	if err != nil {
    64  		return nil, nil, err
    65  	}
    66  
    67  	// Defaults
    68  	if len(c.RunCommand) == 0 {
    69  		c.RunCommand = []string{"-d", "-i", "-t", "{{.Image}}", "/bin/bash"}
    70  	}
    71  
    72  	// Default Pull if it wasn't set
    73  	hasPull := false
    74  	for _, k := range md.Keys {
    75  		if k == "Pull" {
    76  			hasPull = true
    77  			break
    78  		}
    79  	}
    80  
    81  	if !hasPull {
    82  		c.Pull = true
    83  	}
    84  
    85  	// Default to the normal Docker type
    86  	if c.Comm.Type == "" {
    87  		c.Comm.Type = "docker"
    88  	}
    89  
    90  	var errs *packer.MultiError
    91  	if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
    92  		errs = packer.MultiErrorAppend(errs, es...)
    93  	}
    94  	if c.Image == "" {
    95  		errs = packer.MultiErrorAppend(errs, errImageNotSpecified)
    96  	}
    97  
    98  	if (c.ExportPath != "" && c.Commit) || (c.ExportPath != "" && c.Discard) || (c.Commit && c.Discard) {
    99  		errs = packer.MultiErrorAppend(errs, errArtifactUseConflict)
   100  	}
   101  
   102  	if c.ExportPath == "" && !c.Commit && !c.Discard {
   103  		errs = packer.MultiErrorAppend(errs, errArtifactNotUsed)
   104  	}
   105  
   106  	if c.ExportPath != "" {
   107  		if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() {
   108  			errs = packer.MultiErrorAppend(errs, errExportPathNotFile)
   109  		}
   110  	}
   111  
   112  	if c.EcrLogin && c.LoginServer == "" {
   113  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("ECR login requires login server to be provided."))
   114  	}
   115  
   116  	if errs != nil && len(errs.Errors) > 0 {
   117  		return nil, nil, errs
   118  	}
   119  
   120  	return c, nil, nil
   121  }