copy original template

This commit is contained in:
Emmanuel Garette 2022-07-01 18:57:18 +02:00
parent cb1ab19099
commit 6b65f80919
3 changed files with 58 additions and 38 deletions

View file

@ -6,15 +6,15 @@ from os.path import isdir, join
from shutil import rmtree from shutil import rmtree
from copy import copy from copy import copy
from rougail import RougailSystemdTemplate from risotto.utils import CONFIGS, RISOTTO_CONFIG, SERVERS
from risotto.utils import CONFIGS, RISOTTO_CONFIG, SERVERS, value_pprint
from risotto.image import load from risotto.image import load
from risotto.machine import templates
INSTALL_DIR = RISOTTO_CONFIG['directories']['dest'] INSTALL_DIR = RISOTTO_CONFIG['directories']['dest']
CONFIG_DEST_DIR = 'configurations' CONFIG_DEST_DIR = 'configurations'
CONFIG_DIFF_DIR = 'diff' CONFIG_DIFF_DIR = 'diff'
CONFIG_ORI_DIR = 'ori'
SRV_DEST_DIR = 'srv' SRV_DEST_DIR = 'srv'
@ -30,35 +30,11 @@ def tiramisu_display_name(kls,
return name return name
async def templates(server_name,
config,
templates_informations,
srv=False,
**kwargs,
):
values = await config.value.dict()
engine = RougailSystemdTemplate(config, templates_informations)
# if server_name == 'dovecot.in.silique.fr':
# print()
# print(f'=== Configuration: {server_name} ===')
# pprint(values)
try:
await engine.instance_files()
except Exception as err:
print()
print(f'=== Configuration: {server_name} ===')
await value_pprint(values, config)
raise err from err
if srv:
makedirs(srv)
async def main(): async def main():
if isdir(INSTALL_DIR): if isdir(INSTALL_DIR):
rmtree(INSTALL_DIR) rmtree(INSTALL_DIR)
makedirs(INSTALL_DIR) makedirs(INSTALL_DIR)
module_infos = await load(display_name=tiramisu_display_name, clean_directories=True, copy_manual_dir=True) module_infos = await load(display_name=tiramisu_display_name, clean_directories=True, copy_manual_dir=True)
# pprint(await CONFIGS['lemonldap.in.silique.fr'][0].value.dict())
for server_name in SERVERS: for server_name in SERVERS:
module_name = CONFIGS[server_name]['module_name'] module_name = CONFIGS[server_name]['module_name']
add_srv = CONFIGS[server_name]['add_srv'] add_srv = CONFIGS[server_name]['add_srv']
@ -95,7 +71,15 @@ async def main():
await templates(server_name, **CONFIGS[server_name]) await templates(server_name, **CONFIGS[server_name])
for module_name, cfg in module_infos.items(): for module_name, cfg in module_infos.items():
with open(join(INSTALL_DIR, module_name, 'install_machines'), 'w') as fh: with open(join(INSTALL_DIR, module_name, 'install_machines'), 'w') as fh:
for server_name in cfg['infos'].servers: for idx, server_name in enumerate(cfg['infos'].servers):
if not idx:
destinations_dir = join(INSTALL_DIR, module_name, CONFIG_ORI_DIR)
makedirs(destinations_dir)
CONFIGS[server_name]['templates_informations']['destinations_dir'] = destinations_dir
await templates(server_name,
**CONFIGS[server_name],
just_copy=True,
)
fh.write(f'./install_machine {module_name} {server_name}\n') fh.write(f'./install_machine {module_name} {server_name}\n')

View file

@ -221,25 +221,20 @@ async def valid_mandatories(server_name, config):
raise Exception(f'server "{server_name}" has mandatories variables without values "{", ".join(mandatories)}"') raise Exception(f'server "{server_name}" has mandatories variables without values "{", ".join(mandatories)}"')
async def load(display_name=None, def load_config(copy_manual_dir=False, clean_directories=False):
clean_directories=False, module_infos = {}
copy_manual_dir=False, applications = list_applications()
hide_secret=False,
):
with open('servers.json', 'r') as server_fh: with open('servers.json', 'r') as server_fh:
jsonfile = json_load(server_fh) jsonfile = json_load(server_fh)
SERVERS.update(jsonfile['servers']) SERVERS.update(jsonfile['servers'])
modules = jsonfile['modules'] modules = jsonfile['modules']
module_infos = {}
applications = list_applications()
# load images
for module_name, datas in modules.items(): for module_name, datas in modules.items():
providers = {}
install_dir = join(RISOTTO_CONFIG['directories']['dest'], module_name) install_dir = join(RISOTTO_CONFIG['directories']['dest'], module_name)
if clean_directories: if clean_directories:
if isdir(install_dir): if isdir(install_dir):
rmtree(install_dir) rmtree(install_dir)
makedirs(install_dir) makedirs(install_dir)
providers = {}
module_infos[module_name] = {'infos': load_image_informations(install_dir, module_infos[module_name] = {'infos': load_image_informations(install_dir,
datas, datas,
applications, applications,
@ -247,7 +242,19 @@ async def load(display_name=None,
providers, providers,
), ),
'providers': providers, 'providers': providers,
'install_dir': install_dir,
} }
return module_infos
async def load(display_name=None,
clean_directories=False,
copy_manual_dir=False,
hide_secret=False,
):
# load images
module_infos = load_config(copy_manual_dir, clean_directories)
# load machines # load machines
ZONES_SERVER['providers'] = {} ZONES_SERVER['providers'] = {}
for server_name, datas in SERVERS.items(): for server_name, datas in SERVERS.items():

View file

@ -6,8 +6,9 @@ from typing import Any
from tiramisu import Config from tiramisu import Config
from tiramisu.error import ValueWarning from tiramisu.error import ValueWarning
from rougail import RougailConfig, RougailConvert from rougail import RougailConfig, RougailConvert
from .utils import MULTI_FUNCTIONS, CONFIGS, DOMAINS from .utils import MULTI_FUNCTIONS, CONFIGS, DOMAINS, value_pprint
from rougail.utils import normalize_family from rougail.utils import normalize_family
from rougail import RougailSystemdTemplate
ROUGAIL_NAMESPACE = 'general' ROUGAIL_NAMESPACE = 'general'
@ -396,3 +397,31 @@ async def load_machine_config(server_name: str,
'add_srv': add_srv, 'add_srv': add_srv,
'providers': module_info['providers'], 'providers': module_info['providers'],
} }
async def templates(server_name,
config,
templates_informations,
srv=False,
just_copy=False,
**kwargs,
):
values = await config.value.dict()
engine = RougailSystemdTemplate(config, templates_informations)
if just_copy:
for eng in engine.engines:
if eng != 'none':
engine.engines[eng] = engine.engines['none']
# if server_name == 'dovecot.in.silique.fr':
# print()
# print(f'=== Configuration: {server_name} ===')
# pprint(values)
try:
await engine.instance_files()
except Exception as err:
print()
print(f'=== Configuration: {server_name} ===')
await value_pprint(values, config)
raise err from err
if srv:
makedirs(srv)