2013-07-16 00:23:40 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-08-15 22:17:23 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-12 14:52:43 -04:00
|
|
|
"os"
|
2013-07-16 00:23:40 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RunConfig contains configuration for running an instance from a source
|
|
|
|
// AMI and details on how to access that launched image.
|
|
|
|
type RunConfig struct {
|
2013-07-31 14:41:07 -04:00
|
|
|
SourceAmi string `mapstructure:"source_ami"`
|
2013-07-30 14:55:50 -04:00
|
|
|
IamInstanceProfile string `mapstructure:"iam_instance_profile"`
|
2013-07-31 14:41:07 -04:00
|
|
|
InstanceType string `mapstructure:"instance_type"`
|
2013-08-07 17:33:20 -04:00
|
|
|
UserData string `mapstructure:"user_data"`
|
2013-08-12 14:52:43 -04:00
|
|
|
UserDataFile string `mapstructure:"user_data_file"`
|
2013-07-31 14:41:07 -04:00
|
|
|
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
|
|
|
SSHUsername string `mapstructure:"ssh_username"`
|
|
|
|
SSHPort int `mapstructure:"ssh_port"`
|
|
|
|
SecurityGroupId string `mapstructure:"security_group_id"`
|
|
|
|
SubnetId string `mapstructure:"subnet_id"`
|
|
|
|
VpcId string `mapstructure:"vpc_id"`
|
2013-07-16 00:23:40 -04:00
|
|
|
|
|
|
|
// Unexported fields that are calculated from others
|
|
|
|
sshTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:17:23 -04:00
|
|
|
func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
|
2013-08-08 17:33:29 -04:00
|
|
|
if t == nil {
|
|
|
|
var err error
|
2013-08-15 22:17:23 -04:00
|
|
|
t, err = packer.NewConfigTemplate()
|
2013-08-08 17:33:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
// Defaults
|
|
|
|
if c.SSHPort == 0 {
|
|
|
|
c.SSHPort = 22
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RawSSHTimeout == "" {
|
|
|
|
c.RawSSHTimeout = "1m"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
var err error
|
|
|
|
errs := make([]error, 0)
|
|
|
|
if c.SourceAmi == "" {
|
|
|
|
errs = append(errs, errors.New("A source_ami must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InstanceType == "" {
|
|
|
|
errs = append(errs, errors.New("An instance_type must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHUsername == "" {
|
|
|
|
errs = append(errs, errors.New("An ssh_username must be specified"))
|
|
|
|
}
|
|
|
|
|
2013-08-12 14:52:43 -04:00
|
|
|
if c.UserData != "" && c.UserDataFile != "" {
|
|
|
|
errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
|
|
|
|
} else if c.UserDataFile != "" {
|
|
|
|
if _, err := os.Stat(c.UserDataFile); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 17:33:29 -04:00
|
|
|
templates := map[string]*string{
|
|
|
|
"iam_instance_profile": &c.IamInstanceProfile,
|
|
|
|
"instance_type": &c.InstanceType,
|
|
|
|
"ssh_timeout": &c.RawSSHTimeout,
|
|
|
|
"security_group_id": &c.SecurityGroupId,
|
|
|
|
"ssh_username": &c.SSHUsername,
|
|
|
|
"source_ami": &c.SourceAmi,
|
|
|
|
"subnet_id": &c.SubnetId,
|
|
|
|
"vpc_id": &c.VpcId,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = t.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunConfig) SSHTimeout() time.Duration {
|
|
|
|
return c.sshTimeout
|
|
|
|
}
|