github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/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  	VpcId              string `mapstructure:"vpc_id"`
    25  
    26  	// Unexported fields that are calculated from others
    27  	sshTimeout time.Duration
    28  }
    29  
    30  func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
    31  	if t == nil {
    32  		var err error
    33  		t, err = packer.NewConfigTemplate()
    34  		if err != nil {
    35  			return []error{err}
    36  		}
    37  	}
    38  
    39  	// Defaults
    40  	if c.SSHPort == 0 {
    41  		c.SSHPort = 22
    42  	}
    43  
    44  	if c.RawSSHTimeout == "" {
    45  		c.RawSSHTimeout = "1m"
    46  	}
    47  
    48  	// Validation
    49  	var err error
    50  	errs := make([]error, 0)
    51  	if c.SourceAmi == "" {
    52  		errs = append(errs, errors.New("A source_ami must be specified"))
    53  	}
    54  
    55  	if c.InstanceType == "" {
    56  		errs = append(errs, errors.New("An instance_type must be specified"))
    57  	}
    58  
    59  	if c.SSHUsername == "" {
    60  		errs = append(errs, errors.New("An ssh_username must be specified"))
    61  	}
    62  
    63  	if c.UserData != "" && c.UserDataFile != "" {
    64  		errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
    65  	} else if c.UserDataFile != "" {
    66  		if _, err := os.Stat(c.UserDataFile); err != nil {
    67  			errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
    68  		}
    69  	}
    70  
    71  	templates := map[string]*string{
    72  		"iam_instance_profile": &c.IamInstanceProfile,
    73  		"instance_type":        &c.InstanceType,
    74  		"ssh_timeout":          &c.RawSSHTimeout,
    75  		"security_group_id":    &c.SecurityGroupId,
    76  		"ssh_username":         &c.SSHUsername,
    77  		"source_ami":           &c.SourceAmi,
    78  		"subnet_id":            &c.SubnetId,
    79  		"vpc_id":               &c.VpcId,
    80  	}
    81  
    82  	for n, ptr := range templates {
    83  		var err error
    84  		*ptr, err = t.Process(*ptr, nil)
    85  		if err != nil {
    86  			errs = append(
    87  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    88  		}
    89  	}
    90  
    91  	c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
    92  	if err != nil {
    93  		errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
    94  	}
    95  
    96  	return errs
    97  }
    98  
    99  func (c *RunConfig) SSHTimeout() time.Duration {
   100  	return c.sshTimeout
   101  }