-class log():
- def __init__(self, log_level):
- self.log_level = log_level
-
- def debug(self, string):
- if self.log_level >= 4:
- print("DEBUG: {}".format(string))
-
- def error(self, string):
- print("ERROR: {}".format(string))
-
-class libvirt_con():
- def __init__(self, uri):
- self.log = log(4)
- self.uri = uri
- self.connection = None
-
- def get_domain_from_name(self, name):
- dom = self.con.lookupByName(name)
-
- if dom == None:
- raise BaseException
- return dom
-
- @property
- def con(self):
- if self.connection == None:
- try:
- self.connection = libvirt.open(self.uri)
- except BaseException as error:
- self.log.error("Could not connect to: {}".format(self.uri))
-
- self.log.debug("Connected to: {}".format(self.uri))
- return self.connection
-
- return self.connection
-
-
-class vm():
- def __init__(self, vm_xml_file, snapshot_xml_file, image, root_uid):
- self.log = log(4)
- self.con = libvirt_con("qemu:///system")
- try:
- with open(vm_xml_file) as fobj:
- self.vm_xml = fobj.read()
- except FileNotFoundError as error:
- self.log.error("No such file: {}".format(vm_xml_file))
-
- try:
- with open(snapshot_xml_file) as fobj:
- self.snapshot_xml = fobj.read()
- except FileNotFoundError as error:
- self.log.error("No such file: {}".format(snapshot_xml_file))
-
- self.image = image
-
- if not os.path.isfile(self.image):
- self.log.error("No such file: {}".format(self.image))
-
- self.root_uid = root_uid
-
- def define(self):
- self.dom = self.con.con.defineXML(self.vm_xml)
- if self.dom == None:
- self.log.error("Could not define VM")
- raise BaseException
-
- def start(self):
- if self.dom.create() < 0:
- self.log.error("Could not start VM")
- raise BaseException
-
- def shutdown(self):
- if self.is_running():
- if self.dom.shutdown() < 0:
- self.log.error("Could not shutdown VM")
- raise BaseException
- else:
- self.log.error("Domain is not running")
-
- def undefine(self):
- self.dom.undefine()
-
- def create_snapshot(self):
-
- self.snapshot = self.dom.snapshotCreateXML(self.snapshot_xml)
-
- if not self.snapshot:
- self.log.error("Could not create snapshot")
- raise BaseException
-
- def revert_snapshot(self):
- self.dom.revertToSnapshot(self.snapshot)
- self.snapshot.delete()
-
- def is_running(self):
-
- state, reason = self.dom.state()
-
- if state == libvirt.VIR_DOMAIN_RUNNING:
- return True
- else:
- return False
-
- def get_serial_device(self):
-
- if not self.is_running():
- raise BaseException
-
- xml_root = ET.fromstring(self.dom.XMLDesc(0))
-
- elem = xml_root.find("./devices/serial/source")
- return elem.get("path")
-
- def check_is_booted_up(self):
- serial_con = connection(self.get_serial_device())
-
- serial_con.write("\n")
- # This will block till the domain is booted up
- serial_con.read(1)
-
- #serial_con.close()
-
- def login(self, username, password):
- try:
- self.serial_con = connection(self.get_serial_device(), username="root")
- self.serial_con.login("25814@root")
- except BaseException as e:
- self.log.error("Could not connect to the domain via serial console")
-
- def cmd(self, cmd):
- return self.serial_con.command(cmd)
-
-
-class connection():
- def __init__(self, device, username=None):
- self.buffer = b""
- self.back_at_prompt_pattern = None
- self.username = username
- self.log = log(1)
- self.con = serial.Serial(device)
-
- def read(self, size=1):
- if len(self.buffer) >= size:
- # throw away first size bytes in buffer
- data = self.buffer[:size]
- # Set the buffer to the non used bytes
- self.buffer = self.buffer[size:]
- return data
- else:
- data = self.buffer
- # Set the size to the value we have to read now
- size = size - len(self.buffer)
- # Set the buffer empty
- self.buffer = b""
- return data + self.con.read(size)
-
- def peek(self, size=1):
- if len(self.buffer) <= size:
- self.buffer += self.con.read(size=size - len(self.buffer))
-
- return self.buffer[:size]
-
- def readline(self):
- self.log.debug(self.buffer)
- self.buffer = self.buffer + self.con.read(self.con.in_waiting)
- if b"\n" in self.buffer:
- size = self.buffer.index(b"\n") + 1
- self.log.debug("We have a whole line in the buffer")
- self.log.debug(self.buffer)
- self.log.debug("We split at {}".format(size))
- data = self.buffer[:size]
- self.buffer = self.buffer[size:]
- self.log.debug(data)
- self.log.debug(self.buffer)
- return data
-
- data = self.buffer
- self.buffer = b""
- return data + self.con.readline()
-
- def back_at_prompt(self):
- data = self.peek()
- if not data == b"[":
- return False
-
- # We need to use self.in_waiting because with self.con.in_waiting we get
- # not the complete string
- size = len(self.buffer) + self.in_waiting
- data = self.peek(size)
-
-
- if self.back_at_prompt_pattern == None:
- #self.back_at_prompt_pattern = r"^\[{}@.+\]#".format(self.username)
- self.back_at_prompt_pattern = re.compile(r"^\[{}@.+\]#".format(self.username), re.MULTILINE)
-
- if self.back_at_prompt_pattern.search(data.decode()):
- return True
- else:
- return False
-
- def log_console_line(self, line):
- self.log.debug("Get in function log_console_line()")
- sys.stdout.write(line)
-
- @property
- def in_waiting(self):
- in_waiting_before = 0
- sleep(0.5)
-
- while in_waiting_before != self.con.in_waiting:
- in_waiting_before = self.con.in_waiting
- sleep(0.5)
-
- return self.con.in_waiting
-
-
- def readline2(self, pattern=None):
- string = ""
- string2 = b""
- if pattern:
- pattern = re.compile(pattern)
-
- while 1:
- char = self.con.read(1)
- string = string + char.decode("utf-8")
- string2 = string2 + char
- #print(char)
- print(char.decode("utf-8"), end="")
-
- #print(string2)
- if pattern and pattern.match(string):
- #print("get here1")
- #print(string2)
- return {"string" : string, "return-code" : 1}
-
- if char == b"\n":
- #print(char)
- #print(string2)
- #print("get here2")
- return {"return-code" : 0}
-
- def check_logged_in(self, username):
- pattern = "^\[" + username + "@.+\]#"
- data = self.readline(pattern=pattern)
- if data["return-code"] == 1:
- print("We are logged in")
- return True
- else:
- print("We are not logged in")
- return False
-
- def login(self, password):
- if self.username == None:
- self.log.error("Username cannot be blank")
- return False
-
- # Hit enter to see what we get
- self.con.write(b'\n')
- # We get two new lines \r\n ?
- data = self.readline()
- self.log_console_line(data.decode())