From c59c8a60c59225f85eb49c36f00e1acd1d17cf66 Mon Sep 17 00:00:00 2001 From: Xinjiang Shao Date: Sun, 3 Jan 2016 16:17:52 -0600 Subject: [PATCH] Add --amazonec2-tags for extra tags assignment support. Ref #1986 Signed-off-by: Xinjiang Shao --- docs/drivers/aws.md | 3 +++ drivers/amazonec2/amazonec2.go | 49 +++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/docs/drivers/aws.md b/docs/drivers/aws.md index 648fef6109..5136325c91 100644 --- a/docs/drivers/aws.md +++ b/docs/drivers/aws.md @@ -42,6 +42,7 @@ This example assumes the VPC ID was found in the `a` availability zone. Use the` - `--amazonec2-zone`: The AWS zone to launch the instance in (i.e. one of a,b,c,d,e). - `--amazonec2-subnet-id`: AWS VPC subnet id. - `--amazonec2-security-group`: AWS VPC security group name. +- `--amazonec2-tags`: AWS extra tag key-value pairs (comma-separated, e.g. key1,value1,key2,value2). - `--amazonec2-instance-type`: The instance type to run. - `--amazonec2-device-name`: The root device name of the instance. - `--amazonec2-root-size`: The root disk size of the instance (in GB). @@ -54,6 +55,7 @@ This example assumes the VPC ID was found in the `a` availability zone. Use the` - `--amazonec2-private-address-only`: Use the private IP address only. - `--amazonec2-monitoring`: Enable CloudWatch Monitoring. + By default, the Amazon EC2 driver will use a daily image of Ubuntu 14.04 LTS. | Region | AMI ID | @@ -83,6 +85,7 @@ Environment variables and default values: | `--amazonec2-zone` | `AWS_ZONE` | `a` | | `--amazonec2-subnet-id` | `AWS_SUBNET_ID` | - | | `--amazonec2-security-group` | `AWS_SECURITY_GROUP` | `docker-machine` | +| `--amazonec2-tags` | `AWS_TAGS` | - | | `--amazonec2-instance-type` | `AWS_INSTANCE_TYPE` | `t2.micro` | | `--amazonec2-device-name` | `AWS_DEVICE_NAME` | `/dev/sda1` | | `--amazonec2-root-size` | `AWS_ROOT_SIZE` | `16` | diff --git a/drivers/amazonec2/amazonec2.go b/drivers/amazonec2/amazonec2.go index b0f268e6d7..d5172c6fa2 100644 --- a/drivers/amazonec2/amazonec2.go +++ b/drivers/amazonec2/amazonec2.go @@ -65,6 +65,7 @@ type Driver struct { PrivateIPAddress string SecurityGroupId string SecurityGroupName string + Tags string ReservationId string DeviceName string RootSize int64 @@ -131,6 +132,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag { Value: defaultSecurityGroup, EnvVar: "AWS_SECURITY_GROUP", }, + mcnflag.StringFlag{ + Name: "amazonec2-tags", + Usage: "AWS Tags (e.g. key1,value1,key2,value2)", + EnvVar: "AWS_TAGS", + }, mcnflag.StringFlag{ Name: "amazonec2-instance-type", Usage: "AWS instance type", @@ -231,6 +237,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { d.VpcId = flags.String("amazonec2-vpc-id") d.SubnetId = flags.String("amazonec2-subnet-id") d.SecurityGroupName = flags.String("amazonec2-security-group") + d.Tags = flags.String("amazonec2-tags") zone := flags.String("amazonec2-zone") d.Zone = zone[:] d.DeviceName = flags.String("amazonec2-device-name") @@ -518,13 +525,8 @@ func (d *Driver) Create() error { ) log.Debug("Settings tags for instance") - _, err := d.getClient().CreateTags(&ec2.CreateTagsInput{ - Resources: []*string{&d.InstanceId}, - Tags: []*ec2.Tag{{ - Key: aws.String("Name"), - Value: &d.MachineName, - }}, - }) + err := d.configureTags(d.Tags) + if err != nil { return fmt.Errorf("Unable to tag instance %s: %s", d.InstanceId, err) } @@ -754,6 +756,39 @@ func (d *Driver) securityGroupAvailableFunc(id string) func() bool { } } +func (d *Driver) configureTags(tagGroups string) error { + + tags := []*ec2.Tag{} + tags = append(tags, &ec2.Tag{ + Key: aws.String("Name"), + Value: &d.MachineName, + }) + + if tagGroups != "" { + t := strings.Split(tagGroups, ",") + if len(t) > 0 && len(t)%2 != 0 { + log.Warnf("Tags are not key value in pairs. %d elements found", len(t)) + } + for i := 0; i < len(t)-1; i += 2 { + tags = append(tags, &ec2.Tag{ + Key: &t[i], + Value: &t[i+1], + }) + } + } + + _, err := d.getClient().CreateTags(&ec2.CreateTagsInput{ + Resources: []*string{&d.InstanceId}, + Tags: tags, + }) + + if err != nil { + return err + } + + return nil +} + func (d *Driver) configureSecurityGroup(groupName string) error { log.Debugf("configuring security group in %s", d.VpcId)