2 #############################################################################
3 #Copyright 2017 Parker Berberian and others #
5 #Licensed under the Apache License, Version 2.0 (the "License"); #
6 #you may not use this file except in compliance with the License. #
7 #You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 #Unless required by applicable law or agreed to in writing, software #
12 #distributed under the License is distributed on an "AS IS" BASIS, #
13 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 #See the License for the specific language governing permissions and #
15 #limitations under the License. #
16 #############################################################################
21 import xml.dom.minidom
27 This class has a similar role as the Domain class.
28 This class will parse a config file and
29 write the xml definitions of those networks for libvirt.
32 def __init__(self, propertiesDict):
34 init. propertiesDict should be
35 one of the dictionaries returned by parseConfigFile
37 self.name = propertiesDict['name']
38 self.brName = propertiesDict['brName']
39 self.brAddr = propertiesDict['brAddr']
40 self.netmask = propertiesDict['netmask']
41 self.forward = propertiesDict['forward']
42 self.dhcp = propertiesDict['dhcp']
43 self.cidr = propertiesDict['cidr']
47 Takes the config of this network and writes a valid xml definition
51 definition = xml.dom.minidom.parseString("<network>\n</network>")
52 nameElem = definition.createElement('name')
53 nameElem.appendChild(definition.createTextNode(self.name))
54 definition.documentElement.appendChild(nameElem)
56 if self.forward['used']:
57 forwardElem = definition.createElement('forward')
58 forwardElem.setAttribute('mode', self.forward['type'])
59 definition.documentElement.appendChild(forwardElem)
61 bridgeElem = definition.createElement('bridge')
62 bridgeElem.setAttribute('name', self.brName)
63 bridgeElem.setAttribute('stp', 'on')
64 bridgeElem.setAttribute('delay', '5')
65 definition.documentElement.appendChild(bridgeElem)
67 ipElem = definition.createElement('ip')
68 ipElem.setAttribute('address', self.brAddr)
69 ipElem.setAttribute('netmask', self.netmask)
71 dhcpElem = definition.createElement('dhcp')
72 rangeElem = definition.createElement('range')
73 rangeElem.setAttribute('start', self.dhcp['rangeStart'])
74 rangeElem.setAttribute('end', self.dhcp['rangeEnd'])
75 dhcpElem.appendChild(rangeElem)
76 ipElem.appendChild(dhcpElem)
78 definition.documentElement.appendChild(ipElem)
80 self.xml = definition.toprettyxml()
83 def writeXML(self, filePath):
85 writes xml definition to given file
87 f = open(filePath, 'w')
92 def parseConfigFile(path):
94 parses given config file
96 configFile = open(path, 'r')
98 config = yaml.safe_load(configFile)
100 print "Bad network configuration file. exiting"