Browse code

Log to systemd journal and save the log to file.

Log to systemd journal and save the log to file.

Xiaolin Li authored on 2015/08/28 07:25:28
Showing 3 changed files
... ...
@@ -76,7 +76,8 @@ class Installer(object):
76 76
             self.progress_bar.hide()
77 77
             self.window.addstr(0, 0, 'Opps, Installer got interrupted.\n\nPress any key to get to the bash...')
78 78
             self.window.content_window().getch()
79
-        
79
+
80
+        modules.commons.dump(modules.commons.LOG_ERROR, modules.commons.LOG_FILE_NAME)        
80 81
         sys.exit(1)
81 82
 
82 83
     def install(self, params):
... ...
@@ -194,7 +195,7 @@ class Installer(object):
194 194
                 progressbar_num_items +=  q[0].size + q[0].size * self.install_factor
195 195
                 self.rpms_tobeinstalled.append({'package': package, 'size': q[0].size, 'location': q[0].location, 'filename': os.path.basename(q[0].location)})
196 196
             else:
197
-                print >> sys.stderr, "Package %s not found in the repo" % package
197
+                modules.commons.log(modules.commons.LOG_WARNING, "Package {} not found in the repo".format(package))
198 198
                 #self.exit_gracefully(None, None)
199 199
 
200 200
         self.progress_bar.update_num_items(progressbar_num_items)
... ...
@@ -317,39 +318,39 @@ class Installer(object):
317 317
         return process.wait()
318 318
 
319 319
     def execute_modules(self, phase):
320
-        modules = glob.glob('modules/m_*.py')
321
-        for mod_path in modules:
320
+        modules_paths = glob.glob('modules/m_*.py')
321
+        for mod_path in modules_paths:
322 322
             module = mod_path.replace('/', '.', 1)
323 323
             module = os.path.splitext(module)[0]
324 324
             try:
325 325
                 __import__(module)
326 326
                 mod = sys.modules[module]
327 327
             except ImportError:
328
-                print >> sys.stderr,  'Error importing module %s' % module
328
+                modules.commons.log(modules.commons.LOG_ERROR, 'Error importing module {}'.format(module))
329 329
                 continue
330 330
             
331 331
             # the module default is disabled
332 332
             if not hasattr(mod, 'enabled') or mod.enabled == False:
333
-                print >> sys.stderr,  "module %s is not enabled" % module
333
+                modules.commons.log(modules.commons.LOG_INFO, "module {} is not enabled".format(module))
334 334
                 continue
335 335
             # check for the install phase
336 336
             if not hasattr(mod, 'install_phase'):
337
-                print >> sys.stderr,  "Error: can not defind module %s phase" % module
337
+                modules.commons.log(modules.commons.LOG_ERROR, "Error: can not defind module {} phase".format(module))
338 338
                 continue
339 339
             if mod.install_phase != phase:
340
-                print >> sys.stderr,  "Skipping module %s for phase %s" % (module, phase)
340
+                modules.commons.log(modules.commons.LOG_INFO, "Skipping module {0} for phase {1}".format(module, phase))
341 341
                 continue
342 342
             if not hasattr(mod, 'execute'):
343
-                print >> sys.stderr,  "Error: not able to execute module %s" % module
343
+                modules.commons.log(modules.commons.LOG_ERROR, "Error: not able to execute module {}".format(module))
344 344
                 continue
345 345
             mod.execute(module, self.ks_config, self.install_config, self.photon_root)
346 346
 
347 347
     def run(self, command, comment = None):
348 348
         if comment != None:
349
-            print >> sys.stderr, "Installer: {} ".format(comment)
349
+            modules.commons.log(modules.commons.LOG_INFO, "Installer: {} ".format(comment))
350 350
             self.progress_bar.update_loading_message(comment)
351 351
 
352
-        print >> sys.stderr, "Installer: {} ".format(command)
352
+        modules.commons.log(modules.commons.LOG_INFO, "Installer: {} ".format(command))
353 353
         process = subprocess.Popen([command], shell=True, stdout=self.output)
354 354
         retval = process.wait()
355 355
         return retval
356 356
\ No newline at end of file
... ...
@@ -20,6 +20,7 @@ import string
20 20
 import random
21 21
 import urllib
22 22
 import urllib2
23
+import modules.commons
23 24
 from diskpartitioner import DiskPartitioner
24 25
 from packageselector import PackageSelector
25 26
 from custompackageselector import CustomPackageSelector
... ...
@@ -48,7 +49,7 @@ class IsoInstaller(object):
48 48
                     err_msg = response.text
49 49
                 except Exception as e:
50 50
                     err_msg = e
51
-                print >> sys.stderr, "Failed to get the kickstart file at {0}, error msg: {1}".format(path, err_msg)
51
+                modules.commons.log(modules.commons.LOG_ERROR, "Failed to get the kickstart file at {0}, error msg: {1}".format(path, err_msg))
52 52
                 print "Failed to get the kickstart file at {0}, retry in a second".format(path)
53 53
                 time.sleep(1)
54 54
 
... ...
@@ -5,6 +5,18 @@ import re
5 5
 PRE_INSTALL = "pre-install"
6 6
 POST_INSTALL = "post-install"
7 7
 
8
+LOG_LEVEL_DESC = ["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"]
9
+LOG_FILE_NAME  = "/var/log/installer.log"
10
+SIGNATURE   = "Photon echo"
11
+LOG_EMERG   = 0
12
+LOG_ALERT   = 1
13
+LOG_CRIT    = 2
14
+LOG_ERROR   = 3
15
+LOG_WARNING = 4
16
+LOG_NOTICE  = 5
17
+LOG_INFO    = 6
18
+LOG_DEBUG   = 7
19
+
8 20
 def partition_disk(disk, docker_partition_size = None, swap_partition_size = None):
9 21
     partitions_data = {}
10 22
     partitions_data['disk'] = disk
... ...
@@ -78,4 +90,16 @@ def replace_string_in_file(filename,  search_string,  replace_string):
78 78
 
79 79
     with open(filename, "w") as destination:
80 80
         for line in lines:
81
-            destination.write(re.sub(search_string,  replace_string,  line))
82 81
\ No newline at end of file
82
+            destination.write(re.sub(search_string,  replace_string,  line))
83
+
84
+def log(type, message):
85
+    command = 'systemd-cat echo \"<{}> {} : {}\"'.format(type, LOG_LEVEL_DESC[type], message)
86
+    process = subprocess.Popen([command], shell=True)
87
+    retval = process.wait()
88
+    return retval
89
+
90
+def dump(type, filename):
91
+    command = "journalctl -p {0} | grep --line-buffered \"{1}\" > {2}".format(LOG_LEVEL_DESC[type], SIGNATURE, filename)
92
+    process = subprocess.Popen([command], shell=True)
93
+    retval = process.wait()
94
+    return retval
83 95
\ No newline at end of file