boto_asg alarms from pillars

This commit is contained in:
Ryan Lane 2014-12-23 12:55:15 -08:00
parent bc669152b3
commit 1d5660f1c8

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
'''
Manage Autoscale Groups
=======================
.. versionadded:: 2014.7.0
@ -108,6 +109,7 @@ import hashlib
import logging
import salt.ext.six as six
from salt.ext.six.moves import zip
import salt.utils.dictupdate as dictupdate
log = logging.getLogger(__name__)
@ -137,6 +139,8 @@ def present(
termination_policies=None,
suspended_processes=None,
scaling_policies=None,
alarms=None,
alarms_from_pillar="boto_asg_alarms",
region=None,
key=None,
keyid=None,
@ -215,6 +219,16 @@ def present(
List of scaling policies. Each policy is a dict of key-values described by
http://boto.readthedocs.org/en/latest/ref/autoscale.html#boto.ec2.autoscale.policy.ScalingPolicy
alarms:
a dictionary of name->boto_cloudwatch_alarm sections to be associated with this ASG.
All attributes should be specified except for dimension which will be
automatically set to this ASG.
See the boto_cloudwatch_alarm state for information about these attributes.
alarms_from_pillar:
name of pillar dict that contains alarm settings. Alarms defined for this specific
state will override those from pillar.
region
The region to connect to.
@ -243,7 +257,7 @@ def present(
if launch_config:
launch_config_name = launch_config_name + "-" + hashlib.md5(str(launch_config)).hexdigest()
args = {
'name': launch_config_name,
'name': launch_config_name,
'region': region,
'key': key,
'keyid': keyid,
@ -358,18 +372,18 @@ def present(
ret['result'] = None
return ret
updated, msg = __salt__['boto_asg.update'](name, launch_config_name,
availability_zones, min_size,
max_size, desired_capacity,
load_balancers,
default_cooldown,
health_check_type,
health_check_period,
placement_group,
vpc_zone_identifier, tags,
termination_policies,
suspended_processes,
scaling_policies, region,
key, keyid, profile)
availability_zones, min_size,
max_size, desired_capacity,
load_balancers,
default_cooldown,
health_check_type,
health_check_period,
placement_group,
vpc_zone_identifier, tags,
termination_policies,
suspended_processes,
scaling_policies, region,
key, keyid, profile)
if asg["launch_config_name"] != launch_config_name:
# delete the old launch_config_name
deleted = __salt__['boto_asg.delete_launch_configuration'](asg["launch_config_name"], region, key, keyid, profile)
@ -388,9 +402,48 @@ def present(
ret['comment'] = msg
else:
ret['comment'] = 'Autoscale group present.'
# add in alarms
_ret = _alarms_present(name, alarms, alarms_from_pillar, region, key, keyid, profile)
ret['changes'] = dictupdate.update(ret['changes'], _ret['changes'])
ret['comment'] = ' '.join([ret['comment'], _ret['comment']])
return ret
def _alarms_present(name, alarms, alarms_from_pillar, region, key, keyid, profile):
'''helper method for present. ensure that cloudwatch_alarms are set'''
# load data from alarms_from_pillar
tmp = __salt__['config.option'](alarms_from_pillar, {})
# merge with data from alarms
if alarms:
tmp = dictupdate.update(tmp, alarms)
# set alarms, using boto_cloudwatch_alarm.present
merged_return_value = {'name': name, 'result': True, 'comment': '', 'changes': {}}
for _, info in tmp.items():
# add asg to name and description
info["name"] = name + " " + info["name"]
info["attributes"]["description"] = name + " " + info["attributes"]["description"]
# add dimension attribute
info["attributes"]["dimensions"] = {"AutoScalingGroupName": [name]}
# set alarm
kwargs = {
"name": info["name"],
"attributes": info["attributes"],
"region": region,
"key": key,
"keyid": keyid,
"profile": profile,
}
ret = __salt__["state.single"]('boto_cloudwatch_alarm.present', **kwargs)
results = ret.values()[0]
if not results["result"]:
merged_return_value["result"] = False
if results.get("changes", {}) != {}:
merged_return_value["changes"][info["name"]] = results["changes"]
if "comment" in results:
merged_return_value["comment"] += results["comment"]
return merged_return_value
def _recursive_compare(v1, v2):
"return v1 == v2. compares list, dict, OrderedDict, recursively"
if isinstance(v1, list):