Commit 8aabe01b authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazon/common: run_tags to apply to launch [GH-722]

parent 4c8b8d4f
...@@ -47,6 +47,8 @@ IMPROVEMENTS: ...@@ -47,6 +47,8 @@ IMPROVEMENTS:
"Packer Builder" so that they are easily recognizable. [GH-642] "Packer Builder" so that they are easily recognizable. [GH-642]
* builder/amazon/all: Copying AMIs to multiple regions now happens * builder/amazon/all: Copying AMIs to multiple regions now happens
in parallel. [GH-495] in parallel. [GH-495]
* builder/amazon/all: Ability to specify "run\_tags" to tag the instance
while running. [GH-722]
* builder/digitalocean: Private networking support. [GH-698] * builder/digitalocean: Private networking support. [GH-698]
* builder/docker: A "run\_command" can be specified, configuring how * builder/docker: A "run\_command" can be specified, configuring how
the container is started. [GH-648] the container is started. [GH-648]
......
...@@ -11,21 +11,22 @@ import ( ...@@ -11,21 +11,22 @@ 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 {
AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"` AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"`
AvailabilityZone string `mapstructure:"availability_zone"` AvailabilityZone string `mapstructure:"availability_zone"`
IamInstanceProfile string `mapstructure:"iam_instance_profile"` IamInstanceProfile string `mapstructure:"iam_instance_profile"`
InstanceType string `mapstructure:"instance_type"` InstanceType string `mapstructure:"instance_type"`
SourceAmi string `mapstructure:"source_ami"` RunTags map[string]string `mapstructure:"run_tags"`
RawSSHTimeout string `mapstructure:"ssh_timeout"` SourceAmi string `mapstructure:"source_ami"`
SSHUsername string `mapstructure:"ssh_username"` RawSSHTimeout string `mapstructure:"ssh_timeout"`
SSHPort int `mapstructure:"ssh_port"` SSHUsername string `mapstructure:"ssh_username"`
SecurityGroupId string `mapstructure:"security_group_id"` SSHPort int `mapstructure:"ssh_port"`
SecurityGroupIds []string `mapstructure:"security_group_ids"` 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"`
UserData string `mapstructure:"user_data"` TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
UserDataFile string `mapstructure:"user_data_file"` UserData string `mapstructure:"user_data"`
VpcId string `mapstructure:"vpc_id"` UserDataFile string `mapstructure:"user_data_file"`
VpcId string `mapstructure:"vpc_id"`
// Unexported fields that are calculated from others // Unexported fields that are calculated from others
sshTimeout time.Duration sshTimeout time.Duration
...@@ -121,6 +122,27 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { ...@@ -121,6 +122,27 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
} }
} }
newTags := make(map[string]string)
for k, v := range c.RunTags {
k, err := t.Process(k, nil)
if err != nil {
errs = append(errs,
fmt.Errorf("Error processing tag key %s: %s", k, err))
continue
}
v, err := t.Process(v, nil)
if err != nil {
errs = append(errs,
fmt.Errorf("Error processing tag value '%s': %s", v, err))
continue
}
newTags[k] = v
}
c.RunTags = newTags
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))
......
...@@ -9,17 +9,18 @@ import ( ...@@ -9,17 +9,18 @@ import (
) )
type StepRunSourceInstance struct { type StepRunSourceInstance struct {
AssociatePublicIpAddress bool
AvailabilityZone string
BlockDevices BlockDevices
Debug bool Debug bool
ExpectedRootDevice string ExpectedRootDevice string
InstanceType string InstanceType string
UserData string
UserDataFile string
SourceAMI string
IamInstanceProfile string IamInstanceProfile string
SourceAMI string
SubnetId string SubnetId string
AssociatePublicIpAddress bool Tags map[string]string
AvailabilityZone string UserData string
BlockDevices BlockDevices UserDataFile string
instance *ec2.Instance instance *ec2.Instance
} }
...@@ -92,8 +93,13 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi ...@@ -92,8 +93,13 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
s.instance = &runResp.Instances[0] s.instance = &runResp.Instances[0]
ui.Message(fmt.Sprintf("Instance ID: %s", s.instance.InstanceId)) ui.Message(fmt.Sprintf("Instance ID: %s", s.instance.InstanceId))
_, err = ec2conn.CreateTags( ec2Tags := make([]ec2.Tag, 1, len(s.Tags)+1)
[]string{s.instance.InstanceId}, []ec2.Tag{{"Name", "Packer Builder"}}) ec2Tags[0] = ec2.Tag{"Name", "Packer Builder"}
for k, v := range s.Tags {
ec2Tags = append(ec2Tags, ec2.Tag{k, v})
}
_, err = ec2conn.CreateTags([]string{s.instance.InstanceId}, ec2Tags)
if err != nil { if err != nil {
ui.Message( ui.Message(
fmt.Sprintf("Failed to tag a Name on the builder instance: %s", err)) fmt.Sprintf("Failed to tag a Name on the builder instance: %s", err))
......
...@@ -104,6 +104,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -104,6 +104,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone, AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Tags: b.config.RunTags,
}, },
&common.StepConnectSSH{ &common.StepConnectSSH{
SSHAddress: awscommon.SSHAddress(ec2conn, b.config.SSHPort), SSHAddress: awscommon.SSHAddress(ec2conn, b.config.SSHPort),
......
...@@ -208,6 +208,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -208,6 +208,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
AvailabilityZone: b.config.AvailabilityZone, AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Tags: b.config.RunTags,
}, },
&common.StepConnectSSH{ &common.StepConnectSSH{
SSHAddress: awscommon.SSHAddress(ec2conn, b.config.SSHPort), SSHAddress: awscommon.SSHAddress(ec2conn, b.config.SSHPort),
......
...@@ -91,6 +91,10 @@ Optional: ...@@ -91,6 +91,10 @@ Optional:
block device mappings to the launch instance. The block device mappings are block device mappings to the launch instance. The block device mappings are
the same as `ami_block_device_mappings` above. the same as `ami_block_device_mappings` above.
* `run_tags` (object of key/value strings) - Tags to apply to the instance
that is _launched_ to create the AMI. These tags are _not_ applied to
the resulting AMI unless they're duplicated in `tags`.
* `security_group_id` (string) - The ID (_not_ the name) of the security * `security_group_id` (string) - The ID (_not_ the name) of the security
group to assign to the instance. By default this is not set and Packer group to assign to the instance. By default this is not set and Packer
will automatically create a new temporary security group to allow SSH will automatically create a new temporary security group to allow SSH
......
...@@ -130,6 +130,10 @@ Optional: ...@@ -130,6 +130,10 @@ Optional:
block device mappings to the launch instance. The block device mappings are block device mappings to the launch instance. The block device mappings are
the same as `ami_block_device_mappings` above. the same as `ami_block_device_mappings` above.
* `run_tags` (object of key/value strings) - Tags to apply to the instance
that is _launched_ to create the AMI. These tags are _not_ applied to
the resulting AMI unless they're duplicated in `tags`.
* `security_group_id` (string) - The ID (_not_ the name) of the security * `security_group_id` (string) - The ID (_not_ the name) of the security
group to assign to the instance. By default this is not set and Packer group to assign to the instance. By default this is not set and Packer
will automatically create a new temporary security group to allow SSH will automatically create a new temporary security group to allow SSH
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment