def cleanup(self):
     vm = get_vm(self.client, self.vm_name)
     if vm:
         state = self.client.vcenter.vm.Power.get(vm)
         if state == Power.Info(state=Power.State.POWERED_ON):
             self.client.vcenter.vm.Power.stop(vm)
         elif state == Power.Info(state=Power.State.SUSPENDED):
             self.client.vcenter.vm.Power.start(vm)
             self.client.vcenter.vm.Power.stop(vm)
         print("Deleting VM '{}' ({})".format(self.vm_name, vm))
         self.client.vcenter.VM.delete(vm)
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)
Exemple #3
0
    def run(self):
        # find the given VM
        self.vm = get_vm(self.vsphere_client, self.vm_name)
        if not self.vm:
            raise Exception('Sample requires an existing vm with name ({}).'
                            'Please create the vm first.'.format(self.vm_name))
        print("Using VM '{}' ({}) for Guest Info Sample".format(
            self.vm_name, self.vm))

        # power on the VM if necessary
        status = self.vsphere_client.vcenter.vm.Power.get(self.vm)
        if status != HardPower.Info(state=HardPower.State.POWERED_ON):
            print('Powering on VM.')
            self.vsphere_client.vcenter.vm.Power.start(self.vm)

        # wait for guest info to be ready
        wait_for_guest_info_ready(self.vsphere_client, self.vm, 600)

        # get the Identity
        identity = self.vsphere_client.vcenter.vm.guest.Identity.get(self.vm)
        print('vm.guest.Identity.get({})'.format(self.vm))
        print('Identity: {}'.format(pp(identity)))

        # get the local filesystem info
        local_filesysteem = \
             self.vsphere_client.vcenter.vm.guest.LocalFilesystem.get(self.vm)
        print('vm.guest.LocalFilesystem.get({})'.format(self.vm))
        print('LocalFilesystem: {}'.format(pp(local_filesysteem)))
 def run(self):
     """
     Delete User specified VM from Server
     """
     vm = get_vm(self.client, self.vm_name)
     if not vm:
         raise Exception('Sample requires an existing vm with name ({}).'
                         'Please create the vm first.'.format(self.vm_name))
     print("Deleting VM -- '{}-({})')".format(self.vm_name, vm))
     state = self.client.vcenter.vm.Power.get(vm)
     if state == Power.Info(state=Power.State.POWERED_ON):
         self.client.vcenter.vm.Power.stop(vm)
     elif state == Power.Info(state=Power.State.SUSPENDED):
         self.client.vcenter.vm.Power.start(vm)
         self.client.vcenter.vm.Power.stop(vm)
     self.client.vcenter.VM.delete(vm)
     print("Deleted VM -- '{}-({})".format(self.vm_name, vm))
Exemple #5
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 cleanup():
    # Power off the vm
    print('\n# Cleanup: Power off the vm')
    client.vcenter.vm.Power.stop(vm)
    print('vm.Power.stop({})'.format(vm))
    status = client.vcenter.vm.Power.get(vm)
    if status == Power.Info(state=Power.State.POWERED_OFF,
                            clean_power_off=True):
        print('VM is powered off')
    else:
        print('vm.Power Warning: Could not power off vm')
Exemple #7
0
    def run(self):
        self.vm = get_vm(self.vsphere_client, self.vm_name)
        if not self.vm:
            raise Exception('Sample requires an existing vm with name ({}).'
                            'Please create the vm first.'.format(self.vm_name))
        print("Using VM '{}' ({}) for Guest Power Sample".format(
            self.vm_name, self.vm))

        # power on the VM if necessary
        status = self.vsphere_client.vcenter.vm.Power.get(self.vm)
        if status != HardPower.Info(state=HardPower.State.POWERED_ON):
            print("Powering on VM {}.".format(self.vm_name))
            self.vsphere_client.vcenter.vm.Power.start(self.vm)
            print('vm.Power.start({})'.format(self.vm))

        # reboot the guest
        wait_for_power_operations_state(self.vsphere_client, self.vm, True,
                                        STATE_TIMEOUT)
        print('\n# Example: Reboot the vm')
        identity = self.vsphere_client.vcenter.vm.guest.Power.reboot(self.vm)
        wait_for_power_operations_state(self.vsphere_client, self.vm, False,
                                        STATE_TIMEOUT)
        print('Tools have stopped.')
        wait_for_guest_power_state(self.vsphere_client, self.vm,
                                   Power.State.RUNNING, STATE_TIMEOUT)
        wait_for_power_operations_state(self.vsphere_client, self.vm, True,
                                        STATE_TIMEOUT)
        print('vm.guest.Power.reboot({})'.format(self.vm))

        # Standby the guest -- already running from previous step
        print('\n# Example: Standby the vm')
        self.vsphere_client.vcenter.vm.guest.Power.standby(self.vm)
        wait_for_guest_power_state(self.vsphere_client, self.vm,
                                   Power.State.NOT_RUNNING, STATE_TIMEOUT)
        print('vm.guest.Power.standby({})'.format(self.vm))

        # shutdown the guest
        # power back on from previous standby
        self.vsphere_client.vcenter.vm.Power.start(self.vm)
        wait_for_power_operations_state(self.vsphere_client, self.vm, True,
                                        STATE_TIMEOUT)
        print("Shutting down VM {}.".format(self.vm_name))
        self.vsphere_client.vcenter.vm.guest.Power.shutdown(self.vm)
        wait_for_guest_power_state(self.vsphere_client, self.vm,
                                   Power.State.NOT_RUNNING, STATE_TIMEOUT)
        print('vm.guest.Power.shutdown({})'.format(self.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))
Exemple #9
0
from com.vmware.vcenter.vm_client import Power
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()

# Disable cert verification for demo purpose.
# This is not recommended in a production environment.
session.verify = False

# Disable the secure connection warning for demo purpose.
# This is not recommended in a production environment.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Connect to a vCenter Server using username and password
vsphere_client = create_vsphere_client(server='10.10.40.114',
                                       username='******',
                                       password='******',
                                       session=session)

# List all VMs inside the vCenter Server
vms = vsphere_client.vcenter.VM.list()

for vm in vms:
    if (vm.name[:9] == 'ubuntu-hx'):
        vm_instance_status = vsphere_client.vcenter.vm.Power.get(vm.vm)
        if vm_instance_status == Power.Info(state=Power.State.POWERED_ON):
            print(vm.name + ": Power ON")
        else:
            print(vm.name + ": " + str(vm_instance_status))
            vm_instance_status = vsphere_client.vcenter.vm.Power.start(vm.vm)
        #print(vm.name, vm_instance)
Exemple #10
0
session = requests.session()

# Disable cert verification for demo purpose.
# This is not recommended in a production environment.
session.verify = False

# Disable the secure connection warning for demo purpose.
# This is not recommended in a production environment.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Connect to a vCenter Server using username and password
config = VsCreadential.load('.credentials.yaml')
client = create_vsphere_client(server=config.hostname, username=config.username, password=config.password, session=session)

vm = 'TESTVM'

vm = client.vcenter.VM.list(VM.FilterSpec(names=set(['TESTVM'])))[0]
vm_info = client.vcenter.VM.get(vm.vm)

print('vm.get({}) -> {}'.format(vm, vm_info))

state = client.vcenter.vm.Power.get(vm.vm)
if state == Power.Info(state=Power.State.POWERED_ON):
    client.vcenter.vm.Power.stop(vm.vm)
elif state == Power.Info(state=Power.State.SUSPENDED):
    client.vcenter.vm.Power.start(vm.vm)
    client.vcenter.vm.Power.stop(vm.vm)
client.vcenter.VM.delete(vm.vm)
print("Deleted VM -- '{}-({})".format('TESTVM', vm.vm))