]>
git.ipfire.org Git - people/ms/nitsi.git/blob - src/nitsi/virtual_environ.py
11 logger
= logging
.getLogger("nitsi.virtual_environ")
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
)))
19 self
.path
= os
.path
.abspath(path
)
20 except BaseException
as e
:
21 self
.log
.error("Could not get absolute path")
23 self
.log
.debug(self
.path
)
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
))
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"]
37 for machine
in self
.machines_string
.split(","):
38 self
.machines
.append(machine
.strip())
41 for network
in self
.networks_string
.split(","):
42 self
.networks
.append(network
.strip())
44 self
.log
.debug(self
.machines
)
45 self
.log
.debug(self
.networks
)
47 # Number of characters of the longest machine name
48 self
._longest
_machine
_name
= 0
50 self
.uri
= self
.config
["DEFAULT"]["uri"]
53 self
.con
= libvirt
.open(self
.uri
)
54 except BaseException
as error
:
55 self
.log
.error("Could not connect to: {}".format(self
.uri
))
57 self
.log
.debug("Connected to: {}".format(self
.uri
))
59 def get_networks(self
):
61 for _network
in self
.networks
:
62 self
.log
.debug(_network
)
63 networks
.setdefault(_network
, network
.network(self
.con
, os
.path
.normpath(self
.path
+ "/" + self
.config
[_network
]["xml_file"])))
66 def get_machines(self
):
68 for _machine
in self
.machines
:
69 self
.log
.debug(_machine
)
70 machines
.setdefault(_machine
, machine
.machine(
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"]))
82 def machine_names(self
):
86 def network_names(self
):
90 def longest_machine_name(self
):
91 if self
._longest
_machine
_name
:
92 return self
._longest
_machine
_name
94 for _machine
in self
.machines
:
95 if len(_machine
) > self
._longest
_machine
_name
:
96 self
._longest
_machine
_name
= len(_machine
)
98 return self
._longest
_machine
_name