Package virtualization :: Module domain_control
[hide private]
[frames] | no frames]

Source Code for Module virtualization.domain_control

  1  # 
  2  # Copyright (c) 2006--2012 Red Hat, Inc.  Distributed under GPL. 
  3  # 
  4  # Author: Peter Vetere <pvetere@redhat.com> 
  5  # 
  6  # This module handles virtual domain control requests. 
  7  # 
  8   
  9  import sys 
 10   
 11  from virtualization import poller, start_domain 
 12   
 13  from virtualization.errors import VirtualizationException 
 14  from virtualization.util   import hyphenize_uuid 
 15   
 16  try: 
 17      import libvirt 
 18  except: 
 19      # The libvirt library is not installed.  That's ok, we can't assume it will 
 20      # be on every system. 
 21      libvirt = None 
 22   
 23  from spacewalk.common.usix import raise_with_tb 
 24   
 25   
 26  ############################################################################### 
 27  # Public Interface 
 28  ############################################################################### 
 29   
30 -def shutdown(uuid):
31 """ 32 Shuts down the domain with the given UUID. If the instance is crashed, it 33 is destroyed. If the instance is paused, it is unpaused and shutdown 34 cleanly. 35 """ 36 state = poller.poll_state(uuid) 37 if state.is_crashed(): 38 destroy(uuid) 39 elif state.is_paused(): 40 resume(uuid) 41 shutdown(uuid) 42 else: 43 _call_domain_control_routine(uuid, "shutdown")
44
45 -def start(uuid):
46 """ 47 Starts up the domain with the given UUID. 48 """ 49 state = poller.poll_state(uuid) 50 if state.is_crashed(): 51 destroy(uuid) 52 start(uuid) 53 elif state.is_paused(): 54 resume(uuid) 55 else: 56 start_domain.start_domain(uuid)
57
58 -def suspend(uuid):
59 """ 60 Suspends the domain with the given UUID. 61 """ 62 _call_domain_control_routine(uuid, "suspend")
63
64 -def resume(uuid):
65 """ 66 Resumes the domain with the given UUID. 67 """ 68 _call_domain_control_routine(uuid, "resume")
69
70 -def reboot(uuid):
71 """ 72 Reboots the domain with the given UUID. If the system is paused, we 73 unpause and reboot it. If the system is stopped, we start it. If the 74 system is crashed, we destroy and restart it. 75 """ 76 state = poller.poll_state(uuid) 77 if state.is_stopped(): 78 start(uuid) 79 elif state.is_paused(): 80 resume(uuid) 81 reboot(uuid) 82 elif state.is_crashed(): 83 destroy(uuid) 84 start(uuid) 85 else: 86 _call_domain_control_routine(uuid, "reboot", 0)
87
88 -def destroy(uuid):
89 """ 90 Destroys the domain with the given UUID. 91 """ 92 _call_domain_control_routine(uuid, "destroy")
93
94 -def setMemory(uuid, memory):
95 """ 96 Sets the max memory usage for the domain with the given UUID. 97 """ 98 _call_domain_control_routine(uuid, "setMemory", memory)
99
100 -def setVCPUs(uuid, vcpus):
101 """ 102 Sets the number of vcpus for the domain with the given UUID. 103 """ 104 _call_domain_control_routine(uuid, "setVcpus", vcpus)
105 106 107 ############################################################################### 108 # Helper Routines 109 ############################################################################### 110
111 -def _get_domain(uuid):
112 """ 113 Lookup the domain by its UUID. If not found, raise an exception. 114 """ 115 conn = libvirt.open(None) 116 domain = None 117 hyphenized_uuid = hyphenize_uuid(uuid) 118 try: 119 domain = conn.lookupByUUIDString(hyphenized_uuid) 120 except libvirt.libvirtError: 121 lve = sys.exc_info()[1] 122 raise_with_tb(VirtualizationException("Domain UUID '%s' not found: %s" % (hyphenized_uuid, str(lve))), 123 sys.exc_info()[2]) 124 return (conn, domain)
125
126 -def _call_domain_control_routine(uuid, routine_name, *args):
127 """ 128 Call a function in a domain, optionally with a set of arguments. 129 """ 130 131 # If libvirt is not available, this is a no-op. 132 if not libvirt: return 133 134 # Lookup the domain by its UUID. 135 (conn, domain) = _get_domain(uuid) 136 137 # Get a reference to the domain's control routine. 138 ctrl_func = None 139 try: 140 ctrl_func = getattr(domain, routine_name) 141 except AttributeError: 142 raise_with_tb(VirtualizationException("Unknown function: %s" % routine_name), sys.exc_info()[2]) 143 144 result = 0 145 try: 146 if sys.version_info[0] == 3: 147 result = ctrl_func(*args) 148 else: 149 result = apply(ctrl_func, args) 150 except TypeError: 151 te = sys.exc_info()[1] 152 raise_with_tb(VirtualizationException("Invalid arguments (%s) to %s: %s" % (str(args), routine_name, str(te))), 153 sys.exc_info()[2]) 154 except libvirt.libvirtError: 155 le = sys.exc_info()[1] 156 raise_with_tb(VirtualizationException("LibVirt Error %s: %s" % (routine_name, str(le))), sys.exc_info()[2]) 157 158 # Handle the return code. Anything non-zero is an error. 159 if result != 0: 160 raise_with_tb(VirtualizationException("Could not perform function '%s' on domain %s. Error: %s" % 161 (routine_name, uuid, str(result))), sys.exc_info()[2])
162