]> git.ipfire.org Git - nitsi.git/blame - src/nitsi/virtual_environ.py
Make all class names CamelCase
[nitsi.git] / src / nitsi / virtual_environ.py
CommitLineData
ab759374
JS
1#!/usr/bin/python3
2
ab759374 3import configparser
7005787e 4import libvirt
1ed8ca9f 5import logging
6632e137
JS
6import os
7
b560f31a
JS
8from . import machine
9from . import network
1ed8ca9f
JS
10
11logger = logging.getLogger("nitsi.virtual_environ")
ab759374
JS
12
13# Should return all vms and networks in a list
14# and should provide the path to the necessary xml files
d9f6c37f 15class VirtualEnviron():
ab759374 16 def __init__(self, path):
1ed8ca9f 17 self.log = logger.getChild(os.path.basename(os.path.abspath(path)))
ab759374
JS
18 try:
19 self.path = os.path.abspath(path)
20 except BaseException as e:
21 self.log.error("Could not get absolute path")
5addee87 22 raise e
ab759374
JS
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
fc35cba1
JS
48 # Number of characters of the longest machine name
49 self._longest_machine_name = 0
50
7005787e
JS
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))
5addee87 57 raise error
7005787e
JS
58
59 self.log.debug("Connected to: {}".format(self.uri))
60
ab759374
JS
61 def get_networks(self):
62 networks = {}
63 for _network in self.networks:
64 self.log.debug(_network)
ee227ea1 65 networks.setdefault(_network, network.Network(self.con, os.path.normpath(self.path + "/" + self.config[_network]["xml_file"])))
ab759374
JS
66 return networks
67
68 def get_machines(self):
69 machines = {}
70 for _machine in self.machines:
71 self.log.debug(_machine)
ee227ea1 72 machines.setdefault(_machine, machine.Machine(
7005787e 73 self.con,
ab759374
JS
74 os.path.normpath(self.path + "/" + self.config[_machine]["xml_file"]),
75 os.path.normpath(self.path + "/" + self.config[_machine]["snapshot_xml_file"]),
76 self.config[_machine]["image"],
77 self.config[_machine]["root_uid"],
78 self.config[_machine]["username"],
79 self.config[_machine]["password"]))
80
81 return machines
82
83 @property
84 def machine_names(self):
85 return self.machines
86
87 @property
88 def network_names(self):
fc35cba1
JS
89 return self.networks
90
91 @property
92 def longest_machine_name(self):
93 if self._longest_machine_name:
94 return self._longest_machine_name
95 else:
96 for _machine in self.machines:
97 if len(_machine) > self._longest_machine_name:
98 self._longest_machine_name = len(_machine)
99
6632e137 100 return self._longest_machine_name