From 88f864cd2a0865aa4e5f4bf23b9cb12c4570bd6a Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 26 Feb 2021 22:22:38 +0100 Subject: [PATCH] allow multiple IP --- src/rougail/annotator/service.py | 6 --- src/rougail/template/base.py | 19 ++++--- src/rougail/template/systemd.py | 54 +++++++++++++------ .../10leadership_accent/makedict/after.json | 4 -- .../10leadership_accent/makedict/before.json | 4 -- .../makedict/after.json | 4 -- .../makedict/before.json | 4 -- .../00-base.xml | 5 +- .../__init__.py | 0 .../70services_ip_twice/makedict/after.json | 34 ++++++++++++ .../70services_ip_twice/makedict/base.json | 10 ++++ .../70services_ip_twice/makedict/before.json | 34 ++++++++++++ .../system/nut.service.d/rougail_ip.conf | 4 ++ .../result/tmpfiles.d/rougail.conf} | 0 .../70services_ip_twice/tiramisu/base.py | 28 ++++++++++ 15 files changed, 166 insertions(+), 44 deletions(-) rename tests/dictionaries/{80services_ip_twice => 70services_ip_twice}/00-base.xml (69%) rename tests/dictionaries/{80services_ip_twice => 70services_ip_twice}/__init__.py (100%) create mode 100644 tests/dictionaries/70services_ip_twice/makedict/after.json create mode 100644 tests/dictionaries/70services_ip_twice/makedict/base.json create mode 100644 tests/dictionaries/70services_ip_twice/makedict/before.json create mode 100644 tests/dictionaries/70services_ip_twice/result/systemd/system/nut.service.d/rougail_ip.conf rename tests/dictionaries/{80services_ip_twice/errno_67 => 70services_ip_twice/result/tmpfiles.d/rougail.conf} (100%) create mode 100644 tests/dictionaries/70services_ip_twice/tiramisu/base.py diff --git a/src/rougail/annotator/service.py b/src/rougail/annotator/service.py index 61e82d11..f040743e 100644 --- a/src/rougail/annotator/service.py +++ b/src/rougail/annotator/service.py @@ -52,7 +52,6 @@ class ServiceAnnotator: """ def __init__(self, objectspace): self.objectspace = objectspace - self.uniq_ip = [] self.uniq_overrides = [] if 'network_type' not in self.objectspace.types: self.objectspace.types['network_type'] = self.objectspace.types['ip_type'] @@ -287,11 +286,6 @@ class ServiceAnnotator: ip, service_name, ) -> None: - if service_name in self.uniq_ip: - msg = _('only one IP is allowed by service, ' - 'please use a variable multiple if you want have more than one IP') - raise DictConsistencyError(msg, 67, ip.xmlfiles) - self.uniq_ip.append(service_name) variable = self.objectspace.paths.get_variable(ip.name, ip.xmlfiles) if variable.type not in ['ip', 'network', 'network_cidr']: msg = _(f'ip cannot be linked to "{variable.type}" variable "{ip.name}"') diff --git a/src/rougail/template/base.py b/src/rougail/template/base.py index 35fe8232..a609d581 100644 --- a/src/rougail/template/base.py +++ b/src/rougail/template/base.py @@ -254,12 +254,15 @@ class RougailBaseTemplate: else: var = None func = f'_instance_{type}' - filename, source, destfile, var = getattr(self, func)(filevar, - filename, - service_name, - variable, - idx, - ) + data = getattr(self, func)(filevar, + filename, + service_name, + variable, + idx, + ) + if data is None: + continue + filename, source, destfile, var = data destfilename = join(self.destinations_dir, destfile[1:]) makedirs(dirname(destfilename), exist_ok=True) self.log.info(_(f"{filevar['engine']} processing: '{destfilename}'")) @@ -307,6 +310,7 @@ class RougailBaseTemplate: self.instance_file(fill, type_, service_name) else: self.log.debug(_("Instantiation of file '{filename}' disabled")) + self.post_instance_service(service_name) self.post_instance() chdir(ori_dir) @@ -315,6 +319,9 @@ class RougailBaseTemplate: ): raise NotImplementedError(_('cannot desactivate a service')) + def post_instance_service(self, service_name): # pragma: no cover + pass + def post_instance(self): # pragma: no cover pass diff --git a/src/rougail/template/systemd.py b/src/rougail/template/systemd.py index e5ec065e..a81ea321 100644 --- a/src/rougail/template/systemd.py +++ b/src/rougail/template/systemd.py @@ -20,7 +20,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ -from typing import Dict +from typing import Dict, Any from os import makedirs, symlink from os.path import dirname, isfile, join from ipaddress import ip_network @@ -62,6 +62,13 @@ z %%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) + def _instance_files(self, filevar: Dict, destfile: str, @@ -95,25 +102,22 @@ class RougailSystemdTemplate(RougailBaseTemplate): def _instance_ip(self, filevar: Dict, - destfile, + ip, service_name: str, + var: Any, + idx: int, *args, ) -> tuple: + if self.ip_per_service is None: + self.ip_per_service = [] if 'netmask' in filevar: - if isinstance(filevar['name'], list): - variable = [str(ip_network(f'{net}/{mask}')) - for net, mask in zip(filevar['name'], filevar['netmask'])] + if isinstance(filevar["netmask"], list): + netmask = filevar['netmask'][idx] else: - variable = str(ip_network(f'{filevar["name"]}/{filevar["netmask"]}')) - else: - variable = filevar['name'] - if not isinstance(variable, list): - if variable is None: - variable = [] - else: - variable = [variable] - filevar['engine'] = 'creole' - return None, ROUGAIL_IP_TEMPLATE, f'/systemd/system/{service_name}.service.d/rougail_ip.conf', variable + netmask = filevar['netmask'] + self.ip_per_service.append(str(ip_network(f'{ip}/{netmask}'))) + elif ip: + self.ip_per_service.append(ip) def desactive_service(self, service_name: str, @@ -122,6 +126,26 @@ class RougailSystemdTemplate(RougailBaseTemplate): makedirs(dirname(filename), exist_ok=True) symlink('/dev/null', filename) + def post_instance_service(self, + service_name: str, + ) -> None: # pragma: no cover + if self.ip_per_service is None: + return + destfile = f'/systemd/system/{service_name}.service.d/rougail_ip.conf' + destfilename = join(self.destinations_dir, destfile[1:]) + makedirs(dirname(destfilename), exist_ok=True) + self.log.info(_(f"creole processing: '{destfilename}'")) + self.engines['creole'].process(filename=None, + source=ROUGAIL_IP_TEMPLATE, + true_destfilename=destfile, + destfilename=destfilename, + destdir=self.destinations_dir, + variable=self.ip_per_service, + rougail_variables_dict=self.rougail_variables_dict, + eosfunc=self.eosfunc, + ) + self.ip_per_service = None + def post_instance(self): destfile = '/tmpfiles.d/rougail.conf' destfilename = join(self.destinations_dir, destfile[1:]) diff --git a/tests/dictionaries/10leadership_accent/makedict/after.json b/tests/dictionaries/10leadership_accent/makedict/after.json index 6dedaf65..f77abfc0 100644 --- a/tests/dictionaries/10leadership_accent/makedict/after.json +++ b/tests/dictionaries/10leadership_accent/makedict/after.json @@ -14,9 +14,5 @@ "rougail.general1.leader.follower2": { "owner": [], "value": [] - }, - "rougail.general1.leader.follower3": { - "owner": [], - "value": [] } } diff --git a/tests/dictionaries/10leadership_accent/makedict/before.json b/tests/dictionaries/10leadership_accent/makedict/before.json index 6dedaf65..f77abfc0 100644 --- a/tests/dictionaries/10leadership_accent/makedict/before.json +++ b/tests/dictionaries/10leadership_accent/makedict/before.json @@ -14,9 +14,5 @@ "rougail.general1.leader.follower2": { "owner": [], "value": [] - }, - "rougail.general1.leader.follower3": { - "owner": [], - "value": [] } } diff --git a/tests/dictionaries/10leadership_accent_leader/makedict/after.json b/tests/dictionaries/10leadership_accent_leader/makedict/after.json index 6dedaf65..f77abfc0 100644 --- a/tests/dictionaries/10leadership_accent_leader/makedict/after.json +++ b/tests/dictionaries/10leadership_accent_leader/makedict/after.json @@ -14,9 +14,5 @@ "rougail.general1.leader.follower2": { "owner": [], "value": [] - }, - "rougail.general1.leader.follower3": { - "owner": [], - "value": [] } } diff --git a/tests/dictionaries/10leadership_accent_leader/makedict/before.json b/tests/dictionaries/10leadership_accent_leader/makedict/before.json index 6dedaf65..f77abfc0 100644 --- a/tests/dictionaries/10leadership_accent_leader/makedict/before.json +++ b/tests/dictionaries/10leadership_accent_leader/makedict/before.json @@ -14,9 +14,5 @@ "rougail.general1.leader.follower2": { "owner": [], "value": [] - }, - "rougail.general1.leader.follower3": { - "owner": [], - "value": [] } } diff --git a/tests/dictionaries/80services_ip_twice/00-base.xml b/tests/dictionaries/70services_ip_twice/00-base.xml similarity index 69% rename from tests/dictionaries/80services_ip_twice/00-base.xml rename to tests/dictionaries/70services_ip_twice/00-base.xml index 62e32106..16d6c77c 100644 --- a/tests/dictionaries/80services_ip_twice/00-base.xml +++ b/tests/dictionaries/70services_ip_twice/00-base.xml @@ -5,7 +5,7 @@ nut_monitor_host - nut_monitor_host + nut_monitor_host2 @@ -13,6 +13,9 @@ 192.168.0.1 + + 192.168.0.2 +