本文整理汇总了Python中host.hypervisor.esx.vim_client.VimClient.get_vm方法的典型用法代码示例。如果您正苦于以下问题:Python VimClient.get_vm方法的具体用法?Python VimClient.get_vm怎么用?Python VimClient.get_vm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类host.hypervisor.esx.vim_client.VimClient
的用法示例。
在下文中一共展示了VimClient.get_vm方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: vim_delete_vm
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vm [as 别名]
def vim_delete_vm(self, vm_id):
""" Delete a VM using the vim client """
try:
vim_client = VimClient()
vim_client.connect_ticket(self.server, self._get_vim_ticket())
vim_vm = vim_client.get_vm(vm_id)
if vim_vm.runtime.powerState != 'poweredOff':
try:
vim_task = vim_vm.PowerOff()
vim_client.wait_for_task(vim_task)
except:
logger.info("Cannot power off vm", exc_info=True)
vim_task = vim_vm.Destroy()
vim_client.wait_for_task(vim_task)
finally:
if vim_client:
vim_client.disconnect()
示例2: TestVimClient
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vm [as 别名]
class TestVimClient(unittest.TestCase):
def setUp(self):
if "host_remote_test" not in config:
raise SkipTest()
self.host = config["host_remote_test"]["server"]
self.pwd = config["host_remote_test"]["esx_pwd"]
if self.host is None or self.pwd is None:
raise SkipTest()
self.vim_client = VimClient(self.host, "root", self.pwd,
auto_sync=True)
self.vm_config = EsxVmConfig(self.vim_client)
self._logger = logging.getLogger(__name__)
def tearDown(self):
self.vim_client.disconnect(wait=True)
def test_memory_usage(self):
used_memory = self.vim_client.memory_usage_mb
assert_that(used_memory > 0, is_(True))
def test_total_memory(self):
total_memory = self.vim_client.total_vmusable_memory_mb
assert_that(total_memory > 0, is_(True))
def test_total_cpus(self):
num_cpus = self.vim_client.num_physical_cpus
assert_that(num_cpus > 0, is_(True))
def _create_test_vm(self, suffix="host-integ"):
# Create VM
vm_id = "vm_%s-%s-%s" % (
time.strftime("%Y-%m-%d-%H%M%S", time.localtime()),
str(random.randint(100000, 1000000)),
suffix)
datastore = self.vim_client.get_datastore().name
disk_path = "[%s] %s/disk.vmdk" % (datastore, vm_id)
create_spec = self.get_create_spec(datastore, vm_id, disk_path)
folder = self.vim_client.vm_folder
resource_pool = self.vim_client.root_resource_pool
task = folder.CreateVm(create_spec, resource_pool, None)
self.vim_client.wait_for_task(task)
vm = self.vim_client.get_vm(vm_id)
return (vm_id, vm, datastore, disk_path)
def test_get_cached_vm(self):
vm_id, vm, datastore, disk_path = self._create_test_vm("vm-cache-test")
# Verify VM is in cache
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms), is_(1))
assert_that(found_vms[0].name, is_(vm_id))
assert_that(found_vms[0].power_state, is_(PowerState.poweredOff))
assert_that(found_vms[0].memory_mb, is_(64))
assert_that(found_vms[0].path, starts_with("[%s]" % datastore))
assert_that(len(found_vms[0].disks), is_(1))
assert_that(found_vms[0].disks[0], is_(disk_path))
# Make sure get_vm_in_cache works
vm_from_cache = self.vim_client.get_vm_in_cache(vm_id)
assert_that(vm_from_cache.name, is_(vm_id))
self.assertRaises(VmNotFoundException,
self.vim_client.get_vm_in_cache, "missing")
# Add disk
disk2_path = "[%s] %s/disk2.vmdk" % (datastore, vm_id)
update_spec = self.get_update_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(update_spec)
self.vim_client.wait_for_task(task)
# For the ReconfigVM task to remove disk, the hostd could update
# task status to success before updating VM status. Thus when
# wait_for_task returns, the vm_cache is possible to be still in old
# state, though eventually it converges to consistent state. It only
# happens in this task AFAIK. It should be fine for this task, because
# rarely there is other operation that depends on this task.
self._wait_vm_has_disk(vm_id, 2)
# Verify disk added
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms[0].disks), is_(2))
assert_that(found_vms[0].disks,
contains_inanyorder(disk_path, disk2_path))
# Remove disk
vm = self.vim_client.get_vm(vm_id)
remove_spec = self.get_remove_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(remove_spec)
self.vim_client.wait_for_task(task)
# Same as before when disk is added
self._wait_vm_has_disk(vm_id, 1)
# Verify disk removed
vms = self.vim_client.get_vms_in_cache()
#.........这里部分代码省略.........
示例3: TestRemoteAgent
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vm [as 别名]
#.........这里部分代码省略.........
# Connect to server and configure vim_client
self.client_connections()
self.vim_client = VimClient()
self.vim_client.connect_ticket(self.server, self._get_vim_ticket())
connect.SetSi(self.vim_client._si)
# Set host mode to normal
self.set_host_mode(HostMode.NORMAL)
# The first time setup is called the agent will restart.
self.provision_hosts()
# Reconnect to account for the restart
self.client_connections()
self.clear()
@classmethod
def setUpClass(cls):
cls.host_id = str(uuid.uuid4())
cls.deployment_id = "test-deployment"
def _close_agent_connections(self):
self.host_client.close()
self.control_client.close()
def tearDown(self):
self._close_agent_connections()
self.vim_client.disconnect()
def vim_delete_vm(self, vm_id):
""" Delete a VM using the vim client """
try:
vim_client = VimClient()
vim_client.connect_ticket(self.server, self._get_vim_ticket())
vim_vm = vim_client.get_vm(vm_id)
if vim_vm.runtime.powerState != 'poweredOff':
try:
vim_task = vim_vm.PowerOff()
vim_client.wait_for_task(vim_task)
except:
logger.info("Cannot power off vm", exc_info=True)
vim_task = vim_vm.Destroy()
vim_client.wait_for_task(vim_task)
finally:
if vim_client:
vim_client.disconnect()
def clear(self):
"""Remove all the VMs, disks and images """
request = GetResourcesRequest()
response = rpc_call(self.host_client.get_resources, request)
assert_that(response.result, is_(GetResourcesResultCode.OK))
for resource in response.resources:
delete_request = Host.DeleteVmRequest(vm_id=resource.vm.id, force=True)
response = rpc_call(self.host_client.delete_vm, delete_request)
if response.result == DeleteVmResultCode.VM_NOT_POWERED_OFF:
poweroff_request = Host.PowerVmOpRequest(vm_id=resource.vm.id,
op=Host.PowerVmOp.OFF)
response = rpc_call(self.host_client.power_vm_op,
poweroff_request)
assert_that(response.result, is_(PowerVmOpResultCode.OK))
response = rpc_call(self.host_client.delete_vm, delete_request)
if response.result != DeleteVmResultCode.OK:
logger.info("Cannot delete vm %s trying vim_client" % resource.vm.id)
self.vim_delete_vm(resource.vm.id)
示例4: TestHttpTransfer
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vm [as 别名]
#.........这里部分代码省略.........
raise SkipTest()
self.host = config["host_remote_test"]["server"]
self.pwd = config["host_remote_test"]["esx_pwd"]
self.agent_port = config["host_remote_test"].get("agent_port", 8835)
if self.host is None or self.pwd is None:
raise SkipTest()
self.image_datastore = config["host_remote_test"].get("image_datastore", "datastore1")
self._logger = logging.getLogger(__name__)
self.vim_client = VimClient(self.host, "root", self.pwd)
self.http_transferer = HttpNfcTransferer(self.vim_client, self.image_datastore, self.host)
with tempfile.NamedTemporaryFile(delete=False) as source_file:
with open(source_file.name, "wb") as f:
f.write(os.urandom(1024 * 100))
self.random_file = source_file.name
self.remote_files_to_delete = []
def _cleanup_remote_files(self):
file_manager = self.vim_client._content.fileManager
for ds_path in self.remote_files_to_delete:
try:
delete_task = file_manager.DeleteFile(ds_path, None)
task.WaitForTask(delete_task)
except:
pass
def tearDown(self):
os.unlink(self.random_file)
self._cleanup_remote_files()
self.vim_client.disconnect(wait=True)
def _remote_ds_path(self, ds, relpath):
return "[%s] %s" % (ds, relpath)
def _datastore_path_url(self, datastore, relpath):
quoted_dc_name = "ha%252ddatacenter"
url = "https://%s/folder/%s?dcPath=%s&dsName=%s" % (self.host, relpath, quoted_dc_name, datastore)
return url
def test_download_missing_file(self):
url = self._datastore_path_url(self.image_datastore, "_missing_file_.bin")
ticket = self.http_transferer._get_cgi_ticket(self.host, self.agent_port, url, http_op=HttpOp.GET)
with tempfile.NamedTemporaryFile(delete=True) as local_file:
self.assertRaises(
TransferException, self.http_transferer.download_file, url, local_file.name, ticket=ticket
)
def test_upload_file_bad_destination(self):
url = self._datastore_path_url("_missing__datastore_", "random.bin")
ticket = self.http_transferer._get_cgi_ticket(self.host, self.agent_port, url, http_op=HttpOp.PUT)
self.assertRaises(TransferException, self.http_transferer.upload_file, self.random_file, url, ticket=ticket)
def test_raw_file_transfer_roundtrip(self):
relpath = "_test_http_xfer_random.bin"
url = self._datastore_path_url(self.image_datastore, relpath)
ticket = self.http_transferer._get_cgi_ticket(self.host, self.agent_port, url, http_op=HttpOp.PUT)
self.http_transferer.upload_file(self.random_file, url, ticket=ticket)
self.remote_files_to_delete.append(self._remote_ds_path(self.image_datastore, relpath))
ticket = self.http_transferer._get_cgi_ticket(self.host, self.agent_port, url, http_op=HttpOp.GET)
with tempfile.NamedTemporaryFile(delete=True) as downloaded_file:
self.http_transferer.download_file(url, downloaded_file.name, ticket=ticket)
# check that file uploaded and immediately downloaded back is
# identical to the source file used.
assert_that(filecmp.cmp(self.random_file, downloaded_file.name, shallow=False), is_(True))
@patch("os.path.exists", return_value=True)
def test_get_streamoptimized_image_stream(self, _exists):
image_id = "ttylinux"
lease, url = self.http_transferer._get_image_stream_from_shadow_vm(image_id)
try:
with tempfile.NamedTemporaryFile(delete=True) as downloaded_file:
# see if we can download without errors
self.http_transferer.download_file(url, downloaded_file.name)
# check that the first part of the file looks like that from a
# stream-optimized disk
with open(downloaded_file.name, "rb") as f:
data = f.read(65536)
assert_that(len(data), is_(65536))
regex = re.compile("streamOptimized", re.IGNORECASE | re.MULTILINE)
matches = regex.findall(data)
assert_that(matches, not (empty()))
finally:
lease.Complete()
def test_send_image_to_host(self):
image_id = "ttylinux"
tmp_vmdk_path = "/tmp/test_send_image_%s.vmdk" % str(uuid.uuid4())
self.http_transferer.send_image_to_host(
image_id, self.image_datastore, self.host, self.agent_port, intermediate_file_path=tmp_vmdk_path
)
vim_vm = self.vim_client.get_vm(image_id)
vim_task = vim_vm.Destroy()
self.vim_client.wait_for_task(vim_task)
示例5: TestVimClient
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vm [as 别名]
class TestVimClient(unittest.TestCase):
def setUp(self):
if "host_remote_test" not in config:
raise SkipTest()
self.host = config["host_remote_test"]["server"]
self.pwd = config["host_remote_test"]["esx_pwd"]
if self.host is None or self.pwd is None:
raise SkipTest()
self.vim_client = VimClient(auto_sync=True)
self.vim_client.connect_userpwd(self.host, "root", self.pwd)
self._logger = logging.getLogger(__name__)
def tearDown(self):
self.vim_client.disconnect()
def test_memory_usage(self):
used_memory = self.vim_client.memory_usage_mb
assert_that(used_memory > 0, is_(True))
def test_total_memory(self):
total_memory = self.vim_client.total_vmusable_memory_mb
assert_that(total_memory > 0, is_(True))
def test_total_cpus(self):
num_cpus = self.vim_client.num_physical_cpus
assert_that(num_cpus > 0, is_(True))
def _create_test_vm(self, suffix="host-integ"):
# Create VM
vm_id = "vm_%s-%s-%s" % (
time.strftime("%Y-%m-%d-%H%M%S", time.localtime()),
str(random.randint(100000, 1000000)),
suffix)
datastore = self.vim_client.get_all_datastores()[0].name
disk_path = "[%s] %s/disk.vmdk" % (datastore, vm_id)
create_spec = self.get_create_spec(datastore, vm_id, disk_path)
self.vim_client.create_vm(vm_id, create_spec)
vm = self.vim_client.get_vm(vm_id)
return (vm_id, vm, datastore, disk_path)
def test_get_cached_vm(self):
vm_id, vm, datastore, disk_path = self._create_test_vm("vm-cache-test")
# Verify VM is in cache
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms), is_(1))
assert_that(found_vms[0].name, is_(vm_id))
assert_that(found_vms[0].power_state, is_(VmPowerState.STOPPED))
assert_that(found_vms[0].memory_mb, is_(64))
assert_that(found_vms[0].path, starts_with("[%s]" % datastore))
assert_that(len(found_vms[0].disks), is_(1))
assert_that(found_vms[0].disks[0], is_(disk_path))
# Make sure get_vm_in_cache works
vm_from_cache = self.vim_client.get_vm_in_cache(vm_id)
assert_that(vm_from_cache.name, is_(vm_id))
self.assertRaises(VmNotFoundException,
self.vim_client.get_vm_in_cache, "missing")
# Add disk
disk2_path = "[%s] %s/disk2.vmdk" % (datastore, vm_id)
update_spec = self.get_update_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(update_spec.get_spec())
self.vim_client.wait_for_task(task)
# For the ReconfigVM task to remove disk, the hostd could update
# task status to success before updating VM status. Thus when
# wait_for_task returns, the vm_cache is possible to be still in old
# state, though eventually it converges to consistent state. It only
# happens in this task AFAIK. It should be fine for this task, because
# rarely there is other operation that depends on this task.
self._wait_vm_has_disk(vm_id, 2)
# Verify disk added
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms[0].disks), is_(2))
assert_that(found_vms[0].disks,
contains_inanyorder(disk_path, disk2_path))
# Remove disk
vm = self.vim_client.get_vm(vm_id)
remove_spec = self.get_remove_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(remove_spec.get_spec())
self.vim_client.wait_for_task(task)
# Same as before when disk is added
self._wait_vm_has_disk(vm_id, 1)
# Verify disk removed
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms), is_(1))
assert_that(len(found_vms[0].disks), is_(1), "disk2 in " +
str(found_vms[0].disks))
#.........这里部分代码省略.........