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

Source Code for Module virtualization.support

  1  # 
  2  # Copyright (c) 2008--2012 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  import sys 
 17  import gettext 
 18   
 19  from up2date_client import rhncli 
 20  from virtualization import domain_control, poller, schedule_poller 
 21   
 22  from virtualization.constants        import IdentityType,            \ 
 23                                              PropertyType 
 24  from virtualization.notification     import Plan,                    \ 
 25                                              EventType,               \ 
 26                                              TargetType 
 27  from virtualization.domain_config    import DomainConfig 
 28  from virtualization.domain_directory import DomainDirectory 
 29  from spacewalk.common.usix import UnicodeType 
 30   
 31   
 32  t = gettext.translation('rhn-virtualization', fallback=True) 
 33  _ = t.ugettext 
 34   
 35  try: 
 36      import libvirt 
 37  except: 
 38      # The libvirt library is not installed.  That's ok, we can't assume it will 
 39      # be on every system. 
 40      libvirt = None 
 41   
42 -def utf8_encode(msg):
43 """ 44 for RHEL6 just pass the function to rhncli 45 for RHEL5 do the same within this module 46 """ 47 if hasattr(rhncli, 'utf8_encode'): 48 return rhncli.utf8_encode(msg) 49 if isinstance(msg, UnicodeType): 50 msg = msg.encode('utf-8') 51 return(msg)
52 53
54 -def _check_status(daemon):
55 """ 56 Checks to see if daemon is running. 57 """ 58 try: 59 # python 2 60 import commands 61 except ImportError: 62 import subprocess as commands 63 64 cmd = "/etc/init.d/%s status" % daemon 65 status, msg = commands.getstatusoutput(cmd) 66 if status != 0: 67 return False 68 return True
69 70 vdsm_enabled = _check_status("vdsmd") 71 # override vdsm_enabled if we can't read information from it 72 try: 73 from virtualization import localvdsm 74 except ImportError: 75 vdsm_enabled = False 76 77 78 ############################################################################### 79 # Public Interface 80 ############################################################################### 81
82 -def refresh(fail_on_error=False):
83 """ 84 Refreshes the virtualization info for this host and any subdomains on the 85 server. 86 """ 87 if _is_host_domain(fail_on_error): 88 domain_identity = IdentityType.HOST 89 my_uuid = _fetch_host_uuid() 90 else: 91 # Not a host. No-op. 92 return 93 94 # Now that we've gathered some preliminary information, create a plan of 95 # actions that we will eventually pass to the server. 96 plan = Plan() 97 98 # First, declare our own existence. 99 plan.add( 100 EventType.EXISTS, 101 TargetType.SYSTEM, 102 { PropertyType.IDENTITY : domain_identity, 103 PropertyType.UUID : my_uuid }) 104 105 # Now, crawl each of the domains on this host. 106 if vdsm_enabled: 107 server = localvdsm.connect() 108 domains = poller.poll_through_vdsm(server) 109 else: 110 domains = poller.poll_hypervisor() 111 112 if not len(domains) and libvirt.openReadOnly(None).getType() == 'Xen': 113 # On a KVM/QEMU host, libvirt reports no domain entry for host itself 114 # On a Xen host, either there were no domains or xend might not be 115 # running. Don't proceed further. 116 return 117 domain_list = list(domains.values()) 118 domain_uuids = list(domains.keys()) 119 120 if not vdsm_enabled: 121 # We need this only for libvirt 122 domain_dir = DomainDirectory() 123 domain_dir.save_unknown_domain_configs(domain_uuids) 124 125 plan.add(EventType.CRAWL_BEGAN, TargetType.SYSTEM) 126 for domain_properties in domain_list: 127 plan.add(EventType.EXISTS, TargetType.DOMAIN, domain_properties) 128 plan.add(EventType.CRAWL_ENDED, TargetType.SYSTEM) 129 130 # Finally, execute the actions queued up in the plan. 131 plan.execute()
132
133 -def shutdown(uuid):
134 """ 135 Shuts down the domain with the given UUID. 136 """ 137 domain_control.shutdown(uuid)
138
139 -def start(uuid):
140 """ 141 Starts up the domain with the given UUID. 142 """ 143 domain_control.start(uuid)
144
145 -def suspend(uuid):
146 """ 147 Suspends the domain with the given UUID. 148 """ 149 domain_control.suspend(uuid)
150
151 -def resume(uuid):
152 """ 153 Resumes the domain with the given UUID. 154 """ 155 domain_control.resume(uuid)
156
157 -def reboot(uuid):
158 """ 159 Reboots the domain with the given UUID. 160 """ 161 domain_control.reboot(uuid)
162
163 -def destroy(uuid):
164 """ 165 Destroys the domain with the given UUID. 166 """ 167 domain_control.destroy(uuid)
168
169 -def setMemory(uuid, memory):
170 """ 171 Sets the max memory usage for the domain with the given UUID. 172 """ 173 domain_dir = DomainDirectory() 174 config = domain_dir.load_config(uuid) 175 config.setConfigItem(DomainConfig.MEMORY, memory) 176 config.save() 177 domain_control.setMemory(uuid, memory)
178
179 -def setVCPUs(uuid, vcpus):
180 """ 181 Sets the number of vcpus for the domain with the given UUID. 182 """ 183 domain_dir = DomainDirectory() 184 config = domain_dir.load_config(uuid) 185 config.setConfigItem(DomainConfig.VCPU, vcpus) 186 config.save() 187 domain_control.setVCPUs(uuid, vcpus)
188
189 -def schedulePoller(minute, hour, dom, month, dow):
190 """ 191 Sets when poller should run. 192 """ 193 return schedule_poller.schedule_poller(minute, hour, dom, month, dow)
194 195 ############################################################################### 196 # Helper Routines 197 ############################################################################### 198
199 -def _is_host_domain(fail_on_error=False):
200 """ 201 This function returns true if this system is currently a host domain. 202 Simply having virtualization enabled is sufficient. 203 204 We can figure out if Xen/Qemu is running by checking for the type 205 """ 206 if vdsm_enabled: 207 # since vdsm is enabled, lets move further and 208 # see what we get 209 return True 210 if not libvirt: 211 # No libvirt, dont bother with the rest 212 return False 213 try: 214 conn = libvirt.openReadOnly(None) 215 except libvirt.libvirtError: # libvirtd is not running 216 sys.stderr.write(utf8_encode(_("Warning: Could not retrieve virtualization information!\n\tlibvirtd service needs to be running.\n"))) 217 if fail_on_error: 218 sys.exit(1) 219 return False 220 if conn and conn.getType() in ['Xen', 'QEMU']: 221 return True 222 return False
223
224 -def _fetch_host_uuid():
225 """ 226 This function returns the UUID of the host system. This will always be 227 16 zeros. 228 """ 229 return '0000000000000000'
230 231 ############################################################################### 232 # Test Routine 233 ############################################################################### 234 235 if __name__ == "__main__": 236 #refresh() 237 print(_retrieve_virtual_domain_list()) 238