ia64/xen-unstable

view tools/python/xen/xend/XendRoot.py @ 1776:c2f673cea5e4

bitkeeper revision 1.1072.1.1 (40f4e51fLMgcKX4Sn6FNYePX6EqkGA)

Merge http://xen.bkbits.net:8080/xeno-unstable.bk
into gandalf.hpl.hp.com:/var/bk/xeno-unstable.bk
author xenbk@gandalf.hpl.hp.com
date Wed Jul 14 07:47:43 2004 +0000 (2004-07-14)
parents e04e40b4615f ad09d919356e
children e91945007886
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 """Xend root class.
4 Creates the event server and handles configuration.
5 """
7 import os
8 import os.path
9 import sys
10 import EventServer
12 # Initial create of the event server.
13 eserver = EventServer.instance()
15 import sxp
17 def reboots():
18 """Get a list of system reboots from wtmp.
19 """
20 out = os.popen('last reboot', 'r')
21 list = [ x.strip() for x in out if x.startswith('reboot') ]
22 return list
24 def last_reboot():
25 """Get the last known system reboot.
26 """
27 l = reboots()
28 return (l and l[-1]) or None
30 class XendRoot:
31 """Root of the management classes."""
33 lastboot_default = "/var/xen/lastboot"
35 """Default path to the root of the database."""
36 dbroot_default = "/var/xen/xend-db"
38 """Default path to the config file."""
39 config_default = "/etc/xen/xend-config.sxp"
41 """Environment variable used to override config_default."""
42 config_var = "XEND_CONFIG"
44 """Where network control scripts live."""
45 network_script_dir = "/etc/xen"
47 def __init__(self):
48 self.rebooted = 0
49 self.last_reboot = None
50 self.dbroot = None
51 self.config_path = None
52 self.config = None
53 self.configure()
54 self.check_lastboot()
55 eserver.subscribe('xend.*', self.event_handler)
56 #eserver.subscribe('xend.domain.created', self.event_handler)
57 #eserver.subscribe('xend.domain.died', self.event_handler)
59 def start(self):
60 eserver.inject('xend.start', self.rebooted)
62 def event_handler(self, event, val):
63 print >> sys.stderr, "EVENT>", event, val
65 def read_lastboot(self):
66 try:
67 val = file(self.lastboot, 'rb').readlines()[0]
68 except StandardError, ex:
69 print 'warning: Error reading', self.lastboot, ex
70 val = None
71 return val
73 def write_lastboot(self, val):
74 if not val: return
75 try:
76 fdir = os.path.dirname(self.lastboot)
77 if not os.path.isdir(fdir):
78 os.makedirs(fdir)
79 out = file(self.lastboot, 'wb+')
80 out.write(val)
81 out.close()
82 except IOError, ex:
83 print 'warning: Error writing', self.lastboot, ex
84 pass
86 def check_lastboot(self):
87 """Check if there has been a system reboot since we saved lastboot.
88 """
89 last_val = self.read_lastboot()
90 this_val = last_reboot()
91 if this_val == last_val:
92 self.rebooted = 0
93 else:
94 self.rebooted = 1
95 self.write_lastboot(this_val)
96 self.last_reboot = this_val
98 def get_last_reboot(self):
99 return self.last_reboot
101 def get_rebooted(self):
102 return self.rebooted
104 def configure(self):
105 print 'XendRoot>configure>'
106 self.set_config()
107 self.dbroot = self.get_config_value("dbroot", self.dbroot_default)
108 self.lastboot = self.get_config_value("lastboot", self.lastboot_default)
110 def get_dbroot(self):
111 """Get the path to the database root.
112 """
113 return self.dbroot
115 def set_config(self):
116 """If the config file exists, read it. If not, ignore it.
118 The config file is a sequence of sxp forms.
119 """
120 self.config_path = os.getenv(self.config_var, self.config_default)
121 print 'XendRoot>set_config> config_path=', self.config_path
122 if os.path.exists(self.config_path):
123 print 'XendRoot>set_config> loading'
124 fin = file(self.config_path, 'rb')
125 try:
126 config = sxp.parse(fin)
127 config.insert(0, 'xend-config')
128 self.config = config
129 finally:
130 fin.close()
131 else:
132 print 'XendRoot>set_config> not found'
133 self.config = ['xend-config']
134 print 'XendRoot> config=', self.config
136 def get_config(self, name=None):
137 """Get the configuration element with the given name, or
138 the whole configuration if no name is given.
140 @param name: element name (optional)
141 @return: config or none
142 """
143 if name is None:
144 val = self.config
145 else:
146 val = sxp.child(self.config, name)
147 return val
149 def get_config_value(self, name, val=None):
150 """Get the value of an atomic configuration element.
152 @param name: element name
153 @param val: default value (optional, defaults to None)
154 @return: value
155 """
156 return sxp.child_value(self.config, name, val=val)
158 def get_xend_port(self):
159 return int(self.get_config_value('xend-port', '8000'))
161 def get_xend_address(self):
162 return self.get_config_value('xend-address', '')
164 def get_network_script(self):
165 return self.get_config_value('network-script', 'network')
167 def get_vif_bridge(self):
168 return self.get_config_value('vif-bridge', 'xen-br0')
170 def get_vif_script(self):
171 return self.get_config_value('vif-script', 'vif-bridge')
173 def get_vif_antispoof(self):
174 v = self.get_config_value('vif-antispoof', 'yes')
175 return v in ['yes', '1', 'on']
177 def instance():
178 global inst
179 try:
180 inst
181 except:
182 inst = XendRoot()
183 return inst