Skip to content
Snippets Groups Projects
Commit b1293b2d authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Merge branch 'create-tags-on-ec2-instance-creation' into 'main'

Create tags on creation of ec2 instance

See merge request gitlab-org/ci-cd/docker-machine!87
parents e698fda2 93a53a3a
Branches
Tags
No related merge requests found
......@@ -621,6 +621,8 @@ func (d *Driver) Create() error {
func (d *Driver) innerCreate() error {
log.Infof("Launching instance...")
tagSpecifications := d.createTagSpecifications()
if err := d.createKeyPair(); err != nil {
return fmt.Errorf("unable to create key pair: %s", err)
}
......@@ -658,6 +660,7 @@ func (d *Driver) innerCreate() error {
if d.RequestSpotInstance {
req := ec2.RequestSpotInstancesInput{
TagSpecifications: tagSpecifications,
LaunchSpecification: &ec2.RequestSpotLaunchSpecification{
ImageId: &d.AMI,
Placement: &ec2.SpotPlacement{
......@@ -747,6 +750,7 @@ func (d *Driver) innerCreate() error {
}
} else {
inst, err := d.getClient().RunInstances(&ec2.RunInstancesInput{
TagSpecifications: tagSpecifications,
ImageId: &d.AMI,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
......@@ -797,13 +801,6 @@ func (d *Driver) innerCreate() error {
d.PrivateIPAddress,
)
log.Debug("Settings tags for instance")
err := d.configureTags(d.Tags)
if err != nil {
return fmt.Errorf("Unable to tag instance %s: %s", d.InstanceId, err)
}
return nil
}
......@@ -1104,16 +1101,15 @@ func (d *Driver) securityGroupAvailableFunc(id string) func() bool {
}
}
func (d *Driver) configureTags(tagGroups string) error {
tags := []*ec2.Tag{}
func (d *Driver) createTagSpecifications() []*ec2.TagSpecification {
var tags []*ec2.Tag
tags = append(tags, &ec2.Tag{
Key: aws.String("Name"),
Value: &d.MachineName,
})
if tagGroups != "" {
t := strings.Split(tagGroups, ",")
if d.Tags != "" {
t := strings.Split(d.Tags, ",")
if len(t) > 0 && len(t)%2 != 0 {
log.Warnf("Tags are not key value in pairs. %d elements found", len(t))
}
......@@ -1125,16 +1121,20 @@ func (d *Driver) configureTags(tagGroups string) error {
}
}
_, err := d.getClient().CreateTags(&ec2.CreateTagsInput{
Resources: d.getTagResources(),
return []*ec2.TagSpecification{
{
ResourceType: aws.String("instance"),
Tags: tags,
})
if err != nil {
return err
},
{
ResourceType: aws.String("volume"),
Tags: tags,
},
{
ResourceType: aws.String("network-interface"),
Tags: tags,
},
}
return nil
}
func (d *Driver) getTagResources() []*string {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment