def create(args, **_): """Creates an EBS volume. """ ec2_client = connection.EC2ConnectionClient().client() for property_name in constants.VOLUME_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) if _create_external_volume(): return ctx.logger.debug('Creating EBS volume') create_volume_args = dict(size=ctx.node.properties['size'], zone=ctx.node.properties['zone']) create_volume_args.update(args) try: new_volume = ec2_client.create_volume(**create_volume_args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.set_external_resource_id(new_volume.id, ctx.instance, external=False)
def allocate(**_): """This allocates an Elastic IP in the connected account.""" ec2_client = connection.EC2ConnectionClient().client() if _allocate_external_elasticip(): return ctx.logger.debug('Attempting to allocate elasticip.') kw = {} if ctx.node.properties.get('domain'): kw['domain'] = ctx.node.properties['domain'] try: address_object = ec2_client.allocate_address(**kw) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) if 'vpc' in address_object.domain: ctx.instance.runtime_properties[constants.ALLOCATION_ID] = \ address_object.allocation_id utils.set_external_resource_id( address_object.public_ip, ctx.instance, external=False)
def _create_elb(): ctx.logger.info('Attempting to Create Load Balancer.') params_dict = _create_elb_params() elb_client = connection.ELBConnectionClient().client() try: lb = elb_client.create_load_balancer(**params_dict) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: raise RecoverableError('Load Balancer not created ' '{0}'.format(str(e))) if not lb: raise NonRecoverableError( 'Load Balancer not created. While the create request was completed' ' successfully, verifying the load balancer afterwards has failed') ctx.logger.info('Load Balancer Created.') utils.set_external_resource_id(params_dict['name'], ctx.instance, external=False) ctx.instance.runtime_properties['elb_name'] = params_dict['name'] return lb
def _create_external_keypair(): """If use_external_resource is True, this will set the runtime_properties, and then exit. :param ctx: The Cloudify context. :return False: Cloudify resource. Continue operation. :return True: External resource. Set runtime_properties. Ignore operation. :raises NonRecoverableError: If unable to locate the existing key file. """ if not utils.use_external_resource(ctx.node.properties): return False key_pair_name = ctx.node.properties['resource_id'] key_pair_in_account = _get_key_pair_by_id(key_pair_name) key_path_in_filesystem = _get_path_to_key_file() ctx.logger.debug('Path to key file: {0}.'.format(key_path_in_filesystem)) if not key_pair_in_account: raise NonRecoverableError( 'External resource, but the key pair is not in the account.') if not _search_for_key_file(key_path_in_filesystem): raise NonRecoverableError( 'External resource, but the key file does not exist.') utils.set_external_resource_id(key_pair_name, ctx.instance) return True
def post_create(self): ec2_utils.set_external_resource_id(self.resource_id, ctx.instance) ctx.logger.info("Added {0} {1} to Cloudify.".format(self.aws_resource_type, self.resource_id)) return True
def allocate(**_): """This allocates an Elastic IP in the connected account.""" ec2_client = connection.EC2ConnectionClient().client() if _allocate_external_elasticip(): return ctx.logger.debug('Attempting to allocate elasticip.') kw = {} if ctx.node.properties.get('domain'): kw['domain'] = ctx.node.properties['domain'] try: address_object = ec2_client.allocate_address(**kw) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) if 'vpc' in address_object.domain: ctx.instance.runtime_properties[constants.ALLOCATION_ID] = \ address_object.allocation_id utils.set_external_resource_id(address_object.public_ip, ctx.instance, external=False)
def _create_external_keypair(): """If use_external_resource is True, this will set the runtime_properties, and then exit. :param ctx: The Cloudify context. :return False: Cloudify resource. Continue operation. :return True: External resource. Set runtime_properties. Ignore operation. :raises NonRecoverableError: If unable to locate the existing key file. """ if not utils.use_external_resource(ctx.node.properties): return False key_pair_name = ctx.node.properties['resource_id'] key_pair_in_account = _get_key_pair_by_id(key_pair_name) key_path_in_filesystem = _get_path_to_key_file() ctx.logger.debug( 'Path to key file: {0}.'.format(key_path_in_filesystem)) if not key_pair_in_account: raise NonRecoverableError( 'External resource, but the key pair is not in the account.') if not _search_for_key_file(key_path_in_filesystem): raise NonRecoverableError( 'External resource, but the key file does not exist.') utils.set_external_resource_id(key_pair_name, ctx.instance) return True
def create(args, **_): """Creates an EBS volume. """ ec2_client = connection.EC2ConnectionClient().client() for property_name in constants.VOLUME_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) if _create_external_volume(): return ctx.logger.debug('Creating EBS volume') create_volume_args = dict( size=ctx.node.properties['size'], zone=ctx.node.properties[constants.ZONE] ) create_volume_args.update(args) try: new_volume = ec2_client.create_volume(**create_volume_args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) ctx.instance.runtime_properties[constants.ZONE] = new_volume.zone utils.set_external_resource_id( new_volume.id, ctx.instance, external=False)
def create(**kwargs): """Creates a keypair.""" ec2_client = connection.EC2ConnectionClient().client() if _create_external_keypair(): return key_pair_name = utils.get_resource_id() ctx.logger.debug('Attempting to create key pair.') try: kp = ec2_client.create_key_pair(key_pair_name) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: raise NonRecoverableError('Key pair not created. {0}'.format(str(e))) utils.set_external_resource_id( kp.name, ctx.instance, external=False) _save_key_pair(kp) ctx.instance.runtime_properties[constants.AWS_TYPE_PROPERTY] = \ KEYPAIR_AWS_TYPE
def allocate(**_): """This allocates an Elastic IP in the connected account.""" ec2_client = connection.EC2ConnectionClient().client() if _allocate_external_elasticip(): return ctx.logger.debug("Attempting to allocate elasticip.") provider_variables = utils.get_provider_variables() kw = {} domain = ctx.node.properties.get(constants.ELASTIC_IP_DOMAIN_PROPERTY) or provider_variables.get(constants.VPC) if domain: kw[constants.ELASTIC_IP_DOMAIN_PROPERTY] = constants.VPC_DOMAIN try: address_object = ec2_client.allocate_address(**kw) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError("{0}".format(str(e))) if constants.VPC_DOMAIN in address_object.domain: ctx.instance.runtime_properties[constants.ALLOCATION_ID] = address_object.allocation_id utils.set_external_resource_id(address_object.public_ip, ctx.instance, external=False)
def run_instances(**_): ec2_client = connection.EC2ConnectionClient().client() ctx.logger.debug("connection.EC2ConnectionClient().client()") for property_name in constants.INSTANCE_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) ctx.logger.debug(str(ctx.node.properties)) if _create_external_instance(): return instance_parameters = _get_instance_parameters() ctx.logger.debug( "Attempting to create EC2 Instance with these API parameters: {0}.".format(str(instance_parameters)) ) instance_id = _run_instances_if_needed(ec2_client, instance_parameters) instance = _get_instance_from_id(instance_id) if instance is None: return ctx.operation.retry( message="Waiting to verify that instance {0} " "has been added to your account.".format(instance_id) ) utils.set_external_resource_id(instance_id, ctx.instance, external=False)
def post_create(self): vpc = self.get_containing_vpc() ctx.instance.runtime_properties["vpc_id"] = vpc.id ctx.instance.runtime_properties["routes"] = self.routes ec2_utils.set_external_resource_id(self.resource_id, ctx.instance) ctx.logger.info("Added {0} {1} to Cloudify.".format(self.aws_resource_type, self.resource_id)) return True
def run_instances(**_): ec2_client = connection.EC2ConnectionClient().client() for property_name in constants.INSTANCE_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) if _create_external_instance(): return instance_parameters = _get_instance_parameters() ctx.logger.info( 'Attempting to create EC2 Instance with these API parameters: {0}.'. format(instance_parameters)) instance_id = _run_instances_if_needed(ec2_client, instance_parameters) instance = _get_instance_from_id(instance_id) if instance is None: return ctx.operation.retry( message='Waiting to verify that instance {0} ' 'has been added to your account.'.format(instance_id)) utils.set_external_resource_id(instance_id, ctx.instance, external=False)
def create(**_): """Creates an EC2 security group. """ ec2_client = connection.EC2ConnectionClient().client() for property_name in constants.SECURITY_GROUP_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) name = utils.get_resource_id() if _create_external_securitygroup(name): return ctx.logger.debug( 'Creating Security Group: {0}' .format(name)) create_args = dict( name=name, description=ctx.node.properties['description'], vpc_id=_get_connected_vpc() ) try: group_object = ec2_client.create_security_group(**create_args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) _create_group_rules(group_object) utils.set_external_resource_id( group_object.id, ctx.instance, external=False)
def post_create(self): vpc = self.get_containing_vpc() ctx.instance.runtime_properties['vpc_id'] = vpc.id ctx.instance.runtime_properties['routes'] = self.routes ec2_utils.set_external_resource_id(self.resource_id, ctx.instance) ctx.logger.info('Added {0} {1} to Cloudify.'.format( self.aws_resource_type, self.resource_id)) return True
def post_create(self): ec2_utils.set_external_resource_id(self.resource_id, ctx.instance) ctx.logger.info('Added {0} {1} to Cloudify.'.format( self.aws_resource_type, self.resource_id)) return True
def _create_external_instance(): if not utils.use_external_resource(ctx.node.properties): return False instance_id = ctx.node.properties['resource_id'] instance = _get_instance_from_id(instance_id) if instance is None: raise NonRecoverableError( 'Cannot use_external_resource because instance_id {0} ' 'is not in this account.'.format(instance_id)) utils.set_external_resource_id(instance.id, ctx.instance) return True
def test_utils_set_external_resource_id_external(self): ctx = self.mock_ctx( 'test_utils_set_external_resource_id_external') current_ctx.set(ctx=ctx) utils.set_external_resource_id( 'id-value', ctx.instance) self.assertEquals( 'id-value', ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID])
def _create_external_instance(): if not utils.use_external_resource(ctx.node.properties): return False instance_id = ctx.node.properties["resource_id"] instance = _get_instance_from_id(instance_id) if instance is None: raise NonRecoverableError( "Cannot use_external_resource because instance_id {0} " "is not in this account.".format(instance_id) ) utils.set_external_resource_id(instance.id, ctx.instance) return True
def allocate(**_): """This allocates an Elastic IP in the connected account.""" ec2_client = connection.EC2ConnectionClient().client() if _allocate_external_elasticip(): return ctx.logger.debug('Attempting to allocate elasticip.') try: address_object = ec2_client.allocate_address(domain=None) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.set_external_resource_id( address_object.public_ip, ctx.instance, external=False)
def create(**_): """Creates an EC2 security group. """ ec2_client = connection.EC2ConnectionClient().client() for property_name in constants.SECURITY_GROUP_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) name = utils.get_resource_id() if _create_external_securitygroup(name): return ctx.logger.debug( 'Creating Security Group: {0}' .format(name)) create_args = dict( name=name, description=ctx.node.properties['description'], vpc_id=_get_connected_vpc() ) if ctx.operation.retry_number == 0 and constants.EXTERNAL_RESOURCE_ID \ not in ctx.instance.runtime_properties: try: security_group = ec2_client.create_security_group(**create_args) except (exception.EC2ResponseError, exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.set_external_resource_id( security_group.id, ctx.instance, external=False) security_group = _get_security_group_from_id( ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID]) if not security_group: return ctx.operation.retry( message='Waiting to verify that security group {0} ' 'has been added.'.format(constants.EXTERNAL_RESOURCE_ID)) _create_group_rules(security_group)
def _create_external_securitygroup(name): """If use_external_resource is True, this will set the runtime_properties, and then exit. :param ctx: The Cloudify context. :return False: Cloudify resource. Continue operation. :return True: External resource. Set runtime_properties. Ignore operation. """ if not utils.use_external_resource(ctx.node.properties): return False group = _get_security_group_from_id(name) if not group: raise NonRecoverableError( 'External security group was indicated, but the given ' 'security group does not exist.') utils.set_external_resource_id(group.id, ctx.instance) return True
def _allocate_external_elasticip(): """Pretends to allocate an Elastic IP but if it was not created by Cloudify, it just sets runtime_properties and exits the operation. :return False: Cloudify resource. Continue operation. :return True: External resource. Set runtime_properties. Ignore operation. """ if not utils.use_external_resource(ctx.node.properties): return False address_ip = _get_address_by_id(ctx.node.properties["resource_id"]) if not address_ip: raise NonRecoverableError( "External elasticip was indicated, but the given " "elasticip does not exist in the account." ) utils.set_external_resource_id(address_ip, ctx.instance) return True
def allocate(**_): """This allocates an Elastic IP in the connected account.""" ec2_client = connection.EC2ConnectionClient().client() if _allocate_external_elasticip(): return ctx.logger.debug('Attempting to allocate elasticip.') try: address_object = ec2_client.allocate_address(domain=None) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.set_external_resource_id(address_object.public_ip, ctx.instance, external=False)
def _allocate_external_elasticip(): """Pretends to allocate an Elastic IP but if it was not created by Cloudify, it just sets runtime_properties and exits the operation. :return False: Cloudify resource. Continue operation. :return True: External resource. Set runtime_properties. Ignore operation. """ if not utils.use_external_resource(ctx.node.properties): return False address_ip = _get_address_by_id(ctx.node.properties['resource_id']) if not address_ip: raise NonRecoverableError( 'External elasticip was indicated, but the given ' 'elasticip does not exist in the account.') utils.set_external_resource_id(address_ip, ctx.instance) return True
def _create_external_volume(): """If use_external_resource is True, this will set the runtime_properties, and then exit. :return False: Cloudify resource. Continue operation. :return True: External resource. Set runtime_properties. Ignore operation. """ if not utils.use_external_resource(ctx.node.properties): return False volume_id = ctx.node.properties['resource_id'] volume = _get_volumes_from_id(volume_id) if not volume: raise NonRecoverableError('External EBS volume was indicated, but the ' 'volume id does not exist.') utils.set_external_resource_id(volume.id, ctx.instance) return True
def create(**kwargs): """Creates a keypair.""" ec2_client = connection.EC2ConnectionClient().client() if _create_external_keypair(): return key_pair_name = utils.get_resource_id() ctx.logger.debug('Attempting to create key pair.') try: kp = ec2_client.create_key_pair(key_pair_name) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: raise NonRecoverableError('Key pair not created. {0}'.format(str(e))) utils.set_external_resource_id(kp.name, ctx.instance, external=False) _save_key_pair(kp)
def _create_external_volume(): """If use_external_resource is True, this will set the runtime_properties, and then exit. :return False: Cloudify resource. Continue operation. :return True: External resource. Set runtime_properties. Ignore operation. """ if not utils.use_external_resource(ctx.node.properties): return False volume_id = ctx.node.properties['resource_id'] volume = _get_volumes_from_id(volume_id) if not volume: raise NonRecoverableError( 'External EBS volume was indicated, but the ' 'volume id does not exist.') utils.set_external_resource_id(volume.id, ctx.instance) return True