-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmutils.py
91 lines (76 loc) · 2.57 KB
/
vmutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from pyVmomi import vim
import time
def _get_obj(content, vimtype, name):
"""
Get the vsphere object associated with a given text name
"""
obj = None
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj
def _get_all_objs(content, vimtype):
"""
Get all the vsphere objects associated with a given type
"""
obj = {}
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
obj.update({c: c.name})
return obj
def login_in_guest(username, password):
return vim.vm.guest.NamePasswordAuthentication(username=username,password=password)
def start_process(si, vm, auth, program_path, args=None, env=None, cwd=None):
cmdspec = vim.vm.guest.ProcessManager.ProgramSpec(arguments=args, programPath=program_path, envVariables=env, workingDirectory=cwd)
cmdpid = si.content.guestOperationsManager.processManager.StartProgramInGuest(vm=vm, auth=auth, spec=cmdspec)
return cmdpid
def is_ready(vm):
while True:
system_ready = vm.guest.guestOperationsReady
system_state = vm.guest.guestState
system_uptime = vm.summary.quickStats.uptimeSeconds
if system_ready and system_state == 'running' and system_uptime > 90:
break
time.sleep(10)
def get_vm_by_name(si, name):
"""
Find a virtual machine by it's name and return it
"""
return _get_obj(si.RetrieveContent(), [vim.VirtualMachine], name)
def get_host_by_name(si, name):
"""
Find a virtual machine by it's name and return it
"""
return _get_obj(si.RetrieveContent(), [vim.HostSystem], name)
def get_resource_pool(si, name):
"""
Find a virtual machine by it's name and return it
"""
return _get_obj(si.RetrieveContent(), [vim.ResourcePool], name)
def get_resource_pools(si):
"""
Returns all resource pools
"""
return _get_all_objs(si.RetrieveContent(), [vim.ResourcePool])
def get_datastores(si):
"""
Returns all datastores
"""
return _get_all_objs(si.RetrieveContent(), [vim.Datastore])
def get_hosts(si):
"""
Returns all hosts
"""
return _get_all_objs(si.RetrieveContent(), [vim.HostSystem])
def get_datacenters(si):
"""
Returns all datacenters
"""
return _get_all_objs(si.RetrieveContent(), [vim.Datacenter])
def get_registered_vms(si):
"""
Returns all vms
"""
return _get_all_objs(si.RetrieveContent(), [vim.VirtualMachine])