github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/docker/config.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/packer/common"
     6  	"github.com/mitchellh/packer/packer"
     7  )
     8  
     9  type Config struct {
    10  	common.PackerConfig `mapstructure:",squash"`
    11  
    12  	Commit     bool
    13  	ExportPath string `mapstructure:"export_path"`
    14  	Image      string
    15  	Pull       bool
    16  	RunCommand []string `mapstructure:"run_command"`
    17  	Volumes    map[string]string
    18  
    19  	Login         bool
    20  	LoginEmail    string `mapstructure:"login_email"`
    21  	LoginUsername string `mapstructure:"login_username"`
    22  	LoginPassword string `mapstructure:"login_password"`
    23  	LoginServer   string `mapstructure:"login_server"`
    24  
    25  	tpl *packer.ConfigTemplate
    26  }
    27  
    28  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    29  	c := new(Config)
    30  	md, err := common.DecodeConfig(c, raws...)
    31  	if err != nil {
    32  		return nil, nil, err
    33  	}
    34  
    35  	c.tpl, err = packer.NewConfigTemplate()
    36  	if err != nil {
    37  		return nil, nil, err
    38  	}
    39  
    40  	c.tpl.UserVars = c.PackerUserVars
    41  
    42  	// Defaults
    43  	if len(c.RunCommand) == 0 {
    44  		c.RunCommand = []string{
    45  			"-d", "-i", "-t",
    46  			"{{.Image}}",
    47  			"/bin/bash",
    48  		}
    49  	}
    50  
    51  	// Default Pull if it wasn't set
    52  	hasPull := false
    53  	for _, k := range md.Keys {
    54  		if k == "Pull" {
    55  			hasPull = true
    56  			break
    57  		}
    58  	}
    59  
    60  	if !hasPull {
    61  		c.Pull = true
    62  	}
    63  
    64  	errs := common.CheckUnusedConfig(md)
    65  
    66  	templates := map[string]*string{
    67  		"export_path":    &c.ExportPath,
    68  		"image":          &c.Image,
    69  		"login_email":    &c.LoginEmail,
    70  		"login_username": &c.LoginUsername,
    71  		"login_password": &c.LoginPassword,
    72  		"login_server":   &c.LoginServer,
    73  	}
    74  
    75  	for n, ptr := range templates {
    76  		var err error
    77  		*ptr, err = c.tpl.Process(*ptr, nil)
    78  		if err != nil {
    79  			errs = packer.MultiErrorAppend(
    80  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    81  		}
    82  	}
    83  
    84  	for k, v := range c.Volumes {
    85  		var err error
    86  		v, err = c.tpl.Process(v, nil)
    87  		if err != nil {
    88  			errs = packer.MultiErrorAppend(
    89  				errs, fmt.Errorf("Error processing volumes[%s]: %s", k, err))
    90  		}
    91  
    92  		c.Volumes[k] = v
    93  	}
    94  
    95  	if c.Image == "" {
    96  		errs = packer.MultiErrorAppend(errs,
    97  			fmt.Errorf("image must be specified"))
    98  	}
    99  
   100  	if c.ExportPath != "" && c.Commit {
   101  		errs = packer.MultiErrorAppend(errs,
   102  			fmt.Errorf("both commit and export_path cannot be set"))
   103  	}
   104  
   105  	if errs != nil && len(errs.Errors) > 0 {
   106  		return nil, nil, errs
   107  	}
   108  
   109  	return c, nil, nil
   110  }