Skip to main content

Power off and Delete VM

In this article, we’ll learn how to implement the PowerOff and DeleteInstance commands, which shut down and delete the VM from the cloud provider, respectively.

PowerOff method​

The PowerOff method shuts down (or powers off) the VM instance. It is run automatically as part of the sandbox’s teardown, and can also be run manually by the sandbox end-user from the deployed App’s commands pane. When PowerOff completes, the green ‘online’ live status icon is replaced with a grey one on the App resource, indicating it is offline.

Note: CloudShell sets the resource state to ‘offline’ if PowerOff completed successfully.

Signature​

def PowerOff(self, context, ports)

Inputs​

context: context is a ResourceRemoteCommandContext object that contains:

  • connectivity - CloudShell server connectivity data for authentication with CloudShell Automation API

  • resource - resource configuration settings entered by the user when creating the Cloud Provider resource in the Inventory dashboard

  • remote_reservation – reservation details

  • remote_endpoints - will contain a single ResourceContextDetails object which provides data for the operation.

This Github Sample is by QualiSystems

cloudshell/shell/core/driver_context.py view raw

def __init__(self, connectivity, resource, remote_reservation, remote_endpoints):
self.connectivity = connectivity # Connectivity details that can help connect to the APIs
""":type : ConnectivityContext"""
self.resource = resource # The details of the resource using the driver
""":type : ResourceContextDetails"""
self.remote_reservation = remote_reservation # The details of the remote reservation
""":type : ReservationContextDetails"""
self.remote_endpoints = remote_endpoints
""":type : list[ResourceContextDetails]"""

Ports​

Legacy argument. Irrelevant for custom cloud providers.

Error handling​

If an error occurs during the PowerOff operation, the command should raise an exception.

PowerOff method implementation​

The PowerOff method should perform the following steps:

  1. Retrieve the cloud provider resource’s connection credentials

  2. Convert context deployed_app_json string to object

  3. Power off the deployed App resource

PowerOff implementation example​

This Github Sample is by QualiSystems

cloudshell/shell/core/driver_context.py view raw

def PowerOff(self, context, ports):
"""
Will power off the compute resource
:param ResourceRemoteCommandContext context:
:param ports:
"""
with LoggingSessionContext(context) as logger, ErrorHandlingContext(logger):
self._log(logger, 'power_off_context', context)
self._log(logger, 'power_off_ports', ports)
cloud_provider_resource = L2HeavenlyCloudShell.create_from_context(context)
resource_ep = context.remote_endpoints[0]
deployed_app_dict = json.loads(resource_ep.app_context.deployed_app_json)
vm_uid = deployed_app_dict['vmdetails']['uid']
HeavenlyCloudServiceWrapper.power_off(cloud_provider_resource, vm_uid)

Return value​

None

DeleteInstance method​

The DeleteInstance method powers off the VM, deletes the VM from the cloud provider and removes the App from the sandbox. It is run when removing the deployed App from the sandbox or during the sandbox’s teardown.

Signature​

def DeleteInstance(self, context, ports)

Inputs​

context: context is a ResourceRemoteCommandContext object that contains:

  • connectivity - CloudShell server connectivity data for authentication with CloudShell Automation API

  • resource - resource configuration settings entered by the user when creating the Cloud Provider resource in the Inventory dashboard

  • remote_reservation – reservation details

  • remote_endpoints- will contain a single ResourceContextDetails object which provides data for the operation.

This Github Sample is by QualiSystems

cloudshell/shell/core/driver_context.py view raw

def __init__(self, connectivity, resource, remote_reservation, remote_endpoints):
self.connectivity = connectivity # Connectivity details that can help connect to the APIs
""":type : ConnectivityContext"""
self.resource = resource # The details of the resource using the driver
""":type : ResourceContextDetails"""
self.remote_reservation = remote_reservation # The details of the remote reservation
""":type : ReservationContextDetails"""
self.remote_endpoints = remote_endpoints
""":type : list[ResourceContextDetails]"""

Ports​

Legacy argument. Obsolete for custom cloud providers.

DeleteInstance method implementation​

The DeleteInstance method should perform the following steps:

  1. Retrieve the cloud provider resource’s connection credentials.

  2. Convert the deployed_app_json context string to object.

  3. Delete the VM instance from the cloud provider.

DeleteInstance implementation example​

This Github Sample is by QualiSystems

src/driver.py view raw

def DeleteInstance(self, context, ports):
"""
Will delete the compute resource
:param ResourceRemoteCommandContext context:
:param ports:
"""
with LoggingSessionContext(context) as logger, ErrorHandlingContext(logger):
self._log(logger, 'DeleteInstance_context', context)
self._log(logger, 'DeleteInstance_ports', ports)
cloud_provider_resource = L2HeavenlyCloudShell.create_from_context(context)
resource_ep = context.remote_endpoints[0]
deployed_app_dict = json.loads(resource_ep.app_context.deployed_app_json)
vm_uid = deployed_app_dict['vmdetails']['uid']
HeavenlyCloudServiceWrapper.delete_instance(cloud_provider_resource, vm_uid)

Return value​

None