Improving error message and undefining of network objects
[nitsi.git] / src / nitsi / network.py
1 #!/usr/bin/python3
2
3 import logging
4 import os
5
6 logger = logging.getLogger("nitsi.network")
7
8 # # A class which define and undefine a virtual network based on an xml file
9 class network():
10     def __init__(self, libvirt_con, network_xml_file):
11         self.log = logger.getChild(os.path.basename(os.path.dirname(network_xml_file)))
12         self.con = libvirt_con
13         self.network = None
14         try:
15             with open(network_xml_file) as fobj:
16                 self.log.info("Reading xml file for network")
17                 self.network_xml = fobj.read()
18         except FileNotFoundError as error:
19             self.log.error("No such file: {}".format(network_xml_file))
20             raise error
21
22     def define(self):
23         self.log.info("Going to define network")
24         self.network = self.con.networkDefineXML(self.network_xml)
25
26         if network == None:
27             self.log.error("Failed to define virtual network")
28
29     def start(self):
30         self.log.info("Starting Network")
31         self.network.create()
32
33     def undefine(self):
34         if self.network != None:
35             if self.network.isActive() == 1:
36                 self.log.info("Shutting down network")
37                 self.network.destroy()
38
39             self.log.info("Undefining network")
40             self.network.undefine()