Merge branch 'security-groups' of https://github.com/whiteley/packer into whiteley-security-groups

Conflicts:
	builder/amazon/common/run_config.go
This commit is contained in:
Mitchell Hashimoto 2013-11-05 14:34:12 -08:00
commit c8e911cdc3
7 changed files with 66 additions and 29 deletions

View File

@ -11,19 +11,20 @@ import (
// RunConfig contains configuration for running an instance from a source // RunConfig contains configuration for running an instance from a source
// AMI and details on how to access that launched image. // AMI and details on how to access that launched image.
type RunConfig struct { type RunConfig struct {
SourceAmi string `mapstructure:"source_ami"` SourceAmi string `mapstructure:"source_ami"`
IamInstanceProfile string `mapstructure:"iam_instance_profile"` IamInstanceProfile string `mapstructure:"iam_instance_profile"`
InstanceType string `mapstructure:"instance_type"` InstanceType string `mapstructure:"instance_type"`
UserData string `mapstructure:"user_data"` UserData string `mapstructure:"user_data"`
UserDataFile string `mapstructure:"user_data_file"` UserDataFile string `mapstructure:"user_data_file"`
RawSSHTimeout string `mapstructure:"ssh_timeout"` RawSSHTimeout string `mapstructure:"ssh_timeout"`
SSHUsername string `mapstructure:"ssh_username"` SSHUsername string `mapstructure:"ssh_username"`
SSHPort int `mapstructure:"ssh_port"` SSHPort int `mapstructure:"ssh_port"`
SecurityGroupId string `mapstructure:"security_group_id"` SecurityGroupId string `mapstructure:"security_group_id"`
SubnetId string `mapstructure:"subnet_id"` SecurityGroupIds []string `mapstructure:"security_group_ids"`
TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"` SubnetId string `mapstructure:"subnet_id"`
VpcId string `mapstructure:"vpc_id"` TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
AvailabilityZone string `mapstructure:"availability_zone"` VpcId string `mapstructure:"vpc_id"`
AvailabilityZone string `mapstructure:"availability_zone"`
// Unexported fields that are calculated from others // Unexported fields that are calculated from others
sshTimeout time.Duration sshTimeout time.Duration
@ -74,11 +75,19 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
} }
} }
if c.SecurityGroupId != "" {
if len(c.SecurityGroupIds) > 0 {
errs = append(errs, fmt.Errorf("Only one of security_group_id or security_group_ids can be specified."))
} else {
c.SecurityGroupIds = []string{c.SecurityGroupId}
c.SecurityGroupId = ""
}
}
templates := map[string]*string{ templates := map[string]*string{
"iam_instance_profile": &c.IamInstanceProfile, "iam_instance_profile": &c.IamInstanceProfile,
"instance_type": &c.InstanceType, "instance_type": &c.InstanceType,
"ssh_timeout": &c.RawSSHTimeout, "ssh_timeout": &c.RawSSHTimeout,
"security_group_id": &c.SecurityGroupId,
"ssh_username": &c.SSHUsername, "ssh_username": &c.SSHUsername,
"source_ami": &c.SourceAmi, "source_ami": &c.SourceAmi,
"subnet_id": &c.SubnetId, "subnet_id": &c.SubnetId,
@ -96,6 +105,21 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
} }
} }
sliceTemplates := map[string][]string{
"security_group_ids": c.SecurityGroupIds,
}
for n, slice := range sliceTemplates {
for i, elem := range slice {
var err error
slice[i], err = t.Process(elem, nil)
if err != nil {
errs = append(
errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
}
}
}
c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout) c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
if err != nil { if err != nil {
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err)) errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))

View File

@ -27,7 +27,7 @@ type StepRunSourceInstance struct {
func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction { func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
keyName := state.Get("keyPair").(string) keyName := state.Get("keyPair").(string)
securityGroupId := state.Get("securityGroupId").(string) securityGroupIds := state.Get("securityGroupIds").([]string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
userData := s.UserData userData := s.UserData
@ -41,6 +41,11 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
userData = string(contents) userData = string(contents)
} }
securityGroups := make([]ec2.SecurityGroup, len(securityGroupIds))
for n, securityGroupId := range securityGroupIds {
securityGroups[n] = ec2.SecurityGroup{Id: securityGroupId}
}
runOpts := &ec2.RunInstances{ runOpts := &ec2.RunInstances{
KeyName: keyName, KeyName: keyName,
ImageId: s.SourceAMI, ImageId: s.SourceAMI,
@ -48,7 +53,7 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
UserData: []byte(userData), UserData: []byte(userData),
MinCount: 0, MinCount: 0,
MaxCount: 0, MaxCount: 0,
SecurityGroups: []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}}, SecurityGroups: securityGroups,
IamInstanceProfile: s.IamInstanceProfile, IamInstanceProfile: s.IamInstanceProfile,
SubnetId: s.SubnetId, SubnetId: s.SubnetId,
BlockDevices: s.BlockDevices.BuildLaunchDevices(), BlockDevices: s.BlockDevices.BuildLaunchDevices(),

View File

@ -11,9 +11,9 @@ import (
) )
type StepSecurityGroup struct { type StepSecurityGroup struct {
SecurityGroupId string SecurityGroupIds []string
SSHPort int SSHPort int
VpcId string VpcId string
createdGroupId string createdGroupId string
} }
@ -22,9 +22,9 @@ func (s *StepSecurityGroup) Run(state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if s.SecurityGroupId != "" { if len(s.SecurityGroupIds) > 0 {
log.Printf("Using specified security group: %s", s.SecurityGroupId) log.Printf("Using specified security groups: %v", s.SecurityGroupIds)
state.Put("securityGroupId", s.SecurityGroupId) state.Put("securityGroupIds", s.SecurityGroupIds)
return multistep.ActionContinue return multistep.ActionContinue
} }
@ -69,7 +69,7 @@ func (s *StepSecurityGroup) Run(state multistep.StateBag) multistep.StepAction {
} }
// Set some state data for use in future steps // Set some state data for use in future steps
state.Put("securityGroupId", s.createdGroupId) state.Put("securityGroupIds", []string{s.createdGroupId})
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -88,9 +88,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
KeyPairName: b.config.TemporaryKeyPairName, KeyPairName: b.config.TemporaryKeyPairName,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId, SecurityGroupIds: b.config.SecurityGroupIds,
SSHPort: b.config.SSHPort, SSHPort: b.config.SSHPort,
VpcId: b.config.VpcId, VpcId: b.config.VpcId,
}, },
&awscommon.StepRunSourceInstance{ &awscommon.StepRunSourceInstance{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,

View File

@ -191,9 +191,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
KeyPairName: b.config.TemporaryKeyPairName, KeyPairName: b.config.TemporaryKeyPairName,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId, SecurityGroupIds: b.config.SecurityGroupIds,
SSHPort: b.config.SSHPort, SSHPort: b.config.SSHPort,
VpcId: b.config.VpcId, VpcId: b.config.VpcId,
}, },
&awscommon.StepRunSourceInstance{ &awscommon.StepRunSourceInstance{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,

View File

@ -97,6 +97,10 @@ Optional:
access. Note that if this is specified, you must be sure the security access. Note that if this is specified, you must be sure the security
group allows access to the `ssh_port` given below. group allows access to the `ssh_port` given below.
* `security_group_ids` (array of string) - A list of security groups as
described above. Note that if this is specified, you must omit the
security_group_id.
* `ssh_port` (int) - The port that SSH will be available on. This defaults * `ssh_port` (int) - The port that SSH will be available on. This defaults
to port 22. to port 22.

View File

@ -129,6 +129,10 @@ Optional:
access. Note that if this is specified, you must be sure the security access. Note that if this is specified, you must be sure the security
group allows access to the `ssh_port` given below. group allows access to the `ssh_port` given below.
* `security_group_ids` (array of string) - A list of security groups as
described above. Note that if this is specified, you must omit the
security_group_id.
* `ssh_port` (int) - The port that SSH will be available on. This defaults * `ssh_port` (int) - The port that SSH will be available on. This defaults
to port 22. to port 22.