def _get_vm_instance_data(instance: NovaServer,
                          api: OSApi) -> List[VmDetailsProperty]:
    image = api.get_image_from_instance(instance)
    flavor = api.get_flavor_from_instance(instance)
    available_zone = getattr(instance, "OS-EXT-AZ:availability_zone")
    return [
        VmDetailsProperty("Image", image.name),
        VmDetailsProperty("Flavor", flavor.name),
        VmDetailsProperty("Availability Zone", available_zone),
        VmDetailsProperty("CPU", f"{flavor.vcpus} vCPU"),
        VmDetailsProperty("Memory", f"{flavor.ram} GB"),
        VmDetailsProperty("Disk Size", f"{flavor.disk} GB"),
    ]
def refresh_ip(
    api: OSApi,
    deployed_app: OSNovaImgDeployedApp,
    resource_conf: OSResourceConfig,
):
    instance = api.get_instance(deployed_app.vmdetails.uid)

    private_net_name = api.get_network_name(resource_conf.os_mgmt_net_id)
    new_private_ip = get_private_ip(instance, private_net_name)

    new_public_ip = get_floating_ip(instance)

    if new_private_ip != deployed_app.private_ip:
        deployed_app.update_private_ip(deployed_app.name, new_private_ip)
    if new_public_ip != deployed_app.public_ip:
        deployed_app.update_public_ip(new_public_ip)
def _get_vm_network_data(instance: NovaServer, api: OSApi,
                         mgmt_net_id) -> List[VmDetailsNetworkInterface]:
    network_interfaces = []
    for interface in instance.interface_list():
        net_dict = api.get_network_dict(id=interface.net_id)
        net_name = net_dict["name"]
        private_ip = get_private_ip(instance, net_name)
        is_primary_and_predefined = mgmt_net_id == interface.net_id
        public_ip = get_floating_ip(instance)

        network_data = [
            VmDetailsProperty("IP", private_ip),
            VmDetailsProperty("MAC Address", interface.mac_addr),
            VmDetailsProperty("VLAN Name", net_name, hidden=True),
        ]
        if public_ip:
            network_data.append(VmDetailsProperty("Floating IP", public_ip))

        current_interface = VmDetailsNetworkInterface(
            interfaceId=interface.mac_addr,
            networkId=net_dict["provider:segmentation_id"],
            isPrimary=is_primary_and_predefined,
            isPredefined=is_primary_and_predefined,
            networkData=network_data,
            privateIpAddress=private_ip,
            publicIpAddress=public_ip,
        )
        network_interfaces.append(current_interface)
    return sorted(network_interfaces, key=lambda x: x.networkId)
 def get_inventory(self, context: AutoLoadCommandContext) -> AutoLoadDetails:
     """Check connection to OpenStack."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Autoload command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         os_api = OSApi(conf, logger)
         validate_conf_and_connection(os_api, conf)
         return AutoLoadDetails([], [])
 def PowerOn(self, context: ResourceRemoteCommandContext, ports: List[str]):
     """Method spins up the VM."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting PowerOn command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         return PowerFlow(os_api, actions.deployed_app, logger).power_on()
 def DeleteInstance(self, context: ResourceRemoteCommandContext, ports: List[str]):
     """Deletes the VM from the OpenStack."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Delete Instance command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         delete_instance(os_api, actions.deployed_app)
 def ApplyConnectivityChanges(
     self, context: ResourceCommandContext, request: str
 ) -> str:
     """Connects/disconnect VMs to VLANs based on requested actions."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Connectivity command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         os_api = OSApi(conf, logger)
         return ConnectivityFlow(conf, os_api, logger).apply_connectivity_changes(
             request
         )
 def remote_refresh_ip(
     self,
     context: ResourceRemoteCommandContext,
     ports: List[str],
     cancellation_context: CancellationContext,
 ):
     """Retrieves the VM's updated IP address from the OpenStack."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Refresh IP command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         refresh_ip(os_api, actions.deployed_app, conf)
 def console(
     self,
     context: ResourceRemoteCommandContext,
     console_type: str,
     ports: List[str],
 ) -> str:
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Get Console command")
         validate_console_type(console_type)
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         return get_console(os_api, actions.deployed_app, console_type)
 def Deploy(
     self,
     context: ResourceCommandContext,
     request: str,
     cancellation_context: CancellationContext,
 ) -> str:
     """Deploy image."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Deploy command")
         logger.debug(f"Request: {request}")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         cancellation_manager = CancellationContextManager(cancellation_context)
         DeployVMRequestActions.register_deployment_path(OSNovaImgDeployApp)
         request_actions = DeployVMRequestActions.from_request(request, api)
         os_api = OSApi(conf, logger)
         return DeployAppFromNovaImgFlow(
             conf, cancellation_manager, os_api, logger
         ).deploy(request_actions)
 def GetVmDetails(
     self,
     context: ResourceCommandContext,
     requests: str,
     cancellation_context: CancellationContext,
 ) -> str:
     """Get instance operating system, specifications and networking information."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Get VM Details command")
         logger.debug(f"Requests: {requests}")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         cancellation_manager = CancellationContextManager(cancellation_context)
         GetVMDetailsRequestActions.register_deployment_path(OSNovaImgDeployedApp)
         actions = GetVMDetailsRequestActions.from_request(requests, api)
         os_api = OSApi(conf, logger)
         return GetVMDetailsFlow(
             conf, cancellation_manager, os_api, logger
         ).get_vm_details(actions)
예제 #12
0
def delete_instance(api: OSApi, deployed_app: OSNovaImgDeployedApp):
    instance = api.get_instance(deployed_app.vmdetails.uid)
    if deployed_app.public_ip:
        api.delete_floating_ip(deployed_app.public_ip)
    api.terminate_instance(instance)
def get_console(api: OSApi, deployed_app: OSNovaImgDeployedApp,
                console_type: str) -> str:
    inst = api.get_instance(deployed_app.vmdetails.uid)
    os_console_type = CONSOLE_TYPES[console_type]
    resp = inst.get_console_url(os_console_type)
    return resp["console"]["url"]
예제 #14
0
def os_api(resource_conf, logger, os_session, nova, neutron, monkeypatch):
    api = OSApi(resource_conf, logger)
    monkeypatch.setattr(api, "_os_session", os_session)
    monkeypatch.setattr(api, "_nova", nova)
    monkeypatch.setattr(api, "_neutron", neutron)
    return api