github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/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  type Config struct {
    16  	common.PackerConfig `mapstructure:",squash"`
    17  	Comm                communicator.Config `mapstructure:",squash"`
    18  
    19  	Commit     bool
    20  	ExportPath string `mapstructure:"export_path"`
    21  	Image      string
    22  	Pull       bool
    23  	RunCommand []string `mapstructure:"run_command"`
    24  	Volumes    map[string]string
    25  
    26  	Login         bool
    27  	LoginEmail    string `mapstructure:"login_email"`
    28  	LoginUsername string `mapstructure:"login_username"`
    29  	LoginPassword string `mapstructure:"login_password"`
    30  	LoginServer   string `mapstructure:"login_server"`
    31  
    32  	ctx interpolate.Context
    33  }
    34  
    35  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    36  	c := new(Config)
    37  
    38  	var md mapstructure.Metadata
    39  	err := config.Decode(c, &config.DecodeOpts{
    40  		Metadata:           &md,
    41  		Interpolate:        true,
    42  		InterpolateContext: &c.ctx,
    43  		InterpolateFilter: &interpolate.RenderFilter{
    44  			Exclude: []string{
    45  				"run_command",
    46  			},
    47  		},
    48  	}, raws...)
    49  	if err != nil {
    50  		return nil, nil, err
    51  	}
    52  
    53  	// Defaults
    54  	if len(c.RunCommand) == 0 {
    55  		c.RunCommand = []string{
    56  			"-d", "-i", "-t",
    57  			"{{.Image}}",
    58  			"/bin/bash",
    59  		}
    60  	}
    61  
    62  	// Default Pull if it wasn't set
    63  	hasPull := false
    64  	for _, k := range md.Keys {
    65  		if k == "Pull" {
    66  			hasPull = true
    67  			break
    68  		}
    69  	}
    70  
    71  	if !hasPull {
    72  		c.Pull = true
    73  	}
    74  
    75  	// Default to the normal Docker type
    76  	if c.Comm.Type == "" {
    77  		c.Comm.Type = "docker"
    78  	}
    79  
    80  	var errs *packer.MultiError
    81  	if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
    82  		errs = packer.MultiErrorAppend(errs, es...)
    83  	}
    84  	if c.Image == "" {
    85  		errs = packer.MultiErrorAppend(errs,
    86  			fmt.Errorf("image must be specified"))
    87  	}
    88  
    89  	if c.ExportPath != "" && c.Commit {
    90  		errs = packer.MultiErrorAppend(errs,
    91  			fmt.Errorf("both commit and export_path cannot be set"))
    92  	}
    93  
    94  	if c.ExportPath != "" {
    95  		if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() {
    96  			errs = packer.MultiErrorAppend(errs, fmt.Errorf(
    97  				"export_path must be a file, not a directory"))
    98  		}
    99  	}
   100  
   101  	if errs != nil && len(errs.Errors) > 0 {
   102  		return nil, nil, errs
   103  	}
   104  
   105  	return c, nil, nil
   106  }