Ejemplo n.º 1
0
    def test_get_console_output(self):
        vm_ref = fake_vm_ref()
        result = fake_http_resp()
        self._create_instance_in_the_db()
        self.mox.StubOutWithMock(vm_util, "get_vm_ref_from_name")
        self.mox.StubOutWithMock(urllib2, "urlopen")
        vm_util.get_vm_ref_from_name(mox.IgnoreArg(), self.instance["name"]).AndReturn(vm_ref)
        urllib2.urlopen(mox.IgnoreArg()).AndReturn(result)

        self.mox.ReplayAll()
        self.conn.get_console_output(self.instance)
Ejemplo n.º 2
0
    def detach_volume(self, connection_info, instance, mountpoint):
        """Detach volume storage to VM instance."""
        instance_name = instance['name']
        vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name)
        if vm_ref is None:
            raise exception.InstanceNotFound(instance_id=instance_name)
        # Detach Volume from VM
        LOG.debug(_("Detach_volume: %(instance_name)s, %(mountpoint)s")
                    % locals())
        driver_type = connection_info['driver_volume_type']
        if driver_type not in ['iscsi']:
            raise exception.VolumeDriverNotFound(driver_type=driver_type)
        data = connection_info['data']

        # Discover iSCSI Target
        device_name, uuid = volume_util.find_st(self._session, data,
                                                self._cluster)
        if device_name is None:
            raise volume_util.StorageError(_("Unable to find iSCSI Target"))

        # Get the vmdk file name that the VM is pointing to
        hardware_devices = self._session._call_method(vim_util,
                        "get_dynamic_property", vm_ref,
                        "VirtualMachine", "config.hardware.device")
        device = vm_util.get_rdm_disk(hardware_devices, uuid)
        if device is None:
            raise volume_util.StorageError(_("Unable to find volume"))
        self.detach_disk_from_vm(vm_ref, instance_name, device)
        LOG.info(_("Mountpoint %(mountpoint)s detached from "
                   "instance %(instance_name)s") % locals())
Ejemplo n.º 3
0
    def test_get_host_name_for_vm(self):

        fake_vm = fake.ManagedObject(
            "VirtualMachine", fake.ManagedObjectReference(
                "vm-123", "VirtualMachine"))
        fake_vm.propSet.append(
            fake.Property('name', 'vm-123'))
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake_vm)

        vm_ref = vm_util.get_vm_ref_from_name(
                fake_session(fake_objects), 'vm-123')

        self.assertIsNotNone(vm_ref)

        fake_results = [
            fake.ObjectContent(
                None, [
                    fake.Property('runtime.host',
                              fake.ManagedObjectReference(
                                'host-123', 'HostSystem'))
                ])]

        fake_objects = fake.FakeRetrieveResult()
        for results in fake_results:
            fake_objects.add_object(results)

        host_id = vm_util.get_host_id_from_vm_ref(
            fake_session(fake_objects), vm_ref)

        self.assertEqual('host-123', host_id)
Ejemplo n.º 4
0
    def test_get_host_name_for_vm(self, _get_ref_from_uuid):
        fake_host = fake.HostSystem()
        fake_host_id = fake_host.obj.value
        fake_vm = fake.VirtualMachine(name="vm-123", runtime_host=fake_host.obj)
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake_vm)

        vm_ref = vm_util.get_vm_ref_from_name(fake_session(fake_objects), "vm-123")

        self.assertIsNotNone(vm_ref)

        host_id = vm_util.get_host_id_from_vm_ref(fake_session(fake_objects), vm_ref)

        self.assertEqual(fake_host_id, host_id)
Ejemplo n.º 5
0
    def attach_volume(self, connection_info, instance, mountpoint):
        """Attach volume storage to VM instance."""
        instance_name = instance["name"]
        vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name)
        if vm_ref is None:
            raise exception.InstanceNotFound(instance_id=instance_name)
        # Attach Volume to VM
        LOG.debug(
            _("Attach_volume: %(connection_info)s, %(instance_name)s, " "%(mountpoint)s"),
            {"connection_info": connection_info, "instance_name": instance_name, "mountpoint": mountpoint},
        )
        driver_type = connection_info["driver_volume_type"]
        if driver_type not in ["iscsi"]:
            raise exception.VolumeDriverNotFound(driver_type=driver_type)
        data = connection_info["data"]
        mount_unit = volume_util.mountpoint_to_number(mountpoint)

        # Discover iSCSI Target
        device_name, uuid = self.discover_st(data)
        if device_name is None:
            raise volume_util.StorageError(_("Unable to find iSCSI Target"))

        # Get the vmdk file name that the VM is pointing to
        hardware_devices = self._session._call_method(
            vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device"
        )
        vmdk_file_path, controller_key, adapter_type, disk_type, unit_number = vm_util.get_vmdk_path_and_adapter_type(
            hardware_devices
        )
        # Figure out the correct unit number
        if unit_number < mount_unit:
            unit_number = mount_unit
        else:
            unit_number = unit_number + 1
        self.attach_disk_to_vm(
            vm_ref,
            instance_name,
            adapter_type,
            disk_type="rdmp",
            controller_key=controller_key,
            unit_number=unit_number,
            device_name=device_name,
        )
        LOG.info(
            _("Mountpoint %(mountpoint)s attached to " "instance %(instance_name)s"),
            {"mountpoint": mountpoint, "instance_name": instance_name},
        )
Ejemplo n.º 6
0
    def test_get_host_name_for_vm(self, _get_ref_from_uuid):
        fake_host = fake.HostSystem()
        fake_host_id = fake_host.obj.value
        fake_vm = fake.VirtualMachine(name='vm-123',
                                      runtime_host=fake_host.obj)
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake_vm)

        vm_ref = vm_util.get_vm_ref_from_name(fake_session(fake_objects),
                                              'vm-123')

        self.assertIsNotNone(vm_ref)

        host_id = vm_util.get_host_id_from_vm_ref(fake_session(fake_objects),
                                                  vm_ref)

        self.assertEqual(fake_host_id, host_id)
Ejemplo n.º 7
0
    def attach_volume(self, connection_info, instance, mountpoint):
        """Attach volume storage to VM instance."""
        instance_name = instance['name']
        vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name)
        if vm_ref is None:
            raise exception.InstanceNotFound(instance_id=instance_name)
        # Attach Volume to VM
        LOG.debug(
            _("Attach_volume: %(connection_info)s, %(instance_name)s, "
              "%(mountpoint)s") % locals())
        driver_type = connection_info['driver_volume_type']
        if driver_type not in ['iscsi']:
            raise exception.VolumeDriverNotFound(driver_type=driver_type)
        data = connection_info['data']
        mount_unit = volume_util.mountpoint_to_number(mountpoint)

        # Discover iSCSI Target
        device_name, uuid = self.discover_st(data)
        if device_name is None:
            raise volume_util.StorageError(_("Unable to find iSCSI Target"))

        # Get the vmdk file name that the VM is pointing to
        hardware_devices = self._session._call_method(
            vim_util, "get_dynamic_property", vm_ref, "VirtualMachine",
            "config.hardware.device")
        vmdk_file_path, controller_key, adapter_type, disk_type, unit_number \
            = vm_util.get_vmdk_path_and_adapter_type(hardware_devices)
        # Figure out the correct unit number
        if unit_number < mount_unit:
            unit_number = mount_unit
        else:
            unit_number = unit_number + 1
        self.attach_disk_to_vm(vm_ref,
                               instance_name,
                               adapter_type,
                               disk_type="rdmp",
                               controller_key=controller_key,
                               unit_number=unit_number,
                               device_name=device_name)
        LOG.info(
            _("Mountpoint %(mountpoint)s attached to "
              "instance %(instance_name)s") % locals())
Ejemplo n.º 8
0
    def detach_volume(self, connection_info, instance, mountpoint):
        """Detach volume storage to VM instance."""
        instance_name = instance['name']
        vm_ref = vm_util.get_vm_ref_from_name(self._session, instance_name)
        if vm_ref is None:
            raise exception.InstanceNotFound(instance_id=instance_name)
        # Detach Volume from VM
        LOG.debug(_("Detach_volume: %(instance_name)s, %(mountpoint)s"), {
            'mountpoint': mountpoint,
            'instance_name': instance_name
        })
        driver_type = connection_info['driver_volume_type']
        if driver_type not in ['iscsi']:
            raise exception.VolumeDriverNotFound(driver_type=driver_type)
        data = connection_info['data']

        # Discover iSCSI Target
        device_name, uuid = volume_util.find_st(self._session, data,
                                                self._cluster)
        if device_name is None:
            raise volume_util.StorageError(_("Unable to find iSCSI Target"))

        # Get the vmdk file name that the VM is pointing to
        hardware_devices = self._session._call_method(
            vim_util, "get_dynamic_property", vm_ref, "VirtualMachine",
            "config.hardware.device")
        device = vm_util.get_rdm_disk(hardware_devices, uuid)
        if device is None:
            raise volume_util.StorageError(_("Unable to find volume"))
        self.detach_disk_from_vm(vm_ref, instance_name, device)
        LOG.info(
            _("Mountpoint %(mountpoint)s detached from "
              "instance %(instance_name)s"), {
                  'mountpoint': mountpoint,
                  'instance_name': instance_name
              })
Ejemplo n.º 9
0
    def test_get_host_name_for_vm(self):

        fake_vm = fake.ManagedObject(
            "VirtualMachine",
            fake.ManagedObjectReference("vm-123", "VirtualMachine"))
        fake_vm.propSet.append(fake.Property('name', 'vm-123'))

        vm_ref = vm_util.get_vm_ref_from_name(fake_session([fake_vm]),
                                              'vm-123')

        self.assertIsNotNone(vm_ref)

        fake_results = [
            fake.ObjectContent(None, [
                fake.Property(
                    'runtime.host',
                    fake.ManagedObjectReference('host-123', 'HostSystem'))
            ])
        ]

        host_id = vm_util.get_host_id_from_vm_ref(fake_session(fake_results),
                                                  vm_ref)

        self.assertEqual('host-123', host_id)
Ejemplo n.º 10
0
session = driver.VMwareAPISession(host_ip='172.18.211.201',
                                  host_port=443,
                                  username='******',
                                  password='******',
                                  retry_count=10,
                                  scheme="https",
                                  cacert=None,
                                  insecure=True)

client_factory = session.vim.client.factory
# target = client_factory.create('ns0:HostSystem')
# ----------------------------------------------------------------------------------------
#   migrate vm
# ----------------------------------------------------------------------------------------
vm_ref = vm_util.get_vm_ref_from_name(session, 'shengping-test')
host_refa = session._call_method(vim_util, "get_object_property", vm_ref,
                                 "runtime.host")
target = copy.deepcopy(host_refa)
target.value = 'host-13'
pdb.set_trace()
pr = client_factory.create('ns0:VirtualMachineMovePriority')
pr = pr.defaultPriority
result = session._call_method(session.vim,
                              "MigrateVM_Task",
                              vm_ref,
                              host=target,
                              priority=pr)
# result = session._call_method(session, "MigrateVM_Task", vm_ref, host=target)
# host_refb = vm_util.get_host_ref(session)
# ----------------------------------------------------------------------------------------
session = driver.VMwareAPISession(
    host_ip='172.28.8.247',
    host_port=443,
    username='******',
    password='******',
    retry_count=10,
    scheme="https",
    cacert=None,
    insecure=True
)

client_factory = session.vim.client.factory

# vm_ref = vm_util.get_vm_ref(session, instance)
vm_ref = vm_util.get_vm_ref_from_name(session, 'test-vm')

# get manager object from content
content = session.vim.retrieve_service_content()
# manager object for guest operation
gopmgr = content.guestOperationsManager

# crate auth
# auth = client_factory.create('ns0:GuestAuthentication')
# managed object follows ploymorphism
auth = client_factory.create('ns0:NamePasswordAuthentication')
auth.interactiveSession = False
auth.username = '******'
auth.password = '******'

# ---------------- create temp file for scirpt ---------------------#