-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathhyperv.py
More file actions
673 lines (582 loc) · 27.1 KB
/
Copy pathhyperv.py
File metadata and controls
673 lines (582 loc) · 27.1 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# -*- coding: utf-8 -*-
from __future__ import print_function, absolute_import
"""
Module for communcating with Hyper-V, part of virt-who
Copyright (C) 2014 Radek Novacek <rnovacek@redhat.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import urllib
import base64
from xml.etree import ElementTree
from requests.auth import AuthBase
import requests
import gssapi
from requests_gssapi import HTTPSPNEGOAuth, OPTIONAL
from requests_gssapi.exceptions import SPNEGOExchangeError, MutualAuthenticationError
from virtwho import virt
from virtwho.config import VirtConfigSection, accessible_file
try:
from uuid import uuid1
except ImportError:
import subprocess
def uuid1():
# fallback to calling commandline uuidgen
return subprocess.Popen(["uuidgen"], stdout=subprocess.PIPE).communicate()[0].strip()
class HypervConfigSection(VirtConfigSection):
"""
This class is used for validation of Hyper-V virtualization backend
section. It tries to validate options and combination of options that
are specific for this virtualization backend.
"""
VIRT_TYPE = 'hyperv'
HYPERVISOR_ID = ('uuid', 'hostname')
AUTH_METHODS = ('basic', 'kerberos')
def __init__(self, section_name, wrapper, *args, **kwargs):
super(HypervConfigSection, self).__init__(section_name, wrapper, *args, **kwargs)
self.add_key('server', validation_method=self._validate_server, required=True)
self.add_key('username', validation_method=self._validate_username, required=True)
self.add_key('password', validation_method=self._validate_unencrypted_password, required=True)
self.add_key('auth_method', validation_method=self._validate_auth_method, default='basic')
self.add_key('kerberos_keytab', validation_method=self._validate_kerberos_keytab)
self.add_key('kerberos_principal', validation_method=self._validate_kerberos_principal)
def _validate_auth_method(self, key):
if key not in self._values:
return None
value = self._values[key]
result = None
if value not in self.AUTH_METHODS:
result = (
'error',
'Invalid auth_method "%s": must be one of: %s' % (value, ', '.join(self.AUTH_METHODS))
)
return result
def _validate_kerberos_keytab(self, key):
if key not in self._values:
return None
value = self._values[key]
result = None
auth_method = self._values.get('auth_method', self.defaults.get('auth_method', 'basic'))
if auth_method != 'kerberos':
result = (
'warning',
'Option "%s" is only applicable when auth_method=kerberos, ignoring' % key
)
del self._values[key]
return result
try:
accessible_file(value)
except ValueError as e:
result = (
'error',
'Invalid kerberos_keytab: %s' % str(e)
)
return result
def _validate_kerberos_principal(self, key):
if key not in self._values:
return None
value = self._values[key]
result = None
auth_method = self._values.get('auth_method', self.defaults.get('auth_method', 'basic'))
if auth_method != 'kerberos':
result = (
'warning',
'Option "%s" is only applicable when auth_method=kerberos, ignoring' % key
)
del self._values[key]
return result
if not isinstance(value, str) or len(value) == 0:
result = (
'error',
'Option "%s" must be a non-empty string' % key
)
return result
def _pre_validate(self):
auth_method = self._values.get('auth_method', self.defaults.get('auth_method', 'basic'))
if auth_method == 'kerberos':
self._required_keys.discard('password')
self._required_keys.discard('username')
super(HypervConfigSection, self)._pre_validate()
def _post_validate(self):
auth_method = self._values.get('auth_method', self.defaults.get('auth_method', 'basic'))
if auth_method == 'kerberos':
if 'username' in self._values and self._values['username']:
self.validation_messages.append((
'warning',
'Username is ignored when auth_method=kerberos'
))
if 'password' in self._values and self._values['password']:
self.validation_messages.append((
'warning',
'Password is ignored when auth_method=kerberos'
))
super(HypervConfigSection, self)._post_validate()
def _validate_server(self, key):
error = super(HypervConfigSection, self)._validate_server(key)
if error is None:
url_altered = False
result = []
url = self._values[key]
if "//" not in url:
url_altered = True
url = "//" + url
parsed = urllib.parse.urlsplit(url, "http")
if ":" not in parsed[1]:
url_altered = True
if parsed[0] == "https":
self.host = parsed[1] + ":5986"
else:
self.host = parsed[1] + ":5985"
else:
self.host = parsed[1]
if parsed[2] == "":
url_altered = True
path = "wsman"
else:
path = parsed[2]
self.url = urllib.parse.urlunsplit((parsed[0], self.host, path, "", ""))
self._values['url'] = self.url
if url_altered:
result.append((
'info',
"The original server URL was incomplete. It has been enhanced to %s" % self.url
))
return result
return error
class HyperVAuth(AuthBase):
def __init__(self, username, password, logger):
self.username = username
self.password = password
self.logger = logger
self.authenticated = False
self.num_401s = 0
self.basic = None
def prepare_resend(self, response):
'''
Consume content and release the original connection
to allow our new request to reuse the same one.
'''
response.content
response.raw.release_conn()
return response.request.copy()
def retry_basic(self, response, **kwargs):
self.logger.debug("Using Basic authentication")
request = self.prepare_resend(response)
passphrase = '%s:%s' % (self.username, self.password)
self.basic = 'Basic %s' % base64.b64encode(bytes(passphrase, 'utf-8')).decode('utf-8')
request.headers['Authorization'] = self.basic
request.headers['Content-Length'] = len(self._body)
request.body = self._body
r = response.connection.send(request, **kwargs)
if r.status_code == requests.codes.ok:
self.authenticated = True
return r
def handle_response(self, response, **kwargs):
if response.status_code == requests.codes.ok and not self.authenticated:
self.authenticated = True
response.request.body = self._body
r = response.connection.send(response.request, **kwargs)
return r
if response.status_code == 401:
authenticate_header = response.headers.get('www-authenticate', '').lower()
if 'basic' in authenticate_header:
return self.retry_basic(response, **kwargs)
else:
raise HyperVAuthFailed(
"Server doesn't known any supported authentication method "
"(server methods: %s)" % authenticate_header)
return response
def __call__(self, request):
request.headers["Connection"] = "Keep-Alive"
if not self.authenticated:
request.headers["Content-Length"] = "0"
self._body = request.body
request.body = None
request.register_hook('response', self.handle_response)
elif self.basic:
request.headers['Authorization'] = self.basic
return request
class HyperVSoapGenerator(object):
def __init__(self, url):
self.url = url
self.virtualization_namespace = 'root/virtualization'
@property
def namespaces(self):
return {
's': 'http://www.w3.org/2003/05/soap-envelope',
'wsa': 'http://schemas.xmlsoap.org/ws/2004/08/addressing',
'wsman': 'http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd',
'wsen': 'http://schemas.xmlsoap.org/ws/2004/09/enumeration',
}
vsms_namespace = 'http://schemas.microsoft.com/wbem/wsman/1/wmi/%(ns)s/Msvm_VirtualSystemManagementService'
si_namespace = 'http://schemas.microsoft.com/wbem/wsman/1/wmi/%(ns)s/Msvm_SummaryInformation'
def envelope(self, header, body):
return """<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope """ + " ".join(('xmlns:%s="%s"' % (k, v) for k, v in self.namespaces.items())) + """>
%(header)s
%(body)s
</s:Envelope>""" % {'header': header, 'body': body}
def getHeader(self, action, action_namespace=None,
additional_headers=None, resourceURI='*',
resource_namespace='root/virtualization'):
if action_namespace is None:
action_namespace = self.namespaces['wsen']
if additional_headers is None:
additional_headers = ""
return """<s:Header>
<wsa:Action s:mustUnderstand="true">%(action_namespace)s/%(action)s</wsa:Action>
<wsa:To s:mustUnderstand="true">%(url)s</wsa:To>
<wsman:ResourceURI s:mustUnderstand="true">
http://schemas.microsoft.com/wbem/wsman/1/wmi/%(resource_namespace)s/%(resourceURI)s
</wsman:ResourceURI>
<wsa:MessageID s:mustUnderstand="true">uuid:%(uuid)s</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
</wsa:ReplyTo>%(additional_headers)s
</s:Header>""" % {
'uuid': str(uuid1()),
'url': self.url,
'action': action,
'action_namespace': action_namespace,
'additional_headers': additional_headers,
'resourceURI': resourceURI,
'resource_namespace': resource_namespace
}
def enumerateXML(self, query, namespace):
body = """<s:Body>
<wsen:Enumerate>
<wsman:Filter Dialect="http://schemas.microsoft.com/wbem/wsman/1/WQL">%(query)s</wsman:Filter>
</wsen:Enumerate>
</s:Body>""" % {'query': query}
return self.envelope(
self.getHeader('Enumerate', resource_namespace=namespace),
body)
def pullXML(self, enumerationContext, namespace):
body = """<s:Body>
<wsen:Pull>
<wsen:EnumerationContext>%(EnumerationContext)s</wsen:EnumerationContext>
</wsen:Pull>
</s:Body>""" % {'EnumerationContext': enumerationContext}
return self.envelope(
self.getHeader("Pull", resource_namespace=namespace),
body)
def getSummaryInformationXML(self, namespace):
body = """<s:Body>
<wsman:GetSummaryInformation_INPUT xmlns:p="%(namespace)s">
<p:RequestedInformation>0</p:RequestedInformation>
<p:RequestedInformation>1</p:RequestedInformation>
<p:RequestedInformation>100</p:RequestedInformation>
</wsman:GetSummaryInformation_INPUT>
</s:Body>""" % {'namespace': (self.vsms_namespace % {'ns': namespace})}
return self.envelope(
self.getHeader("GetSummaryInformation",
action_namespace=self.vsms_namespace % {'ns': namespace},
resourceURI="Msvm_VirtualSystemManagementService",
resource_namespace=namespace,
additional_headers="""
<wsman:SelectorSet>
<wsman:Selector Name="CreationClassName">Msvm_VirtualSystemManagementService</wsman:Selector>
<wsman:Selector Name="SystemCreationClassName">Msvm_ComputerSystem</wsman:Selector>
</wsman:SelectorSet>"""),
body)
ENABLED_STATE_TO_GUEST_STATE = {
'2': virt.Guest.STATE_RUNNING,
'3': virt.Guest.STATE_SHUTOFF,
'4': virt.Guest.STATE_SHUTINGDOWN,
'9': virt.Guest.STATE_PAUSED,
'32768': virt.Guest.STATE_PAUSED,
'32769': virt.Guest.STATE_PMSUSPENDED
}
class HyperVSoap(object):
def __init__(self, url, connection, logger):
self.url = url
self.connection = connection
self.generator = HyperVSoapGenerator(self.url)
self.logger = logger
def post(self, body):
headers = {
"Content-Type": "application/soap+xml;charset=UTF-8"
}
try:
response = self.connection.post(self.url, body, headers=headers)
except (SPNEGOExchangeError, MutualAuthenticationError) as e:
raise HyperVAuthFailed(
"Kerberos authentication failed: %s. "
"Verify that the keytab and principal are correct, "
"and that the KDC is reachable." % str(e)
)
except requests.RequestException as e:
raise HyperVException("Unable to connect to Hyper-V server: %s" % str(e))
if response.status_code == requests.codes.ok:
self.logger.debug(f'Received valid response from Hyper-V server: {response.status_code}')
return response.content
elif response.status_code == 401:
raise HyperVAuthFailed("Authentication failed")
else:
data = response.content
try:
xml_doc = ElementTree.fromstring(data)
errorcode = xml_doc.find('.//{http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/MSFT_WmiError}error_Code')
# Suppress reporting of invalid namespace, because we're testing
# both old and new namespaces that HyperV uses
if errorcode is None or errorcode.text != '2150858778':
title = xml_doc.find('.//title')
if title is not None:
self.logger.debug(
f"Invalid response ({response.status_code}) from Hyper-V: {title.text}"
)
else:
self.logger.debug(
f"Invalid response ({response.status_code}) from Hyper-V"
)
except Exception as err:
self.logger.debug(
f"Invalid response ({response.status_code}) from Hyper-V (error: {err})"
)
raise HyperVCallFailed("Communication with Hyper-V failed, HTTP error: %d" % response.status_code)
@classmethod
def _Instance(cls, xml_doc):
def stripNamespace(tag):
return tag[tag.find("}") + 1:]
if len(xml_doc) < 1:
return None
child = xml_doc[0]
properties = {}
for ch in child:
properties[stripNamespace(ch.tag)] = ch.text
return properties
def Enumerate(self, query, namespace="root/virtualization"):
data = self.generator.enumerateXML(query=query, namespace=namespace)
body = self.post(data)
xml_doc = ElementTree.fromstring(body)
if xml_doc.tag != "{%(s)s}Envelope" % self.generator.namespaces:
raise HyperVException("Wrong reply format")
responses = xml_doc.findall("{%(s)s}Body/{%(wsen)s}EnumerateResponse" % self.generator.namespaces)
if len(responses) < 1:
raise HyperVException("Wrong reply format")
contexts = responses[0]
if len(contexts) < 1:
raise HyperVException("Wrong reply format")
if contexts[0].tag != "{%(wsen)s}EnumerationContext" % self.generator.namespaces:
raise HyperVException("Wrong reply format")
return contexts[0].text
def _PullOne(self, uuid, namespace):
data = self.generator.pullXML(enumerationContext=uuid, namespace=namespace)
body = self.post(data)
xml_doc = ElementTree.fromstring(body)
if xml_doc.tag != "{%(s)s}Envelope" % self.generator.namespaces:
raise HyperVException("Wrong reply format")
responses = xml_doc.findall("{%(s)s}Body/{%(wsen)s}PullResponse" % self.generator.namespaces)
if len(responses) < 0:
raise HyperVException("Wrong reply format")
uuid = None
instance = None
for node in responses[0]:
if node.tag == "{%(wsen)s}EnumerationContext" % self.generator.namespaces:
uuid = node.text
elif node.tag == "{%(wsen)s}Items" % self.generator.namespaces:
instance = HyperVSoap._Instance(node)
return uuid, instance
def Pull(self, uuid, namespace="root/virtualization"):
instances = []
while uuid is not None:
uuid, instance = self._PullOne(uuid, namespace)
if instance is not None:
instances.append(instance)
return instances
def Invoke_GetSummaryInformation(self, namespace):
'''
Get states of all virtual machines present on the system and
return dict where `ElementName` is key and `virt.GUEST.STATE_*` is value.
'''
data = self.generator.getSummaryInformationXML(namespace)
body = self.post(data)
xml_doc = ElementTree.fromstring(body)
if xml_doc.tag != "{%(s)s}Envelope" % self.generator.namespaces:
raise HyperVException("Wrong reply format")
responses = xml_doc.findall("{%(s)s}Body/{%(vsms)s}GetSummaryInformation_OUTPUT" % {
's': self.generator.namespaces['s'],
'vsms': self.generator.vsms_namespace % {'ns': namespace}
})
if len(responses) < 0:
raise HyperVException("Wrong reply format")
info = {}
si_namespace = self.generator.si_namespace % {'ns': namespace}
for node in responses[0]:
if 'SummaryInformation' in node.tag:
name = node.find("{%(si)s}Name" % {'si': si_namespace}).text
enabledState = node.find("{%(si)s}EnabledState" % {'si': si_namespace}).text
info[name] = ENABLED_STATE_TO_GUEST_STATE.get(enabledState, virt.Guest.STATE_UNKNOWN)
return info
class HyperVException(virt.VirtError):
pass
class HyperVAuthFailed(HyperVException):
pass
class HyperVCallFailed(HyperVException):
pass
class HyperV(virt.Virt):
CONFIG_TYPE = "hyperv"
def __init__(self, logger, config, dest, terminate_event=None,
interval=None, oneshot=False, status=False):
super(HyperV, self).__init__(logger, config, dest,
terminate_event=terminate_event,
interval=interval,
oneshot=oneshot,
status=status)
self.url = self.config['url']
self.auth_method = self.config.get('auth_method', 'basic')
self.username = self.config.get('username', '')
self.password = self.config.get('password', '')
self.kerberos_keytab = self.config.get('kerberos_keytab', None)
self.kerberos_principal = self.config.get('kerberos_principal', None)
# First try to use old API (root/virtualization namespace) if doesn't
# work, go with root/virtualization/v2
self.useNewApi = False
def connect(self):
self.logger.debug('Trying to connect to Hyper-V')
s = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
s.mount('http://', adapter)
if self.auth_method == 'kerberos':
self.logger.debug('Using Kerberos (SPNEGO/Negotiate) authentication')
name = None
store = None
if self.kerberos_keytab:
store = {"client_keytab": f"FILE:{self.kerberos_keytab}"}
self.logger.debug('Using Kerberos keytab: %s', self.kerberos_keytab)
if self.kerberos_principal:
name = gssapi.Name(self.kerberos_principal, name_type=gssapi.NameType.kerberos_principal)
self.logger.debug('Using Kerberos principal: %s', self.kerberos_principal)
try:
# If 'name' is omitted, python-gssapi pulls the default principal from the 'store' (keytab)
# or the default credential cache. If 'store' is omitted, it looks in the default cache.
creds = gssapi.Credentials(name=name, store=store, usage="initiate")
except gssapi.exceptions.GSSError as e:
raise HyperVException(f"Failed to acquire Kerberos credentials: {e}")
if name is None:
self.logger.debug('Resolved default Kerberos principal: %s', creds.name)
s.auth = HTTPSPNEGOAuth(creds=creds, mutual_authentication=OPTIONAL)
else:
self.logger.debug('Using Basic authentication')
s.auth = HyperVAuth(self.username, self.password, self.logger)
return s
@classmethod
def decodeWinUUID(cls, uuid):
""" Windows UUID needs to be decoded using following key
From: {78563412-AB90-EFCD-1234-567890ABCDEF}
To: 12345678-90AB-CDEF-1234-567890ABCDEF
"""
if uuid[0] == "{":
s = uuid[1:-1]
else:
s = uuid
return s[6:8] + s[4:6] + s[2:4] + s[0:2] + "-" + s[11:13] + s[9:11] + "-" + s[16:18] + s[14:16] + s[18:]
def getVmmsVersion(self, hypervsoap):
"""
This method retrieves the version of the vmms executable as it is the authoritative
version of hyper-v running on the machine.
https://social.technet.microsoft.com/Forums/windowsserver/en-US/dce2a4ec-10de-4eba-a19d-ae5213a2382d/how-to-tell-version-of-hyperv-installed?forum=winserverhyperv
"""
vmmsVersion = ""
data = hypervsoap.Enumerate("select * from CIM_Datafile where Path = '\\\\windows\\\\system32\\\\' and FileName='vmms'", "root/cimv2")
for instance in hypervsoap.Pull(data, "root/cimv2"):
if instance['Path'] == '\\windows\\system32\\':
vmmsVersion = instance['Version']
return vmmsVersion
def getHostGuestMapping(self):
guests = []
connection = self.connect()
hypervsoap = HyperVSoap(self.url, connection, self.logger)
uuid = None
if not self.useNewApi:
try:
# SettingType == 3 means current setting, 5 is snapshot - we don't want snapshots
uuid = hypervsoap.Enumerate(
"select BIOSGUID, VirtualSystemIdentifier "
"from Msvm_VirtualSystemSettingData "
"where SettingType = 3",
"root/virtualization")
except HyperVCallFailed:
self.logger.debug("Unable to enumerate using root/virtualization namespace, "
"trying root/virtualization/v2 namespace")
self.useNewApi = True
if self.useNewApi:
# Filter out Planned VMs and snapshots, see
# http://msdn.microsoft.com/en-us/library/hh850257%28v=vs.85%29.aspx
uuid = hypervsoap.Enumerate(
"select BIOSGUID, VirtualSystemIdentifier "
"from Msvm_VirtualSystemSettingData "
"where VirtualSystemType = 'Microsoft:Hyper-V:System:Realized'",
"root/virtualization/v2")
# Get guest states
guest_states = hypervsoap.Invoke_GetSummaryInformation(
"root/virtualization/v2" if self.useNewApi else "root/virtualization")
vmmsVersion = self.getVmmsVersion(hypervsoap)
for instance in hypervsoap.Pull(uuid):
try:
uuid = instance["BIOSGUID"]
assert uuid is not None
except (KeyError, AssertionError):
self.logger.warning("Guest without BIOSGUID found, ignoring")
continue
try:
system_Id = instance["VirtualSystemIdentifier"]
except KeyError:
self.logger.warning("Guest %s is missing VirtualSystemIdentifier", uuid)
continue
try:
state = guest_states[system_Id]
except KeyError:
self.logger.warning("Unknown state for guest %s", uuid)
state = virt.Guest.STATE_UNKNOWN
guests.append(virt.Guest(HyperV.decodeWinUUID(uuid), self.CONFIG_TYPE, state))
# Get the hostname
hostname = None
socket_count = None
data = hypervsoap.Enumerate("select DNSHostName, NumberOfProcessors from Win32_ComputerSystem", "root/cimv2")
for instance in hypervsoap.Pull(data, "root/cimv2"):
hostname = instance["DNSHostName"]
socket_count = instance["NumberOfProcessors"]
uuid = hypervsoap.Enumerate("select UUID from Win32_ComputerSystemProduct", "root/cimv2")
system_uuid = None
for instance in hypervsoap.Pull(uuid, "root/cimv2"):
system_uuid = HyperV.decodeWinUUID(instance["UUID"])
if self.config['hypervisor_id'] == 'uuid':
host = system_uuid
elif self.config['hypervisor_id'] == 'hostname':
host = hostname
facts = {
virt.Hypervisor.CPU_SOCKET_FACT: str(socket_count),
virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'hyperv',
virt.Hypervisor.HYPERVISOR_VERSION_FACT: vmmsVersion,
virt.Hypervisor.SYSTEM_UUID_FACT: system_uuid
}
hypervisor = virt.Hypervisor(hypervisorId=host, name=hostname, guestIds=guests, facts=facts)
return {'hypervisors': [hypervisor]}
def statusConfirmConnection(self):
"""
This call will confirm the credentials. The result outside
of that is not important in the status scenario.
"""
connection = self.connect()
hypervsoap = HyperVSoap(self.url, connection, self.logger)
if self.useNewApi is False:
try:
hypervsoap.Invoke_GetSummaryInformation("root/virtualization")
except HyperVCallFailed:
self.logger.debug("Unable to enumerate using root/virtualization namespace, "
"trying root/virtualization/v2 namespace")
self.useNewApi = True
if self.useNewApi is True:
hypervsoap.Invoke_GetSummaryInformation("root/virtualization/v2")
def ping(self):
return True