get_ip_domain

This commit is contained in:
Emmanuel Garette 2022-05-23 08:48:36 +02:00
parent 76a99dc532
commit 23adccacb3
3 changed files with 75 additions and 39 deletions

View file

@ -17,7 +17,7 @@ from tiramisu.error import ValueWarning
from rougail import RougailConfig, RougailConvert, RougailSystemdTemplate from rougail import RougailConfig, RougailConvert, RougailSystemdTemplate
from rougail.utils import normalize_family from rougail.utils import normalize_family
from risotto.utils import MULTI_FUNCTIONS, CONFIGS from risotto.utils import MULTI_FUNCTIONS, CONFIGS, DOMAINS
with open(environ.get('CONFIG_FILE', 'risotto.conf'), 'r') as fh: with open(environ.get('CONFIG_FILE', 'risotto.conf'), 'r') as fh:
@ -39,13 +39,14 @@ with open('servers.json', 'r') as server_fh:
async def set_linked_multi_variables(value: str, async def set_linked_multi_variables(value: str,
linked_server: str=None, linked_server: str=None,
variable_index: int=None,
**kwargs: dict, **kwargs: dict,
) -> None: ) -> None:
if value is not None and linked_server is not None and 'linked_value_0' not in kwargs: if value is not None and linked_server is not None and 'linked_value_0' not in kwargs:
kwargs['linked_value_0'] = value kwargs['linked_value_0'] = value
elif linked_server is None: elif not linked_server:
linked_server = value linked_server = value
if linked_server is None: if not linked_server:
return return
if linked_server not in CONFIGS: if linked_server not in CONFIGS:
warn_explicit(ValueWarning(f'cannot find linked server "{linked_server}"'), warn_explicit(ValueWarning(f'cannot find linked server "{linked_server}"'),
@ -66,51 +67,76 @@ async def set_linked_multi_variables(value: str,
if not path: if not path:
return return
if index not in variables: if index not in variables:
variables[index] = {'path': None, 'value': None} variables[index] = {'path': None, 'value': None, 'variable_index': False}
variables[index]['path'] = path variables[index]['path'] = path
elif key.startswith('linked_value_'): elif key.startswith('linked_value_'):
index = int(key[13]) index = int(key[13])
if index not in variables: if index not in variables:
variables[index] = {'path': None, 'value': None} variables[index] = {'path': None, 'value': None, 'variable_index': False}
variables[index]['value'] = value variables[index]['value'] = value
elif key.startswith('variable_index_'):
index = int(key[15])
if index not in variables:
variables[index] = {'path': None, 'value': None, 'variable_index': False}
variables[index]['variable_index'] = True
else: else:
raise AttributeError(f'unknown parameter {key}') raise AttributeError(f'unknown parameter {key}')
dynamic = None
slave_idx = None
await config.property.read_write() await config.property.read_write()
first_variable = variables[0]['value'] # print('=====================================')
# pprint(variables)
if not isinstance(variables[0]['value'], list): if not isinstance(variables[0]['value'], list):
variables[0]['value'] = [variables[0]['value']] variables[0]['value'] = [variables[0]['value']]
dynamic = None
try: try:
for var_index in range(len(variables[0]['value'])): if variables[0]['value']:
pass for first_idx, first_value in enumerate(variables[0]['value']):
for index in sorted(list(variables)): slave_idxes = []
path = variables[index]['path'] dynamic = normalize_family(first_value)
if index == 0: for index in sorted(list(variables)):
value = variables[index]['value'][var_index] path = variables[index]['path']
dynamic = normalize_family(value) if '{suffix}' in path:
else: path = path.replace('{suffix}', dynamic)
elif first_idx != 0:
continue
value = variables[index]['value'] value = variables[index]['value']
path = path.replace('{suffix}', dynamic) option = config.forcepermissive.option(path)
option = config.forcepermissive.option(path, slave_idx) if not await option.option.isfollower():
multi = await option.option.ismulti() #print('===>', path, value, await option.option.ismulti(), await option.option.issubmulti())
if multi and await option.option.isfollower(): multi = await option.option.ismulti()
multi = await option.option.issubmulti() if multi:
if multi: isleader = await option.option.isleader()
values = await option.value.get() if not isinstance(value, list):
if value not in values: value = [value]
values.append(value) # elif isleader:
await option.value.set(values) # raise Exception('leader must not be a multi from now ...')
if await option.option.isleader(): values = await option.value.get()
slave_idx = values.index(value) for val in value:
else: if val not in values:
if isinstance(value, list): if isleader:
value = value[var_index] slave_idxes.append(len(values))
await option.value.set(value) values.append(val)
elif isleader:
slave_idxes.append(values.index(val))
await option.value.set(values)
else:
await option.value.set(value)
else:
#print('===<', path, value, await option.option.ismulti(), await option.option.issubmulti())
if not slave_idxes:
raise Exception('please declare the leader variable before the follower')
if variables[index]['variable_index']:
value = value[variable_index]
if not isinstance(value, list):
value = [value] * len(slave_idxes)
# if isinstance(value, list) and not await option.option.issubmulti():
for idx, val in enumerate(value):
option = config.forcepermissive.option(path, slave_idxes[idx])
await option.value.set(val)
except Exception as err: except Exception as err:
await config.property.read_only() await config.property.read_only()
raise err from err raise err from err
await config.property.read_only() await config.property.read_only()
return get_ip_from_domain(linked_server)
async def set_linked(linked_server: str, async def set_linked(linked_server: str,
@ -364,7 +390,7 @@ def build_module(module_name, datas, module_infos):
for filename in listdir(manual_dir): for filename in listdir(manual_dir):
src_file = join(manual_dir, filename) src_file = join(manual_dir, filename)
if type == 'image': if type == 'image':
dst_file = join(install_dir, filename) dst_file = join(install_dir, 'manual', filename)
verify = False verify = False
else: else:
dst_file= join(INSTALL_DIR, filename) dst_file= join(INSTALL_DIR, filename)
@ -398,6 +424,13 @@ def build_module(module_name, datas, module_infos):
calc_depends(applicationservice, added) calc_depends(applicationservice, added)
def get_ip_from_domain(domain):
if not domain:
return
hostname, domainname = domain.split('.', 1)
return DOMAINS[domainname][1][DOMAINS[domainname][0].index(hostname)]
async def build(server_name, datas, module_infos): async def build(server_name, datas, module_infos):
if server_name in CONFIGS: if server_name in CONFIGS:
raise Exception(f'server "{server_name}" is duplicate') raise Exception(f'server "{server_name}" is duplicate')
@ -416,6 +449,7 @@ async def build(server_name, datas, module_infos):
'set_linked_multi_variables': set_linked_multi_variables, 'set_linked_multi_variables': set_linked_multi_variables,
'get_linked_configuration': get_linked_configuration, 'get_linked_configuration': get_linked_configuration,
'set_linked_configuration': set_linked_configuration, 'set_linked_configuration': set_linked_configuration,
'get_ip_from_domain': get_ip_from_domain,
} }
cfg['internal_functions'] = list(optiondescription.keys()) cfg['internal_functions'] = list(optiondescription.keys())
try: try:
@ -535,11 +569,14 @@ async def main():
for server_name in SERVERS: for server_name in SERVERS:
config = CONFIGS[server_name][0] config = CONFIGS[server_name][0]
await config.property.pop('mandatory') await config.property.pop('mandatory')
await config.value.dict() try:
await config.value.dict()
except Exception as err:
raise Exception(f'cannot display config for "{server_name}": {err}')
await config.property.add('mandatory') await config.property.add('mandatory')
for server_name in SERVERS: for server_name in SERVERS:
await valid_mandatories(server_name, CONFIGS[server_name][0]) await valid_mandatories(server_name, CONFIGS[server_name][0])
# print(await CONFIGS['revprox.in.gnunux.info'][0].option('nginx.reverse_proxy_for_netbox_in_gnunux_info.reverse_proxy_netbox_in_gnunux_info.revprox_url_netbox_in_gnunux_info', 0).value.get()) # print(await CONFIGS['dovecot.in.silique.fr'][0].value.dict())
for server_name in SERVERS: for server_name in SERVERS:
await templates(server_name, *CONFIGS[server_name]) await templates(server_name, *CONFIGS[server_name])

View file

@ -7,7 +7,7 @@ from secrets import token_urlsafe as _token_urlsafe
from rougail.utils import normalize_family from rougail.utils import normalize_family
from risotto.utils import multi_function, CONFIGS from risotto.utils import multi_function, CONFIGS, DOMAINS
from risotto.x509 import gen_cert as _x509_gen_cert, gen_ca as _x509_gen_ca, gen_pub as _x509_gen_pub, has_pub as _x509_has_pub from risotto.x509 import gen_cert as _x509_gen_cert, gen_ca as _x509_gen_ca, gen_pub as _x509_gen_pub, has_pub as _x509_has_pub
# ============================================================= # =============================================================
# fork of risotto-setting/src/risotto_setting/config/config.py # fork of risotto-setting/src/risotto_setting/config/config.py
@ -17,7 +17,6 @@ with open('servers.json', 'r') as server_fh:
ZONES = None ZONES = None
DOMAINS = None
HERE = dirname(abspath(__file__)) HERE = dirname(abspath(__file__))
@ -49,9 +48,8 @@ def load_zones():
def load_domains(): def load_domains():
load_zones() load_zones()
global DOMAINS global DOMAINS
if DOMAINS is not None: if DOMAINS:
return return
DOMAINS = {}
for zone_name, zone in ZONES_SERVER['zones'].items(): for zone_name, zone in ZONES_SERVER['zones'].items():
if 'domain_name' in zone: if 'domain_name' in zone:
hosts = [] hosts = []

View file

@ -1,5 +1,6 @@
MULTI_FUNCTIONS = [] MULTI_FUNCTIONS = []
CONFIGS = {} CONFIGS = {}
DOMAINS = {}
def _(s): def _(s):