This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
060getvmhost.py
145 lines (110 loc) · 4.16 KB
/
060getvmhost.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import atexit
import argparse
import getpass
import sys
import textwrap
import time
from pyVim import connect
from pyVmomi import vim
import requests
requests.packages.urllib3.disable_warnings()
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
def get_args():
parser = argparse.ArgumentParser()
# because -h is reserved for 'help' we use -s for service
parser.add_argument('-s', '--host',
required=True,
action='store',
help='vSphere service to connect to')
# because we want -p for password, we use -o for port
parser.add_argument('-o', '--port',
type=int,
default=443,
action='store',
help='Port to connect on')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='User name to use when connecting to host')
parser.add_argument('-p', '--password',
required=False,
action='store',
help='Password to use when connecting to host')
parser.add_argument('-n', '--name',
required=True,
action='store',
help='Name of the virtual_machine to look for.')
args = parser.parse_args()
if not args.password:
args.password = getpass.getpass(
prompt='Enter password for host %s and user %s: ' %
(args.host, args.user))
return args
def _create_char_spinner():
"""Creates a generator yielding a char based spinner.
"""
while True:
for c in '|/-\\':
yield c
_spinner = _create_char_spinner()
def spinner(label=''):
"""Prints label with a spinner.
When called repeatedly from inside a loop this prints
a one line CLI spinner.
"""
sys.stdout.write("\r\t%s %s" % (label, _spinner.next()))
sys.stdout.flush()
def answer_vm_question(virtual_machine):
print "\n"
choices = virtual_machine.runtime.question.choice.choiceInfo
default_option = None
if virtual_machine.runtime.question.choice.defaultIndex is not None:
ii = virtual_machine.runtime.question.choice.defaultIndex
default_option = choices[ii]
choice = None
while choice not in [o.key for o in choices]:
print "VM power on is paused by this question:\n\n"
print "\n".join(textwrap.wrap(
virtual_machine.runtime.question.text, 60))
for option in choices:
print "\t %s: %s " % (option.key, option.label)
if default_option is not None:
print "default (%s): %s\n" % (default_option.label,
default_option.key)
choice = raw_input("\nchoice number: ").strip()
print "..."
return choice
# form a connection...
args = get_args()
si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password,
port=args.port)
# doing this means you don't need to remember to disconnect your script/objects
atexit.register(connect.Disconnect, si)
# search the whole inventory tree recursively... a brutish but effective tactic
vm = None
entity_stack = si.content.rootFolder.childEntity
while entity_stack:
entity = entity_stack.pop()
if entity.name == args.name:
vm = entity
del entity_stack[0:len(entity_stack)]
elif hasattr(entity, 'childEntity'):
entity_stack.extend(entity.childEntity)
elif isinstance(entity, vim.Datacenter):
entity_stack.append(entity.vmFolder)
if not isinstance(vm, vim.VirtualMachine):
print "could not find a virtual machine with the name %s" % args.name
sys.exit(-1)
#print "powering on VM %s" % vm.name
print vm.runtime.host.name
sys.exit(0)