1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 import base64
33 import os
34 import sys
35 import socket
36
37 import gettext
38 t = gettext.translation('rhn-client-tools', fallback=True)
39
40 if not hasattr(t, 'ugettext'):
41 t.ugettext = t.gettext
42 _ = t.ugettext
43
44 import OpenSSL
45
46
47
48 sys.modules['sgmlop'] = None
49
50 from up2date_client import getMethod
51 from up2date_client import up2dateErrors
52 from up2date_client import up2dateAuth
53 from up2date_client import up2dateLog
54 from up2date_client import rpcServer
55 from up2date_client import config
56 from up2date_client import clientCaps
57 from up2date_client import capabilities
58 from up2date_client import rhncli, rhnserver
59
60 from rhn import SSL
61 from rhn import rhnLockfile
62 from rhn.i18n import bstr, sstr
63 from rhn.tb import raise_with_tb
64
65 try:
66 import xmlrpclib
67 except ImportError:
68 import xmlrpc.client as xmlrpclib
69 long = int
70
71 if 'sgmlop' in sys.modules:
72 del sys.modules['sgmlop']
73
74 cfg = config.initUp2dateConfig()
75 log = up2dateLog.initLog()
76
77
78 ACTION_VERSION = 2
79
80
81 DISABLE_FILE = "/etc/sysconfig/rhn/disable"
82
83
84 LOCAL_ACTIONS = [("packages.checkNeedUpdate", ("rhnsd=1",))]
88
94
113
115 try:
116 action = self.server.queue.get(up2dateAuth.getSystemId(),
117 ACTION_VERSION, status_report)
118
119 return action
120 except xmlrpclib.Fault:
121 f = sys.exc_info()[1]
122 if f.faultCode == -31:
123 raise_with_tb(up2dateErrors.InsuffMgmntEntsError(f.faultString))
124 else:
125 print("Could not retrieve action item from server %s" % self.server)
126 print("Error code: %d%s" % (f.faultCode, f.faultString))
127 sys.exit(-1)
128
129 except SSL.socket_error:
130 print("ERROR: SSL handshake to %s failed" % self.server)
131 print("""
132 This could signal that you are *NOT* talking to a server
133 whose certificate was signed by a Certificate Authority
134 listed in the %s file or that the
135 RHNS-CA-CERT file is invalid.""" % self.rhns_ca_cert)
136 sys.exit(-1)
137 except socket.error:
138 print("Could not retrieve action from %s.\n"\
139 "Possible networking problem?" % str(self.server))
140 sys.exit(-1)
141 except up2dateErrors.ServerCapabilityError:
142 print(sys.exc_info()[1])
143 sys.exit(1)
144 except OpenSSL.SSL.Error:
145 print("ERROR: SSL errors detected")
146 print("%s" % sys.exc_info()[1])
147 sys.exit(-1)
148
150 try:
151 actions = self.server.queue.get_future_actions(up2dateAuth.getSystemId(),
152 time_window)
153 return actions
154 except xmlrpclib.Fault:
155 f = sys.exc_info()[1]
156 if f.faultCode == -31:
157 raise_with_tb(up2dateErrors.InsuffMgmntEntsError(f.faultString))
158 else:
159 print("Could not retrieve action item from server %s" % self.server)
160 print("Error code: %d%s" % (f.faultCode, f.faultString))
161 sys.exit(-1)
162
163 except SSL.socket_error:
164 print("ERROR: SSL handshake to %s failed" % self.server)
165 print("""
166 This could signal that you are *NOT* talking to a server
167 whose certificate was signed by a Certificate Authority
168 listed in the %s file or that the
169 RHNS-CA-CERT file is invalid.""" % self.rhns_ca_cert)
170 sys.exit(-1)
171 except socket.error:
172 print("Could not retrieve action from %s.\n"\
173 "Possible networking problem?" % str(self.server))
174 sys.exit(-1)
175 except up2dateErrors.ServerCapabilityError:
176 print(sys.exc_info()[1])
177 sys.exit(1)
178 except SSL.Error:
179 print("ERROR: SSL errors detected")
180 print("%s" % sys.exc_info()[1])
181 sys.exit(-1)
182
184 """ Fetch one specific action from rhnParent """
185
186 pass
187
194
214
224
226 """ Parse action data and returns (method, params) """
227 data = action['action']
228 parser, decoder = xmlrpclib.getparser()
229 parser.feed(bstr(data))
230 parser.close()
231 params = decoder.close()
232 method = decoder.getmethodname()
233 return (method, params)
234
236 """ Submit a response for an action_id. """
237
238
239 self.server = CheckCli.__get_server()
240
241 try:
242 ret = self.server.queue.submit(up2dateAuth.getSystemId(),
243 action_id, status, message, data)
244 except xmlrpclib.Fault:
245 f = sys.exc_info()[1]
246 print("Could not submit results to server %s" % self.server)
247 print("Error code: %d%s" % (f.faultCode, f.faultString))
248 sys.exit(-1)
249
250 except SSL.socket_error:
251 print("ERROR: SSL handshake to %s failed" % self.server)
252 print("""
253 This could signal that you are *NOT* talking to a server
254 whose certificate was signed by a Certificate Authority
255 listed in the %s file or that the
256 RHNS-CA-CERT file is invalid.""" % self.rhns_ca_cert)
257 sys.exit(-1)
258 except socket.error:
259 print("Could not submit to %s.\n"\
260 "Possible networking problem?" % str(self.server))
261 sys.exit(-1)
262 return ret
263
265 """ Wrapper handler for the action we're asked to do. """
266 log.log_debug("handle_action", action)
267 log.log_debug("handle_action actionid = %s, version = %s" % (
268 action['id'], action['version']))
269
270 data = {}
271 action_lock = '/var/lib/up2date/action.%s' % str(action['id'])
272 if os.path.exists(action_lock):
273 ret = 255
274 if not cache_only:
275 if os.path.getsize(action_lock) > 0:
276 data['base64enc'] = 1
277 data['return_code'] = 255
278 data['process_start'] = '1970-01-01 00:00:00'
279 data['process_end'] = '1970-01-01 00:00:00'
280 with open(action_lock) as f:
281 data['output'] = base64.encodestring(f.read())
282 log.log_debug("Sending back response", (255, "Previous run of action didn't completed sucessfully, aborting.", data))
283 ret = self.submit_response(action['id'], 255, "Previous run of action didn't completed sucessfully, aborting.", data)
284 os.remove(action_lock)
285 return ret
286
287 open(action_lock, 'a').close()
288
289 (method, params) = self.__parse_action_data(action)
290 (status, message, data) = CheckCli.__run_action(method, params, {'cache_only': cache_only})
291 ret = 0
292 if not cache_only:
293 log.log_debug("Sending back response", (status, message, data))
294 ret = self.submit_response(action['id'], status, message, data)
295 os.remove(action_lock)
296 return ret
297
298
300 log.log_debug("check_action", action)
301
302
303 if type(action) != type({}):
304 print("Got unparseable action response from server")
305 sys.exit(-1)
306
307 for key in ['id', 'version', 'action']:
308 if not key in action:
309 print("Got invalid response - missing '%s'" % key)
310 sys.exit(-1)
311 try:
312 ver = int(action['version'])
313 except ValueError:
314 ver = -1
315 if ver > ACTION_VERSION or ver < 0:
316 print("Got unknown action version %d" % ver)
317 print(action)
318
319 self.submit_response(action["id"],
320 xmlrpclib.Fault(-99, "Can not handle this version"))
321 return False
322 return True
323
324 @staticmethod
337
338 @staticmethod
345
346 @staticmethod
348 status_report = {}
349 status_report["uname"] = list(os.uname())
350
351 if os.access("/proc/uptime", os.R_OK):
352 uptime = open("/proc/uptime", "r").read().split()
353 try:
354 status_report["uptime"] = [int(float(a)) for a in uptime]
355 except (TypeError, ValueError):
356 status_report["uptime"] = [a[:-3] for a in uptime]
357 except:
358 pass
359
360
361 if status_report['uptime'][1] > long(2)**31-1:
362 status_report['uptime'][1] = -1
363
364 return status_report
365
366 @staticmethod
368 """
369 Hit any actions that we want to always run.
370
371 If we want to run any actions everytime rhnsd runs rhn_check,
372 we can add them to the list LOCAL_ACTIONS
373 """
374
375 for method_params in LOCAL_ACTIONS:
376 method = method_params[0]
377 params = method_params[1]
378 (status, message, data) = CheckCli.__run_action(method, params)
379 log.log_debug("local action status: ", (status, message, data))
380
381 @staticmethod
383 log.log_debug("do_call ", method, params, kwargs)
384
385 method = getMethod.getMethod(method, "rhn.actions")
386 retval = method(*params, **kwargs)
387
388 return retval
389
390 @staticmethod
407
408 @staticmethod
410 """ If we're disabled, go down (almost) quietly. """
411 if os.path.exists(DISABLE_FILE):
412 print("RHN service is disabled. Check %s" % DISABLE_FILE)
413 sys.exit(0)
414
415 @staticmethod
417 """ Retrieve the system_id. This is required. """
418 if not up2dateAuth.getSystemId():
419 print("ERROR: unable to read system id.")
420 sys.exit(-1)
421
422 @staticmethod
430
431 if __name__ == "__main__":
432 cli = CheckCli()
433 cli.run()
434