allow multiple IP

This commit is contained in:
2021-02-26 22:22:38 +01:00
parent 3e7ae41061
commit 88f864cd2a
15 changed files with 166 additions and 44 deletions

View File

@ -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}"')

View File

@ -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

View File

@ -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:])