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

     1  package null
     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  	Host              string `mapstructure:"host"`
    13  	Port              int    `mapstructure:"port"`
    14  	SSHUsername       string `mapstructure:"ssh_username"`
    15  	SSHPassword       string `mapstructure:"ssh_password"`
    16  	SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"`
    17  
    18  	tpl *packer.ConfigTemplate
    19  }
    20  
    21  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    22  	c := new(Config)
    23  	md, err := common.DecodeConfig(c, raws...)
    24  	if err != nil {
    25  		return nil, nil, err
    26  	}
    27  
    28  	c.tpl, err = packer.NewConfigTemplate()
    29  	if err != nil {
    30  		return nil, nil, err
    31  	}
    32  
    33  	c.tpl.UserVars = c.PackerUserVars
    34  
    35  	if c.Port == 0 {
    36  		c.Port = 22
    37  	}
    38  
    39  	errs := common.CheckUnusedConfig(md)
    40  
    41  	templates := map[string]*string{
    42  		"host":                 &c.Host,
    43  		"ssh_username":         &c.SSHUsername,
    44  		"ssh_password":         &c.SSHPassword,
    45  		"ssh_private_key_file": &c.SSHPrivateKeyFile,
    46  	}
    47  
    48  	for n, ptr := range templates {
    49  		var err error
    50  		*ptr, err = c.tpl.Process(*ptr, nil)
    51  		if err != nil {
    52  			errs = packer.MultiErrorAppend(
    53  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    54  		}
    55  	}
    56  
    57  	if c.Host == "" {
    58  		errs = packer.MultiErrorAppend(errs,
    59  			fmt.Errorf("host must be specified"))
    60  	}
    61  
    62  	if c.SSHUsername == "" {
    63  		errs = packer.MultiErrorAppend(errs,
    64  			fmt.Errorf("ssh_username must be specified"))
    65  	}
    66  
    67  	if c.SSHPassword == "" && c.SSHPrivateKeyFile == "" {
    68  		errs = packer.MultiErrorAppend(errs,
    69  			fmt.Errorf("one of ssh_password and ssh_private_key_file must be specified"))
    70  	}
    71  
    72  	if c.SSHPassword != "" && c.SSHPrivateKeyFile != "" {
    73  		errs = packer.MultiErrorAppend(errs,
    74  			fmt.Errorf("only one of ssh_password and ssh_private_key_file must be specified"))
    75  	}
    76  
    77  	if errs != nil && len(errs.Errors) > 0 {
    78  		return nil, nil, errs
    79  	}
    80  
    81  	return c, nil, nil
    82  }