Skip to content

Commit

Permalink
Added the cluster fields (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma authored Aug 12, 2024
1 parent 452671f commit a3b70b0
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions cloud_governance/policy/aws/monitor/cluster_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ class ClusterRun(AWSPolicyOperations):
def __init__(self):
super().__init__()

def __get_creation_date(self, block_devices: list) -> str:
"""
This method returns the creation date of the EC2 instance from the root ebs volume.
:param block_devices:
:return:
"""
for devices in block_devices:
if devices.get('Ebs', {}).get('DeleteOnTermination'):
return devices.get('Ebs', {}).get('AttachTime').strftime('%Y-%m-%d')
return ''

def run_policy_operations(self):
"""
This method returns the running vms in the AAzure cloud and stops based on the action
Expand All @@ -26,27 +37,39 @@ def run_policy_operations(self):
cluster_tag = self._get_cluster_tag(tags=tags).strip()
instance_state = instance.get('State', {}).get('Name')
if cluster_tag and not string_equal_ignore_case(instance_state, 'terminated'):
name_tag = self.get_tag_name_from_tags(tags=tags, tag_name='Name')
launch_time = instance.get('LaunchTime')
running_instances = stopped_instances = 0
running_days = self.calculate_days(instance.get('LaunchTime'))
stopped_date_time = ''
if string_equal_ignore_case(instance_state, 'stopped'):
stopped_instances = 1
state_transition_reason = instance.get('StateTransitionReason')
if state_transition_reason:
extract_data = re.search(r'\((\d{4}-\d{2}-\d{2})', state_transition_reason)
extract_data = re.search(r'\((\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})', state_transition_reason)
if extract_data:
running_days = self.calculate_days(extract_data.group(1).split()[0], start_date=launch_time)
stopped_date_time = extract_data.group(1)
running_days = self.calculate_days(stopped_date_time.split()[0], start_date=launch_time)
instance_state += f"@{extract_data.group(1)}"
else:
running_instances = 1
creation_date = ''
if 'master' in name_tag.lower():
creation_date = self.__get_creation_date(instance.get('BlockDeviceMappings', []))
instance_data = f"{instance.get('InstanceId')}, {self.get_tag_name_from_tags(tags=tags, tag_name='Name')}, {instance.get('InstanceType')}, {instance_state}, {running_days}, {launch_time}"
if cluster_tag in cluster_data:
if creation_date:
cluster_data[cluster_tag]['CreationDate'] = creation_date
cluster_data[cluster_tag]['ClusterState'] = instance_state
cluster_data[cluster_tag]['StoppedDate'] = stopped_date_time
cluster_data[cluster_tag]['Instances'].append(instance_data)
cluster_data[cluster_tag]['InstanceCount'] = len(cluster_data[cluster_tag]['Instances'])
cluster_data[cluster_tag]['Stopped'] = int(cluster_data[cluster_tag]['Stopped']) + stopped_instances
cluster_data[cluster_tag]['Running'] = int(cluster_data[cluster_tag]['Running']) + running_instances
else:
cluster_data[cluster_tag] = {
'ClusterName': cluster_tag.split('/')[-1].upper(),
'ClusterName2': cluster_tag.split('/')[-1].lower(),
'ResourceId': cluster_tag,
'ClusterTag': cluster_tag,
'User': self.get_tag_name_from_tags(tags=tags, tag_name='User'),
Expand All @@ -57,6 +80,11 @@ def run_policy_operations(self):
'InstanceCount': 1,
'Stopped': stopped_instances,
'Running': running_instances,
'index-id': f'{datetime.datetime.utcnow().date()}-{self._cloud_name.lower()}-{self.account.lower()}-{self._region.lower()}-{cluster_tag}'
'index-id': f'{datetime.datetime.now(datetime.timezone.utc).date()}-{self._cloud_name.lower()}-{self.account.lower()}-{self._region.lower()}-{cluster_tag}',
}
if creation_date:
cluster_data[cluster_tag]['creation_date'] = creation_date
cluster_data[cluster_tag]['ClusterState'] = instance_state
cluster_data[cluster_tag]['StoppedDate'] = stopped_date_time

return list(cluster_data.values())

0 comments on commit a3b70b0

Please sign in to comment.