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