From 0f4e49149d90d2c7323c3b02774be7c2ac80f967 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 6 Mar 2022 18:54:57 +0100 Subject: [PATCH] add tmpfile_dest_dir option --- doc/dev/config.md | 5 +++ src/rougail/config.py | 1 + src/rougail/template/systemd.py | 31 ++++++++--------- .../01base_file_tmpfile_dest_dir/00-base.xml | 16 +++++++++ .../01base_file_tmpfile_dest_dir/__init__.py | 0 .../file_tmpfile_dest_dir | 0 .../makedict/after.json | 30 ++++++++++++++++ .../makedict/base.json | 9 +++++ .../makedict/before.json | 30 ++++++++++++++++ .../result/etc/file | 2 ++ .../result/etc/file2 | 2 ++ .../result/tmpfiles.d/0rougail.conf | 2 ++ .../tiramisu/base.py | 34 +++++++++++++++++++ .../01base_file_tmpfile_dest_dir/tmpl/file | 2 ++ .../01base_file_tmpfile_dest_dir/tmpl/file2 | 2 ++ tests/test_3_template.py | 4 +++ 16 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/00-base.xml create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/__init__.py create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/file_tmpfile_dest_dir create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/after.json create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/base.json create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/before.json create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file2 create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/result/tmpfiles.d/0rougail.conf create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/tiramisu/base.py create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file create mode 100644 tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file2 diff --git a/doc/dev/config.md b/doc/dev/config.md index 401d5985b..7a8cad1fa 100644 --- a/doc/dev/config.md +++ b/doc/dev/config.md @@ -102,6 +102,11 @@ Le répertoire de temporaire est géré dans la clef "tmp_dir" et a comme valeur Le répertoire de destination des fichiers générés est géré dans la clef "destinations_dir" et a comme valeur par défaut : "/srv/rougail/destinations". +### Le nom du répertoire où se trouve les fichiers pour tmpfile.d + +Le répertoire où se trouve les fichiers tmpfile.d sont par défaut dans "/usr/local/lib", il est possible de changer ce répertoire avec la clef "tmpfile_dest_dir". + + ## La configuration par défaut des fichiers ### Le moteur de templates par défaut diff --git a/src/rougail/config.py b/src/rougail/config.py index db66fcb26..d4750e571 100644 --- a/src/rougail/config.py +++ b/src/rougail/config.py @@ -40,6 +40,7 @@ RougailConfig = {'dictionaries_dir': [join(ROUGAILROOT, 'dictionaries')], 'tmp_dir': join(ROUGAILROOT, 'tmp'), 'dtdfilename': join(DTDDIR, 'rougail.dtd'), 'functions_file': join(ROUGAILROOT, 'functions.py'), + 'tmpfile_dest_dir': '/usr/local/lib', 'variable_namespace': 'rougail', 'auto_freeze_variable': 'server_deployed', 'internal_functions': [], diff --git a/src/rougail/template/systemd.py b/src/rougail/template/systemd.py index ba3ef5761..b32a285d4 100644 --- a/src/rougail/template/systemd.py +++ b/src/rougail/template/systemd.py @@ -38,18 +38,24 @@ IPAddressDeny=any """ -ROUGAIL_DEST = '/usr/local/lib' ROUGAIL_GLOBAL_SYSTEMD_FILE = '/usr/lib/systemd/system' ROUGAIL_DEST_FILE = '/tmpfiles.d/0rougail.conf' LOCAL_DIR = ('/etc/', '/var/', '/srv/') -ROUGAIL_TMPL_TEMPLATE = f"""%def display(%%file, %%filename) +class RougailSystemdTemplate(RougailBaseTemplate): + def __init__(self, # pylint: disable=R0913 + config: 'Config', + rougailconfig: 'RougailConfig'=None, + ) -> None: + self.ip_per_service = None + super().__init__(config, rougailconfig) + self.rougail_tmpl_template = f"""%def display(%%file, %%filename) """ -TMP_LOCAL_DIR = (f"%%filename.startswith('{local_dir}')" for local_dir in LOCAL_DIR) -ROUGAIL_TMPL_TEMPLATE += '%if ' + ' or '.join(TMP_LOCAL_DIR) -ROUGAIL_TMPL_TEMPLATE += f""" -C %%filename %%file.mode %%file.owner %%file.group - {ROUGAIL_DEST}%%filename + tmp_local_dir = (f"%%filename.startswith('{local_dir}')" for local_dir in LOCAL_DIR) + self.rougail_tmpl_template += '%if ' + ' or '.join(tmp_local_dir) + self.rougail_tmpl_template += f""" +C %%filename %%file.mode %%file.owner %%file.group - {self.rougailconfig['tmpfile_dest_dir']}%%filename %end if %end def %for %%service in %%services @@ -69,15 +75,6 @@ C %%filename %%file.mode %%file.owner %%file.group - {ROUGAIL_DEST}%%filename %end for """ - -class RougailSystemdTemplate(RougailBaseTemplate): - def __init__(self, # pylint: disable=R0913 - config: 'Config', - rougailconfig: 'RougailConfig'=None, - ) -> None: - self.ip_per_service = None - super().__init__(config, rougailconfig) - def get_data_files(self, filevar: Dict, destfile: str, @@ -158,7 +155,7 @@ class RougailSystemdTemplate(RougailBaseTemplate): if global_service: source_filename = f'{ROUGAIL_GLOBAL_SYSTEMD_FILE}/{service_name}' else: - source_filename = f'{ROUGAIL_DEST}/systemd/system/{service_name}' + source_filename = f"{self.rougailconfig['tmpfile_dest_dir']}/systemd/system/{service_name}" symlink(source_filename, filename) def post_instance_service(self, @@ -204,7 +201,7 @@ class RougailSystemdTemplate(RougailBaseTemplate): makedirs(dirname(destfilename), exist_ok=True) self.log.info(_(f"creole processing: '{destfilename}'")) self.engines['creole'].process(filename=None, - source=ROUGAIL_TMPL_TEMPLATE, + source=self.rougail_tmpl_template, true_destfilename=ROUGAIL_DEST_FILE, destfilename=destfilename, destdir=self.destinations_dir, diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/00-base.xml b/tests/dictionaries/01base_file_tmpfile_dest_dir/00-base.xml new file mode 100644 index 000000000..8cb2c47b3 --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/00-base.xml @@ -0,0 +1,16 @@ + + + + + /etc/file + /etc/file2 + + + + + + non + + + + diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/__init__.py b/tests/dictionaries/01base_file_tmpfile_dest_dir/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/file_tmpfile_dest_dir b/tests/dictionaries/01base_file_tmpfile_dest_dir/file_tmpfile_dest_dir new file mode 100644 index 000000000..e69de29bb diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/after.json b/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/after.json new file mode 100644 index 000000000..70ca0c161 --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/after.json @@ -0,0 +1,30 @@ +{ + "rougail.general.mode_conteneur_actif": { + "owner": "default", + "value": "non" + }, + "services.test_service.files.file.name": { + "owner": "default", + "value": "/etc/file" + }, + "services.test_service.files.file.activate": { + "owner": "default", + "value": true + }, + "services.test_service.files.file2.name": { + "owner": "default", + "value": "/etc/file2" + }, + "services.test_service.files.file2.activate": { + "owner": "default", + "value": true + }, + "services.test_service.activate": { + "owner": "default", + "value": true + }, + "services.test_service.manage": { + "owner": "default", + "value": true + } +} diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/base.json b/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/base.json new file mode 100644 index 000000000..8f8bc2b3e --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/base.json @@ -0,0 +1,9 @@ +{ + "rougail.general.mode_conteneur_actif": "non", + "services.test_service.files.file.name": "/etc/file", + "services.test_service.files.file.activate": true, + "services.test_service.files.file2.name": "/etc/file2", + "services.test_service.files.file2.activate": true, + "services.test_service.activate": true, + "services.test_service.manage": true +} diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/before.json b/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/before.json new file mode 100644 index 000000000..70ca0c161 --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/makedict/before.json @@ -0,0 +1,30 @@ +{ + "rougail.general.mode_conteneur_actif": { + "owner": "default", + "value": "non" + }, + "services.test_service.files.file.name": { + "owner": "default", + "value": "/etc/file" + }, + "services.test_service.files.file.activate": { + "owner": "default", + "value": true + }, + "services.test_service.files.file2.name": { + "owner": "default", + "value": "/etc/file2" + }, + "services.test_service.files.file2.activate": { + "owner": "default", + "value": true + }, + "services.test_service.activate": { + "owner": "default", + "value": true + }, + "services.test_service.manage": { + "owner": "default", + "value": true + } +} diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file b/tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file new file mode 100644 index 000000000..4089fbcce --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file @@ -0,0 +1,2 @@ +non +non diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file2 b/tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file2 new file mode 100644 index 000000000..4089fbcce --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/result/etc/file2 @@ -0,0 +1,2 @@ +non +non diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/result/tmpfiles.d/0rougail.conf b/tests/dictionaries/01base_file_tmpfile_dest_dir/result/tmpfiles.d/0rougail.conf new file mode 100644 index 000000000..9b66eccfc --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/result/tmpfiles.d/0rougail.conf @@ -0,0 +1,2 @@ +C /etc/file 0644 root root - /test/new/file/etc/file +C /etc/file2 0644 root root - /test/new/file/etc/file2 diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/tiramisu/base.py b/tests/dictionaries/01base_file_tmpfile_dest_dir/tiramisu/base.py new file mode 100644 index 000000000..59673559a --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/tiramisu/base.py @@ -0,0 +1,34 @@ +from importlib.machinery import SourceFileLoader as _SourceFileLoader +from importlib.util import spec_from_loader as _spec_from_loader, module_from_spec as _module_from_spec +class func: + pass +_loader = _SourceFileLoader('func', 'tests/dictionaries/../eosfunc/test.py') +_spec = _spec_from_loader(_loader.name, _loader) +_func = _module_from_spec(_spec) +_loader.exec_module(_func) +for function in dir(_func): + if function.startswith('_'): + continue + setattr(func, function, getattr(_func, function)) +try: + from tiramisu3 import * +except: + from tiramisu import * +option_3 = StrOption(name="mode_conteneur_actif", doc="Description", default="non", properties=frozenset({"mandatory", "normal"})) +option_2 = OptionDescription(name="general", doc="general", children=[option_3], properties=frozenset({"normal"})) +option_1 = OptionDescription(name="rougail", doc="rougail", children=[option_2]) +option_8 = FilenameOption(name="name", doc="name", default="/etc/file") +option_9 = BoolOption(name="activate", doc="activate", default=True) +option_7 = OptionDescription(name="file", doc="file", children=[option_8, option_9]) +option_7.impl_set_information('source', "file") +option_11 = FilenameOption(name="name", doc="name", default="/etc/file2") +option_12 = BoolOption(name="activate", doc="activate", default=True) +option_10 = OptionDescription(name="file2", doc="file2", children=[option_11, option_12]) +option_10.impl_set_information('engine', "jinja2") +option_10.impl_set_information('source', "file2") +option_6 = OptionDescription(name="files", doc="files", children=[option_7, option_10]) +option_13 = BoolOption(name="activate", doc="activate", default=True) +option_14 = BoolOption(name="manage", doc="manage", default=True) +option_5 = OptionDescription(name="test_service", doc="test.service", children=[option_6, option_13, option_14]) +option_4 = OptionDescription(name="services", doc="services", children=[option_5], properties=frozenset({"hidden"})) +option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[option_1, option_4]) diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file b/tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file new file mode 100644 index 000000000..27ff6834c --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file @@ -0,0 +1,2 @@ +%%mode_conteneur_actif +%%rougail.general.mode_conteneur_actif diff --git a/tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file2 b/tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file2 new file mode 100644 index 000000000..6d214113d --- /dev/null +++ b/tests/dictionaries/01base_file_tmpfile_dest_dir/tmpl/file2 @@ -0,0 +1,2 @@ +{{ mode_conteneur_actif }} +{{ rougail.general.mode_conteneur_actif }} diff --git a/tests/test_3_template.py b/tests/test_3_template.py index 6879310fb..4507a650e 100644 --- a/tests/test_3_template.py +++ b/tests/test_3_template.py @@ -65,6 +65,10 @@ async def test_dictionary(test_dir): RougailConfig['tmp_dir'] = tmp_dir RougailConfig['functions_file'] = funcs_file RougailConfig['destinations_dir'] = dest_dir + if isfile(join(test_dir, 'file_tmpfile_dest_dir')): + RougailConfig['tmpfile_dest_dir'] = '/test/new/file' + else: + RougailConfig['tmpfile_dest_dir'] = '/usr/local/lib' engine = RougailSystemdTemplate(config) await engine.instance_files() list_templates = set()