def get_vm_uuid(self, context, vm_name): """ :param context: ResourceRemoteCommandContext :param vm_name: full resource name of the resource :return: UID of the VM in OCI """ resource_config = ShellResource.create_from_context(context) res_details = resource_config.api.GetResourceDetails(vm_name) return str( jsonpickle.encode(res_details.VmDetails.UID, unpicklable=False))
def get_inventory(self, context): """ """ with LoggingSessionContext(context) as logger: resource_config = ShellResource.create_from_context(context) logger.info("Configuration Templates Location: {path}" "Username: {username}\n" "Password: {password}\n" "Host: {host}\n" "STD: {std_port}\n" "UWM: {uwm_port}".format( path=resource_config.templates_path, host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password)) if resource_config.templates_path and not os.path.exists( resource_config.templates_path): msg = "Wrong path provided for device configuration templates" logger.error("{msg}. Provided path: {path}".format( msg=msg, path=resource_config.templates_path)) raise VIRLShellError(msg) virl_api = VIRL_API(host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password) try: avail_networks = virl_api.get_all_avail_networks() if resource_config.mgmt_network not in avail_networks: raise VIRLShellError( "Provided Management Network <{mgmt_network}> doesn't exist. " "Available networks: {avail_networks}".format( mgmt_network=resource_config.mgmt_network, avail_networks=avail_networks)) # virl_api.health_check() except HTTPError as err: raise VIRLShellError( "Can not connect to VIRL Server. Please, verify provided credentials." "Error: {}".format(err)) return AutoLoadDetails([], [])
def PowerCycle(self, context, ports, delay): """ Perform PowerOff followed up by PowerOn after {delay} seconds - NOT IN USE :param context: ResourceRemoteCommandContext :param ports: list[string] ports: the ports of the connection between the remote resource and the local resource, NOT IN USE!!! :param delay: int : Seconds to delay between powering off and back on. :return: """ resource_config = ShellResource.create_from_context(context) output = self.PowerOff(context, ports) resource_config.api.WriteMessageToReservationOutput( resource_config.reservation_id, output) time.sleep(float(delay)) output = self.PowerOn(context, ports) resource_config.api.WriteMessageToReservationOutput( resource_config.reservation_id, output) return
def remote_refresh_ip(self, context, cancellation_context, ports): """ Refresh the IP of the resource from the VM """ resource_config = ShellResource.create_from_context(context) with resource_config.get_logger() as logger: virl_api = VIRL_API(host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password) nodes_info = virl_api.get_nodes_info( topology_name=resource_config.reservation_id, network_name=resource_config.mgmt_network) node_name = context.remote_endpoints[0].fullname.split('/')[0] resource_config.api.UpdateResourceAddress( node_name, nodes_info.get(node_name, ""))
def console(self, context, ports): """Generates a command for a console access to an instance""" resource_config = ShellResource.create_from_context(context) with LoggingSessionContext(context) as logger: instance_id = resource_config.remote_instance_id virl_api = VIRL_API(host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password) # node_status = virl_api.get_nodes_status(topology_name=resource_config.reservation_id).get(app_name, {}) nodes_status = virl_api.get_nodes_status( topology_name=resource_config.reservation_id) node_status = nodes_status.get(instance_id, {}) ip = node_status.get("console_server") port = node_status.get("console_port") return f"Console IP and port are:\n{ip}:{port}\n"
def CleanupSandboxInfra(self, context, request): """ :param context: :param request: :return: """ json_request = json.loads(request) resource_config = ShellResource.create_from_context(context) cleanup_action_id = next( action["actionId"] for action in json_request["driverRequest"]["actions"] if action["type"] == "cleanupNetwork") with resource_config.get_logger() as logger: virl_api = VIRL_API(host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password) if resource_config.reservation_id in virl_api.get_topologies_list( ): virl_api.stop_topology( topology_name=resource_config.reservation_id) cleanup_result = ActionResultBase("cleanupNetwork", cleanup_action_id) while resource_config.reservation_id in virl_api.get_topologies_list(): time.sleep(30) return str( jsonpickle.encode( {'driverResponse': { 'actionResults': [cleanup_result] }}, unpicklable=False))
def PowerOn(self, context, ports): """ Powers ON the remote VM :param ResourceRemoteCommandContext context: the context the command runs on :param list[string] ports: the ports of the connection between the remote resource and the local resource, NOT IN USE!!! """ resource_config = ShellResource.create_from_context(context) with resource_config.get_logger() as logger: name = context.remote_endpoints[0].fullname.split('/')[0] virl_api = VIRL_API(host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password) virl_api.start_node(topology_name=resource_config.reservation_id, node_name=name) resource_config.api.SetResourceLiveStatus( name, "Online", "Resource is powered on") return "VM started successfully"
def GetVmDetails(self, context, cancellation_context, requests): """ Return VM Details JSON to the Quali Server for refreshing the VM Details pane :param context: ResourceRemoteCommandContext :param cancellation_context: bool - will become True if action is cancelled :param requests: str JSON - requests for VMs to refresh :return: """ with LoggingSessionContext(context) as logger: resource_config = ShellResource.create_from_context(context) virl_api = VIRL_API(host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password) ifaces_info = virl_api.get_ifaces_info( topology_name=resource_config.reservation_id) logger.info(f"[GetVmDetails] IFACE INFO: {ifaces_info}") nodes_info = virl_api.get_nodes_status( topology_name=resource_config.reservation_id) logger.info(f"[GetVmDetails] NODES INFO: {nodes_info}") vm_details_results = [] for refresh_request in json.loads(requests)["items"]: vm_name = refresh_request["deployedAppJson"]["name"] vm_details_results.append( create_vm_details( vm_name=vm_name, mgmt_network=resource_config.mgmt_network, node_type=nodes_info.get(vm_name, {}).get("node_type", ""), node_ifaces=ifaces_info.get(vm_name, []))) return str(jsonpickle.encode(vm_details_results, unpicklable=False))
def Deploy(self, context, request=None, cancellation_context=None): """ """ actions = self.request_parser.convert_driver_request_to_actions( request) resource_config = ShellResource.create_from_context(context) # resource_config.api.WriteMessageToReservationOutput(context.reservation.reservation_id, # 'Request JSON: ' + request) with LoggingSessionContext(context) as logger: deploy_action = None subnet_actions = list() for action in actions: if isinstance(action, DeployApp): deploy_action = action if isinstance(action, ConnectSubnet): subnet_actions.append(action) if deploy_action: deployment_name = deploy_action.actionParams.deployment.deploymentPath try: deploy_method = next( self.deployments[deployment] for deployment in self.deployments.keys() if deployment_name.endswith(deployment)) except StopIteration: raise VIRLShellError("Could not find the deployment " + deployment_name) results = deploy_method(resource_config, logger, deploy_action, subnet_actions, cancellation_context) logger.info("Deploy result {}".format( DriverResponse(results).to_driver_response_json())) return DriverResponse(results).to_driver_response_json() else: raise VIRLShellError("Failed to deploy VM")
def PrepareSandboxInfra(self, context, request, cancellation_context): """ Called by CloudShell Orchestration during the Setup process in order to populate information about the networking environment used by the sandbox :param context: ResourceRemoteCommandContext :param request: Actions to be performed to prepare the networking environment sent by CloudShell Server :param cancellation_context: :return: """ with LoggingSessionContext(context) as logger: resource_config = ShellResource.create_from_context(context) resource_config.api.WriteMessageToReservationOutput( resource_config.reservation_id, "Preparing Sandbox Connectivity...") logger.info(f"REQUEST: {request}") logger.info("Cloud Provider Name: {}".format(resource_config.name)) r_id, reservation_details = get_reservation_details( api=resource_config.api, reservation_id=resource_config.reservation_id, cloud_provider_name=resource_config.name) logger.info( "Initial Reservation <{res_id}> Details: {details}".format( res_id=r_id, details=reservation_details)) json_request = json.loads(request) vcn_action_id = "" subnet_dict = {} subnet_results = [] for action in json_request["driverRequest"]["actions"]: if action["type"] == "prepareCloudInfra": vcn_action_id = action.get("actionId") elif action["type"] == "prepareSubnet": subnet_action_id = action.get("actionId") subnet_cidr = action.get("actionParams", {}).get("cidr") subnet_alias = action.get("actionParams", {}).get("alias") subnet_dict[subnet_cidr] = (subnet_action_id, subnet_alias) elif action["type"] == "createKeys": keys_action_id = action.get("actionId") prepare_network_result = PrepareCloudInfraResult(vcn_action_id) virl_api = VIRL_API(host=resource_config.address, std_port=resource_config.std_port, uwm_port=resource_config.uwm_port, username=resource_config.username, password=resource_config.password) avail_image_types = virl_api.get_available_image_types() default_gateway_info = virl_api.get_default_gateway() # resources = {} for res_name, res_params in reservation_details["resources"].items( ): image_type = res_params.get("image type") if image_type not in avail_image_types: raise VIRLShellError( f"Unable to find requested Image Type {image_type}>. " f"Avail images: {avail_image_types}") # NX-OS hack with IP address determination # if image_type in self.NEED_IP_ADDRESS: # res_params["ip address"] = virl_api.get_dhcp_ipaddr(network_name=resource_config.mgmt_network) # resources.update({res_name: res_params}) # res_details.update({"resources": resources, "default_gateway_info": default_gateway_info}) reservation_details.update( {"default_gateway_info": default_gateway_info}) logger.info(f"Updated Reservation Details: {reservation_details}") topology = Topology(**reservation_details) topology_data = topology.create_topology( mgmt_net_name=resource_config.mgmt_network, template_path=resource_config.templates_path) logger.info(f"Topology Data: {topology_data}") virl_api.upload_topology( topology_data=topology_data, reservation_id=resource_config.reservation_id) ifaces_info = virl_api.get_ifaces_info( topology_name=resource_config.reservation_id) for subnet_action_cidr, (subnet_action_id, alias) in subnet_dict.items(): logger.info( f"ACTIONS: {subnet_action_id}, {alias}, {subnet_action_cidr}" ) action_id = None if alias == "DefaultSubnet": action_id = subnet_action_id subnet_id = resource_config.mgmt_network else: for connection in reservation_details.get( "connections", []): logger.info("ACTION CIDR: {}, CONN NETWORK: {}".format( subnet_action_cidr, connection.get("network"))) if connection.get("network") == subnet_action_cidr: action_id = subnet_action_id break if not action_id and subnet_action_cidr in reservation_details.get( "subnets", {}).values(): action_id = subnet_action_id # else: if not action_id: raise VIRLShellError( f"Couldn't find appropriate network for action id {subnet_action_id}" ) subnet_id = None for _, node_params in ifaces_info.items(): for node in node_params: address = node.get("ipv4") if address and ipaddress.ip_address( address) in ipaddress.ip_network( subnet_action_cidr): subnet_id = node.get("network") break if subnet_id: break subnet_result = PrepareSubnetActionResult() subnet_result.actionId = action_id subnet_result.subnetId = subnet_id subnet_result.infoMessage = "Success" subnet_results.append(subnet_result) prepare_network_result.infoMessage = "PrepareConnectivity finished successfully" create_key_result = CreateKeysActionResult(actionId=keys_action_id, infoMessage="", accessKey="") results = [prepare_network_result, create_key_result] results.extend(subnet_results) result = DriverResponse(results).to_driver_response_json() logger.info(f"Prepare result {result}") return result