Revision 58e356a9
b/lib/hypervisor/hv_base.py | ||
---|---|---|
206 | 206 |
"""Reboot an instance.""" |
207 | 207 |
raise NotImplementedError |
208 | 208 |
|
209 |
def ListInstances(self): |
|
209 |
def ListInstances(self, hvparams=None):
|
|
210 | 210 |
"""Get the list of running instances.""" |
211 | 211 |
raise NotImplementedError |
212 | 212 |
|
b/lib/hypervisor/hv_chroot.py | ||
---|---|---|
102 | 102 |
""" |
103 | 103 |
return utils.PathJoin(cls._ROOT_DIR, instance_name) |
104 | 104 |
|
105 |
def ListInstances(self): |
|
105 |
def ListInstances(self, hvparams=None):
|
|
106 | 106 |
"""Get the list of running instances. |
107 | 107 |
|
108 | 108 |
""" |
b/lib/hypervisor/hv_fake.py | ||
---|---|---|
50 | 50 |
hv_base.BaseHypervisor.__init__(self) |
51 | 51 |
utils.EnsureDirs([(self._ROOT_DIR, constants.RUN_DIRS_MODE)]) |
52 | 52 |
|
53 |
def ListInstances(self): |
|
53 |
def ListInstances(self, hvparams=None):
|
|
54 | 54 |
"""Get the list of running instances. |
55 | 55 |
|
56 | 56 |
""" |
b/lib/hypervisor/hv_kvm.py | ||
---|---|---|
961 | 961 |
# Run CPU pinning, based on configured mask |
962 | 962 |
self._AssignCpuAffinity(cpu_mask, pid, thread_dict) |
963 | 963 |
|
964 |
def ListInstances(self): |
|
964 |
def ListInstances(self, hvparams=None):
|
|
965 | 965 |
"""Get the list of running instances. |
966 | 966 |
|
967 | 967 |
We can do this by listing our live instances directory and |
b/lib/hypervisor/hv_lxc.py | ||
---|---|---|
157 | 157 |
|
158 | 158 |
return memory |
159 | 159 |
|
160 |
def ListInstances(self): |
|
160 |
def ListInstances(self, hvparams=None):
|
|
161 | 161 |
"""Get the list of running instances. |
162 | 162 |
|
163 | 163 |
""" |
b/lib/hypervisor/hv_xen.py | ||
---|---|---|
358 | 358 |
|
359 | 359 |
return cmd |
360 | 360 |
|
361 |
def _RunXen(self, args): |
|
361 |
def _RunXen(self, args, hvparams=None):
|
|
362 | 362 |
"""Wrapper around L{utils.process.RunCmd} to run Xen command. |
363 | 363 |
|
364 |
@type hvparams: dict of strings |
|
365 |
@param hvparams: dictionary of hypervisor params |
|
364 | 366 |
@see: L{utils.process.RunCmd} |
365 | 367 |
|
366 | 368 |
""" |
367 |
cmd = [self._GetCommand()] |
|
369 |
cmd = [self._GetCommand(hvparams=hvparams)]
|
|
368 | 370 |
cmd.extend(args) |
369 | 371 |
|
370 | 372 |
return self._run_cmd_fn(cmd) |
... | ... | |
433 | 435 |
utils.RenameFile(old_filename, new_filename) |
434 | 436 |
return new_filename |
435 | 437 |
|
436 |
def _GetInstanceList(self, include_node): |
|
438 |
def _GetInstanceList(self, include_node, hvparams=None):
|
|
437 | 439 |
"""Wrapper around module level L{_GetInstanceList}. |
438 | 440 |
|
439 | 441 |
""" |
440 |
return _GetInstanceList(lambda: self._RunXen(["list"]), include_node) |
|
442 |
return _GetInstanceList(lambda: self._RunXen(["list"], hvparams=hvparams), |
|
443 |
include_node) |
|
441 | 444 |
|
442 |
def ListInstances(self): |
|
445 |
def ListInstances(self, hvparams=None):
|
|
443 | 446 |
"""Get the list of running instances. |
444 | 447 |
|
445 | 448 |
""" |
446 |
instance_list = self._GetInstanceList(False) |
|
449 |
instance_list = self._GetInstanceList(False, hvparams=hvparams)
|
|
447 | 450 |
names = [info[0] for info in instance_list] |
448 | 451 |
return names |
449 | 452 |
|
... | ... | |
498 | 501 |
cmd.append("-p") |
499 | 502 |
cmd.append(self._ConfigFileName(instance.name)) |
500 | 503 |
|
501 |
result = self._RunXen(cmd) |
|
504 |
result = self._RunXen(cmd, hvparams=instance.hvparams)
|
|
502 | 505 |
if result.failed: |
503 | 506 |
# Move the Xen configuration file to the log directory to avoid |
504 | 507 |
# leaving a stale config file behind. |
... | ... | |
515 | 518 |
if name is None: |
516 | 519 |
name = instance.name |
517 | 520 |
|
518 |
return self._StopInstance(name, force) |
|
521 |
return self._StopInstance(name, force, instance.hvparams)
|
|
519 | 522 |
|
520 |
def _StopInstance(self, name, force): |
|
523 |
def _StopInstance(self, name, force, hvparams):
|
|
521 | 524 |
"""Stop an instance. |
522 | 525 |
|
526 |
@type name: string |
|
527 |
@param name: name of the instance to be shutdown |
|
528 |
@type force: boolean |
|
529 |
@param force: flag specifying whether shutdown should be forced |
|
530 |
@type hvparams: dict of string |
|
531 |
@param hvparams: hypervisor parameters of the instance |
|
532 |
|
|
523 | 533 |
""" |
524 | 534 |
if force: |
525 | 535 |
action = "destroy" |
526 | 536 |
else: |
527 | 537 |
action = "shutdown" |
528 | 538 |
|
529 |
result = self._RunXen([action, name]) |
|
539 |
result = self._RunXen([action, name], hvparams=hvparams)
|
|
530 | 540 |
if result.failed: |
531 | 541 |
raise errors.HypervisorError("Failed to stop instance %s: %s, %s" % |
532 | 542 |
(name, result.fail_reason, result.output)) |
... | ... | |
544 | 554 |
raise errors.HypervisorError("Failed to reboot instance %s," |
545 | 555 |
" not running" % instance.name) |
546 | 556 |
|
547 |
result = self._RunXen(["reboot", instance.name]) |
|
557 |
result = self._RunXen(["reboot", instance.name], hvparams=instance.hvparams)
|
|
548 | 558 |
if result.failed: |
549 | 559 |
raise errors.HypervisorError("Failed to reboot instance %s: %s, %s" % |
550 | 560 |
(instance.name, result.fail_reason, |
... | ... | |
577 | 587 |
@param mem: actual memory size to use for instance runtime |
578 | 588 |
|
579 | 589 |
""" |
580 |
result = self._RunXen(["mem-set", instance.name, mem]) |
|
590 |
result = self._RunXen(["mem-set", instance.name, mem], |
|
591 |
hvparams=instance.hvparams) |
|
581 | 592 |
if result.failed: |
582 | 593 |
raise errors.HypervisorError("Failed to balloon instance %s: %s (%s)" % |
583 | 594 |
(instance.name, result.fail_reason, |
... | ... | |
731 | 742 |
|
732 | 743 |
args.extend([instance_name, target]) |
733 | 744 |
|
734 |
result = self._RunXen(args) |
|
745 |
result = self._RunXen(args, hvparams=hvparams)
|
|
735 | 746 |
if result.failed: |
736 | 747 |
raise errors.HypervisorError("Failed to migrate instance %s: %s" % |
737 | 748 |
(instance_name, result.output)) |
b/test/py/ganeti.hypervisor.hv_xen_unittest.py | ||
---|---|---|
27 | 27 |
import shutil |
28 | 28 |
import random |
29 | 29 |
import os |
30 |
import mock |
|
30 | 31 |
|
31 | 32 |
from ganeti import constants |
32 | 33 |
from ganeti import objects |
... | ... | |
335 | 336 |
self.assertRaises(KeyError, hv_xen._GetConfigFileDiskData, disks, "sd") |
336 | 337 |
|
337 | 338 |
|
338 |
class TestXenHypervisorUnknownCommand(unittest.TestCase): |
|
339 |
def test(self): |
|
339 |
class TestXenHypervisorRunXen(unittest.TestCase): |
|
340 |
|
|
341 |
XEN_SUB_CMD = "help" |
|
342 |
|
|
343 |
def testCommandUnknown(self): |
|
340 | 344 |
cmd = "#unknown command#" |
341 | 345 |
self.assertFalse(cmd in constants.KNOWN_XEN_COMMANDS) |
342 | 346 |
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, |
... | ... | |
344 | 348 |
_cmd=cmd) |
345 | 349 |
self.assertRaises(errors.ProgrammerError, hv._RunXen, []) |
346 | 350 |
|
351 |
def testCommandValid(self): |
|
352 |
xen_cmd = "xm" |
|
353 |
mock_run_cmd = mock.Mock() |
|
354 |
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, |
|
355 |
_run_cmd_fn=mock_run_cmd) |
|
356 |
hv._RunXen([self.XEN_SUB_CMD]) |
|
357 |
mock_run_cmd.assert_called_with([xen_cmd, self.XEN_SUB_CMD]) |
|
358 |
|
|
359 |
def testCommandFromHvparams(self): |
|
360 |
expected_xen_cmd = "xl" |
|
361 |
hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
|
362 |
mock_run_cmd = mock.Mock() |
|
363 |
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, |
|
364 |
_run_cmd_fn=mock_run_cmd) |
|
365 |
hv._RunXen([self.XEN_SUB_CMD], hvparams=hvparams) |
|
366 |
mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_SUB_CMD]) |
|
367 |
|
|
368 |
|
|
369 |
class TestXenHypervisorGetInstanceList(unittest.TestCase): |
|
370 |
|
|
371 |
RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) |
|
372 |
XEN_LIST = "list" |
|
373 |
|
|
374 |
def testOk(self): |
|
375 |
expected_xen_cmd = "xm" |
|
376 |
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) |
|
377 |
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, |
|
378 |
_run_cmd_fn=mock_run_cmd) |
|
379 |
hv._GetInstanceList(True) |
|
380 |
mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST]) |
|
381 |
|
|
382 |
def testFromHvparams(self): |
|
383 |
expected_xen_cmd = "xl" |
|
384 |
hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
|
385 |
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) |
|
386 |
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, |
|
387 |
_run_cmd_fn=mock_run_cmd) |
|
388 |
hv._GetInstanceList(True, hvparams=hvparams) |
|
389 |
mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST]) |
|
390 |
|
|
391 |
|
|
392 |
class TestXenHypervisorListInstances(unittest.TestCase): |
|
393 |
|
|
394 |
RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) |
|
395 |
XEN_LIST = "list" |
|
396 |
|
|
397 |
def testDefaultXm(self): |
|
398 |
expected_xen_cmd = "xm" |
|
399 |
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) |
|
400 |
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, |
|
401 |
_run_cmd_fn=mock_run_cmd) |
|
402 |
hv.ListInstances() |
|
403 |
mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST]) |
|
404 |
|
|
405 |
def testHvparamsXl(self): |
|
406 |
expected_xen_cmd = "xl" |
|
407 |
hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
|
408 |
mock_run_cmd = mock.Mock( return_value=self.RESULT_OK ) |
|
409 |
hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, |
|
410 |
_run_cmd_fn=mock_run_cmd) |
|
411 |
hv.ListInstances(hvparams=hvparams) |
|
412 |
mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST]) |
|
413 |
|
|
347 | 414 |
|
348 | 415 |
class TestXenHypervisorWriteConfigFile(unittest.TestCase): |
349 | 416 |
def setUp(self): |
... | ... | |
620 | 687 |
|
621 | 688 |
if fail: |
622 | 689 |
try: |
623 |
hv._StopInstance(name, force) |
|
690 |
hv._StopInstance(name, force, None)
|
|
624 | 691 |
except errors.HypervisorError, err: |
625 | 692 |
self.assertTrue(str(err).startswith("Failed to stop instance")) |
626 | 693 |
else: |
... | ... | |
629 | 696 |
msg=("Configuration was removed when stopping" |
630 | 697 |
" instance failed")) |
631 | 698 |
else: |
632 |
hv._StopInstance(name, force) |
|
699 |
hv._StopInstance(name, force, None)
|
|
633 | 700 |
self.assertFalse(os.path.exists(cfgfile)) |
634 | 701 |
|
635 | 702 |
def _MigrateNonRunningInstCmd(self, cmd): |
Also available in: Unified diff