]> git.ipfire.org Git - nitsi.git/commitdiff
We now use the logging module
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 29 Apr 2018 09:06:34 +0000 (11:06 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 29 Apr 2018 09:06:34 +0000 (11:06 +0200)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
disk.py
machine.py
network.py
nitsi [changed mode: 0644->0755]
recipe.py
serial_connection.py
test.py
virtual_environ.py

diff --git a/disk.py b/disk.py
index d93328c2515270fcaf185f719ec32a342a9cd71b..44f9054a12a0e57d296070573f00dc68205edc15 100755 (executable)
--- a/disk.py
+++ b/disk.py
@@ -6,29 +6,42 @@ import tempfile
 import tarfile
 import os
 
 import tarfile
 import os
 
+import logging
+
+logger = logging.getLogger("nitsi.disk")
+
 
 class disk():
     def __init__(self, disk):
 
 class disk():
     def __init__(self, disk):
+        self.log = logger.getChild(os.path.basename(disk))
+        self.log.debug("Initiated a disk class for {}".format(disk))
         self.con = guestfs.GuestFS(python_return_dict=True)
         self.con.add_drive_opts(disk, format="qcow2")
 
     def mount(self, uuid, path):
         self.con = guestfs.GuestFS(python_return_dict=True)
         self.con.add_drive_opts(disk, format="qcow2")
 
     def mount(self, uuid, path):
+        self.log.debug("Trying to mount the partion with uuid: {} under {}".format(uuid, path))
         self.con.launch()
         part = self.con.findfs_uuid(uuid)
         self.con.mount(part, path)
 
     def copy_in(self, fr, to):
         self.con.launch()
         part = self.con.findfs_uuid(uuid)
         self.con.mount(part, path)
 
     def copy_in(self, fr, to):
+        self.log.debug("Going to copy some files into the image.")
         tmp = tempfile.mkstemp()
         tmp = tmp[1] + ".tar"
         with tarfile.open(tmp, "w") as tar:
             for file in fr:
         tmp = tempfile.mkstemp()
         tmp = tmp[1] + ".tar"
         with tarfile.open(tmp, "w") as tar:
             for file in fr:
+                self.log.debug("Adding {} to be copied into the image".format(file))
                 tar.add(file, arcname=os.path.basename(file))
                 tar.add(file, arcname=os.path.basename(file))
+
+        self.log.debug("Going to copy the files into the image")
         self.con.tar_in_opts(tmp, to)
 
     def umount(self, path):
         self.con.tar_in_opts(tmp, to)
 
     def umount(self, path):
+        self.log.debug("Unmounting the image")
         self.con.umount_opts(path)
 
     def close(self):
         self.con.umount_opts(path)
 
     def close(self):
+        self.log.debug("Flush the image and closing the connection")
         self.con.shutdown()
         self.con.close()
 
         self.con.shutdown()
         self.con.close()
 
index 9fbdf3fb67d05ecd22328235ec3009a22047b00a..867a8a01f791bbbc5bdff5b8285239fc6e041d68 100644 (file)
@@ -9,10 +9,13 @@ from serial_connection import serial_connection
 import os
 import libvirt
 
 import os
 import libvirt
 
+import logging
+
+logger = logging.getLogger("nitsi.machine")
 
 class machine():
     def __init__(self, libvirt_con, vm_xml_file, snapshot_xml_file, image, root_uid, username, password):
 
 class machine():
     def __init__(self, libvirt_con, vm_xml_file, snapshot_xml_file, image, root_uid, username, password):
-        self.log = log(4)
+        self.log = logger.getChild(os.path.basename(vm_xml_file))
         self.con = libvirt_con
         try:
             with open(vm_xml_file) as fobj:
         self.con = libvirt_con
         try:
             with open(vm_xml_file) as fobj:
index 0e76274a55d1c5884e25fa789ecd23ca40f96f91..d9d09244a1d70871248412c3f1dc0b17467dd93f 100644 (file)
@@ -1,9 +1,14 @@
 #!/usr/bin/python3
 
 #!/usr/bin/python3
 
+import logging
+import os
+
+logger = logging.getLogger("nitsi.network")
+
 # # A class which define and undefine a virtual network based on an xml file
 class network():
     def __init__(self, libvirt_con, network_xml_file):
 # # A class which define and undefine a virtual network based on an xml file
 class network():
     def __init__(self, libvirt_con, network_xml_file):
-        self.log = log(4)
+        self.log = logger.getChild(os.path.basename(network_xml_file))
         self.con = libvirt_con
         try:
             with open(network_xml_file) as fobj:
         self.con = libvirt_con
         try:
             with open(network_xml_file) as fobj:
diff --git a/nitsi b/nitsi
old mode 100644 (file)
new mode 100755 (executable)
index 3c88b6a..d107a34
--- a/nitsi
+++ b/nitsi
@@ -1,6 +1,23 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 from test import test
 
 from test import test
+import logging
+
+logger = logging.getLogger("nitsi")
+logger.setLevel(logging.DEBUG)
+# create file handler which logs even debug messages
+fh = logging.FileHandler('nitsi.log')
+fh.setLevel(logging.DEBUG)
+# create console handler with a higher log level
+ch = logging.StreamHandler()
+ch.setLevel(logging.DEBUG)
+
+formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+fh.setFormatter(formatter)
+ch.setFormatter(formatter)
+# add the handlers to the logger
+logger.addHandler(fh)
+logger.addHandler(ch)
 
 if __name__ == "__main__":
     import argparse
 
 if __name__ == "__main__":
     import argparse
index 07617f7c66578e9cf8a3797abaaa017a7eaaa2b9..3a9b009deba6e8990cb8b384dd22f579c4d19292 100644 (file)
--- a/recipe.py
+++ b/recipe.py
@@ -2,6 +2,10 @@
 
 import os
 
 
 import os
 
+import logging
+
+logger = logging.getLogger("nitsi.recipe")
+
 
 
 class RecipeExeption(Exception):
 
 
 class RecipeExeption(Exception):
@@ -13,8 +17,8 @@ class RecipeExeption(Exception):
 # and return tuples with the ( host, command ) structure
 class recipe():
     def __init__(self, path, circle=[]):
 # and return tuples with the ( host, command ) structure
 class recipe():
     def __init__(self, path, circle=[]):
-        self.log = log(4)
         self.recipe_file = path
         self.recipe_file = path
+        self.log = logger.getChild(os.path.basename(self.recipe_file))
         self.path = os.path.dirname(self.recipe_file)
         self.log.debug("Path of recipe is: {}".format(self.recipe_file))
         self._recipe = None
         self.path = os.path.dirname(self.recipe_file)
         self.log.debug("Path of recipe is: {}".format(self.recipe_file))
         self._recipe = None
index 4dc524e557330eed13375d50e2dde0e9596a87e6..511d34927aeff9e7702a0f408ceed5b1454356a5 100644 (file)
@@ -2,16 +2,20 @@
 import serial
 
 import re
 import serial
 
 import re
+import os
 
 from time import sleep
 import sys
 
 from time import sleep
 import sys
+import logging
+
+logger = logging.getLogger("nitsi.serial")
 
 class serial_connection():
     def __init__(self, device, username=None):
         self.buffer = b""
         self.back_at_prompt_pattern =  None
         self.username = username
 
 class serial_connection():
     def __init__(self, device, username=None):
         self.buffer = b""
         self.back_at_prompt_pattern =  None
         self.username = username
-        self.log = log(1)
+        self.log = logger.getChild(os.path.basename(device))
         self.con = serial.Serial(device)
 
     def read(self, size=1):
         self.con = serial.Serial(device)
 
     def read(self, size=1):
diff --git a/test.py b/test.py
index c2d62ce9412efd9c99e8b60f8bd337fed0c9853a..9ea57981e5b4a47acad742d593c40fe1e965e44f 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -7,26 +7,20 @@ import os
 
 import configparser
 
 
 import configparser
 
-from disk import disk
+from virtual_environ import virtual_environ
+from recipe import recipe
 
 
-class log():
-    def __init__(self, log_level):
-        self.log_level = log_level
+import logging
 
 
-    def debug(self, string):
-        if self.log_level >= 4:
-            print("DEBUG: {}".format(string))
-
-    def error(self, string):
-        print("ERROR: {}".format(string))
+logger = logging.getLogger("nitsi.test")
 
 class test():
     def __init__(self, path):
 
 class test():
     def __init__(self, path):
-        self.log = log(4)
         try:
             self.path = os.path.abspath(path)
         try:
             self.path = os.path.abspath(path)
+            self.log = logger.getChild(os.path.basename(self.path))
         except BaseException as e:
         except BaseException as e:
-            self.log.error("Could not get absolute path")
+            logger.error("Could not get absolute path")
 
         self.log.debug(self.path)
 
 
         self.log.debug(self.path)
 
@@ -79,6 +73,7 @@ class test():
 
         self.log.debug("Try to login on all machines")
         for name in self.virtual_environ.machine_names:
 
         self.log.debug("Try to login on all machines")
         for name in self.virtual_environ.machine_names:
+            self.log.debug("Try to login on {}".format(name))
             self.virtual_machines[name].login()
 
     def load_recipe(self):
             self.virtual_machines[name].login()
 
     def load_recipe(self):
index d8570729211d10fb0ca8b02e3c15d7becbdd7313..16f7d21b50fc11024b17e3b39db55bf0c43abdeb 100644 (file)
@@ -7,12 +7,15 @@ from network import network
 import os
 import configparser
 import libvirt
 import os
 import configparser
 import libvirt
+import logging
+
+logger = logging.getLogger("nitsi.virtual_environ")
 
 # Should return all vms and networks in a list
 # and should provide the path to the necessary xml files
 class virtual_environ():
     def __init__(self, path):
 
 # Should return all vms and networks in a list
 # and should provide the path to the necessary xml files
 class virtual_environ():
     def __init__(self, path):
-        self.log = log(4)
+        self.log = logger.getChild(os.path.basename(os.path.abspath(path)))
         try:
             self.path = os.path.abspath(path)
         except BaseException as e:
         try:
             self.path = os.path.abspath(path)
         except BaseException as e: