2016-05-29 18:56:50 +02:00
|
|
|
from exceptions.HieraException import HieraException
|
|
|
|
from generator.Filter import Filter
|
2016-07-19 16:31:53 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2016-05-29 18:56:50 +02:00
|
|
|
class NginxConfGen(object):
|
|
|
|
def __init__(self, domains, args):
|
|
|
|
self.__domains__ = domains
|
|
|
|
self.__args__ = args
|
|
|
|
self.__filter__ = Filter(self.__args__)
|
|
|
|
self.__generatedDomains__ = self.__genDomains__()
|
|
|
|
|
|
|
|
def __genDomains__(self):
|
|
|
|
domains = {}
|
|
|
|
for k,v in self.__domains__.items():
|
|
|
|
domains[k] = self.__genDomain__(v)
|
|
|
|
return domains
|
|
|
|
|
|
|
|
def __genDomain__(self, domain):
|
|
|
|
nodes = {}
|
2017-08-19 23:25:30 +02:00
|
|
|
for localGraph in self.__filter__.filterLocalGraphs(domain, domain.localGraphs):
|
2016-05-29 18:56:50 +02:00
|
|
|
try:
|
2017-08-19 23:25:30 +02:00
|
|
|
for node in self.__filter__.filterNodes(domain, localGraph.getNodesWithNoDependencies()):
|
2016-05-29 18:56:50 +02:00
|
|
|
nodes[node.nodeID] = {
|
|
|
|
'hostname' : node.hostname,
|
|
|
|
'ipv6_addresses' : node.publicIPv6Addresses
|
|
|
|
}
|
|
|
|
except HieraException:
|
|
|
|
print('Was not able to add local cloud, because no VPN link was found.')
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
def writeNginxConfigFile(self):
|
|
|
|
f = open(self.__args__.out_file,'w')
|
|
|
|
f.write(self.__genNginxConfigFileContent__())
|
|
|
|
f.close()
|
2016-05-29 20:44:26 +02:00
|
|
|
print('Nginx configfile has been created. You can find it at:', self.__args__.out_file)
|
2016-05-29 18:56:50 +02:00
|
|
|
|
|
|
|
def __genNginxConfigFileContent__(self):
|
|
|
|
content = ''
|
2016-07-19 16:31:53 +02:00
|
|
|
for k, v in OrderedDict(sorted(self.__generatedDomains__.items())).items():
|
2016-05-29 18:56:50 +02:00
|
|
|
content += 'geo $' + k + ' {\n default 0;'
|
2016-07-19 16:31:53 +02:00
|
|
|
for ksub, vsub in OrderedDict(sorted(v.items())).items():
|
2016-05-29 18:56:50 +02:00
|
|
|
for address in vsub['ipv6_addresses']:
|
|
|
|
content += '\n ' + address + ' 1; #' + vsub['hostname']
|
|
|
|
content += '\n}\n'
|
|
|
|
return content
|
|
|
|
|