コード例 #1
0
 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)
コード例 #2
0
ファイル: info.py プロジェクト: Jolin-blank/vsphere_cobbler
    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)))
コード例 #3
0
 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))
コード例 #4
0
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')
コード例 #5
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))
コード例 #6
0
def run():
    global vm
    vm = get_vm(client, 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))

    # Get the vm power state
    print('\n# Example: Get current vm power state')
    status = client.vcenter.vm.Power.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')
        client.vcenter.vm.Power.stop(vm)
        print('vm.Power.stop({})'.format(vm))

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

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

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

    # Reset the vm
    print('\n# Example: Reset the vm')
    client.vcenter.vm.Power.reset(vm)
    print('vm.Power.reset({})'.format(vm))
コード例 #7
0
ファイル: power.py プロジェクト: strefethen/samples
from com.vmware.vcenter.vm_client import Power
...
stub_config = vapiconnect.connect(server, username, password,
                                  skip_verification)

# Create Power stub used for making requests
global vm_power_svc
vm_power_svc = Power(stub_config)
コード例 #8
0
ファイル: vm_start.py プロジェクト: wkliu/ansible-samples
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)
コード例 #9
0
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)
コード例 #10
0
def run():
    # * Backings types are FILE, HOST_DEVICE, PIPE_SERVER, PIPE_CLIENT,
    # NETWORK_SERVER, NETWORK_CLIENT
    #   * NetworkLocation: See
    # https://kb.vmware.com/selfservice/microsites/search.do?language=en_US
    # &cmd=displayKC&externalId=2004954
    #   * Proxy: https://www.vmware.com/support/developer/vc-sdk/visdk41pubs
    # /vsp41_usingproxy_virtual_serial_ports.pdf

    global vm, serial_svc
    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 Serial Sample".format(vm_name, vm))

    # Create Serial port stub used for making requests
    serial_svc = Serial(stub_config)
    vm_power_svc = Power(stub_config)

    print('\n# Example: List all Serial ports for a VM')
    serial_summaries = serial_svc.list(vm=vm)
    print('vm.hardware.Serial.list({}) -> {}'.format(vm, serial_summaries))

    # Save current list of Serial ports to verify that we have cleaned up
    # properly
    global orig_serial_summaries
    orig_serial_summaries = serial_summaries

    # Get information for each Serial port on the VM
    for serial_summary in serial_summaries:
        serial = serial_summary.port
        serial_info = serial_svc.get(vm=vm, port=serial)
        print('vm.hardware.Serial.get({}, {}) -> {}'.format(
            vm, serial, pp(serial_info)))

    global serials_to_delete

    print('\n# Example: Create Serial port with defaults')
    serial_create_spec = Serial.CreateSpec()
    serial = serial_svc.create(vm, serial_create_spec)
    print('vm.hardware.Serial.create({}, {}) -> {}'.format(
        vm, serial_create_spec, serial))
    serials_to_delete.append(serial)
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    # Make sure output file doesn't exist already
    cleanup_backends()

    print('\n# Example: Create Serial port with FILE backing')
    serial_port_datastore_path = testbed.config['SERIAL_PORT_DATASTORE_PATH']
    serial_create_spec = Serial.CreateSpec(
        start_connected=True,
        allow_guest_control=True,
        backing=Serial.BackingSpec(type=Serial.BackingType.FILE,
                                   file=serial_port_datastore_path))
    serial = serial_svc.create(vm, serial_create_spec)
    print('vm.hardware.Serial.create({}, {}) -> {}'.format(
        vm, serial_create_spec, serial))
    serials_to_delete.append(serial)
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    print('\n# Example: Create Serial port to use NETWORK_SERVER')
    serial_port_network_server_location = \
        testbed.config['SERIAL_PORT_NETWORK_SERVER_LOCATION']
    serial_create_spec = Serial.CreateSpec(
        start_connected=True,
        allow_guest_control=True,
        backing=Serial.BackingSpec(
            type=Serial.BackingType.NETWORK_SERVER,
            network_location=serial_port_network_server_location))
    serial = serial_svc.create(vm, serial_create_spec)
    print('vm.hardware.Serial.create({}, {}) -> {}'.format(
        vm, serial_create_spec, serial))
    serials_to_delete.append(serial)
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    print('\n# Example: Update Serial port to use NETWORK_CLIENT')
    serial_port_network_client_location = \
        testbed.config['SERIAL_PORT_NETWORK_CLIENT_LOCATION']
    serial_port_network_proxy = testbed.config['SERIAL_PORT_NETWORK_PROXY']
    serial_update_spec = Serial.UpdateSpec(
        start_connected=False,
        allow_guest_control=False,
        backing=Serial.BackingSpec(
            type=Serial.BackingType.NETWORK_CLIENT,
            network_location=serial_port_network_client_location,
            proxy=serial_port_network_proxy))
    serial_svc.update(vm, serial, serial_update_spec)
    print('vm.hardware.Serial.update({}, {}) -> {}'.format(
        vm, serial_update_spec, serial))
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    print('\n# Starting VM to run connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.start(vm)
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    print('\n# Example: Connect Serial port after powering on VM')
    serial_svc.connect(vm, serial)
    print('vm.hardware.Serial.connect({}, {})'.format(vm, serial))
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    print('\n# Example: Disconnect Serial port while VM is powered on')
    serial_svc.disconnect(vm, serial)
    print('vm.hardware.Serial.disconnect({}, {})'.format(vm, serial))
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    print('\n# Stopping VM after connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.stop(vm)
    serial_info = serial_svc.get(vm, serial)
    print('vm.hardware.Serial.get({}, {}) -> {}'.format(
        vm, serial, pp(serial_info)))

    # List all Serial ports for a VM
    serial_summaries = serial_svc.list(vm=vm)
    print('vm.hardware.Serial.list({}) -> {}'.format(vm, serial_summaries))

    # Always cleanup output file so the VM can be powered on next time
    cleanup_backends()
コード例 #11
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))
コード例 #12
0
def run():
    # * Floppy images must be pre-existing.  This API does not expose
    #   a way to create new floppy images.
    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 Floppy Sample".format(vm_name, vm))
    img_datastore_path = testbed.config['FLOPPY_DATASTORE_PATH']

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

    print('\n# Example: List all Floppys for a VM')
    floppy_summaries = floppy_svc.list(vm=vm)
    print('vm.hardware.Floppy.list({}) -> {}'.format(vm, floppy_summaries))

    # Save current list of Floppys to verify that we have cleaned up properly
    global orig_floppy_summaries
    orig_floppy_summaries = floppy_summaries

    # Get information for each Floppy on the VM
    global floppy
    for floppy_summary in floppy_summaries:
        floppy = floppy_summary.floppy
        floppy_info = floppy_svc.get(vm=vm, floppy=floppy)
        print('vm.hardware.Floppy.get({}, {}) -> {}'.
              format(vm, floppy, pp(floppy_info)))

    # Maximum 2 Floppy devices allowed so delete them as they are created except
    # for the last one which will be deleted at the end

    print('\n# Example: Create Floppy port with defaults')
    floppy_create_spec = Floppy.CreateSpec()
    floppy = floppy_svc.create(vm, floppy_create_spec)
    print('vm.hardware.Floppy.create({}, {}) -> {}'.
          format(vm, floppy_create_spec, floppy))
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))
    floppy_svc.delete(vm, floppy)
    print('vm.hardware.Floppy.delete({}, {})'.format(vm, floppy))

    print('\n# Example: Create Floppy with CLIENT_DEVICE backing')
    floppy_create_spec = Floppy.CreateSpec(
        backing=Floppy.BackingSpec(type=Floppy.BackingType.CLIENT_DEVICE))
    floppy = floppy_svc.create(vm, floppy_create_spec)
    print('vm.hardware.Floppy.create({}, {}) -> {}'.
          format(vm, floppy_create_spec, floppy))
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))
    floppy_svc.delete(vm, floppy)
    print('vm.hardware.Floppy.delete({}, {})'.format(vm, floppy))

    print('\n# Example: Create Floppy with IMAGE_FILE backing, '
          'start_connected=True,')
    print('           allow_guest_control=True')
    floppy_create_spec = Floppy.CreateSpec(
        allow_guest_control=True,
        start_connected=True,
        backing=Floppy.BackingSpec(type=Floppy.BackingType.IMAGE_FILE,
                                   image_file=img_datastore_path))
    floppy = floppy_svc.create(vm, floppy_create_spec)
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))

    print('\n# Example: Update start_connected=False, '
          'allow_guest_control=False')
    floppy_update_spec = Floppy.UpdateSpec(
        start_connected=False, allow_guest_control=False)
    print('vm.hardware.Floppy.update({}, {}, {})'.
          format(vm, floppy, floppy_update_spec))
    floppy_svc.update(vm, floppy, floppy_update_spec)
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))

    print('\n# Starting VM to run connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.start(vm)
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))

    print('\n# Example: Connect Floppy after powering on VM')
    floppy_svc.connect(vm, floppy)
    print('vm.hardware.Floppy.connect({}, {})'.format(vm, floppy))
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))

    print('\n# Example: Disconnect Floppy while VM is powered on')
    floppy_svc.disconnect(vm, floppy)
    print('vm.hardware.Floppy.disconnect({}, {})'.format(vm, floppy))
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))

    print('\n# Stopping VM after connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.stop(vm)
    floppy_info = floppy_svc.get(vm, floppy)
    print('vm.hardware.Floppy.get({}, {}) -> {}'.
          format(vm, floppy, pp(floppy_info)))

    # List all Floppys for a VM
    floppy_summaries = floppy_svc.list(vm=vm)
    print('vm.hardware.Floppy.list({}) -> {}'.format(vm, floppy_summaries))
コード例 #13
0
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 CD-ROM Sample".format(vm_name, vm))
    iso_datastore_path = testbed.config['ISO_DATASTORE_PATH']

    # Create CD-ROM stub used for making requests
    global cdrom_svc, vm_power_svc, sata_svc
    cdrom_svc = Cdrom(stub_config)
    vm_power_svc = Power(stub_config)

    # Create SATA controller
    print('\n# Setup: Create a SATA controller')
    sata_svc = Sata(stub_config)
    sata_create_spec = Sata.CreateSpec()
    print('# Adding SATA controller for SATA Disk samples')
    global sata
    sata = sata_svc.create(vm, sata_create_spec)
    print('vm.hardware.adapter.Sata.create({}, {}) -> {}'.
          format(vm, sata_create_spec, sata))

    print('\n# Example: List all Cdroms for a VM')
    cdrom_summaries = cdrom_svc.list(vm=vm)
    print('vm.hardware.Cdrom.list({}) -> {}'.format(vm, cdrom_summaries))

    # Save current list of Cdroms to verify that we have cleaned up properly
    global orig_cdrom_summaries
    orig_cdrom_summaries = cdrom_summaries

    # Get information for each CD-ROM on the VM
    for cdrom_summary in cdrom_summaries:
        cdrom = cdrom_summary.cdrom
        cdrom_info = cdrom_svc.get(vm=vm, cdrom=cdrom)
        print('vm.hardware.Cdrom.get({}, {}) -> {}'.
              format(vm, cdrom, pp(cdrom_info)))

    global cdroms_to_delete

    print('\n# Example: Create CD-ROM with ISO_FILE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        start_connected=True,
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.ISO_FILE,
                                  iso_file=iso_datastore_path))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Create CD-ROM with CLIENT_DEVICE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.CLIENT_DEVICE))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    print('vm.hardware.Cdrom.create({}, {}) -> {}'.
          format(vm, cdrom_create_spec, cdrom))
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Create CD-ROM using auto-detect HOST_DEVICE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.HOST_DEVICE))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    print('vm.hardware.Cdrom.create({}, {}) -> {}'.
          format(vm, cdrom_create_spec, cdrom))
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Create SATA CD-ROM using CLIENT_DEVICE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        type=Cdrom.HostBusAdapterType.SATA,
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.CLIENT_DEVICE))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    print('vm.hardware.Cdrom.create({}, {}) -> {}'.
          format(vm, cdrom_create_spec, cdrom))
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Create SATA CD-ROM on specific bus using '
          'CLIENT_DEVICE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        type=Cdrom.HostBusAdapterType.SATA,
        sata=SataAddressSpec(bus=0),
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.CLIENT_DEVICE))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    print('vm.hardware.Cdrom.create({}, {}) -> {}'.
          format(vm, cdrom_create_spec, cdrom))
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Create SATA CD-ROM on specific bus and unit using '
          'CLIENT_DEVICE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        type=Cdrom.HostBusAdapterType.SATA,
        sata=SataAddressSpec(bus=0, unit=10),
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.CLIENT_DEVICE))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    print('vm.hardware.Cdrom.create({}, {}) -> {}'.
          format(vm, cdrom_create_spec, cdrom))
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Create IDE CD-ROM using CLIENT_DEVICE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        type=Cdrom.HostBusAdapterType.IDE,
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.CLIENT_DEVICE))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    print('vm.hardware.Cdrom.create({}, {}) -> {}'.
          format(vm, cdrom_create_spec, cdrom))
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Create IDE CD-ROM on specific bus and unit using '
          'CLIENT_DEVICE backing')
    cdrom_create_spec = Cdrom.CreateSpec(
        type=Cdrom.HostBusAdapterType.IDE,
        ide=IdeAddressSpec(False, True),
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.CLIENT_DEVICE))
    cdrom = cdrom_svc.create(vm, cdrom_create_spec)
    print('vm.hardware.Cdrom.create({}, {}) -> {}'.
          format(vm, cdrom_create_spec, cdrom))
    cdroms_to_delete.append(cdrom)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    # Change the last cdrom that was created

    print('\n# Example: Update backing from CLIENT_DEVICE to ISO_FILE')
    cdrom_update_spec = Cdrom.UpdateSpec(
        backing=Cdrom.BackingSpec(type=Cdrom.BackingType.ISO_FILE,
                                  iso_file=iso_datastore_path))
    print('vm.hardware.Cdrom.update({}, {}, {})'.
          format(vm, cdrom, cdrom_update_spec))
    cdrom_svc.update(vm, cdrom, cdrom_update_spec)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Update start_connected=False, '
          'allow_guest_control=False')
    cdrom_update_spec = Cdrom.UpdateSpec(
        start_connected=False, allow_guest_control=False)
    print('vm.hardware.Cdrom.update({}, {}, {})'.
          format(vm, cdrom, cdrom_update_spec))
    cdrom_svc.update(vm, cdrom, cdrom_update_spec)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Starting VM to run connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.start(vm)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Connect CD-ROM after powering on VM')
    cdrom_svc.connect(vm, cdrom)
    print('vm.hardware.Cdrom.connect({}, {})'.format(vm, cdrom))
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Example: Disconnect CD-ROM while VM is powered on')
    cdrom_svc.disconnect(vm, cdrom)
    print('vm.hardware.Cdrom.disconnect({}, {})'.format(vm, cdrom))
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    print('\n# Stopping VM after connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.stop(vm)
    cdrom_info = cdrom_svc.get(vm, cdrom)
    print('vm.hardware.Cdrom.get({}, {}) -> {}'.
          format(vm, cdrom, pp(cdrom_info)))

    # List all Cdroms for a VM
    cdrom_summaries = cdrom_svc.list(vm=vm)
    print('vm.hardware.Cdrom.list({}) -> {}'.format(vm, cdrom_summaries))
コード例 #14
0
def run():
    global vm, parallel_svc
    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 Parallel Sample".format(vm_name, vm))

    # Create Parallel port stub used for making requests
    parallel_svc = Parallel(stub_config)
    vm_power_svc = Power(stub_config)

    print('\n# Example: List all Parallel ports for a VM')
    parallel_summaries = parallel_svc.list(vm=vm)
    print('vm.hardware.Parallel.list({}) -> {}'.format(vm, parallel_summaries))

    # Save current list of Parallel ports to verify that we have cleaned up
    # properly
    global orig_parallel_summaries
    orig_parallel_summaries = parallel_summaries

    # Get information for each Parallel port on the VM
    for parallel_summary in parallel_summaries:
        parallel = parallel_summary.port
        parallel_info = parallel_svc.get(vm=vm, port=parallel)
        print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
            vm, parallel, pp(parallel_info)))

    # Make sure output file doesn't exist already
    cleanup_backends()

    print('\n# Example: Create Parallel port with defaults')
    parallel_create_spec = Parallel.CreateSpec()
    parallel = parallel_svc.create(vm, parallel_create_spec)
    print('vm.hardware.Parallel.create({}, {}) -> {}'.format(
        vm, parallel_create_spec, parallel))
    global parallels_to_delete
    parallels_to_delete.append(parallel)
    parallel_info = parallel_svc.get(vm, parallel)
    print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
        vm, parallel, pp(parallel_info)))

    print('\n# Example: Create Parallel port with FILE backing')
    parallel_port_datastore_path = testbed.config[
        'PARALLEL_PORT_DATASTORE_PATH']
    parallel_create_spec = Parallel.CreateSpec(
        start_connected=True,
        allow_guest_control=True,
        backing=Parallel.BackingSpec(type=Parallel.BackingType.FILE,
                                     file=parallel_port_datastore_path))
    parallel = parallel_svc.create(vm, parallel_create_spec)
    print('vm.hardware.Parallel.create({}, {}) -> {}'.format(
        vm, parallel_create_spec, parallel))
    parallels_to_delete.append(parallel)
    parallel_info = parallel_svc.get(vm, parallel)
    print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
        vm, parallel, pp(parallel_info)))

    print('\n# Example: Update Parallel port with same file but '
          'start_connected=False')
    print('#          and allow_guest_control=False')
    parallel_port_datastore_path = testbed.config[
        'PARALLEL_PORT_DATASTORE_PATH']
    parallel_update_spec = Parallel.UpdateSpec(
        start_connected=False,
        allow_guest_control=False,
        backing=Parallel.BackingSpec(type=Parallel.BackingType.FILE,
                                     file=parallel_port_datastore_path))
    parallel_svc.update(vm, parallel, parallel_update_spec)
    print('vm.hardware.Parallel.update({}, {}) -> {}'.format(
        vm, parallel_update_spec, parallel))
    parallel_info = parallel_svc.get(vm, parallel)
    print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
        vm, parallel, pp(parallel_info)))

    print('\n# Starting VM to run connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.start(vm)
    parallel_info = parallel_svc.get(vm, parallel)
    print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
        vm, parallel, pp(parallel_info)))

    print('\n# Example: Connect Parallel port after powering on VM')
    parallel_svc.connect(vm, parallel)
    print('vm.hardware.Parallel.connect({}, {})'.format(vm, parallel))
    parallel_info = parallel_svc.get(vm, parallel)
    print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
        vm, parallel, pp(parallel_info)))

    print('\n# Example: Disconnect Parallel port while VM is powered on')
    parallel_svc.disconnect(vm, parallel)
    print('vm.hardware.Parallel.disconnect({}, {})'.format(vm, parallel))
    parallel_info = parallel_svc.get(vm, parallel)
    print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
        vm, parallel, pp(parallel_info)))

    print('\n# Stopping VM after connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.stop(vm)
    parallel_info = parallel_svc.get(vm, parallel)
    print('vm.hardware.Parallel.get({}, {}) -> {}'.format(
        vm, parallel, pp(parallel_info)))

    # List all Parallel ports for a VM
    parallel_summaries = parallel_svc.list(vm=vm)
    print('vm.hardware.Parallel.list({}) -> {}'.format(vm, parallel_summaries))

    # Always cleanup output file so the VM can be powered on next time
    cleanup_backends()
コード例 #15
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))
コード例 #16
0
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 Disk Sample".format(vm_name, vm))

    # Get standard portgroup to use as backing for sample
    standard_network = network_helper.get_standard_network_backing(
        stub_config, testbed.config['STDPORTGROUP_NAME'],
        testbed.config['VM_DATACENTER_NAME'])

    # Get distributed portgroup to use as backing for sample
    distributed_network = network_helper.get_distributed_network_backing(
        stub_config, testbed.config['VDPORTGROUP1_NAME'],
        testbed.config['VM_DATACENTER_NAME'])

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

    print('\n# Example: List all Ethernet adapters for a VM')
    nic_summaries = ethernet_svc.list(vm=vm)
    print('vm.hardware.Ethernet.list({}) -> {}'.format(vm, nic_summaries))

    # Save current list of Ethernet adapters to verify that we have cleaned
    # up properly
    global orig_nic_summaries
    orig_nic_summaries = nic_summaries

    # Get information for each Ethernet on the VM
    for nic_summary in nic_summaries:
        nic = nic_summary.nic
        nic_info = ethernet_svc.get(vm=vm, nic=nic)
        print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
            vm, nic, nic_info))

    global nics_to_delete

    print('\n# Example: Create Ethernet Nic using STANDARD_PORTGROUP with '
          'default settings')
    nic_create_spec = Ethernet.CreateSpec(backing=Ethernet.BackingSpec(
        type=Ethernet.BackingType.STANDARD_PORTGROUP,
        network=standard_network))
    nic = ethernet_svc.create(vm, nic_create_spec)
    print('vm.hardware.Ethernet.create({}, {}) -> {}'.format(
        vm, nic_create_spec, nic))
    nics_to_delete.append(nic)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Example: Create Ethernet Nic using DISTRIBUTED_PORTGROUP '
          'with defaults')
    nic_create_spec = Ethernet.CreateSpec(backing=Ethernet.BackingSpec(
        type=Ethernet.BackingType.DISTRIBUTED_PORTGROUP,
        network=distributed_network))
    nic = ethernet_svc.create(vm, nic_create_spec)
    print('vm.hardware.Ethernet.create({}, {}) -> {}'.format(
        vm, nic_create_spec, nic))
    nics_to_delete.append(nic)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Example: Create Ethernet Nic using STANDARD_'
          'PORTGROUP specifying')
    print('#          start_connected=True, allow_guest_control=True,')
    print('#          mac_type, mac_Address, wake_on_lan_enabled')
    nic_create_spec = Ethernet.CreateSpec(
        start_connected=True,
        allow_guest_control=True,
        mac_type=Ethernet.MacAddressType.MANUAL,
        mac_address='01:23:45:67:89:10',
        wake_on_lan_enabled=True,
        backing=Ethernet.BackingSpec(
            type=Ethernet.BackingType.STANDARD_PORTGROUP,
            network=standard_network))
    nic = ethernet_svc.create(vm, nic_create_spec)
    print('vm.hardware.Ethernet.create({}, {}) -> {}'.format(
        vm, nic_create_spec, nic))
    nics_to_delete.append(nic)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Example: Create Ethernet Nic using DISTRIBUTED_PORTGROUP '
          'specifying')
    print('#          start_connected=True, allow_guest_control=True,')
    print('#          mac_type, mac_Address, wake_on_lan_enabled')
    nic_create_spec = Ethernet.CreateSpec(
        start_connected=True,
        allow_guest_control=True,
        mac_type=Ethernet.MacAddressType.MANUAL,
        mac_address='24:68:10:12:14:16',
        wake_on_lan_enabled=True,
        backing=Ethernet.BackingSpec(
            type=Ethernet.BackingType.DISTRIBUTED_PORTGROUP,
            network=distributed_network))
    nic = ethernet_svc.create(vm, nic_create_spec)
    print('vm.hardware.Ethernet.create({}, {}) -> {}'.format(
        vm, nic_create_spec, nic))
    nics_to_delete.append(nic)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    # Change the last nic that was created
    print('\n# Example: Update Ethernet Nic with different backing')
    nic_update_spec = Ethernet.UpdateSpec(backing=Ethernet.BackingSpec(
        type=Ethernet.BackingType.STANDARD_PORTGROUP,
        network=standard_network))
    print('vm.hardware.Ethernet.update({}, {}, {})'.format(
        vm, nic, nic_update_spec))
    ethernet_svc.update(vm, nic, nic_update_spec)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Example: Update Ethernet Nic wake_on_lan_enabled=False')
    print('#                              mac_type=GENERATED,')
    print('#                              start_connected=False,')
    print('#                              allow_guest_control=False')
    nic_update_spec = Ethernet.UpdateSpec(
        wake_on_lan_enabled=False,
        mac_type=Ethernet.MacAddressType.GENERATED,
        start_connected=False,
        allow_guest_control=False)
    print('vm.hardware.Ethernet.update({}, {}, {})'.format(
        vm, nic, nic_update_spec))
    ethernet_svc.update(vm, nic, nic_update_spec)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Starting VM to run connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.start(vm)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Example: Connect Ethernet Nic after powering on VM')
    ethernet_svc.connect(vm, nic)
    print('vm.hardware.Ethernet.connect({}, {})'.format(vm, nic))
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Example: Disconnect Ethernet Nic while VM is powered on')
    ethernet_svc.disconnect(vm, nic)
    print('vm.hardware.Ethernet.disconnect({}, {})'.format(vm, nic))
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    print('\n# Stopping VM after connect/disconnect sample')
    print('vm.Power.start({})'.format(vm))
    vm_power_svc.stop(vm)
    nic_info = ethernet_svc.get(vm, nic)
    print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
        vm, nic, pp(nic_info)))

    # List all Nics for a VM
    nic_summaries = ethernet_svc.list(vm=vm)
    print('vm.hardware.Ethernet.list({}) -> {}'.format(vm, nic_summaries))