def prepare(self, request): """ :param request: :return: """ fabric = self._get_or_create_fabric(name=self.DEFAULT_FABRIC_NAME) self._get_or_create_subnet( name=self.DEFAULT_SUBNET_NAME, cidr=self._resource_config.default_subnet, gateway_ip=self._resource_config.default_gateway, vlan=fabric.vlans[0], managed=self._resource_config.managed_allocation, ) actions = self._request_parser.convert_driver_request_to_actions(request) # ignore prepare infra actions prep_network_action = single( actions, lambda x: isinstance(x, PrepareCloudInfra) ) prep_network_action_result = PrepareCloudInfraResult( prep_network_action.actionId ) prep_subnet_action = single(actions, lambda x: isinstance(x, PrepareSubnet)) prep_subnet_action_result = PrepareSubnetActionResult( prep_subnet_action.actionId ) if self._ssh_keys_exists(): public_key = self._get_ssh_public_key() else: public_key = self._generate_ssh_key_pair() # send to MAAS only public key self._maas_client.ssh_keys.create(key=public_key) access_keys_action = single(actions, lambda x: isinstance(x, CreateKeys)) access_keys_action_results = CreateKeysActionResult( actionId=access_keys_action.actionId, accessKey=public_key ) action_results = [ prep_network_action_result, prep_subnet_action_result, access_keys_action_results, ] return DriverResponse(action_results).to_driver_response_json()
def deploy(self, request): """ :param request: :return: """ actions = self._request_parser.convert_driver_request_to_actions( request) deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) attrs = deploy_action.actionParams.deployment.attributes machine = self._get_free_machine( cpus=int(attrs["Maas.MAAS Machine.CPU Cores"]), memory=float(attrs["Maas.MAAS Machine.RAM GiB"]), disks=int(attrs["Maas.MAAS Machine.Disks"]), storage=float(attrs["Maas.MAAS Machine.Storage GB"])) operating_system = attrs["Maas.MAAS Machine.Operation System"] machine.deploy(distro_series=operating_system, wait=True) self._reconnect_machine_to_sandbox_subnet(machine=machine) deploy_result = DeployAppResult(deploy_action.actionId, vmUuid=machine.system_id, vmName=operating_system, vmDetailsData=None, deployedAppAdditionalData={}) return DriverResponse([deploy_result]).to_driver_response_json()
def deploy_ami(self, command_context, actions, cancellation_context): """# noqa Will deploy Amazon Image on the cloud provider :param ResourceCommandContext command_context: :param list[RequestActionBase] actions:: :param CancellationContext cancellation_context: """ with AwsShellContext( context=command_context, aws_session_manager=self.aws_session_manager) as shell_context: shell_context.logger.info("Deploying AMI") deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) network_actions = [ a for a in actions if isinstance(a, ConnectSubnet) ] deploy_data = self.deploy_ami_operation.deploy( ec2_session=shell_context.aws_api.ec2_session, s3_session=shell_context.aws_api.s3_session, name=deploy_action.actionParams.appName, reservation=self.model_parser.convert_to_reservation_model( command_context.reservation), aws_ec2_cp_resource_model=shell_context.aws_ec2_resource_model, ami_deploy_action=deploy_action, network_actions=network_actions, ec2_client=shell_context.aws_api.ec2_client, cancellation_context=cancellation_context, logger=shell_context.logger, ) return deploy_data
def deploy_ami(self, command_context, actions, cancellation_context): """ Will deploy Amazon Image on the cloud provider :param ResourceCommandContext command_context: :param list[RequestActionBase] actions:: :param CancellationContext cancellation_context: """ with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context: with ErrorHandlingContext(shell_context.logger): shell_context.logger.info('Deploying AMI') deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) network_actions = [a for a in actions if isinstance(a, ConnectSubnet)] deploy_data = self.deploy_ami_operation \ .deploy(ec2_session=shell_context.aws_api.ec2_session, s3_session=shell_context.aws_api.s3_session, name=deploy_action.actionParams.appName, reservation=self.model_parser.convert_to_reservation_model(command_context.reservation), aws_ec2_cp_resource_model=shell_context.aws_ec2_resource_model, ami_deploy_action=deploy_action, network_actions=network_actions, ec2_client=shell_context.aws_api.ec2_client, cancellation_context=cancellation_context, logger=shell_context.logger) return deploy_data
def Deploy(self, context, request=None, cancellation_context=None): actions = self.request_parser.convert_driver_request_to_actions(request) deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) deployment_name = deploy_action.actionParams.deployment.deploymentPath if deployment_name in self.deployments.keys(): deploy_method = self.deployments[deployment_name] result = deploy_method(context, deploy_action, cancellation_context) result.actionId = deploy_action.actionId return DriverResponse([result]).to_driver_response_json() else: raise Exception('Could not find the deployment')
def Deploy(self, context, request=None, cancellation_context=None): actions = self.request_parser.convert_driver_request_to_actions(request) deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) deployment_name = deploy_action.actionParams.deployment.deploymentPath self.parse_vnicename(actions) if deployment_name in self.deployments.keys(): deploy_method = self.deployments[deployment_name] deploy_result = deploy_method(context, actions, cancellation_context) return DriverResponse(deploy_result).to_driver_response_json() else: raise Exception('Could not find the deployment')
def cleanup(self, request): """ :param request: :return: """ # self._delete_subnet(name=self.DEFAULT_SUBNET_NAME) # self._delete_fabric(name=self.DEFAULT_FABRIC_NAME) actions = self._request_parser.convert_driver_request_to_actions(request) cleanup_action = single(actions, lambda x: isinstance(x, CleanupNetwork)) action_result = CleanupNetworkResult(cleanup_action.actionId) return DriverResponse([action_result]).to_driver_response_json()
def Deploy(self, context, request=None, cancellation_context=None): """ Deploy :param ResourceCommandContext context: :param str request: A JSON string with the list of requested deployment actions :param CancellationContext cancellation_context: :return: :rtype: str """ with LoggingSessionContext(context) as logger, ErrorHandlingContext(logger): with CloudShellSessionContext(context) as cloudshell_session: self._log(logger, 'deploy_request', request) self._log(logger, 'deploy_context', context) cloud_provider_resource = Nutanixshell.create_from_context(context) decrypted_pass = cloudshell_session.DecryptPassword(cloud_provider_resource.password).Value nutanix_service = NutanixService(context.resource.address, cloud_provider_resource.user, decrypted_pass) # parse the json strings into action objects actions = self.request_parser.convert_driver_request_to_actions(request) # extract DeployApp action deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) # if we have multiple supported deployment options use the 'deploymentPath' property # to decide which deployment option to use. # deployment_name = deploy_action.actionParams.deployment.deploymentPath deploy_result = nutanix_service.clone_vm(deploy_action, cloud_provider_resource.storage_container_name) self._log(logger, 'deploy_result', deploy_result) return DriverResponse([deploy_result]).to_driver_response_json()
def Deploy(self, context, request=None, cancellation_context=None): """ :param ResourceCommandContext context: :param str request: json string to be parsed :param cancellation_context: :return: """ RESOURCE_POOL_ATTR = "App Pool Name" CP_RESTRICTED_ATTR = "Restricted App Model Pools" api = CloudShellSessionContext(context).get_api() res_id = context.reservation.reservation_id # VALIDATE THAT CLOUD PROVIDER RESOURCE HAS POOL LIST Attribute cp_attrs = context.resource.attributes try: cp_pool_list_val = cp_attrs[CP_RESTRICTED_ATTR] except KeyError: pass else: api.WriteMessageToReservationOutput(res_id, "=== full request json ===") api.WriteMessageToReservationOutput(res_id, request) request_obj = json.loads(request) request_action_params = request_obj["driverRequest"]["actions"][0][ "actionParams"] app_name = request_action_params["appName"] deployment = request_action_params["deployment"] app_resource = request_action_params["appResource"] app_resource_attrs = app_resource["attributes"] pool_attr_search = [ attr for attr in app_resource_attrs if attr["attributeName"] == RESOURCE_POOL_ATTR ] if pool_attr_search: app_pool_attr_val = pool_attr_search[0]["attributeValue"] restricted_attrs_dict = get_cp_restricted_attrs_dict( cp_pool_list_val) try: app_pool_limit = restricted_attrs_dict[app_pool_attr_val] except KeyError: not_found_msg = "{} pool name key not in cp restricted list {}".format( app_pool_attr_val, restricted_attrs_dict) api.WriteMessageToReservationOutput(res_id, not_found_msg) else: # count deployed apps all_generic_app_resources = api.FindResources( resourceFamily="Generic App Family").Resources # collect matching apps matching_apps = [] for resource in all_generic_app_resources: attrs = api.GetResourceDetails( resource.Name).ResourceAttributes attr_search = [ attr for attr in attrs if attr.Name == RESOURCE_POOL_ATTR ] if attr_search: attr_val = attr_search[0].Value if attr_val == app_pool_attr_val: matching_apps.append(resource) # PERFORM VALIDATION if len(matching_apps) >= int(app_pool_limit): matching_app_names = [r.Name for r in matching_apps] exc_msg = "Can not deploy '{}'. The pool '{}' has reached it's limit of {}. Current Apps in Pool: {}".format( app_name, app_pool_attr_val, app_pool_limit, matching_app_names) api.WriteMessageToReservationOutput( res_id, '<span style="color:red">{}</span>'.format( exc_msg)) raise AppLimitDeploymentError(exc_msg) actions = self.request_parser.convert_driver_request_to_actions( request) deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) deployment_name = deploy_action.actionParams.deployment.deploymentPath if deployment_name in self.deployments.keys(): deploy_method = self.deployments[deployment_name] deploy_result = deploy_method(context, deploy_action, cancellation_context) return DriverResponse([deploy_result]).to_driver_response_json() else: raise Exception('Could not find the deployment')
def Deploy(self, context, request=None, cancellation_context=None): self.logger.info('inside Deploy') """ Deploy :param ResourceCommandContext context: :param str request: A JSON string with the list of requested deployment actions :param CancellationContext cancellation_context: :return: :rtype: str """ ''' # parse the json strings into action objects actions = self.request_parser.convert_driver_request_to_actions(request) # extract DeployApp action deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) # if we have multiple supported deployment options use the 'deploymentPath' property # to decide which deployment option to use. deployment_name = deploy_action.actionParams.deployment.deploymentPath deploy_result = _my_deploy_method(context, actions, cancellation_context) return DriverResponse(deploy_result).to_driver_response_json() ''' self.logger.info('got into deploy') json_req = json.dumps(request, default=lambda o: o.__dict__, sort_keys=True, indent=4) self.logger.info(json_req) actions = self.request_parser.convert_driver_request_to_actions( request) self.logger.info('connecting') cloud = self._connect_to_cloud(context) self.logger.info('deploying') self.logger.info('deployed') deploy_action = single(actions, lambda x: isinstance(x, DeployApp)) self.logger.info('deploy action JSON') #delete me if (isinstance(deploy_action, DeployApp)): pass deployment_name = deploy_action.actionParams.deployment.deploymentPath raw_result = self.deploy_hwc_from_image(context, deploy_action, cancellation_context, cloud) #delete me if (isinstance(raw_result, Server)): pass deploy_result = DeployAppResult( vmName=raw_result.name, actionId=deploy_action.actionId, vmUuid='', success=True, deployedAppAddress=raw_result.addresses.get( raw_result.addresses.keys()[0])[0].get('addr'), deployedAppAttributes=[], vmDetailsData=None #vmDetailsData=VmDetailsCreator.extract_vm_details(raw_result) ) # handle if Elastic IP is needed if deploy_action.actionParams.deployment.attributes.get( 'Huaweicloud.HWC_deploy_from_image.EIP') == 'True': new_ip = cloud.createEIP(cloud.get_vm_port_id(raw_result)) my_response = DriverResponse([deploy_result]).to_driver_response_json() self.logger.info('my response is : {0}'.format(my_response)) return my_response