Skip to content

Commit

Permalink
extend tag logic to all resrouces
Browse files Browse the repository at this point in the history
  • Loading branch information
morga471 committed Mar 14, 2025
1 parent ef549f5 commit 4a54e01
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
6 changes: 5 additions & 1 deletion local-app/python-tools/gfl-resource-actions/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import datetime
import config
from aws_utils import create_boto3_client
from aws_utils import create_boto3_client, get_partition_for_region
from logging_utils import setup_logging

logger = setup_logging()
Expand Down Expand Up @@ -44,3 +44,7 @@ def log_action(self, resource_id, region, action, resource_name=None, dry_run=Fa
def should_exclude(self, resource):
"""Check if resource should be excluded based on tags."""
return config.EXCLUSION_TAG in resource.get("tags", {})

def get_partition_for_region(self, region):
"""Get AWS partition for region."""
return get_partition_for_region(region)
11 changes: 11 additions & 0 deletions local-app/python-tools/gfl-resource-actions/managers/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ def start(self, instances, dry_run=False):
logger.info(f"{'[DRY RUN] Would start' if dry_run else 'Starting'} EC2 instance {instance['id']} in region {region}")
if not dry_run:
ec2_client.start_instances(InstanceIds=[instance['id']])

# Remove the stop tag after successful start
logger.info(f"Removing {config.STOP_TAG} tag from EC2 instance {instance['id']}")
ec2_client.delete_tags(
Resources=[instance['id']],
Tags=[
{
'Key': config.STOP_TAG
}
]
)

# Log with detailed information
self.log_action(instance['id'], region, "start", dry_run=dry_run)
Expand Down
18 changes: 18 additions & 0 deletions local-app/python-tools/gfl-resource-actions/managers/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def stop(self, clusters, dry_run=False):

timestamp = self.get_timestamp()

# Tag the cluster before scaling down
logger.info(f"{'[DRY RUN] Would tag' if dry_run else 'Tagging'} EKS cluster {cluster['name']} with {config.STOP_TAG}={timestamp}")
if not dry_run:
eks_client.tag_resource(
resourceArn=f"arn:{self.get_partition_for_region(region)}:eks:{region}:{self.account_id}:cluster/{cluster['name']}",
tags={
config.STOP_TAG: timestamp
}
)

# Scale down each nodegroup
for nodegroup in nodegroups:
ng_info = eks_client.describe_nodegroup(
Expand Down Expand Up @@ -188,6 +198,14 @@ def start(self, clusters, dry_run=False):
else:
logger.info(f"ASG {asg_name} already has capacity. Skipping scale up.")

# Remove the stop tag after successful scale up
if not dry_run:
logger.info(f"Removing {config.STOP_TAG} tag from EKS cluster {cluster['name']}")
eks_client.untag_resource(
resourceArn=f"arn:{self.get_partition_for_region(region)}:eks:{region}:{self.account_id}:cluster/{cluster['name']}",
tagKeys=[config.STOP_TAG]
)

# Log with detailed information
self.log_action(cluster['name'], region, "scale_up", dry_run=dry_run)

Expand Down
33 changes: 33 additions & 0 deletions local-app/python-tools/gfl-resource-actions/managers/emr.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ def stop(self, clusters, dry_run=False):

timestamp = self.get_timestamp()

# Tag the cluster before stopping
logger.info(f"{'[DRY RUN] Would tag' if dry_run else 'Tagging'} EMR cluster {cluster['name']} ({cluster['id']}) with {config.STOP_TAG}={timestamp}")
if not dry_run:
emr_client.add_tags(
ResourceId=cluster['id'],
Tags=[
{
'Key': config.STOP_TAG,
'Value': timestamp
}
]
)

logger.info(f"{'[DRY RUN] Would stop' if dry_run else 'Stopping'} EMR cluster {cluster['name']} ({cluster['id']}) in region {region}")
if not dry_run:
emr_client.stop_cluster(ClusterId=cluster['id'])
Expand All @@ -46,6 +59,19 @@ def stop(self, clusters, dry_run=False):

timestamp = self.get_timestamp()

# Tag the cluster before terminating
logger.info(f"{'[DRY RUN] Would tag' if dry_run else 'Tagging'} EMR cluster {cluster['name']} ({cluster['id']}) with {config.STOP_TAG}={timestamp}")
if not dry_run:
emr_client.add_tags(
ResourceId=cluster['id'],
Tags=[
{
'Key': config.STOP_TAG,
'Value': timestamp
}
]
)

logger.info(f"{'[DRY RUN] Would terminate' if dry_run else 'Terminating'} EMR cluster {cluster['name']} ({cluster['id']}) in region {region} (cannot stop a cluster in {cluster['status']} state)")
if not dry_run:
emr_client.terminate_job_flows(JobFlowIds=[cluster['id']])
Expand Down Expand Up @@ -74,6 +100,13 @@ def start(self, clusters, dry_run=False):
logger.info(f"{'[DRY RUN] Would start' if dry_run else 'Starting'} EMR cluster {cluster['name']} ({cluster['id']}) in region {region}")
if not dry_run:
emr_client.start_cluster(ClusterId=cluster['id'])

# Remove the stop tag after successful start
logger.info(f"Removing {config.STOP_TAG} tag from EMR cluster {cluster['id']}")
emr_client.remove_tags(
ResourceId=cluster['id'],
TagKeys=[config.STOP_TAG]
)

# Log with detailed information
self.log_action(cluster['id'], region, "start", resource_name=cluster['name'], dry_run=dry_run)
Expand Down
20 changes: 20 additions & 0 deletions local-app/python-tools/gfl-resource-actions/managers/rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ def stop(self, instances, dry_run=False):
# Create ISO 8601 timestamp
timestamp = self.get_timestamp()

# Tag the instance before stopping
logger.info(f"{'[DRY RUN] Would tag' if dry_run else 'Tagging'} RDS instance {instance['id']} with {config.STOP_TAG}={timestamp}")
if not dry_run:
rds_client.add_tags_to_resource(
ResourceName=f"arn:{self.get_partition_for_region(region)}:rds:{region}:{self.account_id}:db:{instance['id']}",
Tags=[
{
'Key': config.STOP_TAG,
'Value': timestamp
}
]
)

logger.info(f"{'[DRY RUN] Would stop' if dry_run else 'Stopping'} RDS instance {instance['id']} in region {region}")
if not dry_run:
rds_client.stop_db_instance(DBInstanceIdentifier=instance['id'])
Expand All @@ -52,6 +65,13 @@ def start(self, instances, dry_run=False):
logger.info(f"{'[DRY RUN] Would start' if dry_run else 'Starting'} RDS instance {instance['id']} in region {region}")
if not dry_run:
rds_client.start_db_instance(DBInstanceIdentifier=instance['id'])

# Remove the stop tag after successful start
logger.info(f"Removing {config.STOP_TAG} tag from RDS instance {instance['id']}")
rds_client.remove_tags_from_resource(
ResourceName=f"arn:{self.get_partition_for_region(region)}:rds:{region}:{self.account_id}:db:{instance['id']}",
TagKeys=[config.STOP_TAG]
)

# Log with detailed information
self.log_action(instance['id'], region, "start", dry_run=dry_run)
Expand Down

0 comments on commit 4a54e01

Please sign in to comment.