]> git.ipfire.org Git - nitsi.git/blob - src/nitsi/virtual_environ.py
Sort imports: part 1
[nitsi.git] / src / nitsi / virtual_environ.py
1 #!/usr/bin/python3
2
3 import configparser
4 import libvirt
5 import logging
6 import os
7
8 from .machine import machine
9 from .network import network
10
11 logger = logging.getLogger("nitsi.virtual_environ")
12
13 # Should return all vms and networks in a list
14 # and should provide the path to the necessary xml files
15 class virtual_environ():
16 def __init__(self, path):
17 self.log = logger.getChild(os.path.basename(os.path.abspath(path)))
18 try:
19 self.path = os.path.abspath(path)
20 except BaseException as e:
21 self.log.error("Could not get absolute path")
22
23 self.log.debug(self.path)
24
25 self.settings_file = "{}/settings".format(self.path)
26 if not os.path.isfile(self.settings_file):
27 self.log.error("No such file: {}".format(self.settings_file))
28
29 self.log.debug(self.settings_file)
30 self.config = configparser.ConfigParser()
31 self.config.read(self.settings_file)
32 self.name = self.config["DEFAULT"]["name"]
33 self.machines_string = self.config["DEFAULT"]["machines"]
34 self.networks_string = self.config["DEFAULT"]["networks"]
35
36 self.machines = []
37 for machine in self.machines_string.split(","):
38 self.machines.append(machine.strip())
39
40 self.networks = []
41 for network in self.networks_string.split(","):
42 self.networks.append(network.strip())
43
44 self.log.debug(self.machines)
45 self.log.debug(self.networks)
46
47 # Number of characters of the longest machine name
48 self._longest_machine_name = 0
49
50 self.uri = self.config["DEFAULT"]["uri"]
51
52 try:
53 self.con = libvirt.open(self.uri)
54 except BaseException as error:
55 self.log.error("Could not connect to: {}".format(self.uri))
56
57 self.log.debug("Connected to: {}".format(self.uri))
58
59 def get_networks(self):
60 networks = {}
61 for _network in self.networks:
62 self.log.debug(_network)
63 networks.setdefault(_network, network(self.con, os.path.normpath(self.path + "/" + self.config[_network]["xml_file"])))
64 return networks
65
66 def get_machines(self):
67 machines = {}
68 for _machine in self.machines:
69 self.log.debug(_machine)
70 machines.setdefault(_machine, machine(
71 self.con,
72 os.path.normpath(self.path + "/" + self.config[_machine]["xml_file"]),
73 os.path.normpath(self.path + "/" + self.config[_machine]["snapshot_xml_file"]),
74 self.config[_machine]["image"],
75 self.config[_machine]["root_uid"],
76 self.config[_machine]["username"],
77 self.config[_machine]["password"]))
78
79 return machines
80
81 @property
82 def machine_names(self):
83 return self.machines
84
85 @property
86 def network_names(self):
87 return self.networks
88
89 @property
90 def longest_machine_name(self):
91 if self._longest_machine_name:
92 return self._longest_machine_name
93 else:
94 for _machine in self.machines:
95 if len(_machine) > self._longest_machine_name:
96 self._longest_machine_name = len(_machine)
97
98 return self._longest_machine_name