auto calculate zones

This commit is contained in:
egarette@silique.fr 2023-03-03 14:06:30 +01:00
parent b1098a966b
commit 7b74984fb7
2 changed files with 36 additions and 10 deletions

View file

@ -7,6 +7,7 @@ from os import remove, makedirs, listdir, chmod
from os.path import isfile, isdir, abspath, join, dirname from os.path import isfile, isdir, abspath, join, dirname
from json import dump as json_dump, load as json_load from json import dump as json_dump, load as json_load
from yaml import load as yaml_load, SafeLoader 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 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 from rougail.utils import normalize_family
@ -219,9 +220,36 @@ class Loader:
functions_files = set() functions_files = set()
applicationservices = Applications() applicationservices = Applications()
zones = self.servers_json['zones'] zones_name = {}
rougail = RougailConvert(cfg) 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(): for host_name, datas in self.servers_json['hosts'].items():
# load modules associate to this host # load modules associate to this host
modules_name = set() modules_name = set()
@ -250,13 +278,12 @@ class Loader:
) )
# load servers # load servers
modules_info = {} modules_info = {}
#FIXME ADD TLS in servers !!!
for server_name, server_datas in datas['servers'].items(): for server_name, server_datas in datas['servers'].items():
module_info = modules.get(server_datas['applicationservice']) module_info = modules.get(server_datas['applicationservice'])
zones_name = server_datas['informations']['zones_name'] 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': 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: else:
true_host_name = values[0] true_host_name = values[0]
cfg['risotto_globals'][true_host_name] = {'global:host_name': host_name, 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.read_write()
await config.property.pop('validator') await config.property.pop('validator')
await config.property.pop('cache') await config.property.pop('cache')
load_zones(self.servers_json) load_zones(self.zones, self.servers_json['hosts'])
await config.information.set('zones', self.servers_json['zones']) await config.information.set('zones', self.zones)
for host_name, hosts_datas in self.servers_json['hosts'].items(): for host_name, hosts_datas in self.servers_json['hosts'].items():
information = config.option(normalize_family(host_name)).information information = config.option(normalize_family(host_name)).information
await information.set('module', 'host') await information.set('module', 'host')

View file

@ -44,17 +44,16 @@ async def value_pprint(dico, config):
pprint(pprint_dict) pprint(pprint_dict)
def load_zones(servers_json): def load_zones(zones, hosts):
if not isdir(IP_DIR): if not isdir(IP_DIR):
makedirs(IP_DIR) makedirs(IP_DIR)
zones = servers_json['zones']
json_file = join(IP_DIR, 'zones.json') json_file = join(IP_DIR, 'zones.json')
if isfile(json_file): if isfile(json_file):
with open(json_file, 'r') as fh: with open(json_file, 'r') as fh:
zones_ip = load(fh) zones_ip = load(fh)
else: else:
zones_ip = {} 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(): for server_name, server in hosts['servers'].items():
server_zones = server['informations']['zones_name'] server_zones = server['informations']['zones_name']
for idx, zone_name in enumerate(server_zones): for idx, zone_name in enumerate(server_zones):