Initial checkin
[nitsi.git] / test.py
CommitLineData
5e7f6db7
JS
1#!/usr/bin/python3
2
3import serial
4
5import re
6from time import sleep
7import sys
8
9import libvirt
10
11import xml.etree.ElementTree as ET
12
13import inspect
14import os
15
16import configparser
17
18class log():
19 def __init__(self, log_level):
20 self.log_level = log_level
21
22 def debug(self, string):
23 if self.log_level >= 4:
24 print("DEBUG: {}".format(string))
25
26 def error(self, string):
27 print("ERROR: {}".format(string))
28
29class libvirt_con():
30 def __init__(self, uri):
31 self.log = log(4)
32 self.uri = uri
33 self.connection = None
34
35 def get_domain_from_name(self, name):
36 dom = self.con.lookupByName(name)
37
38 if dom == None:
39 raise BaseException
40 return dom
41
42 @property
43 def con(self):
44 if self.connection == None:
45 try:
46 self.connection = libvirt.open(self.uri)
47 except BaseException as error:
48 self.log.error("Could not connect to: {}".format(self.uri))
49
50 self.log.debug("Connected to: {}".format(self.uri))
51 return self.connection
52
53 return self.connection
54
55
56class vm():
57 def __init__(self, vm_xml_file, snapshot_xml_file, image, root_uid):
58 self.log = log(4)
59 self.con = libvirt_con("qemu:///system")
60 try:
61 with open(vm_xml_file) as fobj:
62 self.vm_xml = fobj.read()
63 except FileNotFoundError as error:
64 self.log.error("No such file: {}".format(vm_xml_file))
65
66 try:
67 with open(snapshot_xml_file) as fobj:
68 self.snapshot_xml = fobj.read()
69 except FileNotFoundError as error:
70 self.log.error("No such file: {}".format(snapshot_xml_file))
71
72 self.image = image
73
74 if not os.path.isfile(self.image):
75 self.log.error("No such file: {}".format(self.settings_file))
76
77 self.root_uid = root_uid
78
79 def define(self):
80 self.dom = self.con.con.defineXML(self.vm_xml)
81 if self.dom == None:
82 self.log.error("Could not define VM")
83 raise BaseException
84
85 def start(self):
86 if self.dom.create() < 0:
87 self.log.error("Could not start VM")
88 raise BaseException
89
90 def shutdown(self):
91 if self.is_running():
92 if self.dom.shutdown() < 0:
93 self.log.error("Could not shutdown VM")
94 raise BaseException
95 else:
96 self.log.error("Domain is not running")
97
98 def undefine(self):
99 self.dom.undefine()
100
101 def create_snapshot(self):
102
103 self.snapshot = self.dom.snapshotCreateXML(self.snapshot_xml)
104
105 if not self.snapshot:
106 self.log.error("Could not create snapshot")
107 raise BaseException
108
109 def revert_snapshot(self):
110 print(inspect.getmembers(self.dom, predicate=inspect.ismethod))
111 self.dom.revertToSnapshot(self.snapshot)
112 #self.dom.SnapshotDelete(self.snapshot)
113
114 def is_running(self):
115
116 state, reason = self.dom.state()
117
118 if state == libvirt.VIR_DOMAIN_RUNNING:
119 return True
120 else:
121 return False
122
123 def get_serial_device(self):
124
125 if not self.is_running():
126 raise BaseException
127
128 xml_root = ET.fromstring(self.dom.XMLDesc(0))
129
130 elem = xml_root.find("./devices/serial/source")
131 return elem.get("path")
132
133 def check_is_booted_up(self):
134 serial_con = connection(self.get_serial_device())
135
136 serial_con.write("\n")
137 # This will block till the domain is booted up
138 serial_con.read(1)
139
140 #serial_con.close()
141
142 def login(self, username, password):
143 try:
144 self.serial_con = connection(self.get_serial_device(), username="root")
145 self.serial_con.login("25814@root")
146 except BaseException as e:
147 self.log.error("Could not connect to the domain via serial console")
148
149 def cmd(self, cmd):
150 return self.serial_con.command(cmd)
151
152
153
154
155
156# try:
157# dom = conn.lookupByUUIDString(uuid)
158# except:
159# flash(u"Failed to get the domain object", 'alert-danger')
160# print('Failed to get the domain object', file=sys.stderr)
161# conn.close()
162# return redirect("/vm")
163
164# domname = dom.name()
165# if action == "start":
166# try:
167# dom.create()
168# except:
169# flash(u"Can not boot guest domain.", 'alert-danger')
170# print('Can not boot guest domain.', file=sys.stderr)
171# conn.close()
172# return redirect("/vm")
173
174# flash(u"Sucessfully started Domain \"{}\"".format(domname), 'alert-info')
175# conn.close()
176# return redirect("/vm")
177
178# elif action == "shutdown":
179# try:
180# dom.shutdown()
181# except:
182# flash(u"Can not shutdown guest domain.", 'alert-danger')
183# print('Can not shutdown guest domain.', file=sys.stderr)
184# conn.close()
185# return redirect("/vm")
186
187# flash(u"Sucessfully shutdowned Domain \"{}\"".format(domname), 'alert-info')
188# conn.close()
189# return redirect("/vm")
190
191# elif action == "destroy":
192# try:
193# dom.destroy()
194# except:
195# flash(u"Can not destroy guest domain.", 'alert-danger')
196# print('Can not destroy guest domain.', file=sys.stderr)
197# conn.close()
198# return redirect("/vm")
199
200# flash(u"Sucessfully destroyed Domain \"{}\"".format(domname), 'alert-info')
201# conn.close()
202# return redirect("/vm")
203
204# elif action == "pause":
205# try:
206# dom.suspend()
207# except:
208# flash(u"Can not pause guest domain.", 'alert-danger')
209# print('Can not pause guest domain.', file=sys.stderr)
210# conn.close()
211# return redirect("/vm")
212
213# flash(u"Sucessfully paused Domain \"{}\"".format(domname), 'alert-info')
214# conn.close()
215# return redirect("/vm")
216
217# elif action == "resume":
218# try:
219# dom.resume()
220# except:
221# flash(u"Can not eesume guest domain.:", 'alert-danger')
222# print('Can not resume guest domain.', file=sys.stderr)
223# conn.close()
224# return redirect("/vm")
225
226# flash(u"Sucessfully resumed Domain \"{}\"".format(domname), 'alert-info')
227# conn.close()
228# return redirect("/vm")
229
230# else:
231# flash(u"No such action: \"{}\"".format(action), 'alert-warning')
232# conn.close()
233# return redirect("/vm")
234
235
236# @vms.route('/vm')
237# @login_required
238# def vm_overview():
239# import sys
240# import libvirt
241# conn = libvirt.open('qemu:///system')
242# doms = conn.listAllDomains(0)
243# domains = []
244
245# if len(doms) != 0:
246# for dom in doms:
247# domain = {}
248# domain.setdefault("name", dom.name())
249# domain.setdefault("uuid", dom.UUIDString())
250# state, reason = dom.state()
251# domain.setdefault("state", dom_state(state))
252# domains.append(domain)
253
254# conn.close()
255
256# return render_template("virtberry_vm_basic-vm.html", domains=domains)
257
258
259
260class connection():
261 def __init__(self, device, username=None):
262 self.buffer = b""
263 self.back_at_prompt_pattern = None
264 self.username = username
265 self.log = log(1)
266 self.con = serial.Serial(device)
267 # # Just press enter one time to see what we get
268 # self.con.write(b'\n')
269 # # We get two new lines \r\n ?
270 # data = self.readline()
271 # self.log_console_line(data.decode())
272
273
274 # if not self.back_at_prompt():
275 # self.log.debug("We need to login")
276 # if not self.login(password):
277 # self.log.error("Login failed")
278 # return False
279 # else:
280 # self.log.debug("We are logged in")
281
282
283 ''' in_waiting_before = 0
284 sleep(1)
285
286 while in_waiting_before != self.con.in_waiting:
287 in_waiting_before = self.con.in_waiting
288 sleep(0.5)
289
290 print(self.con.in_waiting)
291 data = self.con.read(self.con.in_waiting)
292 print(data)
293 print(data.decode(),end='')
294
295 string = 'root\n'
296 self.con.write(string.encode())
297 self.con.flush() '''
298
299 ''' in_waiting_before = 0
300 sleep(1)
301
302 while in_waiting_before != self.con.in_waiting:
303 in_waiting_before = self.con.in_waiting
304 sleep(0.5)
305
306 print(self.con.in_waiting)
307 data = self.con.read(self.con.in_waiting)
308 print(data)
309 print(data.decode(), end='')
310
311 string = '25814@root\n'
312 self.con.write(string.encode())
313 self.con.flush()
314
315 in_waiting_before = 0
316 sleep(1)
317
318 while in_waiting_before != self.con.in_waiting:
319 in_waiting_before = self.con.in_waiting
320 sleep(0.5)
321
322 print(self.con.in_waiting)
323 data = self.con.read(self.con.in_waiting)
324 print(data)
325 print(data.decode(), end='') '''
326
327 # check if we already logged in
328 # If we we get something like [root@localhost ~]#
329 #self.readline()
330
331 # if not self.check_logged_in(username):
332 #print("Try to login")
333 #if self.login(username, password):
334 # print("Could not login")
335 # return False
336
337 #pattern = "^\[" + username + "@.+\]#"
338 #print(pattern)
339 #data = self.readline(pattern=pattern)
340 #if data["return-code"] == 1:
341 # print("We are logged in")
342 # else:
343 # print("We are not logged in")
344 # login
345
346 #while 1:
347 #data = self.readline("^.*login:")
348 # if data["return-code"] == 1:
349 # break
350
351 # string = 'cd / && ls \n'
352 # self.con.write(string.encode())
353 # self.con.flush()
354 # #print(self.con.read(5))
355
356 # data = self.readline()
357 # self.log_console_line(data.decode())
358
359 # while not self.back_at_prompt():
360 # data = self.readline()
361 # self.log_console_line(data.decode())
362
363 ''' in_waiting_before = 0
364 sleep(1)
365
366 while in_waiting_before != self.con.in_waiting:
367 in_waiting_before = self.con.in_waiting
368 sleep(0.5)
369
370 print(self.con.in_waiting)
371 data = self.con.read(self.con.in_waiting)
372 data = data.decode()
373
374 pattern = "^\[" + username + "@.+\]# $"
375 pattern = re.compile(pattern, re.MULTILINE)
376 if pattern.match(data, re.MULTILINE):
377 print("It works")
378
379 print(data, end='') '''
380
381
382 #@property
383 #def con(self):
384 # return self.con
385
386
387 def read(self, size=1):
388 if len(self.buffer) >= size:
389 # throw away first size bytes in buffer
390 data = self.buffer[:size]
391 # Set the buffer to the non used bytes
392 self.buffer = self.buffer[size:]
393 return data
394 else:
395 data = self.buffer
396 # Set the size to the value we have to read now
397 size = size - len(self.buffer)
398 # Set the buffer empty
399 self.buffer = b""
400 return data + self.con.read(size)
401
402 def peek(self, size=1):
403 if len(self.buffer) <= size:
404 self.buffer += self.con.read(size=size - len(self.buffer))
405
406 return self.buffer[:size]
407
408 def readline(self):
409 self.log.debug(self.buffer)
410 self.buffer = self.buffer + self.con.read(self.con.in_waiting)
411 if b"\n" in self.buffer:
412 size = self.buffer.index(b"\n") + 1
413 self.log.debug("We have a whole line in the buffer")
414 self.log.debug(self.buffer)
415 self.log.debug("We split at {}".format(size))
416 data = self.buffer[:size]
417 self.buffer = self.buffer[size:]
418 self.log.debug(data)
419 self.log.debug(self.buffer)
420 return data
421
422 data = self.buffer
423 self.buffer = b""
424 return data + self.con.readline()
425
426 def back_at_prompt(self):
427 data = self.peek()
428 if not data == b"[":
429 return False
430
431 # We need to use self.in_waiting because with self.con.in_waiting we get
432 # not the complete string
433 size = len(self.buffer) + self.in_waiting
434 data = self.peek(size)
435
436
437 if self.back_at_prompt_pattern == None:
438 #self.back_at_prompt_pattern = r"^\[{}@.+\]#".format(self.username)
439 self.back_at_prompt_pattern = re.compile(r"^\[{}@.+\]#".format(self.username), re.MULTILINE)
440
441 if self.back_at_prompt_pattern.search(data.decode()):
442 return True
443 else:
444 return False
445
446 def log_console_line(self, line):
447 self.log.debug("Get in function log_console_line()")
448 sys.stdout.write(line)
449
450 @property
451 def in_waiting(self):
452 in_waiting_before = 0
453 sleep(0.5)
454
455 while in_waiting_before != self.con.in_waiting:
456 in_waiting_before = self.con.in_waiting
457 sleep(0.5)
458
459 return self.con.in_waiting
460
461
462 def readline2(self, pattern=None):
463 string = ""
464 string2 = b""
465 if pattern:
466 pattern = re.compile(pattern)
467
468 while 1:
469 char = self.con.read(1)
470 string = string + char.decode("utf-8")
471 string2 = string2 + char
472 #print(char)
473 print(char.decode("utf-8"), end="")
474
475 #print(string2)
476 if pattern and pattern.match(string):
477 #print("get here1")
478 #print(string2)
479 return {"string" : string, "return-code" : 1}
480
481 if char == b"\n":
482 #print(char)
483 #print(string2)
484 #print("get here2")
485 return {"return-code" : 0}
486
487 def check_logged_in(self, username):
488 pattern = "^\[" + username + "@.+\]#"
489 data = self.readline(pattern=pattern)
490 if data["return-code"] == 1:
491 print("We are logged in")
492 return True
493 else:
494 print("We are not logged in")
495 return False
496
497 def login(self, password):
498 if self.username == None:
499 self.log.error("Username cannot be blank")
500 return False
501
502 # Hit enter to see what we get
503 self.con.write(b'\n')
504 # We get two new lines \r\n ?
505 data = self.readline()
506 self.log_console_line(data.decode())
507
508
509 if self.back_at_prompt():
510 self.log.debug("We are already logged in.")
511 return True
512
513 # Read all line till we get login:
514 while 1:
515 data = self.peek()
516 if not data.decode() == "l":
517 self.log.debug("We get no l at the start")
518 self.log_console_line(self.readline().decode())
519
520 # We need to use self.in_waiting because with self.con.in_waiting we get
521 # not the complete string
522 size = len(self.buffer) + self.in_waiting
523 data = self.peek(size)
524
525 pattern = r"^.*login: "
526 pattern = re.compile(pattern)
527
528 if pattern.search(data.decode()):
529 break
530 else:
531 self.log.debug("The pattern does not match")
532 self.log_console_line(self.readline().decode())
533
534 # We can login
535 string = "{}\n".format(self.username)
536 self.con.write(string.encode())
537 self.con.flush()
538 # read the login out of the buffer
539 data = self.readline()
540 self.log.debug("This is the login:{}".format(data))
541 self.log_console_line(data.decode())
542
543 # We need to wait her till we get the full string "Password:"
544 #This is useless but self.in_waiting will wait the correct amount of time
545 size = self.in_waiting
546
547 string = "{}\n".format(password)
548 self.con.write(string.encode())
549 self.con.flush()
550 # Print the 'Password:' line
551 data = self.readline()
552 self.log_console_line(data.decode())
553
554 while not self.back_at_prompt():
555 # This will fail if the login failed so we need to look for the failed keyword
556 data = self.readline()
557 self.log_console_line(data.decode())
558
559 return True
560
561 def write(self, string):
562 self.log.debug(string)
563 self.con.write(string.encode())
564 self.con.flush()
565
566 def command(self, command):
567 self.write("{}\n".format(command))
568
569 # We need to read out the prompt for this command first
570 # If we do not do this we will break the loop immediately
571 # because the prompt for this command is still in the buffer
572 data = self.readline()
573 self.log_console_line(data.decode())
574
575 while not self.back_at_prompt():
576 data = self.readline()
577 self.log_console_line(data.decode())
578
579
580# while True:
581
582# line = self.readline()
583
584# print (line)
585
586# print("Hello")
587
588# print("World")
589
590# Hello
591# World
592
593# Hello World
594
595# # Peek for prompt?
596# if self.back_at_prompt():
597# break
598
599# def back_at_prompt():
600# data = self.peek()
601
602# if not char == "[":
603# return False
604
605# data = self.peek(in_waiting)
606# m = re.search(..., data)
607# if m:
608# return True
609
610
611
612
613
614# pattern = r"^\[root@.+\]#"
615
616# pattern = re.compile(pattern, re.MULTILINE)
617
618# data = """cd / && ls
619# bin dev home lib64 media opt root sbin sys usr
620# boot etc lib lost+found mnt proc run srv tmp var
621# [root@localhost /]# """
622
623# #data = "[root@localhost /]# "
624# data2 = pattern.search(data)
625
626# #if pattern.search(data):
627# # print("get here")
628
629# #print(data2.group())
630
631
632
633# vm = vm("qemu:///system")
634
635# dom = vm.domain_start_from_xml_file("/home/jonatan/python-testing-kvm/centos2.xml")
636
637# # This block till the vm is booted
638# print("Waiting till the domain is booted up")
639# vm.check_domain_is_booted_up(dom)
640# print("Domain is booted up")
641# #vm.domain_get_serial_device(dom)
642# #vm.domain_get_serial_device(dom)
643
644# #centos2 = vm("/home/jonatan/python-testing-kvm/centos2.xml", "/home/jonatan/python-testing-kvm/centos2-snapshot.xml")
645
646# centos2.define()
647# centos2.create_snapshot()
648# centos2.start()
649# #centos2.check_is_booted_up()
650# centos2.login("root", "25814@root")
651# centos2.cmd("echo 1 > test3")
652
653
654# #versuch1 = connection(centos2.get_serial_device, username="root")
655# #versuch1.login()
656# #versuch1.command("cd / && ls")
657
658# input("Press Enter to continue...")
659# centos2.shutdown()
660# centos2.revert_snapshot()
661# centos2.undefine()
662
663# A class which define and undefine a virtual network based on an xml file
664class network():
665 def __init__(self, path):
666 self.log = log(4)
667 self.log.debug("Path is: {}".format(path))
668
669# Should read the test, check if the syntax are valid
670# and return tuples with the ( host, command ) structure
671class recipe():
672 def __init__(self, path):
673 self.log = log(4)
674 self.recipe_file = path
675 if not os.path.isfile(self.recipe_file):
676 self.log.error("No such file: {}".format(self.recipe_file))
677
678 try:
679 with open(self.recipe_file) as fobj:
680 self.raw_recipe = fobj.readlines()
681 except FileNotFoundError as error:
682 self.log.error("No such file: {}".format(vm_xml_file))
683
684 for line in self.raw_recipe:
685 print(line)
686
687
688
689class test():
690 def __init__(self, path):
691 self.log = log(4)
692 try:
693 self.path = os.path.abspath(path)
694 except BaseException as e:
695 self.log.error("Could not get absolute path")
696
697 self.log.debug(self.path)
698
699 self.settings_file = "{}/settings".format(self.path)
700 if not os.path.isfile(self.settings_file):
701 self.log.error("No such file: {}".format(self.settings_file))
702
703 self.recipe_file = "{}/recipe".format(self.path)
704 if not os.path.isfile(self.recipe_file):
705 self.log.error("No such file: {}".format(self.recipe_file))
706
707 def read_settings(self):
708 self.config = configparser.ConfigParser()
709 self.config.read(self.settings_file)
710 self.name = self.config["DEFAULT"]["Name"]
711 self.description = self.config["DEFAULT"]["Description"]
712
713 self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
714 self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
715 self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
716
717 def virtual_environ_setup(self):
718 self.virtual_environ = virtual_environ(self.virtual_environ_path)
719
720 self.virtual_networks = self.virtual_environ.get_networks()
721
722 self.virtual_machines = self.virtual_environ.get_machines()
723
724 def virtual_environ_start(self):
725 pass
726
727 def load_recipe(self):
728 pass
729
730 def run_recipe():
731 pass
732
733 def virtual_environ_stop():
734 pass
735
736
737
738
739# Should return all vms and networks in a list
740# and should provide the path to the necessary xml files
741class virtual_environ():
742 def __init__(self, path):
743 self.log = log(4)
744 try:
745 self.path = os.path.abspath(path)
746 except BaseException as e:
747 self.log.error("Could not get absolute path")
748
749 self.log.debug(self.path)
750
751 self.settings_file = "{}/settings".format(self.path)
752 if not os.path.isfile(self.settings_file):
753 self.log.error("No such file: {}".format(self.settings_file))
754
755 self.log.debug(self.settings_file)
756 self.config = configparser.ConfigParser()
757 self.config.read(self.settings_file)
758 self.name = self.config["DEFAULT"]["name"]
759 self.machines_string = self.config["DEFAULT"]["machines"]
760 self.networks_string = self.config["DEFAULT"]["networks"]
761
762 self.machines = []
763 for machine in self.machines_string.split(","):
764 self.machines.append(machine.strip())
765
766 self.networks = []
767 for network in self.networks_string.split(","):
768 self.networks.append(network.strip())
769
770 self.log.debug(self.machines)
771 self.log.debug(self.networks)
772
773 def get_networks(self):
774 networks = {}
775 for _network in self.networks:
776 self.log.debug(_network)
777 networks.setdefault(_network, network(os.path.normpath(self.path + "/" + self.config[_network]["xml_file"])))
778 return networks
779
780 def get_machines(self):
781 machines = {}
782 for _machine in self.machines:
783 self.log.debug(_machine)
784 machines.setdefault(_machine, vm(
785 os.path.normpath(self.path + "/" + self.config[_machine]["xml_file"]),
786 os.path.normpath(self.path + "/" + self.config[_machine]["snapshot_xml_file"])))
787
788 return machines
789
790
791
792
793
794# def command(self, command):
795# self._send_command(command)
796
797# while True:
798# line = self.readline()
799
800# print (line)
801
802# print("Hello")
803
804# print("World")
805
806# Hello
807# World
808
809# Hello World
810
811# # Peek for prompt?
812# if self.back_at_prompt():
813# break
814
815# def back_at_prompt():
816# data = self.peek()
817
818# if not char == "[":
819# return False
820
821# data = self.peek(in_waiting)
822# m = re.search(..., data)
823# if m:
824# return True
825
826
827
828
829
830# class connection()
831# buffer = b""
832
833# def read(self, size=1):
834# if len(buffer) >= size:
835# # throw away first size bytes in buffer
836# data, buffer = buffer[:size], buffer[size:]
837# return data
838
839# return self.serial.read(size)
840
841# def peek(self, size=1):
842# if len(buffer) <= size:
843# buffer += self.serial.read(size=size - len(buffer))
844
845# return buffer[:size]
846
847
848# def readline(self):
849# buffer = buffer + self.serial.read(in_wating)
850# if "\n" in buffer:
851# return alle zeichen bis zum \n
852
853# return buffer + self.serial.readline()
854
855
856if __name__ == "__main__":
857 import argparse
858
859 parser = argparse.ArgumentParser()
860
861 parser.add_argument("-d", "--directory", dest="dir")
862
863 args = parser.parse_args()
864
865 _recipe = recipe("/home/jonatan/python-testing-kvm/test/recipe")
866 currenttest = test(args.dir)
867 currenttest.read_settings()
868 currenttest.virtual_environ_setup()