risotto/bootstrap.py
Emmanuel Garette 382a5322a6 split code
2022-06-24 19:02:45 +02:00

99 lines
3.3 KiB
Python
Executable file

#!/usr/bin/env python3
from asyncio import run
from os import listdir, link, makedirs
from os.path import isdir, join
from shutil import rmtree
from copy import copy
from rougail import RougailSystemdTemplate
from risotto.utils import CONFIGS, RISOTTO_CONFIG, SERVERS, value_pprint
from risotto.image import load
INSTALL_DIR = RISOTTO_CONFIG['directories']['dest']
CONFIG_DEST_DIR = 'configurations'
CONFIG_DIFF_DIR = 'diff'
SRV_DEST_DIR = 'srv'
def tiramisu_display_name(kls,
dyn_name: 'Base'=None,
suffix: str=None,
) -> str:
# FIXME
if dyn_name is not None:
name = kls.impl_getpath() + str(suffix)
else:
name = kls.impl_getpath()
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():
if isdir(INSTALL_DIR):
rmtree(INSTALL_DIR)
makedirs(INSTALL_DIR)
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:
module_name = CONFIGS[server_name]['module_name']
add_srv = CONFIGS[server_name]['add_srv']
cfg = CONFIGS[server_name]['templates_informations']
cfg['tmp_dir'] = 'tmp'
cfg['destinations_dir'] = join(INSTALL_DIR, module_name, CONFIG_DEST_DIR, server_name)
if isdir('tmp'):
rmtree('tmp')
makedirs(cfg['tmp_dir'])
makedirs(cfg['destinations_dir'])
if add_srv:
srv = join(INSTALL_DIR, SRV_DEST_DIR, server_name)
else:
srv = None
await templates(server_name, **CONFIGS[server_name], srv=srv)
for server_name in SERVERS:
config = CONFIGS[server_name]['config']
await config.property.read_write()
try:
# pass
await config.option('general.hide_secret').value.set(True)
except AttributeError:
# if rougail.general.hide_secret not exists
pass
await config.property.read_only()
for server_name in SERVERS:
module_name = CONFIGS[server_name]['module_name']
destinations_dir = join(INSTALL_DIR, module_name, CONFIG_DIFF_DIR, server_name)
makedirs(destinations_dir)
CONFIGS[server_name]['templates_informations']['destinations_dir'] = destinations_dir
await templates(server_name, **CONFIGS[server_name])
for module_name, cfg in module_infos.items():
with open(join(INSTALL_DIR, module_name, 'install_machines'), 'w') as fh:
for server_name in cfg.servers:
fh.write(f'./install_machine {module_name} {server_name}\n')
run(main())