"""Template langage for Rougail to create file and systemd file Cadoles (http://www.cadoles.com) Copyright (C) 2021 distribued with GPL-2 or later license This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License 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, Any from os import makedirs, symlink, chmod from os.path import dirname, isfile, join from ipaddress import ip_network from .base import RougailBaseTemplate from ..i18n import _ from ..error import FileNotFound, TemplateError ROUGAIL_IP_TEMPLATE = """[Service] %for %%ip in %%rougail_variable IPAddressAllow=%%ip %end for 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) """ 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 %end if %end def %for %%service in %%services %if %%service.activate is True and %%hasattr(%%service, 'files') %for %%file in %%service.files %if %%file.activate is True and %%file.included != 'content' %if %%isinstance(%%file.name, list) %for %%filename in %%file.name %%display(%%file, %%filename)%slurp %end for %else %%display(%%file, %%file.name)%slurp %end if %end if %end for %end if %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, service_name: str, variable, idx: int, ) -> tuple: source = filevar['source'] if not isfile(source): # pragma: no cover raise FileNotFound(_(f'Source file "{source}" does not exist in {", ".join(self.templates_dir)}')) tmp_file = join(self.tmp_dir, source) #self.instance_file(fill, 'files') if variable: var = variable[idx] else: var = None return tmp_file, None, destfile, var def get_data_overrides(self, filevar: Dict, destfile, service_name: str, *args, ) -> tuple: source = filevar['source'] if not isfile(source): # pragma: no cover raise FileNotFound(_(f'Override source file "{source}" does not exist in {", ".join(self.templates_dir)}')) tmp_file = join(self.tmp_dir, source) service_name = filevar['name'] destfile = f'/systemd/system/{service_name}.d/rougail.conf' return tmp_file, None, destfile, None def get_data_ip(self, filevar: Dict, 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["netmask"], list): netmask = filevar['netmask'][idx] else: netmask = filevar['netmask'] self.ip_per_service.append(str(ip_network(f'{ip}/{netmask}'))) elif ip: self.ip_per_service.append(ip) def get_data_service(self, servicevar: Dict, info, service_name: str, *args, ): tmp_file = join(self.tmp_dir, service_name) var = None destfile = f'/systemd/system/{service_name}' return tmp_file, None, destfile, var def desactive_service(self, service_name: str, ): filename = f'{self.destinations_dir}/systemd/system/{service_name}' makedirs(dirname(filename), exist_ok=True) symlink('/dev/null', filename) def target_service(self, service_name: str, target_name: str, global_service: str, ): filename = f'{self.destinations_dir}/systemd/system/{target_name}.target.wants/{service_name}' makedirs(dirname(filename), exist_ok=True) if global_service: source_filename = f'{ROUGAIL_GLOBAL_SYSTEMD_FILE}/{service_name}' else: source_filename = f'{ROUGAIL_DEST}/systemd/system/{service_name}' symlink(source_filename, 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}.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, index=None, rougail_variables_dict=self.rougail_variables_dict, eosfunc=self.eosfunc, ) self.ip_per_service = None def process(self, filename: str, destfilename: str, mode: str, owner: str, group: str, ) -> None: for local_dir in LOCAL_DIR: if filename.startswith(local_dir): return if owner not in [None, self.rougailconfig['default_files_owner']]: raise TemplateError(_(f'cannot change owner of file {destfilename}')) if group not in [None, self.rougailconfig['default_files_group']]: raise TemplateError(_(f'cannot change group of file {destfilename}')) if mode not in [None, self.rougailconfig['default_files_mode']]: chmod(destfilename, eval(f'0o{mode}')) def post_instance(self): destfilename = join(self.destinations_dir, ROUGAIL_DEST_FILE[1:]) makedirs(dirname(destfilename), exist_ok=True) self.log.info(_(f"creole processing: '{destfilename}'")) self.engines['creole'].process(filename=None, source=ROUGAIL_TMPL_TEMPLATE, true_destfilename=ROUGAIL_DEST_FILE, destfilename=destfilename, destdir=self.destinations_dir, variable=None, index=None, rougail_variables_dict=self.rougail_variables_dict, eosfunc=self.eosfunc, )