From: Jonatan Schlag Date: Sun, 29 Apr 2018 09:06:34 +0000 (+0200) Subject: We now use the logging module X-Git-Url: http://git.ipfire.org/?p=nitsi.git;a=commitdiff_plain;h=1ed8ca9f2ddd8d9cd0cf1a47ed1ebb4376a0db53 We now use the logging module Signed-off-by: Jonatan Schlag --- diff --git a/disk.py b/disk.py index d93328c..44f9054 100755 --- a/disk.py +++ b/disk.py @@ -6,29 +6,42 @@ import tempfile import tarfile import os +import logging + +logger = logging.getLogger("nitsi.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.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.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: + self.log.debug("Adding {} to be copied into the image".format(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.log.debug("Unmounting the image") self.con.umount_opts(path) def close(self): + self.log.debug("Flush the image and closing the connection") self.con.shutdown() self.con.close() diff --git a/machine.py b/machine.py index 9fbdf3f..867a8a0 100644 --- a/machine.py +++ b/machine.py @@ -9,10 +9,13 @@ from serial_connection import serial_connection 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): - 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: diff --git a/network.py b/network.py index 0e76274..d9d0924 100644 --- a/network.py +++ b/network.py @@ -1,9 +1,14 @@ #!/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): - 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: diff --git a/nitsi b/nitsi old mode 100644 new mode 100755 index 3c88b6a..d107a34 --- a/nitsi +++ b/nitsi @@ -1,6 +1,23 @@ -#!/usr/bin/python +#!/usr/bin/python3 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 diff --git a/recipe.py b/recipe.py index 07617f7..3a9b009 100644 --- a/recipe.py +++ b/recipe.py @@ -2,6 +2,10 @@ import os +import logging + +logger = logging.getLogger("nitsi.recipe") + 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=[]): - self.log = log(4) 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 diff --git a/serial_connection.py b/serial_connection.py index 4dc524e..511d349 100644 --- a/serial_connection.py +++ b/serial_connection.py @@ -2,16 +2,20 @@ import serial import re +import os 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 - self.log = log(1) + self.log = logger.getChild(os.path.basename(device)) self.con = serial.Serial(device) def read(self, size=1): diff --git a/test.py b/test.py index c2d62ce..9ea5798 100755 --- a/test.py +++ b/test.py @@ -7,26 +7,20 @@ import os 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): - self.log = log(4) try: self.path = os.path.abspath(path) + self.log = logger.getChild(os.path.basename(self.path)) except BaseException as e: - self.log.error("Could not get absolute path") + logger.error("Could not get absolute 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 {}".format(name)) self.virtual_machines[name].login() def load_recipe(self): diff --git a/virtual_environ.py b/virtual_environ.py index d857072..16f7d21 100644 --- a/virtual_environ.py +++ b/virtual_environ.py @@ -7,12 +7,15 @@ from network import network 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): - 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: