]> git.ipfire.org Git - nitsi.git/blame - virtual_environ.py
We now use the logging module
[nitsi.git] / virtual_environ.py
CommitLineData
ab759374
JS
1#!/usr/bin/python3
2
3from machine import machine
4
5from network import network
6
7import os
8import configparser
7005787e 9import libvirt
1ed8ca9f
JS
10import logging
11
12logger = logging.getLogger("nitsi.virtual_environ")
ab759374
JS
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):
1ed8ca9f 18 self.log = logger.getChild(os.path.basename(os.path.abspath(path)))
ab759374
JS
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
7005787e
JS
48 self.uri = self.config["DEFAULT"]["uri"]
49
50 try:
51 self.con = libvirt.open(self.uri)
52 except BaseException as error:
53 self.log.error("Could not connect to: {}".format(self.uri))
54
55 self.log.debug("Connected to: {}".format(self.uri))
56
ab759374
JS
57 def get_networks(self):
58 networks = {}
59 for _network in self.networks:
60 self.log.debug(_network)
7005787e 61 networks.setdefault(_network, network(self.con, os.path.normpath(self.path + "/" + self.config[_network]["xml_file"])))
ab759374
JS
62 return networks
63
64 def get_machines(self):
65 machines = {}
66 for _machine in self.machines:
67 self.log.debug(_machine)
68 machines.setdefault(_machine, machine(
7005787e 69 self.con,
ab759374
JS
70 os.path.normpath(self.path + "/" + self.config[_machine]["xml_file"]),
71 os.path.normpath(self.path + "/" + self.config[_machine]["snapshot_xml_file"]),
72 self.config[_machine]["image"],
73 self.config[_machine]["root_uid"],
74 self.config[_machine]["username"],
75 self.config[_machine]["password"]))
76
77 return machines
78
79 @property
80 def machine_names(self):
81 return self.machines
82
83 @property
84 def network_names(self):
85 return self.networks