From e2b21938575c8467e586b116427f384c54dc4767 Mon Sep 17 00:00:00 2001 From: Thirumalesh Aaraveti <97395760+athiruma@users.noreply.github.com> Date: Fri, 26 Apr 2024 22:56:54 +0530 Subject: [PATCH] Added the shutdown_period boolean to stop the running instances (#763) --- .../main/environment_variables.py | 2 +- .../policy/aws/cleanup/instance_run.py | 19 ++++++++++++------- .../policy/azure/cleanup/instance_run.py | 17 +++++++++++------ .../helpers/abstract_policy_operations.py | 1 + .../policy/aws/cleanup/test_instance_run.py | 6 +++++- .../policy/azure/test_instance_run.py | 6 ++++++ 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/cloud_governance/main/environment_variables.py b/cloud_governance/main/environment_variables.py index 85ee2219..1fa66d3c 100644 --- a/cloud_governance/main/environment_variables.py +++ b/cloud_governance/main/environment_variables.py @@ -125,7 +125,7 @@ def __init__(self): self._environment_variables_dict['bucket'] = EnvironmentVariables.get_env('bucket', '') self._environment_variables_dict['file_path'] = EnvironmentVariables.get_env('file_path', '') self._environment_variables_dict['file_name'] = EnvironmentVariables.get_env('file_name', '') - + self._environment_variables_dict['SHUTDOWN_PERIOD'] = EnvironmentVariables.get_boolean_from_environment('SHUTDOWN_PERIOD', False) # common elastic search vars self._environment_variables_dict['upload_data_elk'] = EnvironmentVariables.get_env('upload_data_elk', '') self._environment_variables_dict['upload_data_es'] = EnvironmentVariables.get_env('upload_data_es', '') diff --git a/cloud_governance/policy/aws/cleanup/instance_run.py b/cloud_governance/policy/aws/cleanup/instance_run.py index f9518475..4ff7af3e 100644 --- a/cloud_governance/policy/aws/cleanup/instance_run.py +++ b/cloud_governance/policy/aws/cleanup/instance_run.py @@ -61,12 +61,16 @@ def run_policy_operations(self): running_instances_data = [] for instance in instances: tags = instance.get('Tags', []) + cleanup_result = False if instance.get('State', {}).get('Name') == 'running': running_days = self.calculate_days(instance.get('LaunchTime')) - cleanup_days = self.get_clean_up_days_count(tags=tags) - cleanup_result = self.verify_and_delete_resource( - resource_id=instance.get('InstanceId'), tags=tags, - clean_up_days=cleanup_days) + if self._shutdown_period: + cleanup_days = self.get_clean_up_days_count(tags=tags) + cleanup_result = self.verify_and_delete_resource( + resource_id=instance.get('InstanceId'), tags=tags, + clean_up_days=cleanup_days) + else: + cleanup_days = 0 resource_data = self._get_es_schema( resource_id=instance.get('InstanceId'), skip_policy=self.get_skip_policy_value(tags=tags), @@ -81,12 +85,13 @@ def run_policy_operations(self): region=self._region, cleanup_result=str(cleanup_result), cloud_name=self._cloud_name, ) - if self._force_delete and self._dry_run == 'no': + if self._shutdown_period and self._force_delete and self._dry_run == 'no': resource_data.update({'ForceDeleted': str(self._force_delete)}) running_instances_data.append(resource_data) else: cleanup_days = 0 - self.update_resource_day_count_tag(resource_id=instance.get('InstanceId'), cleanup_days=cleanup_days, - tags=tags) + if self._shutdown_period: + self.update_resource_day_count_tag(resource_id=instance.get('InstanceId'), cleanup_days=cleanup_days, + tags=tags) return running_instances_data diff --git a/cloud_governance/policy/azure/cleanup/instance_run.py b/cloud_governance/policy/azure/cleanup/instance_run.py index 61ae9b94..af5bbb54 100644 --- a/cloud_governance/policy/azure/cleanup/instance_run.py +++ b/cloud_governance/policy/azure/cleanup/instance_run.py @@ -63,18 +63,22 @@ def run_policy_operations(self): for vm in vms_list: status = self._get_instance_status(resource_id=vm.id, vm_name=vm.name) tags = vm.tags if vm.tags else {} + cleanup_result = False if 'running' in status: running_days = self.calculate_days(vm.time_created) - cleanup_days = self.get_clean_up_days_count(tags=tags) - cleanup_result = self.verify_and_delete_resource(resource_id=vm.id, tags=tags, - clean_up_days=cleanup_days) + if self._shutdown_period: + cleanup_days = self.get_clean_up_days_count(tags=tags) + cleanup_result = self.verify_and_delete_resource(resource_id=vm.id, tags=tags, + clean_up_days=cleanup_days) + else: + cleanup_days = 0 resource_data = self._get_es_schema( resource_id=vm.name, skip_policy=self.get_skip_policy_value(tags=tags), user=self.get_tag_name_from_tags(tags=tags, tag_name='User'), launch_time=vm.time_created, resource_type=vm.hardware_profile.vm_size, - resource_state=status if cleanup_result else 'Vm Stopped', + resource_state=status if not cleanup_result else 'Vm Stopped', running_days=running_days, cleanup_days=cleanup_days, dry_run=self._dry_run, name=vm.name, @@ -82,10 +86,11 @@ def run_policy_operations(self): region=vm.location, cleanup_result=str(cleanup_result), cloud_name=self._cloud_name ) - if self._force_delete and self._dry_run == 'no': + if self._shutdown_period and self._force_delete and self._dry_run == 'no': resource_data.update({'ForceDeleted': str(self._force_delete)}) running_vms.append(resource_data) else: cleanup_days = 0 - self.update_resource_day_count_tag(resource_id=vm.id, cleanup_days=cleanup_days, tags=tags) + if self._shutdown_period: + self.update_resource_day_count_tag(resource_id=vm.id, cleanup_days=cleanup_days, tags=tags) return running_vms diff --git a/cloud_governance/policy/helpers/abstract_policy_operations.py b/cloud_governance/policy/helpers/abstract_policy_operations.py index b325bf53..5a69df12 100644 --- a/cloud_governance/policy/helpers/abstract_policy_operations.py +++ b/cloud_governance/policy/helpers/abstract_policy_operations.py @@ -25,6 +25,7 @@ def __init__(self): self._force_delete = self._environment_variables_dict.get('FORCE_DELETE') self._resource_id = self._environment_variables_dict.get('RESOURCE_ID') self._es_upload = ElasticUpload() + self._shutdown_period = self._environment_variables_dict.get('SHUTDOWN_PERIOD') def calculate_days(self, create_date: Union[datetime, str], start_date: Union[datetime, str] = datetime.utcnow()): """ diff --git a/tests/unittest/cloud_governance/policy/aws/cleanup/test_instance_run.py b/tests/unittest/cloud_governance/policy/aws/cleanup/test_instance_run.py index 62a5fbef..951a5e65 100644 --- a/tests/unittest/cloud_governance/policy/aws/cleanup/test_instance_run.py +++ b/tests/unittest/cloud_governance/policy/aws/cleanup/test_instance_run.py @@ -63,7 +63,6 @@ def test_instance_run_alert(): assert len(ec2_client.describe_instances(Filters=[{"Name": "instance-state-name", "Values": ["running"]}])['Reservations']) == 1 - @mock_ec2 @mock_s3 @mock_iam @@ -74,6 +73,7 @@ def test_instance_run_alert_stopped(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 2 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' environment_variables.environment_variables_dict['AWS_DEFAULT_REGION'] = 'ap-south-1' @@ -136,6 +136,7 @@ def test_instance_run_stop_reset(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 2 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' environment_variables.environment_variables_dict['AWS_DEFAULT_REGION'] = 'ap-south-1' @@ -172,6 +173,7 @@ def test_instance_run_stop_start(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 2 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' environment_variables.environment_variables_dict['AWS_DEFAULT_REGION'] = 'ap-south-1' @@ -214,6 +216,7 @@ def test_ec2_force_delete(): :rtype: """ environment_variables.environment_variables_dict['FORCE_DELETE'] = True + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' environment_variables.environment_variables_dict['AWS_DEFAULT_REGION'] = 'ap-south-1' @@ -247,6 +250,7 @@ def test_ec2_force_delete_skip(): :rtype: """ environment_variables.environment_variables_dict['FORCE_DELETE'] = True + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'yes' environment_variables.environment_variables_dict['AWS_DEFAULT_REGION'] = 'ap-south-1' diff --git a/tests/unittest/cloud_governance/policy/azure/test_instance_run.py b/tests/unittest/cloud_governance/policy/azure/test_instance_run.py index 713325c4..c7cb02d6 100644 --- a/tests/unittest/cloud_governance/policy/azure/test_instance_run.py +++ b/tests/unittest/cloud_governance/policy/azure/test_instance_run.py @@ -37,6 +37,7 @@ def test_instance_run_stop_false(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 3 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' mock_virtual_machines = Mock() @@ -60,6 +61,7 @@ def test_instance_run_stopped(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 0 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' mock_virtual_machines = Mock() @@ -84,6 +86,7 @@ def test_instance_run_stopped_skip(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 0 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' mock_virtual_machines = Mock() @@ -108,6 +111,7 @@ def test_instance_run_stopped_test_days(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 3 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' date = (datetime.datetime.utcnow() - datetime.timedelta(days=1)).date() @@ -137,6 +141,7 @@ def test_instance_run_stopped_test_current_day(): :rtype: """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 3 + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['policy'] = 'instance_run' environment_variables.environment_variables_dict['dry_run'] = 'no' date = (datetime.datetime.utcnow()).date() @@ -167,6 +172,7 @@ def test_instance_run_vm_already_stopped(): """ environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 3 environment_variables.environment_variables_dict['policy'] = 'instance_run' + environment_variables.environment_variables_dict['SHUTDOWN_PERIOD'] = True environment_variables.environment_variables_dict['dry_run'] = 'no' date = (datetime.datetime.utcnow()).date() mock_virtual_machines = Mock()