| Trees | Indices | Help |
|---|
|
|
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
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
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
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
138
144
150
156
162
168
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
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
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
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
230
231 ###############################################################################
232 # Test Routine
233 ###############################################################################
234
235 if __name__ == "__main__":
236 #refresh()
237 print(_retrieve_virtual_domain_list())
238
| Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Wed Mar 4 07:37:48 2020 | http://epydoc.sourceforge.net |