def cleanup():
    vm = get_vm(stub_config, vm_name)
    if vm:
        power_svc = Power(stub_config)
        vm_svc = VM(stub_config)
        state = power_svc.get(vm)
        if state == Power.Info(state=Power.State.POWERED_ON):
            power_svc.stop(vm)
        elif state == Power.Info(state=Power.State.SUSPENDED):
            power_svc.start(vm)
            power_svc.stop(vm)
        print("Deleting VM '{}' ({})".format(vm_name, vm))
        vm_svc.delete(vm)
Exemplo n.º 2
0
 def run(self):
     """
     Delete User specified VM from Server
     """
     vm_svc = VM(self.service_manager.stub_config)
     power_svc = Power(self.service_manager.stub_config)
     vm = get_vm(self.service_manager.stub_config, self.vm_name)
     if not vm:
         raise Exception('Sample requires an existing vm with name ({}).'
                         'Please create the vm first.'.format(vm_name))
     print("Deleting VM -- '{}-({})')".format(self.vm_name, vm))
     state = power_svc.get(vm)
     if state == Power.Info(state=Power.State.POWERED_ON):
         power_svc.stop(vm)
     elif state == Power.Info(state=Power.State.SUSPENDED):
         power_svc.start(vm)
         power_svc.stop(vm)
     vm_svc.delete(vm)
     print("Deleted VM -- '{}-({})".format(self.vm_name, vm))
def run():
    global vm
    vm = get_vm(stub_config, vm_name)
    if not vm:
        raise Exception('Sample requires an existing vm with name ({}).'
                        'Please create the vm first.'.format(vm_name))
    print("Using VM '{}' ({}) for Power Sample".format(vm_name, vm))

    # Create Power stub used for making requests
    global vm_power_svc
    vm_power_svc = Power(stub_config)

    # Get the vm power state
    print('\n# Example: Get current vm power state')
    status = vm_power_svc.get(vm)
    print('vm.Power.get({}) -> {}'.format(vm, pp(status)))

    # Power off the vm if it is on
    if status == Power.Info(state=Power.State.POWERED_ON):
        print('\n# Example: VM is powered on, power it off')
        vm_power_svc.stop(vm)
        print('vm.Power.stop({})'.format(vm))

    # Power on the vm
    print('# Example: Power on the vm')
    vm_power_svc.start(vm)
    print('vm.Power.start({})'.format(vm))

    # Suspend the vm
    print('\n# Example: Suspend the vm')
    vm_power_svc.suspend(vm)
    print('vm.Power.suspend({})'.format(vm))

    # Resume the vm
    print('\n# Example: Resume the vm')
    vm_power_svc.start(vm)
    print('vm.Power.start({})'.format(vm))

    # Reset the vm
    print('\n# Example: Reset the vm')
    vm_power_svc.reset(vm)
    print('vm.Power.reset({})'.format(vm))