def remove_instance_from_elb(**_): elb_name = \ utils.get_external_resource_id_or_raise( 'elb_name', ctx.target.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'instance_id', ctx.source.instance) instance_list = [instance_id] lb = _get_existing_elb(elb_name) ctx.logger.info('Attemping to remove instance: {0} from elb {1}' .format(instance_id, elb_name)) try: lb.deregister_instances(instance_list) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: if instance_id in _get_instance_list(): raise RecoverableError('Instance not removed from Load Balancer ' '{0}'.format(str(e))) ctx.logger.info( 'Instance {0} removed from Load Balancer {1}.' .format(instance_id, elb_name)) _remove_instance_from_elb_list_in_properties(instance_id)
def disassociate(**_): """ Disassocates an Elastic IP created by Cloudify from an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'disassociate elasticip', ctx.source.instance) elasticip = \ utils.get_external_resource_id_or_raise( 'disassociate elasticip', ctx.target.instance) if _disassociate_external_elasticip_or_instance(): return ctx.logger.debug('Disassociating Elastic IP {0}'.format(elasticip)) try: ec2_client.disassociate_address(public_ip=elasticip) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.unassign_runtime_property_from_resource('public_ip_address', ctx.source.instance) ctx.logger.info('Disassociated Elastic IP {0} from instance {1}.'.format( elasticip, instance_id))
def remove_instance_from_elb(**_): elb_name = \ utils.get_external_resource_id_or_raise( 'elb_name', ctx.target.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'instance_id', ctx.source.instance) instance_list = [instance_id] lb = _get_existing_elb(elb_name) ctx.logger.info('Attemping to remove instance: {0} from elb {1}'.format( instance_id, elb_name)) try: lb.deregister_instances(instance_list) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: if instance_id in _get_instance_list(): raise RecoverableError('Instance not removed from Load Balancer ' '{0}'.format(str(e))) ctx.logger.info('Instance {0} removed from Load Balancer {1}.'.format( instance_id, elb_name)) _remove_instance_from_elb_list_in_properties(instance_id)
def associate(**_): """ Associates an Elastic IP created by Cloudify with an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() instance_id = utils.get_external_resource_id_or_raise("associate elasticip", ctx.source.instance) elasticip = utils.get_external_resource_id_or_raise("associate elasticip", ctx.target.instance) if _associate_external_elasticip_or_instance(elasticip): return kw = dict(instance_id=instance_id, public_ip=elasticip) if constants.ALLOCATION_ID in ctx.target.instance.runtime_properties: kw.pop("public_ip") kw.update({constants.ALLOCATION_ID: ctx.target.instance.runtime_properties[constants.ALLOCATION_ID]}) ctx.logger.debug("Attempting to associate: {0}".format(kw)) try: ec2_client.associate_address(**kw) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError("{0}".format(str(e))) ctx.logger.info("Associated Elastic IP {0} with instance {1}.".format(elasticip, instance_id)) ctx.source.instance.runtime_properties["public_ip_address"] = elasticip ctx.target.instance.runtime_properties["instance_id"] = ctx.source.instance.runtime_properties[ constants.EXTERNAL_RESOURCE_ID ] vpc_id = ctx.source.instance.runtime_properties.get("vpc_id") if vpc_id: ctx.target.instance.runtime_properties["vpc_id"] = vpc_id
def disassociate(**_): """ Disassocates an Elastic IP created by Cloudify from an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() instance_id = utils.get_external_resource_id_or_raise("disassociate elasticip", ctx.source.instance) elasticip = utils.get_external_resource_id_or_raise("disassociate elasticip", ctx.target.instance) if _disassociate_external_elasticip_or_instance(): return elasticip_object = _get_address_object_by_id(elasticip) if not elasticip_object: raise NonRecoverableError("no matching elastic ip in account: {0}".format(elasticip)) disassociate_args = dict(public_ip=elasticip_object.public_ip, association_id=elasticip_object.association_id) ctx.logger.debug("Disassociating Elastic IP {0}".format(elasticip)) try: ec2_client.disassociate_address(**disassociate_args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError("{0}".format(str(e))) utils.unassign_runtime_property_from_resource("public_ip_address", ctx.source.instance) utils.unassign_runtime_property_from_resource("instance_id", ctx.target.instance) if ctx.source.instance.runtime_properties.get("vpc_id"): utils.unassign_runtime_property_from_resource("vpc_id", ctx.target.instance) ctx.logger.info("Disassociated Elastic IP {0} from instance {1}.".format(elasticip, instance_id))
def disassociate(**_): """ Disassocates an Elastic IP created by Cloudify from an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'disassociate elasticip', ctx.source.instance) elasticip = \ utils.get_external_resource_id_or_raise( 'disassociate elasticip', ctx.target.instance) if _disassociate_external_elasticip_or_instance(): return ctx.logger.debug('Disassociating Elastic IP {0}'.format(elasticip)) try: ec2_client.disassociate_address(public_ip=elasticip) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.unassign_runtime_property_from_resource( 'public_ip_address', ctx.source.instance) ctx.logger.info( 'Disassociated Elastic IP {0} from instance {1}.' .format(elasticip, instance_id))
def associate(**_): """ Associates an Elastic IP created by Cloudify with an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'associate elasticip', ctx.source.instance) elasticip = \ utils.get_external_resource_id_or_raise( 'associate elasticip', ctx.target.instance) if _associate_external_elasticip_or_instance(elasticip): return ctx.logger.debug( 'Attempting to associate elasticip {0} and instance {1}.' .format(elasticip, instance_id)) try: ec2_client.associate_address( instance_id=instance_id, public_ip=elasticip) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.info( 'Associated Elastic IP {0} with instance {1}.' .format(elasticip, instance_id)) ctx.source.instance.runtime_properties['public_ip_address'] = elasticip
def associate(**_): """ Associates an Elastic IP created by Cloudify with an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'associate elasticip', ctx.source.instance) elasticip = \ utils.get_external_resource_id_or_raise( 'associate elasticip', ctx.target.instance) if _associate_external_elasticip_or_instance(elasticip): return ctx.logger.debug( 'Attempting to associate elasticip {0} and instance {1}.'.format( elasticip, instance_id)) try: ec2_client.associate_address(instance_id=instance_id, public_ip=elasticip) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.info('Associated Elastic IP {0} with instance {1}.'.format( elasticip, instance_id)) ctx.source.instance.runtime_properties['public_ip_address'] = elasticip
def add_instance_to_elb(**_): elb_name = \ utils.get_external_resource_id_or_raise( 'elb_name', ctx.target.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'instance_id', ctx.source.instance) ctx.logger.info('Attemping to remove instance: {0} from elb {1}'.format( instance_id, elb_name)) lb = _get_existing_elb(elb_name) try: lb.register_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: raise NonRecoverableError('Instance not added to Load Balancer ' '{0}'.format(str(e))) ctx.logger.info('Instance {0} added to Load Balancer {1}.'.format( instance_id, elb_name)) _add_instance_to_elb_list_in_properties(instance_id)
def add_instance_to_elb(**_): elb_name = \ utils.get_external_resource_id_or_raise( 'elb_name', ctx.target.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'instance_id', ctx.source.instance) ctx.logger.info('Attemping to remove instance: {0} from elb {1}' .format(instance_id, elb_name)) lb = _get_existing_elb(elb_name) try: lb.register_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: raise NonRecoverableError('Instance not added to Load Balancer ' '{0}'.format(str(e))) ctx.logger.info( 'Instance {0} added to Load Balancer {1}.' .format(instance_id, elb_name)) _add_instance_to_elb_list_in_properties(instance_id)
def attach(**_): """ Attaches an EBS volume created by Cloudify with an EC2 Instance that was also created by Cloudify. """ volume_id = \ utils.get_external_resource_id_or_raise( 'attach volume', ctx.source.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'attach volume', ctx.target.instance) if ctx.source.node.properties[constants.ZONE] not in \ ctx.target.instance.runtime_properties.get('placement'): ctx.logger.info( 'Volume Zone {0} and Instance Zone {1} do not match. ' 'This may lead to an error.'.format( ctx.source.node.properties[constants.ZONE], ctx.target.instance.runtime_properties.get('placement') ) ) if _attach_external_volume_or_instance(instance_id): return volume_object = _get_volumes_from_id(volume_id) if not volume_object: raise NonRecoverableError( 'EBS volume {0} not found in account.'.format(volume_id)) if constants.VOLUME_CREATING in volume_object.update(): return ctx.operation.retry( message='Waiting for volume to be ready. ' 'Volume in state {0}' .format(volume_object.status)) elif constants.VOLUME_AVAILABLE not in volume_object.update(): raise NonRecoverableError( 'Cannot attach Volume {0} because it is in state {1}.' .format(volume_object.id, volume_object.status)) ctx.logger.debug( 'Attempting to attach volume {0} to instance {1}.' .format(volume_id, instance_id)) try: volume_object.attach( instance_id, ctx.source.node.properties['device']) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.source.instance.runtime_properties['instance_id'] = \ instance_id ctx.logger.info( 'Attached EBS volume {0} with instance {1}.' .format(volume_id, instance_id))
def attach(**_): """ Attaches an EBS volume created by Cloudify with an EC2 Instance that was also created by Cloudify. """ volume_id = \ utils.get_external_resource_id_or_raise( 'attach volume', ctx.source.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'attach volume', ctx.target.instance) if ctx.source.node.properties['zone'] not in \ ctx.target.instance.runtime_properties.get('placement'): ctx.logger.info( 'Volume Zone {0} and Instance Zone {1} do not match. ' 'This may lead to an error.'.format( ctx.source.node.properties['zone'], ctx.target.instance.runtime_properties.get('placement'))) if _attach_external_volume_or_instance(instance_id): return volume_object = _get_volumes_from_id(volume_id) if not volume_object: raise NonRecoverableError( 'EBS volume {0} not found in account.'.format(volume_id)) if constants.VOLUME_CREATING in volume_object.update(): return ctx.operation.retry( message='Waiting for volume to be ready. ' 'Volume in state {0}'.format(volume_object.status)) elif constants.VOLUME_AVAILABLE not in volume_object.update(): raise NonRecoverableError( 'Cannot attach Volume {0} because it is in state {1}.'.format( volume_object.id, volume_object.status)) ctx.logger.debug('Attempting to attach volume {0} to instance {1}.'.format( volume_id, instance_id)) try: volume_object.attach(instance_id, ctx.source.node.properties['device']) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.source.instance.runtime_properties['instance_id'] = \ instance_id ctx.logger.info('Attached EBS volume {0} with instance {1}.'.format( volume_id, instance_id))
def associate(**_): """ Associates an Elastic IP created by Cloudify with an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'associate elasticip', ctx.source.instance) elasticip = \ utils.get_external_resource_id_or_raise( 'associate elasticip', ctx.target.instance) if _associate_external_elasticip_or_instance(elasticip): return ctx.logger.debug( 'Attempting to associate elasticip {0} and instance {1}.' .format(elasticip, instance_id)) address_object = _get_address_object_by_id(elasticip) try: instance_ins =ec2_client.get_only_instances([instance_id])[0] interfaces_list=instance_ins.interfaces interface_id=None for interfaces_ins in interfaces_list: ctx.logger.info('interfaces_ins.description {0} with interface_id {1}.'.format(interfaces_ins.description, interfaces_ins.id)) if interfaces_ins.description == '0' or ("primary" in interfaces_ins.description): interface_id=interfaces_ins.id break if not interface_id: interface_id=interfaces_list[0].id ctx.logger.info( 'Associated Elastic IP {0} with interface_id {1}.' .format(elasticip, interface_id)) if len(interfaces_list) > 1: ec2_client.associate_address(allocation_id =address_object.allocation_id , network_interface_id=interface_id) else: ec2_client.associate_address(instance_id=instance_id, public_ip=elasticip) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.info( 'Associated Elastic IP {0} with instance {1}.' .format(elasticip, instance_id)) ctx.source.instance.runtime_properties['public_ip_address'] = elasticip
def disassociate_elasticip(**_): """ Disassocates an Elastic IP created by Cloudify from an EC2 Instance that was also created by Cloudify. """ ec2_client = connection.EC2ConnectionClient().client() elasticip = \ utils.get_external_resource_id_or_raise( 'disassociate elasticip', ctx.target.instance) elasticip_object = _get_address_object_by_id(elasticip) if not elasticip_object: raise NonRecoverableError( 'no matching elastic ip in account: {0}'.format(elasticip)) disassociate_args = dict( public_ip=elasticip_object.public_ip, association_id=elasticip_object.association_id ) ctx.logger.debug('Disassociating Elastic IP {0}'.format(elasticip)) try: ec2_client.disassociate_address(**disassociate_args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.unassign_runtime_property_from_resource( 'public_ip_address', ctx.source.instance) utils.unassign_runtime_property_from_resource( 'instance_id', ctx.target.instance)
def associate_elasticip(fixed_ip, **_): """ Associates the host's fixed Elastic IP to the other host. """ ec2_client = get_client() instance_id = \ utils.get_external_resource_id_or_raise( 'associate elasticip', ctx.source.instance) kw = dict(instance_id=instance_id, public_ip=fixed_ip) ctx.logger.debug( 'Attempting to associate: {0}' .format(kw)) try: ec2_client.associate_address(**kw) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.info( 'Associated Elastic IP {0} with instance {1}.' .format(fixed_ip, instance_id)) ctx.source.instance.runtime_properties['public_ip_address'] = fixed_ip ctx.target.instance.runtime_properties['instance_id'] = \ ctx.source.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID]
def get_elastic_ip_address(): elasticip = \ utils.get_external_resource_id_or_raise( 'get elasticip', ctx.target.instance) elasticip_object = elasticip._get_address_object_by_id(elasticip) return elasticip_object.public_ip
def delete(**_): """ Deletes an EBS Volume. """ volume_id = utils.get_external_resource_id_or_raise( 'delete EBS volume', ctx.instance) if _delete_external_volume(): return ctx.logger.debug('Deleting EBS volume: {0}'.format(volume_id)) if not _delete_volume(volume_id): return ctx.operation.retry( message='Failed to delete volume {0}.' .format(volume_id)) utils.unassign_runtime_property_from_resource( constants.ZONE, ctx.instance) utils.unassign_runtime_property_from_resource( constants.EXTERNAL_RESOURCE_ID, ctx.instance) ctx.logger.info( 'Deleted EBS volume: {0}.' .format(volume_id))
def release(**_): """This releases an Elastic IP created by Cloudify in the connected account. """ elasticip = utils.get_external_resource_id_or_raise("release elasticip", ctx.instance) if _release_external_elasticip(): return address_object = _get_address_object_by_id(elasticip) if not address_object: raise NonRecoverableError("Unable to release elasticip. Elasticip not in account.") ctx.logger.debug("Attempting to release an Elastic IP.") try: deleted = address_object.delete() except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError("{0}".format(str(e))) if not deleted: raise NonRecoverableError( "Elastic IP {0} deletion failed for an unknown reason.".format(address_object.public_ip) ) address = _get_address_object_by_id(address_object.public_ip) if not address: for runtime_property in [constants.ALLOCATION_ID, constants.EXTERNAL_RESOURCE_ID]: utils.unassign_runtime_property_from_resource(runtime_property, ctx.instance) else: return ctx.operation.retry(message="Elastic IP not released. Retrying...")
def start(**_): ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'start instance', ctx.instance) if _start_external_instance(instance_id): return if _get_instance_state() == constants.INSTANCE_STATE_STARTED: _instance_started_assign_runtime_properties(instance_id) return ctx.logger.debug('Attempting to start instance: {0}.)'.format(instance_id)) try: ec2_client.start_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.debug('Attempted to start instance {0}.'.format(instance_id)) if _get_instance_state() == constants.INSTANCE_STATE_STARTED: _instance_started_assign_runtime_properties(instance_id) else: return ctx.operation.retry( message='Waiting server to be running. Retrying...')
def terminate(**_): ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'terminate instance', ctx.instance) if _terminate_external_instance(instance_id): return ctx.logger.debug( 'Attempting to terminate EC2 Instance. {0}.)'.format(instance_id)) try: ec2_client.terminate_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.debug( 'Attemped to terminate instance {0}'.format(instance_id)) if _get_instance_state() == \ constants.INSTANCE_STATE_TERMINATED: ctx.logger.info('Terminated instance: {0}.'.format(instance_id)) utils.unassign_runtime_property_from_resource( constants.EXTERNAL_RESOURCE_ID, ctx.instance) else: return ctx.operation.retry( message='Waiting server to terminate. Retrying...')
def create_snapshot(args, **_): """ Create a snapshot of an EBS Volume """ volume_id = \ utils.get_external_resource_id_or_raise( 'create snapshot', ctx.instance) ctx.logger.info( 'Trying to create a snapshot of EBS volume {0}.'.format(volume_id)) volume_object = _get_volumes_from_id(volume_id) if not args: snapshot_desc = \ unicode(datetime.datetime.now()) + \ ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID] args = dict(description=snapshot_desc) try: new_snapshot = volume_object.create_snapshot(**args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.info('Created snapshot of EBS volume {0}.'.format(volume_id)) if constants.VOLUME_SNAPSHOT_ATTRIBUTE not in \ ctx.instance.runtime_properties: ctx.instance.runtime_properties[ constants.VOLUME_SNAPSHOT_ATTRIBUTE] = list() ctx.instance.runtime_properties[ constants.VOLUME_SNAPSHOT_ATTRIBUTE].append(new_snapshot.id)
def stop(**_): ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'stop instance', ctx.instance) if _stop_external_instance(instance_id): return ctx.logger.debug( 'Attempting to stop EC2 Instance. {0}.)'.format(instance_id)) try: ec2_client.stop_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.debug('Attempted to stop instance {0}.'.format(instance_id)) if _get_instance_state() == constants.INSTANCE_STATE_STOPPED: _unassign_runtime_properties( runtime_properties=constants.INSTANCE_INTERNAL_ATTRIBUTES, ctx_instance=ctx.instance) ctx.logger.info('Stopped instance {0}.'.format(instance_id)) else: return ctx.operation.retry( message='Waiting server to stop. Retrying...')
def start(**_): ec2_client = connection.EC2ConnectionClient().client() instance_id = utils.get_external_resource_id_or_raise("start instance", ctx.instance) if _start_external_instance(instance_id): return if _get_instance_state() == constants.INSTANCE_STATE_STARTED: _instance_started_assign_runtime_properties(instance_id) return ctx.logger.debug("Attempting to start instance: {0}.)".format(instance_id)) try: ec2_client.start_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError("{0}".format(str(e))) ctx.logger.debug("Attempted to start instance {0}.".format(instance_id)) if _get_instance_state() == constants.INSTANCE_STATE_STARTED: _instance_started_assign_runtime_properties(instance_id) else: return ctx.operation.retry(message="Waiting server to be running. Retrying...")
def stop(**_): ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'stop instance', ctx.instance) if _stop_external_instance(instance_id): return ctx.logger.debug( 'Attempting to stop EC2 Instance. {0}.)'.format(instance_id)) try: ec2_client.stop_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.debug('Attempted to stop instance {0}.'.format(instance_id)) if _get_instance_state() == constants.INSTANCE_STATE_STOPPED: _unassign_runtime_properties( runtime_properties=constants.INSTANCE_INTERNAL_ATTRIBUTES, ctx_instance=ctx.instance) ctx.logger.info('Stopped instance {0}.'.format(instance_id)) else: return ctx.operation.retry( message='Waiting server to stop. Retrying...')
def terminate(**_): ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'terminate instance', ctx.instance) if _terminate_external_instance(instance_id): return ctx.logger.debug( 'Attempting to terminate EC2 Instance. {0}.)'.format(instance_id)) try: ec2_client.terminate_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.debug('Attemped to terminate instance {0}'.format(instance_id)) if _get_instance_state() == \ constants.INSTANCE_STATE_TERMINATED: ctx.logger.info('Terminated instance: {0}.'.format(instance_id)) utils.unassign_runtime_property_from_resource( constants.EXTERNAL_RESOURCE_ID, ctx.instance) else: return ctx.operation.retry( message='Waiting server to terminate. Retrying...')
def start(**_): """Add tags to EC2 security group. """ group_id = utils.get_external_resource_id_or_raise( 'start security group', ctx.instance) security_group = _get_security_group_from_id(group_id) utils.add_tag(security_group)
def detach(args, **_): """ Detaches an EBS Volume created by Cloudify from an EC2 Instance that was also created by Cloudify. """ volume_id = \ utils.get_external_resource_id_or_raise( 'detach volume', ctx.source.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'detach volume', ctx.target.instance) if _detach_external_volume_or_instance(): return ctx.logger.debug('Detaching EBS volume {0}'.format(volume_id)) volume_object = _get_volumes_from_id(volume_id) if not volume_object: raise NonRecoverableError( 'EBS volume {0} not found in account.'.format(volume_id)) try: detached = volume_object.detach(**args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) if not detached: raise NonRecoverableError( 'Failed to detach volume {0} from instance {1}' .format(volume_id, instance_id)) utils.unassign_runtime_property_from_resource( 'instance_id', ctx.source.instance) ctx.logger.info( 'Detached volume {0} from instance {1}.' .format(volume_id, instance_id))
def detach(args, **_): """ Detaches an EBS Volume created by Cloudify from an EC2 Instance that was also created by Cloudify. """ volume_id = \ utils.get_external_resource_id_or_raise( 'detach volume', ctx.source.instance) instance_id = \ utils.get_external_resource_id_or_raise( 'detach volume', ctx.target.instance) if _detach_external_volume_or_instance(): return ctx.logger.debug('Detaching EBS volume {0}'.format(volume_id)) volume_object = _get_volumes_from_id(volume_id) if not volume_object: raise NonRecoverableError( 'EBS volume {0} not found in account.'.format(volume_id)) try: detached = volume_object.detach(**args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) if not detached: raise NonRecoverableError( 'Failed to detach volume {0} from instance {1}'.format( volume_id, instance_id)) utils.unassign_runtime_property_from_resource('instance_id', ctx.source.instance) ctx.logger.info('Detached volume {0} from instance {1}.'.format( volume_id, instance_id))
def test_utils_get_external_resource_id_or_raise(self): ctx = self.mock_ctx( 'test_utils_get_external_resource_id_or_raise') current_ctx.set(ctx=ctx) ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID] = \ 'test_utils_get_external_resource_id_or_raise' output = utils.get_external_resource_id_or_raise( 'test_operation', ctx.instance) self.assertEquals( 'test_utils_get_external_resource_id_or_raise', output)
def start(start_retry_interval=30, private_key_path=None, **_): ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'start instance', ctx.instance) if _start_external_instance(instance_id): return if _get_instance_state() == constants.INSTANCE_STATE_STARTED: if ctx.node.properties['use_password']: password_success = _retrieve_windows_pass( ec2_client=ec2_client, instance_id=instance_id, private_key_path=private_key_path) if not password_success: return ctx.operation.retry( message='Waiting for server to post generated password', retry_after=start_retry_interval) _instance_started_assign_runtime_properties_and_tag(instance_id) return ctx.logger.debug('Attempting to start instance: {0}.)'.format(instance_id)) try: ec2_client.start_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.debug('Attempted to start instance {0}.'.format(instance_id)) if _get_instance_state() == constants.INSTANCE_STATE_STARTED: if ctx.node.properties['use_password']: password_success = _retrieve_windows_pass( ec2_client=ec2_client, instance_id=instance_id, private_key_path=private_key_path) if not password_success: return ctx.operation.retry( message='Waiting for server to post generated password', retry_after=start_retry_interval) _instance_started_assign_runtime_properties_and_tag(instance_id) else: return ctx.operation.retry( message='Waiting server to be running. Retrying...', retry_after=start_retry_interval)
def start(start_retry_interval=30, private_key_path=None, **_): ec2_client = connection.EC2ConnectionClient().client() instance_id = \ utils.get_external_resource_id_or_raise( 'start instance', ctx.instance) if _start_external_instance(instance_id): return if _get_instance_state() == constants.INSTANCE_STATE_STARTED: if ctx.node.properties['use_password']: password_success = _retrieve_windows_pass( ec2_client=ec2_client, instance_id=instance_id, private_key_path=private_key_path) if not password_success: return ctx.operation.retry( message='Waiting for server to post generated password', retry_after=start_retry_interval) _instance_started_assign_runtime_properties_and_tag(instance_id) return ctx.logger.debug('Attempting to start instance: {0}.)'.format(instance_id)) try: ec2_client.start_instances(instance_id) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.debug('Attempted to start instance {0}.'.format(instance_id)) if _get_instance_state() == constants.INSTANCE_STATE_STARTED: if ctx.node.properties['use_password']: password_success = _retrieve_windows_pass( ec2_client=ec2_client, instance_id=instance_id, private_key_path=private_key_path) if not password_success: return ctx.operation.retry( message='Waiting for server to post generated password', retry_after=start_retry_interval) _instance_started_assign_runtime_properties_and_tag(instance_id) else: return ctx.operation.retry( message='Waiting server to be running. Retrying...', retry_after=start_retry_interval)
def delete(**_): """ Deletes an EC2 security group. """ group_id = utils.get_external_resource_id_or_raise('delete security group', ctx.instance) if _delete_external_securitygroup(): return ctx.logger.debug('Deleting Security Group: {0}'.format(group_id)) _delete_security_group(group_id) utils.unassign_runtime_property_from_resource( constants.EXTERNAL_RESOURCE_ID, ctx.instance) ctx.logger.info( 'Attempted to delete Security Group: {0}.'.format(group_id))
def release(**_): """This releases an Elastic IP created by Cloudify in the connected account. """ elasticip = \ utils.get_external_resource_id_or_raise( 'release elasticip', ctx.instance) if _release_external_elasticip(): return address_object = _get_address_object_by_id(elasticip) if not address_object: raise NonRecoverableError( 'Unable to release elasticip. Elasticip not in account.') ctx.logger.debug('Attempting to release an Elastic IP.') try: deleted = address_object.delete() except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) if not deleted: raise NonRecoverableError( 'Elastic IP {0} deletion failed for an unknown reason.'.format( address_object.public_ip)) address = _get_address_object_by_id(address_object.public_ip) if not address: for runtime_property in \ [constants.ALLOCATION_ID, constants.EXTERNAL_RESOURCE_ID]: utils.unassign_runtime_property_from_resource( runtime_property, ctx.instance) else: return ctx.operation.retry( message='Elastic IP not released. Retrying...')
def delete(**_): """ Deletes an EC2 security group. """ group_id = utils.get_external_resource_id_or_raise( 'delete security group', ctx.instance) if _delete_external_securitygroup(): return ctx.logger.debug('Deleting Security Group: {0}'.format(group_id)) _delete_security_group(group_id) utils.unassign_runtime_property_from_resource( constants.EXTERNAL_RESOURCE_ID, ctx.instance) ctx.logger.info( 'Attempted to delete Security Group: {0}.' .format(group_id))
def delete(**_): """ Deletes an EBS Volume. """ volume_id = utils.get_external_resource_id_or_raise( 'delete EBS volume', ctx.instance) if _delete_external_volume(): return ctx.logger.debug('Deleting EBS volume: {0}'.format(volume_id)) if not _delete_volume(volume_id): return ctx.operation.retry( message='Failed to delete volume {0}.'.format(volume_id)) utils.unassign_runtime_property_from_resource( constants.EXTERNAL_RESOURCE_ID, ctx.instance) ctx.logger.info('Deleted EBS volume: {0}.'.format(volume_id))
def delete(**kwargs): """Deletes a keypair.""" ec2_client = connection.EC2ConnectionClient().client() key_pair_name = utils.get_external_resource_id_or_raise( 'delete key pair', ctx.instance) if _delete_external_keypair(): return ctx.logger.debug('Attempting to delete key pair from account.') try: ec2_client.delete_key_pair(key_pair_name) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.unassign_runtime_properties_from_resource(RUNTIME_PROPERTIES, ctx.instance) _delete_key_file() ctx.logger.info('Deleted key pair: {0}.'.format(key_pair_name))
def delete(**kwargs): """Deletes a keypair.""" ec2_client = connection.EC2ConnectionClient().client() key_pair_name = utils.get_external_resource_id_or_raise( 'delete key pair', ctx.instance) if _delete_external_keypair(): return ctx.logger.debug('Attempting to delete key pair from account.') try: ec2_client.delete_key_pair(key_pair_name) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.unassign_runtime_properties_from_resource(RUNTIME_PROPERTIES, ctx.instance) _delete_key_file() ctx.logger.info('Deleted key pair: {0}.'.format(key_pair_name))
def create_snapshot(args, **_): """ Create a snapshot of an EBS Volume """ volume_id = \ utils.get_external_resource_id_or_raise( 'create snapshot', ctx.instance) ctx.logger.info( 'Trying to create a snapshot of EBS volume {0}.' .format(volume_id)) volume_object = _get_volumes_from_id(volume_id) if not args: snapshot_desc = \ unicode(datetime.datetime.now()) + \ ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID] args = dict(description=snapshot_desc) try: new_snapshot = volume_object.create_snapshot(**args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.logger.info( 'Created snapshot of EBS volume {0}.'.format(volume_id)) if constants.VOLUME_SNAPSHOT_ATTRIBUTE not in \ ctx.instance.runtime_properties: ctx.instance.runtime_properties[ constants.VOLUME_SNAPSHOT_ATTRIBUTE] = list() ctx.instance.runtime_properties[ constants.VOLUME_SNAPSHOT_ATTRIBUTE].append(new_snapshot.id)