risotto/ansible/action_plugins/rougail.py

56 lines
2.1 KiB
Python
Raw Normal View History

2022-10-01 22:33:11 +02:00
#!/usr/bin/python3
from ansible.plugins.action import ActionBase
from asyncio import run
from shutil import rmtree
from os.path import isdir, join
from os import makedirs
from risotto.machine import templates, load, ROUGAIL_NAMESPACE
from risotto.utils import RISOTTO_CONFIG
from rougail.utils import normalize_family
TIRAMISU_CACHE = 'tiramisu_cache.py'
VALUES_CACHE = 'values_cache.py'
INSTALL_DIR = RISOTTO_CONFIG['directories']['dest']
async def build_files(server_name, is_host):
module_infos, rougailconfig, config = await load(TIRAMISU_CACHE,
VALUES_CACHE,
)
subconfig = config.option(normalize_family(server_name))
module_name = await subconfig.option(await subconfig.information.get('provider:global:module_name')).value.get()
module_info = module_infos[module_name]
rougailconfig['tmp_dir'] = 'tmp'
rougailconfig['destinations_dir'] = INSTALL_DIR
rougailconfig['templates_dir'] = module_info['infos'].templates_dir
if is_host:
tmpfile = await subconfig.option(f'{ROUGAIL_NAMESPACE}.host_install_dir').value.get()
rougailconfig['tmpfile_dest_dir'] = f'{tmpfile}'
rougailconfig['default_systemd_directory'] = '/usr/local/lib/systemd'
else:
rougailconfig['tmpfile_dest_dir'] = '/usr/local/lib'
rougailconfig['default_systemd_directory'] = '/systemd'
if isdir(rougailconfig['destinations_dir']):
rmtree(rougailconfig['destinations_dir'])
if isdir(rougailconfig['tmp_dir']):
rmtree(rougailconfig['tmp_dir'])
makedirs(rougailconfig['tmp_dir'])
makedirs(rougailconfig['destinations_dir'])
await templates(server_name,
subconfig,
rougailconfig,
)
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
super(ActionModule, self).run(tmp, task_vars)
module_args = self._task.args.copy()
name = module_args['hostname']
is_host = module_args['is_host']
run(build_files(name, is_host))
return dict(ansible_facts=dict({}))