github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/builder/amazon/common/run_config.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/mitchellh/packer/packer"
     7  	"os"
     8  	"time"
     9  )
    10  
    11  // RunConfig contains configuration for running an instance from a source
    12  // AMI and details on how to access that launched image.
    13  type RunConfig struct {
    14  	SourceAmi            string `mapstructure:"source_ami"`
    15  	IamInstanceProfile   string `mapstructure:"iam_instance_profile"`
    16  	InstanceType         string `mapstructure:"instance_type"`
    17  	UserData             string `mapstructure:"user_data"`
    18  	UserDataFile         string `mapstructure:"user_data_file"`
    19  	RawSSHTimeout        string `mapstructure:"ssh_timeout"`
    20  	SSHUsername          string `mapstructure:"ssh_username"`
    21  	SSHPort              int    `mapstructure:"ssh_port"`
    22  	SecurityGroupId      string `mapstructure:"security_group_id"`
    23  	SubnetId             string `mapstructure:"subnet_id"`
    24  	TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
    25  	VpcId                string `mapstructure:"vpc_id"`
    26  
    27  	// Unexported fields that are calculated from others
    28  	sshTimeout time.Duration
    29  }
    30  
    31  func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
    32  	if t == nil {
    33  		var err error
    34  		t, err = packer.NewConfigTemplate()
    35  		if err != nil {
    36  			return []error{err}
    37  		}
    38  	}
    39  
    40  	// Defaults
    41  	if c.SSHPort == 0 {
    42  		c.SSHPort = 22
    43  	}
    44  
    45  	if c.RawSSHTimeout == "" {
    46  		c.RawSSHTimeout = "1m"
    47  	}
    48  
    49  	if c.TemporaryKeyPairName == "" {
    50  		c.TemporaryKeyPairName = "packer {{uuid}}"
    51  	}
    52  
    53  	// Validation
    54  	var err error
    55  	errs := make([]error, 0)
    56  	if c.SourceAmi == "" {
    57  		errs = append(errs, errors.New("A source_ami must be specified"))
    58  	}
    59  
    60  	if c.InstanceType == "" {
    61  		errs = append(errs, errors.New("An instance_type must be specified"))
    62  	}
    63  
    64  	if c.SSHUsername == "" {
    65  		errs = append(errs, errors.New("An ssh_username must be specified"))
    66  	}
    67  
    68  	if c.UserData != "" && c.UserDataFile != "" {
    69  		errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
    70  	} else if c.UserDataFile != "" {
    71  		if _, err := os.Stat(c.UserDataFile); err != nil {
    72  			errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
    73  		}
    74  	}
    75  
    76  	templates := map[string]*string{
    77  		"iam_instance_profile":    &c.IamInstanceProfile,
    78  		"instance_type":           &c.InstanceType,
    79  		"ssh_timeout":             &c.RawSSHTimeout,
    80  		"security_group_id":       &c.SecurityGroupId,
    81  		"ssh_username":            &c.SSHUsername,
    82  		"source_ami":              &c.SourceAmi,
    83  		"subnet_id":               &c.SubnetId,
    84  		"temporary_key_pair_name": &c.TemporaryKeyPairName,
    85  		"vpc_id":                  &c.VpcId,
    86  	}
    87  
    88  	for n, ptr := range templates {
    89  		var err error
    90  		*ptr, err = t.Process(*ptr, nil)
    91  		if err != nil {
    92  			errs = append(
    93  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    94  		}
    95  	}
    96  
    97  	c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
    98  	if err != nil {
    99  		errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
   100  	}
   101  
   102  	return errs
   103  }
   104  
   105  func (c *RunConfig) SSHTimeout() time.Duration {
   106  	return c.sshTimeout
   107  }