<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import datetime <EOL> import logging <EOL> import json <EOL> import sys <EOL> import email . mime . multipart <EOL> log = logging . getLogger ( __name__ ) <EOL> DATE_FORMAT = "<STR_LIT>" <EOL> import yaml <EOL> import salt . ext . six as six <EOL> try : <EOL> import boto <EOL> import boto . ec2 <EOL> import boto . ec2 . instance <EOL> import boto . ec2 . blockdevicemapping as blockdevicemapping <EOL> import boto . ec2 . autoscale as autoscale <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . CRITICAL ) <EOL> HAS_BOTO = True <EOL> except ImportError : <EOL> HAS_BOTO = False <EOL> import salt . utils . odict as odict <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if not HAS_BOTO : <EOL> return ( False , '<STR_LIT>' ) <EOL> __utils__ [ '<STR_LIT>' ] ( __name__ , '<STR_LIT>' , module = '<STR_LIT>' , pack = __salt__ ) <EOL> setattr ( sys . modules [ __name__ ] , '<STR_LIT>' , <EOL> __utils__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) ) <EOL> return True <EOL> def exists ( name , region = None , key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> try : <EOL> _conn = conn . get_all_groups ( names = [ name ] ) <EOL> if _conn : <EOL> return True <EOL> else : <EOL> msg = '<STR_LIT>' . format ( region ) <EOL> log . debug ( msg ) <EOL> return False <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> return False <EOL> def get_config ( name , region = None , key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> try : <EOL> asg = conn . get_all_groups ( names = [ name ] ) <EOL> if asg : <EOL> asg = asg [ <NUM_LIT:0> ] <EOL> else : <EOL> return { } <EOL> ret = odict . OrderedDict ( ) <EOL> attrs = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> for attr in attrs : <EOL> if attr == '<STR_LIT>' : <EOL> _tags = [ ] <EOL> for tag in asg . tags : <EOL> _tag = odict . OrderedDict ( ) <EOL> _tag [ '<STR_LIT:key>' ] = tag . key <EOL> _tag [ '<STR_LIT:value>' ] = tag . value <EOL> _tag [ '<STR_LIT>' ] = tag . propagate_at_launch <EOL> _tags . append ( _tag ) <EOL> ret [ '<STR_LIT>' ] = _tags <EOL> elif attr == '<STR_LIT>' : <EOL> ret [ attr ] = getattr ( asg , attr ) . split ( '<STR_LIT:U+002C>' ) <EOL> elif attr == '<STR_LIT>' : <EOL> suspended_processes = getattr ( asg , attr ) <EOL> ret [ attr ] = sorted ( [ x . process_name for x in suspended_processes ] ) <EOL> else : <EOL> ret [ attr ] = getattr ( asg , attr ) <EOL> policies = conn . get_all_policies ( as_group = name ) <EOL> ret [ "<STR_LIT>" ] = [ ] <EOL> for policy in policies : <EOL> ret [ "<STR_LIT>" ] . append ( <EOL> dict ( [ <EOL> ( "<STR_LIT:name>" , policy . name ) , <EOL> ( "<STR_LIT>" , policy . adjustment_type ) , <EOL> ( "<STR_LIT>" , policy . scaling_adjustment ) , <EOL> ( "<STR_LIT>" , policy . min_adjustment_step ) , <EOL> ( "<STR_LIT>" , policy . cooldown ) <EOL> ] ) <EOL> ) <EOL> actions = conn . get_all_scheduled_actions ( as_group = name ) <EOL> ret [ '<STR_LIT>' ] = { } <EOL> for action in actions : <EOL> end_time = None <EOL> if action . end_time : <EOL> end_time = action . end_time . isoformat ( ) <EOL> ret [ '<STR_LIT>' ] [ action . name ] = dict ( [ <EOL> ( "<STR_LIT>" , action . min_size ) , <EOL> ( "<STR_LIT>" , action . max_size ) , <EOL> ( "<STR_LIT>" , int ( action . desired_capacity ) ) , <EOL> ( "<STR_LIT>" , action . start_time . isoformat ( ) ) , <EOL> ( "<STR_LIT>" , end_time ) , <EOL> ( "<STR_LIT>" , action . recurrence ) <EOL> ] ) <EOL> return ret <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> return { } <EOL> def create ( name , launch_config_name , availability_zones , min_size , max_size , <EOL> desired_capacity = None , load_balancers = None , default_cooldown = None , <EOL> health_check_type = None , health_check_period = None , <EOL> placement_group = None , vpc_zone_identifier = None , tags = None , <EOL> termination_policies = None , suspended_processes = None , <EOL> scaling_policies = None , scheduled_actions = None , region = None , <EOL> notification_arn = None , notification_types = None , <EOL> key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> if isinstance ( availability_zones , six . string_types ) : <EOL> availability_zones = json . loads ( availability_zones ) <EOL> if isinstance ( load_balancers , six . string_types ) : <EOL> load_balancers = json . loads ( load_balancers ) <EOL> if isinstance ( vpc_zone_identifier , six . string_types ) : <EOL> vpc_zone_identifier = json . loads ( vpc_zone_identifier ) <EOL> if isinstance ( tags , six . string_types ) : <EOL> tags = json . loads ( tags ) <EOL> _tags = [ ] <EOL> if tags : <EOL> for tag in tags : <EOL> try : <EOL> key = tag . get ( '<STR_LIT:key>' ) <EOL> except KeyError : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> try : <EOL> value = tag . get ( '<STR_LIT:value>' ) <EOL> except KeyError : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> propagate_at_launch = tag . get ( '<STR_LIT>' , False ) <EOL> _tag = autoscale . Tag ( key = key , value = value , resource_id = name , <EOL> propagate_at_launch = propagate_at_launch ) <EOL> _tags . append ( _tag ) <EOL> if isinstance ( termination_policies , six . string_types ) : <EOL> termination_policies = json . loads ( termination_policies ) <EOL> if isinstance ( suspended_processes , six . string_types ) : <EOL> suspended_processes = json . loads ( suspended_processes ) <EOL> if isinstance ( scheduled_actions , six . string_types ) : <EOL> scheduled_actions = json . loads ( scheduled_actions ) <EOL> try : <EOL> _asg = autoscale . AutoScalingGroup ( <EOL> name = name , launch_config = launch_config_name , <EOL> availability_zones = availability_zones , <EOL> min_size = min_size , max_size = max_size , <EOL> desired_capacity = desired_capacity , load_balancers = load_balancers , <EOL> default_cooldown = default_cooldown , <EOL> health_check_type = health_check_type , <EOL> health_check_period = health_check_period , <EOL> placement_group = placement_group , tags = _tags , <EOL> vpc_zone_identifier = vpc_zone_identifier , <EOL> termination_policies = termination_policies , <EOL> suspended_processes = suspended_processes ) <EOL> conn . create_auto_scaling_group ( _asg ) <EOL> _create_scaling_policies ( conn , name , scaling_policies ) <EOL> _create_scheduled_actions ( conn , name , scheduled_actions ) <EOL> if notification_arn and notification_types : <EOL> conn . put_notification_configuration ( _asg , notification_arn , notification_types ) <EOL> log . info ( '<STR_LIT>' . format ( name ) ) <EOL> return True <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> log . error ( msg ) <EOL> return False <EOL> def update ( name , launch_config_name , availability_zones , min_size , max_size , <EOL> desired_capacity = None , load_balancers = None , default_cooldown = None , <EOL> health_check_type = None , health_check_period = None , <EOL> placement_group = None , vpc_zone_identifier = None , tags = None , <EOL> termination_policies = None , suspended_processes = None , <EOL> scaling_policies = None , scheduled_actions = None , <EOL> notification_arn = None , notification_types = None , <EOL> region = None , key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> if not conn : <EOL> return False , "<STR_LIT>" <EOL> if isinstance ( availability_zones , six . string_types ) : <EOL> availability_zones = json . loads ( availability_zones ) <EOL> if isinstance ( load_balancers , six . string_types ) : <EOL> load_balancers = json . loads ( load_balancers ) <EOL> if isinstance ( vpc_zone_identifier , six . string_types ) : <EOL> vpc_zone_identifier = json . loads ( vpc_zone_identifier ) <EOL> if isinstance ( tags , six . string_types ) : <EOL> tags = json . loads ( tags ) <EOL> _tags = [ ] <EOL> if tags : <EOL> for tag in tags : <EOL> try : <EOL> key = tag . get ( '<STR_LIT:key>' ) <EOL> except KeyError : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False , "<STR_LIT>" . format ( tag ) <EOL> try : <EOL> value = tag . get ( '<STR_LIT:value>' ) <EOL> except KeyError : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False , "<STR_LIT>" . format ( tag ) <EOL> propagate_at_launch = tag . get ( '<STR_LIT>' , False ) <EOL> _tag = autoscale . Tag ( key = key , value = value , resource_id = name , <EOL> propagate_at_launch = propagate_at_launch ) <EOL> _tags . append ( _tag ) <EOL> if isinstance ( termination_policies , six . string_types ) : <EOL> termination_policies = json . loads ( termination_policies ) <EOL> if isinstance ( suspended_processes , six . string_types ) : <EOL> suspended_processes = json . loads ( suspended_processes ) <EOL> if isinstance ( scheduled_actions , six . string_types ) : <EOL> scheduled_actions = json . loads ( scheduled_actions ) <EOL> try : <EOL> _asg = autoscale . AutoScalingGroup ( <EOL> connection = conn , <EOL> name = name , launch_config = launch_config_name , <EOL> availability_zones = availability_zones , <EOL> min_size = min_size , max_size = max_size , <EOL> desired_capacity = desired_capacity , load_balancers = load_balancers , <EOL> default_cooldown = default_cooldown , <EOL> health_check_type = health_check_type , <EOL> health_check_period = health_check_period , <EOL> placement_group = placement_group , tags = _tags , <EOL> vpc_zone_identifier = vpc_zone_identifier , <EOL> termination_policies = termination_policies ) <EOL> if notification_arn and notification_types : <EOL> conn . put_notification_configuration ( _asg , notification_arn , notification_types ) <EOL> _asg . update ( ) <EOL> if _tags : <EOL> conn . create_or_update_tags ( _tags ) <EOL> _asg . resume_processes ( ) <EOL> if suspended_processes is not None and len ( suspended_processes ) > <NUM_LIT:0> : <EOL> _asg . suspend_processes ( suspended_processes ) <EOL> log . info ( '<STR_LIT>' . format ( name ) ) <EOL> for policy in conn . get_all_policies ( as_group = name ) : <EOL> conn . delete_policy ( policy . name , autoscale_group = name ) <EOL> _create_scaling_policies ( conn , name , scaling_policies ) <EOL> for scheduled_action in conn . get_all_scheduled_actions ( as_group = name ) : <EOL> conn . delete_scheduled_action ( <EOL> scheduled_action . name , autoscale_group = name <EOL> ) <EOL> _create_scheduled_actions ( conn , name , scheduled_actions ) <EOL> return True , '<STR_LIT>' <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> log . error ( msg ) <EOL> return False , str ( e ) <EOL> def _create_scaling_policies ( conn , as_name , scaling_policies ) : <EOL> '<STR_LIT>' <EOL> if scaling_policies : <EOL> for policy in scaling_policies : <EOL> policy = autoscale . policy . ScalingPolicy ( <EOL> name = policy [ "<STR_LIT:name>" ] , <EOL> as_name = as_name , <EOL> adjustment_type = policy [ "<STR_LIT>" ] , <EOL> scaling_adjustment = policy [ "<STR_LIT>" ] , <EOL> min_adjustment_step = policy . get ( "<STR_LIT>" , None ) , <EOL> cooldown = policy [ "<STR_LIT>" ] ) <EOL> conn . create_scaling_policy ( policy ) <EOL> def _create_scheduled_actions ( conn , as_name , scheduled_actions ) : <EOL> '''<STR_LIT>''' <EOL> if scheduled_actions : <EOL> for name , action in scheduled_actions . iteritems ( ) : <EOL> if '<STR_LIT>' in action and isinstance ( action [ '<STR_LIT>' ] , six . string_types ) : <EOL> action [ '<STR_LIT>' ] = datetime . datetime . strptime ( <EOL> action [ '<STR_LIT>' ] , DATE_FORMAT <EOL> ) <EOL> if '<STR_LIT>' in action and isinstance ( action [ '<STR_LIT>' ] , six . string_types ) : <EOL> action [ '<STR_LIT>' ] = datetime . datetime . strptime ( <EOL> action [ '<STR_LIT>' ] , DATE_FORMAT <EOL> ) <EOL> conn . create_scheduled_group_action ( as_name , name , <EOL> desired_capacity = action . get ( '<STR_LIT>' ) , <EOL> min_size = action . get ( '<STR_LIT>' ) , <EOL> max_size = action . get ( '<STR_LIT>' ) , <EOL> start_time = action . get ( '<STR_LIT>' ) , <EOL> end_time = action . get ( '<STR_LIT>' ) , <EOL> recurrence = action . get ( '<STR_LIT>' ) <EOL> ) <EOL> def delete ( name , force = False , region = None , key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> try : <EOL> conn . delete_auto_scaling_group ( name , force ) <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> log . info ( msg ) <EOL> return True <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> log . error ( msg ) <EOL> return False <EOL> def get_cloud_init_mime ( cloud_init ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( cloud_init , six . string_types ) : <EOL> cloud_init = json . loads ( cloud_init ) <EOL> _cloud_init = email . mime . multipart . MIMEMultipart ( ) <EOL> if '<STR_LIT>' in cloud_init : <EOL> for script_name , script in six . iteritems ( cloud_init [ '<STR_LIT>' ] ) : <EOL> _script = email . mime . text . MIMEText ( script , '<STR_LIT>' ) <EOL> _cloud_init . attach ( _script ) <EOL> if '<STR_LIT>' in cloud_init : <EOL> for script_name , script in six . iteritems ( cloud_init [ '<STR_LIT>' ] ) : <EOL> _script = email . mime . text . MIMEText ( script , '<STR_LIT>' ) <EOL> _cloud_init . attach ( _script ) <EOL> if '<STR_LIT>' in cloud_init : <EOL> cloud_config = cloud_init [ '<STR_LIT>' ] <EOL> _cloud_config = email . mime . text . MIMEText ( _safe_dump ( cloud_config ) , <EOL> '<STR_LIT>' ) <EOL> _cloud_init . attach ( _cloud_config ) <EOL> return _cloud_init . as_string ( ) <EOL> def _safe_dump ( data ) : <EOL> def ordered_dict_presenter ( dumper , data ) : <EOL> return dumper . represent_dict ( six . iteritems ( data ) ) <EOL> yaml . add_representer ( odict . OrderedDict , ordered_dict_presenter , <EOL> Dumper = yaml . dumper . SafeDumper ) <EOL> return yaml . safe_dump ( data , default_flow_style = False ) <EOL> def launch_configuration_exists ( name , region = None , key = None , keyid = None , <EOL> profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> try : <EOL> lc = conn . get_all_launch_configurations ( names = [ name ] ) <EOL> if lc : <EOL> return True <EOL> else : <EOL> msg = '<STR_LIT>' . format ( region ) <EOL> log . debug ( msg ) <EOL> return False <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> return False <EOL> def create_launch_configuration ( name , image_id , key_name = None , <EOL> security_groups = None , user_data = None , <EOL> instance_type = '<STR_LIT>' , kernel_id = None , <EOL> ramdisk_id = None , block_device_mappings = None , <EOL> instance_monitoring = False , spot_price = None , <EOL> instance_profile_name = None , <EOL> ebs_optimized = False , <EOL> associate_public_ip_address = None , <EOL> volume_type = None , delete_on_termination = True , <EOL> iops = None , use_block_device_types = False , <EOL> region = None , key = None , keyid = None , <EOL> profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> if isinstance ( security_groups , six . string_types ) : <EOL> security_groups = json . loads ( security_groups ) <EOL> if isinstance ( block_device_mappings , six . string_types ) : <EOL> block_device_mappings = json . loads ( block_device_mappings ) <EOL> _bdms = [ ] <EOL> if block_device_mappings : <EOL> _block_device_map = blockdevicemapping . BlockDeviceMapping ( ) <EOL> for block_device_dict in block_device_mappings : <EOL> for block_device , attributes in six . iteritems ( block_device_dict ) : <EOL> _block_device = blockdevicemapping . EBSBlockDeviceType ( ) <EOL> for attribute , value in six . iteritems ( attributes ) : <EOL> setattr ( _block_device , attribute , value ) <EOL> _block_device_map [ block_device ] = _block_device <EOL> _bdms = [ _block_device_map ] <EOL> lc = autoscale . LaunchConfiguration ( <EOL> name = name , image_id = image_id , key_name = key_name , <EOL> security_groups = security_groups , user_data = user_data , <EOL> instance_type = instance_type , kernel_id = kernel_id , <EOL> ramdisk_id = ramdisk_id , block_device_mappings = _bdms , <EOL> instance_monitoring = instance_monitoring , spot_price = spot_price , <EOL> instance_profile_name = instance_profile_name , <EOL> ebs_optimized = ebs_optimized , <EOL> associate_public_ip_address = associate_public_ip_address , <EOL> volume_type = volume_type , delete_on_termination = delete_on_termination , <EOL> iops = iops , use_block_device_types = use_block_device_types ) <EOL> try : <EOL> conn . create_launch_configuration ( lc ) <EOL> log . info ( '<STR_LIT>' . format ( name ) ) <EOL> return True <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> log . error ( msg ) <EOL> return False <EOL> def delete_launch_configuration ( name , region = None , key = None , keyid = None , <EOL> profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> try : <EOL> conn . delete_launch_configuration ( name ) <EOL> log . info ( '<STR_LIT>' . format ( name ) ) <EOL> return True <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> log . error ( msg ) <EOL> return False <EOL> def get_scaling_policy_arn ( as_group , scaling_policy_name , region = None , <EOL> key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> policies = conn . get_all_policies ( as_group = as_group ) <EOL> for policy in policies : <EOL> if policy . name == scaling_policy_name : <EOL> return policy . policy_arn <EOL> log . error ( '<STR_LIT>' . format ( as_group ) ) <EOL> return None <EOL> def get_instances ( name , lifecycle_state = "<STR_LIT>" , health_status = "<STR_LIT>" , <EOL> attribute = "<STR_LIT>" , attributes = None , region = None , <EOL> key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = _get_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> ec2_conn = _get_ec2_conn ( region = region , key = key , keyid = keyid , profile = profile ) <EOL> try : <EOL> asgs = conn . get_all_groups ( names = [ name ] ) <EOL> except boto . exception . BotoServerError as e : <EOL> log . debug ( e ) <EOL> return False <EOL> if len ( asgs ) != <NUM_LIT:1> : <EOL> log . debug ( "<STR_LIT>" . format ( name , [ asg . name for asg in asgs ] ) ) <EOL> return False <EOL> asg = asgs [ <NUM_LIT:0> ] <EOL> instance_ids = [ ] <EOL> for i in asg . instances : <EOL> if lifecycle_state is not None and i . lifecycle_state != lifecycle_state : <EOL> continue <EOL> if health_status is not None and i . health_status != health_status : <EOL> continue <EOL> instance_ids . append ( i . instance_id ) <EOL> instances = ec2_conn . get_only_instances ( instance_ids = instance_ids ) <EOL> if attributes : <EOL> return [ [ getattr ( instance , attr ) . encode ( "<STR_LIT:ascii>" ) for attr in attributes ] for instance in instances ] <EOL> else : <EOL> return [ getattr ( instance , attribute ) . encode ( "<STR_LIT:ascii>" ) for instance in instances if getattr ( instance , attribute ) ] <EOL> return [ getattr ( instance , attribute ) . encode ( "<STR_LIT:ascii>" ) for instance in instances ] </s>
