Skip to content

Commit

Permalink
Issue #3534: Tag compute resources according to the tool's metadata -…
Browse files Browse the repository at this point in the history
… tag ebs volumes when reassign
  • Loading branch information
ekazachkova committed May 28, 2024
1 parent a159aaa commit ea1a306
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion scripts/autoscaling/aws/node_reassign.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,36 @@ def build_custom_tags(input_tags):
return instance_tags


def find_volumes_ids(instance_description):
volume_ids = []
volumes = instance_description.get('BlockDeviceMappings', [])
for volume in volumes:
volume_id = volume.get('Ebs', {}).get('VolumeId')
if volume_id:
volume_ids.append(volume_id)
return volume_ids


def find_and_tag_instance(ec2, old_id, new_id, custom_tags):
response = ec2.describe_instances(Filters=[{'Name': 'tag:Name', 'Values': [old_id]},
{'Name': 'instance-state-name', 'Values': ['pending', 'running']}])
tags = [{'Key': 'Name', 'Value': new_id}]
if custom_tags:
tags.extend(custom_tags)
if len(response['Reservations']) > 0:
ins_id = response['Reservations'][0]['Instances'][0]['InstanceId']
instance_description = response['Reservations'][0]['Instances'][0]
ins_id = instance_description['InstanceId']
ec2.create_tags(
Resources=[ins_id],
Tags=tags
)
if custom_tags:
volume_ids = find_volumes_ids(instance_description)
if volume_ids:
ec2.create_tags(
Resources=volume_ids,
Tags=custom_tags
)
return ins_id
else:
raise RuntimeError("Failed to find instance {}".format(old_id))
Expand Down

0 comments on commit ea1a306

Please sign in to comment.