def _launch_auto_scaling_group(instance): _setup_auto_scaling_connection() group_name = instance['auto-scaling-group'] launcher = instance['launch-configuration'] launcher = auto_scaling.get_all_launch_configurations(names=[ launcher ]) if len(launcher) == 0: params = xl.translate(_create_launch_configuration_mapping, instance) launcher = boto.ec2.autoscale.launchconfig.LaunchConfiguration( auto_scaling, **params) _try_and_retry("Creating launch configuration", lambda: auto_scaling.create_launch_configuration(launcher), lambda err: err.status == 400 and err.error_message.startswith( 'Invalid IamInstanceProfile: ')) else: logger.debug('Launch configuration %s already exists, skipping', launcher) group = auto_scaling.get_all_groups(names=[ group_name ]) if len(group) == 0: # TODO: Make this a plugin if 'load-balancers' in instance: lb = instance['load-balancers'] if not isinstance(lb, list): lb = [ lb ] logger.debug('Checking that load balancers exist: %s', lb) hc2002.resource.load_balancer.list(lb) params = xl.translate(_create_auto_scaling_group_mapping, instance) group = boto.ec2.autoscale.group.AutoScalingGroup( auto_scaling, **params) auto_scaling.create_auto_scaling_group(group) else: logger.debug('Auto-scaling group already exists, skipping') # TODO: Handle load balancers # TODO: Handle updates to both launch configuration and group if 'notification' in instance: notification = instance['notification'] if not isinstance(notification['type'], list): notification['type'] = [ notification['type'] ] auto_scaling.put_notification_configuration(group_name, notification['topic'], notification['type']) if 'schedule' in instance: for name, schedule in instance['schedule'].iteritems(): params = xl.translate(_scheduled_auto_scaling_action, schedule) params['AutoScalingGroupName'] = group_name params['ScheduledActionName'] = name response = auto_scaling.make_request( 'PutScheduledUpdateGroupAction', params) if response.status != 200: logger.warning('Failed to schedule %s scaling action', name) logger.warning(response.read()) return True
def _block_devices(destination, value): bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping() for k, v in value.iteritems(): params = xl.translate(_block_device_mapping, v, { 'delete_on_termination': True }) bdm[k] = boto.ec2.blockdevicemapping.BlockDeviceType(**params) destination[key] = bdm
def _block_devices(destination, value): bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping() for k, v in value.iteritems(): params = xl.translate(_block_device_mapping, v, {'delete_on_termination': True}) bdm[k] = boto.ec2.blockdevicemapping.BlockDeviceType(**params) destination[key] = bdm
def _launch_spot_instance(instance): _setup_ec2_connection() params = xl.translate(_launch_spot_instance_mapping, instance) return _try_and_retry("Creating spot instance request", lambda: ec2.request_spot_instances(**params), lambda err: err.status == 400 and err.error_message.endswith( 'Invalid IAM Instance Profile name'))
def _launch_spot_instance(instance): _setup_ec2_connection() params = xl.translate(_launch_spot_instance_mapping, instance) return _try_and_retry( "Creating spot instance request", lambda: ec2.request_spot_instances(**params), lambda err: err.status == 400 and err.error_message.endswith( 'Invalid IAM Instance Profile name'))
def _launch_instance(instance): _setup_ec2_connection() params = xl.translate(_launch_instance_mapping, instance) reservation = _try_and_retry("Launching instances", lambda: ec2.run_instances(**params), lambda err: err.status == 400 and err.error_message.endswith( 'Invalid IAM Instance Profile name')) if 'tags' in instance: instances = [ inst.id for inst in reservation.instances ] _try_and_retry("Adding tags to instance(s)", lambda: ec2.create_tags(instances, instance['tags']), lambda err: err.status == 400 and err.error_code == 'InvalidInstanceID.NotFound') return reservation
def _launch_instance(instance): _setup_ec2_connection() params = xl.translate(_launch_instance_mapping, instance) reservation = _try_and_retry( "Launching instances", lambda: ec2.run_instances(**params), lambda err: err.status == 400 and err.error_message.endswith( 'Invalid IAM Instance Profile name')) if 'tags' in instance: instances = [inst.id for inst in reservation.instances] _try_and_retry( "Adding tags to instance(s)", lambda: ec2.create_tags(instances, instance['tags']), lambda err: err.status == 400 and err.error_code == 'InvalidInstanceID.NotFound') return reservation
def _launch_auto_scaling_group(instance): _setup_auto_scaling_connection() group_name = instance['auto-scaling-group'] launcher = instance['launch-configuration'] launcher = auto_scaling.get_all_launch_configurations(names=[launcher]) if len(launcher) == 0: params = xl.translate(_create_launch_configuration_mapping, instance) launcher = boto.ec2.autoscale.launchconfig.LaunchConfiguration( auto_scaling, **params) _try_and_retry( "Creating launch configuration", lambda: auto_scaling.create_launch_configuration(launcher), lambda err: err.status == 400 and err.error_message.startswith( 'Invalid IamInstanceProfile: ')) else: logger.debug('Launch configuration %s already exists, skipping', launcher) group = auto_scaling.get_all_groups(names=[group_name]) if len(group) == 0: # TODO: Make this a plugin if 'load-balancers' in instance: lb = instance['load-balancers'] if not isinstance(lb, list): lb = [lb] logger.debug('Checking that load balancers exist: %s', lb) hc2002.resource.load_balancer.list(lb) params = xl.translate(_create_auto_scaling_group_mapping, instance) group = boto.ec2.autoscale.group.AutoScalingGroup( auto_scaling, **params) auto_scaling.create_auto_scaling_group(group) else: logger.debug('Auto-scaling group already exists, skipping') # TODO: Handle load balancers # TODO: Handle updates to both launch configuration and group if 'notification' in instance: notification = instance['notification'] if not isinstance(notification['type'], list): notification['type'] = [notification['type']] auto_scaling.put_notification_configuration(group_name, notification['topic'], notification['type']) if 'schedule' in instance: for name, schedule in instance['schedule'].iteritems(): params = xl.translate(_scheduled_auto_scaling_action, schedule) params['AutoScalingGroupName'] = group_name params['ScheduledActionName'] = name response = auto_scaling.make_request( 'PutScheduledUpdateGroupAction', params) if response.status != 200: logger.warning('Failed to schedule %s scaling action', name) logger.warning(response.read()) return True