Skip to content

Commit

Permalink
Merge pull request #35 from sbraverman/INFRASYS-7552
Browse files Browse the repository at this point in the history
(13.6.0) INFRASYS-7552 targets cloudwatch alarms specific to stack if stack_name is passed
  • Loading branch information
Steven Braverman committed Jul 13, 2016
2 parents c71b3bd + 47c3a03 commit 087b4e5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
30 changes: 22 additions & 8 deletions License2Deploy/rolling_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self,
self.stack_name = stack_name
self.stack_resources = False
self.autoscaling_groups = False
self.cloudwatch_alarms = False
self.environments = AWSConn.load_config(self.regions_conf).get(self.env)
self.region = AWSConn.determine_region(self.environments)
self.conn_ec2 = AWSConn.aws_conn_ec2(self.region, self.profile_name)
Expand Down Expand Up @@ -93,8 +94,14 @@ def get_autoscale_group_name(self):

def get_autoscaling_group_name_from_cloudformation(self):
if not self.autoscaling_groups:
self.autoscaling_groups = [asg for asg in self.get_stack_resources() if asg['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup']
return [asg['PhysicalResourceId'] for asg in self.autoscaling_groups if self.project in asg['PhysicalResourceId']][0]
self.autoscaling_groups = self.get_resources_from_stack_of_type('AWS::AutoScaling::AutoScalingGroup')
return self.get_resources_physical_ids_by_project(self.autoscaling_groups)[0]

def get_resources_from_stack_of_type(self, resource_type):
return [resource for resource in self.get_stack_resources() if resource['ResourceType'] == resource_type]

def get_resources_physical_ids_by_project(self, resources):
return [resource['PhysicalResourceId'] for resource in resources if self.project in resource['PhysicalResourceId']]

def get_stack_resources(self):
if not self.stack_resources:
Expand Down Expand Up @@ -270,14 +277,21 @@ def launch_new_instances(self, group_name): # pragma: no cover
logging.error('Load balancer healthcheck has exceeded the timeout threshold. Rolling back.')
self.revert_deployment()

def get_cloudwatch_alarms_from_stack(self):
if not self.cloudwatch_alarms:
self.cloudwatch_alarms = self.get_resources_from_stack_of_type('AWS::CloudWatch::Alarm')
return self.get_resources_physical_ids_by_project(self.cloudwatch_alarms)

def retrieve_project_cloudwatch_alarms(self):
""" Retrieve all the Cloud-Watch alarms for the given project and environment """
try:
if self.stack_name:
return self.get_cloudwatch_alarms_from_stack()
all_cloud_watch_alarms = self.conn_cloudwatch.describe_alarms()
except Exception as e:
logging.error("Error while retrieving the list of cloud-watch alarms. Error: {0}".format(e))
exit(self.exit_error_code)
project_cloud_watch_alarms = filter(lambda alarm: self.project in alarm.name and self.env in alarm.name, all_cloud_watch_alarms)
project_cloud_watch_alarms = [alarm.name for alarm in all_cloud_watch_alarms if self.project in alarm.name and self.env in alarm.name]
if len(project_cloud_watch_alarms) == 0:
logging.info("No cloud-watch alarm found")
return project_cloud_watch_alarms
Expand All @@ -287,8 +301,8 @@ def disable_project_cloudwatch_alarms(self):
project_cloud_watch_alarms = self.retrieve_project_cloudwatch_alarms()
for alarm in project_cloud_watch_alarms:
try:
self.conn_cloudwatch.disable_alarm_actions(alarm.name)
logging.info("Disabled cloud-watch alarm. {0}".format(alarm.name))
self.conn_cloudwatch.disable_alarm_actions(alarm)
logging.info("Disabled cloud-watch alarm. {0}".format(alarm))
except Exception as e:
logging.error("Unable to disable the cloud-watch alarm, please investigate: {0}".format(e))
exit(self.exit_error_code)
Expand All @@ -297,10 +311,10 @@ def enable_project_cloudwatch_alarms(self):
''' Enable all the cloud watch alarms '''
project_cloud_watch_alarms = self.retrieve_project_cloudwatch_alarms()
for alarm in project_cloud_watch_alarms:
logging.info("Found an alarm. {0}".format(alarm.name))
logging.info("Found an alarm. {0}".format(alarm))
try:
self.conn_cloudwatch.enable_alarm_actions(alarm.name)
logging.info("Enabled cloud-watch alarm. {0}".format(alarm.name))
self.conn_cloudwatch.enable_alarm_actions(alarm)
logging.info("Enabled cloud-watch alarm. {0}".format(alarm))
except Exception as e:
logging.error("Unable to enable the cloud-watch alarm, please investigate: {0}".format(e))
exit(self.exit_error_code)
Expand Down
5 changes: 5 additions & 0 deletions tests/cloudformation_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ def test_get_autoscaling_group_name_via_cloudformation(self):
self.assertTrue(self.rolling_deploy.autoscaling_groups)
self.assertEquals(asg_name, 'dnbi-backend-qa-servergmsextenderASGqa-QO8UAEHUFJD')

def test_retrieve_project_cloudwatch_alarms(self):
self.assertEquals(self.rolling_deploy.cloudwatch_alarms, False)
cloudwatch_alarms = self.rolling_deploy.retrieve_project_cloudwatch_alarms()
self.assertEquals(cloudwatch_alarms, ['dnbi-servergmsextender-SCALEDOWNALARMqa-123123', 'dnbi-servergmsextender-SCALEUPALARMqa-4asdhjks'])

64 changes: 64 additions & 0 deletions tests/test_data/cloudformation.ListStackResources_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,70 @@
},
"ResourceStatus": "UPDATE_COMPLETE",
"LogicalResourceId": "LC2"
},
{
"ResourceType": "AWS::CloudWatch::Alarm",
"PhysicalResourceId": "dnbi-servergmsextender-SCALEDOWNALARMqa-123123",
"LastUpdatedTimestamp": {
"hour": 16,
"__class__": "datetime",
"month": 4,
"second": 20,
"microsecond": 217000,
"year": 2016,
"day": 6,
"minute": 39
},
"ResourceStatus": "CREATE_COMPLETE",
"LogicalResourceId": "dnbiSCALEDOWNALARMqa"
},
{
"ResourceType": "AWS::CloudWatch::Alarm",
"PhysicalResourceId": "dnbi-servergmsextender-SCALEUPALARMqa-4asdhjks",
"LastUpdatedTimestamp": {
"hour": 16,
"__class__": "datetime",
"month": 4,
"second": 21,
"microsecond": 108000,
"year": 2016,
"day": 6,
"minute": 39
},
"ResourceStatus": "CREATE_COMPLETE",
"LogicalResourceId": "dnbiSCALEUPALARMqa"
},
{
"ResourceType": "AWS::CloudWatch::Alarm",
"PhysicalResourceId": "dnbi-projectbrave-SCALEDOWNALARMqa-asdasd123",
"LastUpdatedTimestamp": {
"hour": 16,
"__class__": "datetime",
"month": 4,
"second": 20,
"microsecond": 217000,
"year": 2016,
"day": 6,
"minute": 39
},
"ResourceStatus": "CREATE_COMPLETE",
"LogicalResourceId": "dnbiSCALEDOWNALARMqa"
},
{
"ResourceType": "AWS::CloudWatch::Alarm",
"PhysicalResourceId": "dnbi-projectbrave-SCALEUPALARMqa-4asdasdasda",
"LastUpdatedTimestamp": {
"hour": 16,
"__class__": "datetime",
"month": 4,
"second": 21,
"microsecond": 108000,
"year": 2016,
"day": 6,
"minute": 39
},
"ResourceStatus": "CREATE_COMPLETE",
"LogicalResourceId": "dnbiSCALEUPALARMqa"
}
]
}
Expand Down

0 comments on commit 087b4e5

Please sign in to comment.