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()
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:
#!/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:
-#!/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
import os
+import logging
+
+logger = logging.getLogger("nitsi.recipe")
+
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
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):
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)
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):
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: