risotto/bootstrap.py

103 lines
3.4 KiB
Python
Raw Normal View History

2022-03-08 20:47:55 +01:00
#!/usr/bin/env python3
from asyncio import run
2022-06-24 19:02:45 +02:00
from os import listdir, link, makedirs
from os.path import isdir, join
from shutil import rmtree
2022-03-08 20:47:55 +01:00
from copy import copy
2022-06-24 19:02:45 +02:00
from rougail import RougailSystemdTemplate
2022-03-08 20:47:55 +01:00
2022-06-24 19:02:45 +02:00
from risotto.utils import CONFIGS, RISOTTO_CONFIG, SERVERS, value_pprint
from risotto.image import load
2022-03-08 20:47:55 +01:00
2022-03-11 18:39:32 +01:00
2022-06-24 19:02:45 +02:00
INSTALL_DIR = RISOTTO_CONFIG['directories']['dest']
2022-03-08 20:47:55 +01:00
CONFIG_DEST_DIR = 'configurations'
2022-06-24 19:02:45 +02:00
CONFIG_DIFF_DIR = 'diff'
2022-03-08 20:47:55 +01:00
SRV_DEST_DIR = 'srv'
def tiramisu_display_name(kls,
dyn_name: 'Base'=None,
suffix: str=None,
) -> str:
2022-06-24 19:02:45 +02:00
# FIXME
2022-03-08 20:47:55 +01:00
if dyn_name is not None:
2022-06-24 19:02:45 +02:00
name = kls.impl_getpath() + str(suffix)
2022-03-08 20:47:55 +01:00
else:
name = kls.impl_getpath()
return name
2022-06-24 19:02:45 +02:00
async def templates(server_name,
config,
templates_informations,
srv=False,
**kwargs,
):
2022-03-08 20:47:55 +01:00
values = await config.value.dict()
2022-06-24 19:02:45 +02:00
engine = RougailSystemdTemplate(config, templates_informations)
2022-04-28 21:46:26 +02:00
# if server_name == 'dovecot.in.silique.fr':
2022-03-08 20:47:55 +01:00
# 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)
2022-06-24 19:02:45 +02:00
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()
2022-06-26 19:34:26 +02:00
for server_name in SERVERS:
config = CONFIGS[server_name]['config']
await config.value.dict()
2022-06-24 19:02:45 +02:00
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])
2022-03-08 20:47:55 +01:00
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['infos'].servers:
2022-03-08 20:47:55 +01:00
fh.write(f'./install_machine {module_name} {server_name}\n')
run(main())