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