node_hierarchy/domain_selector.py

94 lines
3 KiB
Python
Raw Normal View History

2015-12-23 14:16:18 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -
#Imports:
import json, urllib, os, glob
2015-12-23 14:16:18 +01:00
from graph import Graph
from hieraException import HieraException
2015-12-23 14:16:18 +01:00
class DomainSelector:
def __init__(self, nodesFile, graphFile, dataPath = './', printStatus = False, targets = None, branch = 'stable'):
if not os.path.isdir(dataPath):
print "\033[91mError:\033[0m Output folder was not found or is not writable. Given path:", dataPath
raise HieraException
2015-12-23 14:16:18 +01:00
self.printStatus = printStatus
self.targets = targets
self.dataPath = dataPath.rstrip('/')
2015-12-23 14:16:18 +01:00
self.nodesData = self.__getFile__(nodesFile)
self.graphData = self.__getFile__(graphFile)
self.__prepareOutDir__()
2015-12-23 14:16:18 +01:00
self.graph = Graph(self.nodesData, self.graphData)
if self.targets == None:
self.writeConfigFiles(self.graph.nodes_list,"all")
self.writeDumpFile(self.graph.nodes_list,"all")
2015-12-23 14:16:18 +01:00
else:
nodes = {}
for k,v in self.targets.iteritems():
nodes = self.graph.getNodeCloudsIn(v,branch)
2015-12-23 14:16:18 +01:00
self.writeConfigFiles(nodes,k)
self.writeDumpFile(nodes,k)
2015-12-23 14:16:18 +01:00
nodes = {}
self.writeConfigFiles(self.graph.getProblemNodes(noAutoupdater = True),"no_autoupdater")
self.writeConfigFiles(self.graph.getProblemNodes(noGeodata = True),"no_geo")
self.writeConfigFiles(self.graph.getProblemNodes(noGeodata = True, noAutoupdater = True),"no_nothing")
def __prepareOutDir__(self):
files = glob.glob(self.dataPath+'/*')
for f in files:
os.remove(f)
2015-12-23 14:16:18 +01:00
def __getFile__(self, nodesFile):
if nodesFile.startswith('https://') or nodesFile.startswith('http://'):
if self.printStatus:
print 'Download', nodesFile.rsplit('/', 1)[1] , 'from URL:', nodesFile
2015-12-23 14:16:18 +01:00
resource = urllib.urlopen(nodesFile)
else:
if self.printStatus:
print 'Open', nodesFile.rsplit('/', 1)[1] , 'from file:', nodesFile
2015-12-23 14:16:18 +01:00
resource = open(nodesFile)
try:
data = json.loads(resource.read())
except:
print "\033[91mError:\033[0m Error while parsing a json file (perhapes misformed file): ", nodesFile
raise HieraException
finally:
resource.close()
2015-12-23 14:16:18 +01:00
return data
def writeConfigFiles(self, nodes, name):
maxDepth = self.maxDepth(nodes)
if len(nodes) > 0:
for i in range(0,maxDepth):
content = 'geo $switch {\n default 0;'
f = open(self.dataPath.rstrip('/')+'/'+name+'_node_level'+str(i),'w')
for node in nodes.itervalues():
if node.stepsToVpn == i:
if node.ipv6 and node.hostname:
content += '\n '+node.ipv6+' 1; #'+node.hostname
content += '\n}'
f.write(content.encode('utf8'))
f.close()
2015-12-23 14:16:18 +01:00
def writeDumpFile(self, nodes, name):
content = {}
for node in nodes.itervalues():
if node.ipv6 and node.hostname:
content[node.nodeid] = {
'nodeid' : node.nodeid,
'ipv6' : node.ipv6,
'hostname' : node.hostname,
'level' : node.stepsToVpn,
}
with open(self.dataPath+'/'+name+'_node_statistics.json', 'w') as outfile:
json.dump(content, outfile)
2015-12-23 14:16:18 +01:00
def maxDepth(self, nodes):
maxDepth = 0
for v in nodes.itervalues():
if v.stepsToVpn > maxDepth:
maxDepth = v.stepsToVpn
return maxDepth+1