]>
Commit | Line | Data |
---|---|---|
ab759374 JS |
1 | #!/usr/bin/python3 |
2 | ||
ab759374 | 3 | import configparser |
7005787e | 4 | import libvirt |
1ed8ca9f | 5 | import logging |
6632e137 JS |
6 | import os |
7 | ||
b560f31a JS |
8 | from . import machine |
9 | from . import network | |
1ed8ca9f JS |
10 | |
11 | logger = 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 | |
15 | class virtual_environ(): | |
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") | |
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 | ||
fc35cba1 JS |
47 | # Number of characters of the longest machine name |
48 | self._longest_machine_name = 0 | |
49 | ||
7005787e JS |
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 | ||
ab759374 JS |
59 | def get_networks(self): |
60 | networks = {} | |
61 | for _network in self.networks: | |
62 | self.log.debug(_network) | |
b560f31a | 63 | networks.setdefault(_network, network.network(self.con, os.path.normpath(self.path + "/" + self.config[_network]["xml_file"]))) |
ab759374 JS |
64 | return networks |
65 | ||
66 | def get_machines(self): | |
67 | machines = {} | |
68 | for _machine in self.machines: | |
69 | self.log.debug(_machine) | |
b560f31a | 70 | machines.setdefault(_machine, machine.machine( |
7005787e | 71 | self.con, |
ab759374 JS |
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): | |
fc35cba1 JS |
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 | ||
6632e137 | 98 | return self._longest_machine_name |