-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVMManager.py
More file actions
257 lines (191 loc) · 7.18 KB
/
Copy pathVMManager.py
File metadata and controls
257 lines (191 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import libvirt
import sys
import subprocess
import glob
import os
class VMManager(object):
# The Path of the new Images
pathOfServerDirectory = "" # username@remote_host:/path/to/dir
# The Path of the read only Images
pathOfCleanImages = "/vlab/domains"
# The Path of the Images wich are currently in use
pathOfWorkingImages = "/vlab/working"
# The Path of the script that runs on sync
pathOfScript = "./vlab/vlab-setup/prepare_domain_xml.sh"
def __init__(self):
# start the connection to Libvirt
self.path ="qemu:///system"
self.conn = None
try:
self.conn = libvirt.open(self.path)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
#catch prints libvirt
#libvirt.registerErrorHandler(f=VMManager.libvirt_callback, ctx=None)
#LIBVIRT
def restartConnection(self):
self.conn.close()
try:
conn = libvirt.open(self.path)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
def getDomains(self):
domains = self.conn.listAllDomains(0)
nameslist = []
for domain in domains:
nameslist.append(domain.name())
return nameslist
def getDomainActive(self,domName):
dom = None
try:
dom = self.conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
flag = dom.isActive()
return flag # return the state of the VM 1 means active/running 0 means inactive/stopped
def getFirstActiveDomain(self):
domains = self.getDomains()
for dom in domains:
if self.getDomainActive(dom) == 1:
return dom
return None
def startDomain(self,domName):
dom = None
try:
dom = self.conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
dom.create()
def restartDomain(self,domName):
dom = None
try:
dom = self.conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
dom.reboot()
def stopDomain(self,domName):
dom = None
try:
dom = self.conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
dom.shutdown()
def forceStopDomain(self,domName):
dom = None
try:
dom = self.conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
dom.destroy()
def defineDomain(self,xml):
try:
self.conn.defineXMLFlags(xml, 0)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
def undefineDomain(self,domName):
dom = None
try:
dom = self.conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
raise
dom.undefineFlags(libvirt.VIR_DOMAIN_UNDEFINE_NVRAM)
# FILE MANAGMENT
def checkimage(self,vmname):
return os.path.isfile(VMManager.pathOfWorkingImages+"/"+vmname+".qcow2")
def resetImg(self, vmname):
#creatingWorkingdirectory if not exists
try:
subprocess.check_output(["mkdir","-p", VMManager.pathOfWorkingImages], stderr=subprocess.STDOUT)
except Exception as e:
pass
#search for path of old file
for path in glob.glob(VMManager.pathOfWorkingImages+"/**/"+vmname+".qcow2",recursive=True):
# delte old file
subprocess.check_output(["rm", path], stderr=subprocess.STDOUT)
#search for path of new file
newFilePath = glob.glob(VMManager.pathOfCleanImages+"/**/"+vmname+".qcow2")[0]
# create new subimg
subprocess.check_output(
["qemu-img", "create", "-f", "qcow2", "-F", "qcow2", "-b", newFilePath,
VMManager.pathOfWorkingImages + "/" + vmname + ".qcow2"], stderr=subprocess.STDOUT)
def syncImg(self,vmname):
# sync all files of the server into the local read only directory
if VMManager.pathOfServerDirectory:
try:
subprocess.check_output(
["rsync", "-rlpt", VMManager.pathOfServerDirectory + "/"+vmname, VMManager.pathOfCleanImages+"/"+vmname],
stderr=subprocess.STDOUT)
except Exception as e:
pass
else:
pass
def syncAllImgs(self):
# sync all files of the server into the local read only directory
if VMManager.pathOfServerDirectory:
try:
subprocess.check_output(["rsync", "-rlpt", VMManager.pathOfServerDirectory + "/", VMManager.pathOfCleanImages], stderr=subprocess.STDOUT)
except Exception as e:
pass
else:
pass
def redefineImage(self,vmname):
#undefine image
self.undefineDomain(vmname)
#remove working image
try:
subprocess.check_output(["rm", VMManager.pathOfWorkingImages+"/"+vmname+".qcow2"], stderr=subprocess.STDOUT)
except Exception as e:
pass
# run shellscript to generate xml
try:
subprocess.check_output([VMManager.pathOfScript, VMManager.pathOfCleanImages+"/"+vmname], stderr=subprocess.STDOUT)
except Exception as e:
pass
# load file
xml_file = open(VMManager.pathOfCleanImages+"/"+vmname+"/"+vmname+".xml", "r")
# read whole file to a string
xmlList.append(xml_file.read())
# close file
xml_file.close()
# redifining VM from the XML File
self.redefineImage(vmname)
def redefineAllImages(self):
#undefine all domains
for domName in self.getDomains():
self.undefineDomain(domName)
#delete all working images
try:
subprocess.check_output(["rm",VMManager.pathOfWorkingImages], stderr=subprocess.STDOUT)
except Exception as e:
pass
# run shellscript to generate xml
for pathOfFile in glob.glob(VMManager.pathOfCleanImages + "/**/*.xml.TEMPLATE", recursive=True):
pathOfDir = os.path.dirname(pathOfFile)
try:
subprocess.check_output([VMManager.pathOfScript, pathOfDir], stderr=subprocess.STDOUT)
except Exception as e:
pass
xmlList = []
# getting all xml files
for xmlpath in glob.glob(VMManager.pathOfCleanImages + "/**/*.xml", recursive=True):
xml_file = open(xmlpath, "r")
# read whole file to a string
xmlList.append(xml_file.read())
# close file
xml_file.close()
# redifining all VMS from the XML Files
for xml in xmlList:
if xml:
self.defineDomain(xml)
# Removing Libvirt Console out
#def libvirt_callback(userdata, err):
# pass