11 CONFIG_FILE="/tmp/source/configuration"
12 SESSION_FILE="/etc/planetlab/session"
14 def read_config_file(filename):
15 ## NOTE: text copied from BootManager.py
16 # TODO: unify this code to make it common. i.e. use ConfigParser module
18 vars_file= file(filename,'r')
20 for line in vars_file:
21 # if its a comment or a whitespace line, ignore
22 if line[:1] == "#" or string.strip(line) == "":
25 parts= string.split(line,"=")
27 print "Invalid line in vars file: %s" % line
31 name= string.strip(parts[0])
32 value= string.strip(parts[1])
37 print "Unable to read configuration vars."
42 sys.path = ['/etc/planetlab'] + sys.path
44 api_server_url = "https://" + plc_config.PLC_API_HOST + plc_config.PLC_API_PATH
47 vars = read_config_file(filename)
48 api_server_url = vars['BOOT_API_SERVER']
52 def __init__(self, username=None, password=None, **kwargs):
53 if 'session' in kwargs:
54 self.auth= { 'AuthMethod' : 'session',
55 'session' : kwargs['session'] }
57 if username==None and password==None:
58 self.auth = {'AuthMethod': "anonymous"}
60 self.auth = {'Username' : username,
61 'AuthMethod' : 'password',
62 'AuthString' : password}
64 def __init__(self, auth, url):
67 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
69 def __getattr__(self, name):
70 method = getattr(self.api, name)
72 raise AssertionError("method does not exist")
74 return lambda *params : method(self.auth.auth, *params)
77 return self.api.__repr__()
79 def extract_from(filename, pattern):
80 f = os.popen("grep -E %s %s" % (pattern, filename))
81 val = f.read().strip()
84 def check_running(commandname):
85 f = os.popen("ps ax | grep -E %s | grep -v grep" % (commandname))
86 val = f.read().strip()
92 f=open(SESSION_FILE,'r')
93 session_str=f.read().strip()
94 api = PLC(Auth(session=session_str), api_server_url)
95 # NOTE: What should we do if this call fails?
100 if len(sys.argv) > 1:
103 traceback.print_exc()
107 # NOTE: here we are inferring the runlevel by environmental
108 # observations. We know how this process was started by the
109 # given command line argument. Then in bootmanager
110 # runlevle, the bm.log gives information about the current
113 # call plc for current boot state?
114 # how long have we been running?
115 if env == "bootmanager":
116 bs_val = extract_from('/tmp/bm.log', 'Current boot state:')
117 if len(bs_val) > 0: bs_val = bs_val.split()[-1]
118 ex_val = extract_from('/tmp/bm.log', 'Exception')
119 fs_val = extract_from('/tmp/bm.log', 'mke2fs')
120 bm_val = check_running("BootManager.py")
122 if bs_val in ['diag', 'diagnose', 'safeboot', 'disabled', 'disable']:
123 api.ReportRunlevel({'run_level' : 'safeboot'})
125 elif len(ex_val) > len("Exception"):
126 api.ReportRunlevel({'run_level' : 'failboot'})
128 elif len(fs_val) > 0 and len(bm_val) > 0:
129 api.ReportRunlevel({'run_level' : 'reinstall'})
132 api.ReportRunlevel({'run_level' : 'failboot'})
134 elif env == "production":
135 api.ReportRunlevel({'run_level' : 'boot'})
137 api.ReportRunlevel({'run_level' : 'failboot'})
140 print "reporting error: ", os.popen("uptime").read().strip()
141 traceback.print_exc()
144 # TODO: change to a configurable value
147 if __name__ == "__main__":