]> git.ipfire.org Git - nitsi.git/blob - machine.py
Move recipe class into an own file
[nitsi.git] / machine.py
1 #!/usr/bin/python3
2
3 import xml.etree.ElementTree as ET
4
5 from disk import disk
6
7 from serial_connection import serial_connection
8
9 from test import libvirt_con
10
11 import os
12 import libvirt
13
14
15 class machine():
16 def __init__(self, vm_xml_file, snapshot_xml_file, image, root_uid, username, password):
17 self.log = log(4)
18 self.con = libvirt_con("qemu:///system")
19 try:
20 with open(vm_xml_file) as fobj:
21 self.vm_xml = fobj.read()
22 except FileNotFoundError as error:
23 self.log.error("No such file: {}".format(vm_xml_file))
24
25 try:
26 with open(snapshot_xml_file) as fobj:
27 self.snapshot_xml = fobj.read()
28 except FileNotFoundError as error:
29 self.log.error("No such file: {}".format(snapshot_xml_file))
30
31 self.image = image
32
33 if not os.path.isfile(self.image):
34 self.log.error("No such file: {}".format(self.image))
35
36 self.root_uid = root_uid
37 self.disk = disk(image)
38
39 self.username = username
40 self.password = password
41
42 def define(self):
43 self.dom = self.con.con.defineXML(self.vm_xml)
44 if self.dom == None:
45 self.log.error("Could not define VM")
46 raise BaseException
47
48 def start(self):
49 if self.dom.create() < 0:
50 self.log.error("Could not start VM")
51 raise BaseException
52
53 def shutdown(self):
54 if self.is_running():
55 if self.dom.shutdown() < 0:
56 self.log.error("Could not shutdown VM")
57 raise BaseException
58 else:
59 self.log.error("Domain is not running")
60
61 def undefine(self):
62 self.dom.undefine()
63
64 def create_snapshot(self):
65
66 self.snapshot = self.dom.snapshotCreateXML(self.snapshot_xml)
67
68 if not self.snapshot:
69 self.log.error("Could not create snapshot")
70 raise BaseException
71
72 def revert_snapshot(self):
73 self.dom.revertToSnapshot(self.snapshot)
74 self.snapshot.delete()
75
76 def is_running(self):
77
78 state, reason = self.dom.state()
79
80 if state == libvirt.VIR_DOMAIN_RUNNING:
81 return True
82 else:
83 return False
84
85 def get_serial_device(self):
86
87 if not self.is_running():
88 raise BaseException
89
90 xml_root = ET.fromstring(self.dom.XMLDesc(0))
91
92 elem = xml_root.find("./devices/serial/source")
93 return elem.get("path")
94
95 def check_is_booted_up(self):
96 serial_con = serial_connection(self.get_serial_device())
97
98 serial_con.write("\n")
99 # This will block till the domain is booted up
100 serial_con.read(1)
101
102 #serial_con.close()
103
104 def login(self):
105 try:
106 self.serial_con = serial_connection(self.get_serial_device(), username=self.username)
107 self.serial_con.login(self.password)
108 except BaseException as e:
109 self.log.error("Could not connect to the domain via serial console")
110
111 def cmd(self, cmd):
112 return self.serial_con.command(cmd)
113
114 def copy_in(self, fr, to):
115 try:
116 self.disk.mount(self.root_uid, "/")
117 self.disk.copy_in(fr, to)
118 except BaseException as e:
119 self.log.error(e)
120 finally:
121 self.disk.umount("/")
122 self.disk.close()