auto calculate zones
This commit is contained in:
parent
b1098a966b
commit
7b74984fb7
2 changed files with 36 additions and 10 deletions
|
@ -7,6 +7,7 @@ from os import remove, makedirs, listdir, chmod
|
|||
from os.path import isfile, isdir, abspath, join, dirname
|
||||
from json import dump as json_dump, load as json_load
|
||||
from yaml import load as yaml_load, SafeLoader
|
||||
from ipaddress import ip_network
|
||||
#
|
||||
from tiramisu import Config, valid_network_netmask, valid_ip_netmask, valid_broadcast, valid_in_network, valid_not_equal, calc_value
|
||||
from rougail.utils import normalize_family
|
||||
|
@ -219,9 +220,36 @@ class Loader:
|
|||
|
||||
functions_files = set()
|
||||
applicationservices = Applications()
|
||||
zones = self.servers_json['zones']
|
||||
|
||||
zones_name = {}
|
||||
rougail = RougailConvert(cfg)
|
||||
for host_name, datas in self.servers_json['hosts'].items():
|
||||
for server_name, server_datas in datas['servers'].items():
|
||||
for zone in server_datas['informations']['zones_name']:
|
||||
zones_name.setdefault(zone, []).append(server_name)
|
||||
self.zones = {}
|
||||
zones_network = ip_network(self.servers_json['zones']['network'])
|
||||
zone_start_ip = zones_network.network_address
|
||||
domain_name = self.servers_json['zones']['prefix_domain_name']
|
||||
for idx, zone_name in enumerate(zones_name):
|
||||
sub_network = ip_network(f'{zone_start_ip}/28')
|
||||
if not sub_network.subnet_of(zones_network):
|
||||
raise Exception('not enough IP available')
|
||||
if sub_network.num_addresses < len(zones_name[zone_name]):
|
||||
#FIXME should try to increase network!
|
||||
raise Exception(f'network too small for zone {zone_name}')
|
||||
if idx == 0:
|
||||
zone_domaine_name = domain_name
|
||||
else:
|
||||
zone_domaine_name = zone_name + '.' + domain_name
|
||||
|
||||
network = sub_network.network_address
|
||||
self.zones[zone_name] = {'domain_name': zone_domaine_name,
|
||||
'network': str(sub_network),
|
||||
'host_ip': str(network + 1),
|
||||
'start_ip': str(network + 2)
|
||||
}
|
||||
zone_start_ip = str(sub_network.broadcast_address + 1)
|
||||
|
||||
for host_name, datas in self.servers_json['hosts'].items():
|
||||
# load modules associate to this host
|
||||
modules_name = set()
|
||||
|
@ -250,13 +278,12 @@ class Loader:
|
|||
)
|
||||
# load servers
|
||||
modules_info = {}
|
||||
#FIXME ADD TLS in servers !!!
|
||||
for server_name, server_datas in datas['servers'].items():
|
||||
module_info = modules.get(server_datas['applicationservice'])
|
||||
zones_name = server_datas['informations']['zones_name']
|
||||
values = [f'{server_name}.{zones[zone_name]["domain_name"]}' for zone_name in zones_name]
|
||||
values = [f'{server_name}.{self.zones[zone_name]["domain_name"]}' for zone_name in zones_name]
|
||||
if server_datas['applicationservice'] == 'tls':
|
||||
true_host_name = f'{server_name}.{zones[list(zones)[0]]["domain_name"]}'
|
||||
true_host_name = f'{server_name}.{self.zones[list(self.zones)[0]]["domain_name"]}'
|
||||
else:
|
||||
true_host_name = values[0]
|
||||
cfg['risotto_globals'][true_host_name] = {'global:host_name': host_name,
|
||||
|
@ -338,8 +365,8 @@ class Loader:
|
|||
await config.property.read_write()
|
||||
await config.property.pop('validator')
|
||||
await config.property.pop('cache')
|
||||
load_zones(self.servers_json)
|
||||
await config.information.set('zones', self.servers_json['zones'])
|
||||
load_zones(self.zones, self.servers_json['hosts'])
|
||||
await config.information.set('zones', self.zones)
|
||||
for host_name, hosts_datas in self.servers_json['hosts'].items():
|
||||
information = config.option(normalize_family(host_name)).information
|
||||
await information.set('module', 'host')
|
||||
|
|
|
@ -44,17 +44,16 @@ async def value_pprint(dico, config):
|
|||
pprint(pprint_dict)
|
||||
|
||||
|
||||
def load_zones(servers_json):
|
||||
def load_zones(zones, hosts):
|
||||
if not isdir(IP_DIR):
|
||||
makedirs(IP_DIR)
|
||||
zones = servers_json['zones']
|
||||
json_file = join(IP_DIR, 'zones.json')
|
||||
if isfile(json_file):
|
||||
with open(json_file, 'r') as fh:
|
||||
zones_ip = load(fh)
|
||||
else:
|
||||
zones_ip = {}
|
||||
for host_name, hosts in servers_json['hosts'].items():
|
||||
for host_name, hosts in hosts.items():
|
||||
for server_name, server in hosts['servers'].items():
|
||||
server_zones = server['informations']['zones_name']
|
||||
for idx, zone_name in enumerate(server_zones):
|
||||
|
|
Loading…
Reference in a new issue