remove override and rougail.conf from template.py

This commit is contained in:
Emmanuel Garette 2020-07-08 16:20:06 +02:00
parent b021460b94
commit 8c5a9f2cd3
23 changed files with 151 additions and 92 deletions

View File

@ -129,7 +129,8 @@ class ServiceAnnotator:
family = self.gen_family(eltname) family = self.gen_family(eltname)
if isinstance(values, dict): if isinstance(values, dict):
values = list(values.values()) values = list(values.values())
family.family = self.make_group_from_elts(elttype, family.family = self.make_group_from_elts(service_name,
elttype,
values, values,
f'services.{service_name}.{eltname}', f'services.{service_name}.{eltname}',
) )
@ -138,6 +139,7 @@ class ServiceAnnotator:
self.space.services.service = families self.space.services.service = families
def make_group_from_elts(self, def make_group_from_elts(self,
service_name,
name, name,
elts, elts,
path, path,
@ -156,7 +158,11 @@ class ServiceAnnotator:
# try to launch _update_xxxx() function # try to launch _update_xxxx() function
update_elt = '_update_' + elt_name update_elt = '_update_' + elt_name
if hasattr(self, update_elt): if hasattr(self, update_elt):
getattr(self, update_elt)(elt, index, path) getattr(self, update_elt)(elt,
index,
path,
service_name,
)
if hasattr(elt, 'source'): if hasattr(elt, 'source'):
c_name = elt.source c_name = elt.source
@ -256,13 +262,29 @@ class ServiceAnnotator:
result_elts.append({'elt_name': name, 'elt': elt}) result_elts.append({'elt_name': name, 'elt': elt})
return result_elts return result_elts
def _update_override(self, *args): def _update_override(self,
self._update_file(*args) file_,
index,
service_path,
service_name,
):
file_.name = f'/systemd/system/{service_name}.service.d/rougail.conf'
# retrieve default value from File object
for attr in ['owner', 'group', 'mode']:
setattr(file_, attr, getattr(self.objectspace.file, attr))
if not hasattr(file_, 'source'):
file_.source = f'{service_name}.service'
self._update_file(file_,
index,
service_path,
service_name,
)
def _update_file(self, def _update_file(self,
file_, file_,
index, index,
service_path, service_path,
service_name,
): ):
if not hasattr(file_, 'file_type') or file_.file_type == "UnicodeOption": if not hasattr(file_, 'file_type') or file_.file_type == "UnicodeOption":
if not hasattr(file_, 'source'): if not hasattr(file_, 'source'):

View File

@ -83,7 +83,7 @@
<!ATTLIST digitalcertificate ca CDATA #REQUIRED > <!ATTLIST digitalcertificate ca CDATA #REQUIRED >
<!ELEMENT override EMPTY> <!ELEMENT override EMPTY>
<!ATTLIST override name CDATA #REQUIRED > <!ATTLIST override source CDATA #IMPLIED >
<!ATTLIST override templating (True|False) "True"> <!ATTLIST override templating (True|False) "True">
<!ELEMENT variables (family*, separators*)> <!ELEMENT variables (family*, separators*)>

View File

@ -254,6 +254,9 @@ class CreoleExtra:
def __repr__(self): def __repr__(self):
return self.suboption.__str__() return self.suboption.__str__()
def __iter__(self):
return iter(self.suboption.values())
class CreoleTemplateEngine: class CreoleTemplateEngine:
"""Engine to process Creole cheetah template """Engine to process Creole cheetah template
@ -264,17 +267,11 @@ class CreoleTemplateEngine:
distrib_dir: str, distrib_dir: str,
tmp_dir: str, tmp_dir: str,
dest_dir: str, dest_dir: str,
override_dest_dir: str,
tmpfile_name: str,
factory_prefix: str,
) -> None: ) -> None:
self.config = config self.config = config
self.dest_dir = dest_dir self.dest_dir = dest_dir
self.override_dest_dir = override_dest_dir
self.tmp_dir = tmp_dir self.tmp_dir = tmp_dir
self.distrib_dir = distrib_dir self.distrib_dir = distrib_dir
self.tmpfile_name = tmpfile_name
self.factory_prefix = factory_prefix
eos = {} eos = {}
if eosfunc_file is not None: if eosfunc_file is not None:
eosfunc = imp.load_source('eosfunc', eosfunc_file) eosfunc = imp.load_source('eosfunc', eosfunc_file)
@ -309,7 +306,8 @@ class CreoleTemplateEngine:
for family in await optiondescription.list('all'): for family in await optiondescription.list('all'):
variables = {} variables = {}
for variable in await family.list('all'): for variable in await family.list('all'):
if await variable.option.isoptiondescription() and await variable.option.isleadership(): if await variable.option.isoptiondescription():
if await variable.option.isleadership():
for idx, suboption in enumerate(await variable.list('all')): for idx, suboption in enumerate(await variable.list('all')):
if idx == 0: if idx == 0:
leader = CreoleLeader(await suboption.value.get()) leader = CreoleLeader(await suboption.value.get())
@ -319,10 +317,15 @@ class CreoleTemplateEngine:
await suboption.option.name(), await suboption.option.name(),
await suboption.option.path()) await suboption.option.path())
variables[leader_name] = leader variables[leader_name] = leader
else:
subfamilies = await self.load_eole_variables(await variable.option.name(),
variable,
)
variables[await variable.option.name()] = subfamilies
else: else:
variables[await variable.option.name()] = await variable.value.get() variables[await variable.option.name()] = await variable.value.get()
families[await family.option.name()] = CreoleExtra(variables) families[await family.option.name()] = CreoleExtra(variables)
self.rougail_variables_dict[namespace] = CreoleExtra(families) return CreoleExtra(families)
def patch_template(self, def patch_template(self,
filename: str): filename: str):
@ -352,6 +355,7 @@ class CreoleTemplateEngine:
def process(self, def process(self,
source: str, source: str,
true_destfilename: str,
destfilename: str, destfilename: str,
filevar: Dict, filevar: Dict,
variable: Any): variable: Any):
@ -363,7 +367,7 @@ class CreoleTemplateEngine:
cheetah_template = CheetahTemplate(source, cheetah_template = CheetahTemplate(source,
self.rougail_variables_dict, self.rougail_variables_dict,
self.eosfunc, self.eosfunc,
destfilename, true_destfilename,
variable, variable,
) )
data = str(cheetah_template) data = str(cheetah_template)
@ -378,8 +382,6 @@ class CreoleTemplateEngine:
def instance_file(self, def instance_file(self,
filevar: Dict, filevar: Dict,
systemd_rights: list,
override: bool,
service_name: str) -> None: service_name: str) -> None:
"""Run templatisation on one file """Run templatisation on one file
""" """
@ -388,18 +390,12 @@ class CreoleTemplateEngine:
variable = filevar['variable'] variable = filevar['variable']
else: else:
variable = None variable = None
if override:
filenames = [f'/systemd/system/{service_name}.service.d/rougail.conf']
else:
filenames = filevar['name'] filenames = filevar['name']
if not isinstance(filenames, list): if not isinstance(filenames, list):
filenames = [filenames] filenames = [filenames]
if variable: if variable:
variable = [variable] variable = [variable]
for idx, filename in enumerate(filenames): for idx, filename in enumerate(filenames):
if override:
destfilename = join(self.override_dest_dir, filename[1:])
else:
destfilename = join(self.dest_dir, filename[1:]) destfilename = join(self.dest_dir, filename[1:])
makedirs(dirname(destfilename), exist_ok=True) makedirs(dirname(destfilename), exist_ok=True)
if variable: if variable:
@ -409,30 +405,29 @@ class CreoleTemplateEngine:
source = join(self.tmp_dir, filevar['source']) source = join(self.tmp_dir, filevar['source'])
if filevar['templating']: if filevar['templating']:
self.process(source, self.process(source,
filename,
destfilename, destfilename,
filevar, filevar,
var) var)
else: else:
copy(source, destfilename) copy(source, destfilename)
if not override and self.tmpfile_name: # if self.tmpfile_name:
systemd_rights.append(f'C {filename} {filevar["mode"]} {filevar["owner"]} {filevar["group"]} - {self.factory_prefix}{filename}') # systemd_rights.append(f'C {filename} {filevar["mode"]} {filevar["owner"]} {filevar["group"]} - {self.factory_prefix}{filename}')
systemd_rights.append(f'z {filename} - - - - -') # systemd_rights.append(f'z {filename} - - - - -')
async def instance_files(self) -> None: async def instance_files(self) -> None:
"""Run templatisation on all files """Run templatisation on all files
""" """
for option in await self.config.option.list(type='all'): for option in await self.config.option.list(type='all'):
namespace = await option.option.name() namespace = await option.option.name()
if namespace in ['services', 'actions']: if namespace == VARIABLE_NAMESPACE:
continue
elif namespace == VARIABLE_NAMESPACE:
await self.load_eole_variables_rougail(option) await self.load_eole_variables_rougail(option)
else: else:
await self.load_eole_variables(namespace, families = await self.load_eole_variables(namespace,
option) option)
self.rougail_variables_dict[namespace] = families
for template in listdir(self.distrib_dir): for template in listdir(self.distrib_dir):
self.prepare_template(join(self.distrib_dir, template)) self.prepare_template(join(self.distrib_dir, template))
systemd_rights = []
for service_obj in await self.config.option('services').list('all'): for service_obj in await self.config.option('services').list('all'):
service_name = await service_obj.option.doc() service_name = await service_obj.option.doc()
for fills in await service_obj.list('all'): for fills in await service_obj.list('all'):
@ -443,20 +438,17 @@ class CreoleTemplateEngine:
distib_file = join(self.distrib_dir, filename) distib_file = join(self.distrib_dir, filename)
if not isfile(distib_file): if not isfile(distib_file):
raise FileNotFound(_(f"File {distib_file} does not exist.")) raise FileNotFound(_(f"File {distib_file} does not exist."))
override = await fills.option.name() == 'overrides' if fill.get('activate', False):
if override or fill.get('activate', False):
self.instance_file(fill, self.instance_file(fill,
systemd_rights,
override,
service_name, service_name,
) )
else: else:
log.debug(_("Instantiation of file '{filename}' disabled")) log.debug(_("Instantiation of file '{filename}' disabled"))
if self.tmpfile_name: #if self.tmpfile_name:
with open(self.tmpfile_name, 'w') as fh: # with open(self.tmpfile_name, 'w') as fh:
fh.write('\n'.join(systemd_rights)) # fh.write('\n'.join(systemd_rights))
fh.write('\n') # fh.write('\n')
async def generate(config: Config, async def generate(config: Config,
@ -464,22 +456,11 @@ async def generate(config: Config,
distrib_dir: str, distrib_dir: str,
tmp_dir: str, tmp_dir: str,
dest_dir: str, dest_dir: str,
override_dest_dir: str,
tmpfile_name: str=None,
factory_prefix: str=None,
) -> None: ) -> None:
if not tmpfile_name and factory_prefix:
raise Exception(_(f'only specify factory_prefix if tmpfile_name is set'))
if tmpfile_name and not factory_prefix:
raise Exception(_(f'if tmpfile_name is specify, set factory_prefix too'))
engine = CreoleTemplateEngine(config, engine = CreoleTemplateEngine(config,
eosfunc_file, eosfunc_file,
distrib_dir, distrib_dir,
tmp_dir, tmp_dir,
dest_dir, dest_dir,
override_dest_dir,
tmpfile_name,
factory_prefix,
) )
await engine.instance_files() await engine.instance_files()

View File

@ -3,7 +3,7 @@
<services> <services>
<service name="test"> <service name="test">
<override name="test.service"/> <override/>
</service> </service>
</services> </services>

View File

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": "non", "services.test.overrides.test_service.name": "test.service", "services.test.overrides.test_service.source": "test.service", "services.test.overrides.test_service.templating": true, "services.test.overrides.test_service.activate": true} {"rougail.general.mode_conteneur_actif": "non", "services.test.overrides.test_service.group": "root", "services.test.overrides.test_service.mode": "0644", "services.test.overrides.test_service.name": "/systemd/system/test.service.d/rougail.conf", "services.test.overrides.test_service.owner": "root", "services.test.overrides.test_service.source": "test.service", "services.test.overrides.test_service.templating": true, "services.test.overrides.test_service.activate": true}

View File

@ -5,8 +5,17 @@
<family doc="test" name="test"> <family doc="test" name="test">
<family name="overrides" doc="overrides"> <family name="overrides" doc="overrides">
<family doc="test.service" name="test_service"> <family doc="test.service" name="test_service">
<variable doc="group" multi="False" name="group" type="string">
<value>root</value>
</variable>
<variable doc="mode" multi="False" name="mode" type="string">
<value>0644</value>
</variable>
<variable doc="name" multi="False" name="name" type="string"> <variable doc="name" multi="False" name="name" type="string">
<value>test.service</value> <value>/systemd/system/test.service.d/rougail.conf</value>
</variable>
<variable doc="owner" multi="False" name="owner" type="string">
<value>root</value>
</variable> </variable>
<variable doc="source" multi="False" name="source" type="string"> <variable doc="source" multi="False" name="source" type="string">
<value>test.service</value> <value>test.service</value>

View File

@ -5,6 +5,7 @@
<services> <services>
<service name='test'> <service name='test'>
<file name='/etc/mailname'/> <file name='/etc/mailname'/>
<file name='/rougail.conf'/>
</service> </service>
</services> </services>

View File

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": "oui", "services.test.files.mailname.group": "root", "services.test.files.mailname.mode": "0644", "services.test.files.mailname.name": "/etc/mailname", "services.test.files.mailname.owner": "root", "services.test.files.mailname.source": "mailname", "services.test.files.mailname.templating": true, "services.test.files.mailname.activate": true} {"rougail.general.mode_conteneur_actif": "oui", "services.test.files.mailname.group": "root", "services.test.files.mailname.mode": "0644", "services.test.files.mailname.name": "/etc/mailname", "services.test.files.mailname.owner": "root", "services.test.files.mailname.source": "mailname", "services.test.files.mailname.templating": true, "services.test.files.mailname.activate": true, "services.test.files.rougail_conf.group": "root", "services.test.files.rougail_conf.mode": "0644", "services.test.files.rougail_conf.name": "/rougail.conf", "services.test.files.rougail_conf.owner": "root", "services.test.files.rougail_conf.source": "rougail.conf", "services.test.files.rougail_conf.templating": true, "services.test.files.rougail_conf.activate": true}

View File

@ -27,6 +27,29 @@
<value>True</value> <value>True</value>
</variable> </variable>
</family> </family>
<family doc="rougail.conf" name="rougail_conf">
<variable doc="group" multi="False" name="group" type="string">
<value>root</value>
</variable>
<variable doc="mode" multi="False" name="mode" type="string">
<value>0644</value>
</variable>
<variable doc="name" multi="False" name="name" type="string">
<value>/rougail.conf</value>
</variable>
<variable doc="owner" multi="False" name="owner" type="string">
<value>root</value>
</variable>
<variable doc="source" multi="False" name="source" type="string">
<value>rougail.conf</value>
</variable>
<variable doc="templating" multi="False" name="templating" type="boolean">
<value>True</value>
</variable>
<variable doc="activate" multi="False" name="activate" type="boolean">
<value>True</value>
</variable>
</family>
</family> </family>
</family> </family>
</family> </family>

View File

@ -1,2 +0,0 @@
C /etc/file 0644 root root - rougail.conf/etc/file
z /etc/file - - - - -

View File

@ -1,2 +0,0 @@
C /etc/file 0644 root root - rougail.conf/etc/file
z /etc/file - - - - -

View File

@ -5,8 +5,17 @@
<family doc="test" name="test"> <family doc="test" name="test">
<family name="overrides" doc="overrides"> <family name="overrides" doc="overrides">
<family doc="test.service" name="test_service"> <family doc="test.service" name="test_service">
<variable doc="group" multi="False" name="group" type="string">
<value>root</value>
</variable>
<variable doc="mode" multi="False" name="mode" type="string">
<value>0644</value>
</variable>
<variable doc="name" multi="False" name="name" type="string"> <variable doc="name" multi="False" name="name" type="string">
<value>test.service</value> <value>/systemd/system/test.service.d/rougail.conf</value>
</variable>
<variable doc="owner" multi="False" name="owner" type="string">
<value>root</value>
</variable> </variable>
<variable doc="source" multi="False" name="source" type="string"> <variable doc="source" multi="False" name="source" type="string">
<value>test.service</value> <value>test.service</value>

View File

@ -1,2 +0,0 @@
C /etc/mailname 0644 root root - rougail.conf/etc/mailname
z /etc/mailname - - - - -

View File

@ -27,6 +27,29 @@
<value>True</value> <value>True</value>
</variable> </variable>
</family> </family>
<family doc="rougail.conf" name="rougail_conf">
<variable doc="group" multi="False" name="group" type="string">
<value>root</value>
</variable>
<variable doc="mode" multi="False" name="mode" type="string">
<value>0644</value>
</variable>
<variable doc="name" multi="False" name="name" type="string">
<value>/rougail.conf</value>
</variable>
<variable doc="owner" multi="False" name="owner" type="string">
<value>root</value>
</variable>
<variable doc="source" multi="False" name="source" type="string">
<value>rougail.conf</value>
</variable>
<variable doc="templating" multi="False" name="templating" type="boolean">
<value>True</value>
</variable>
<variable doc="activate" multi="False" name="activate" type="boolean">
<value>True</value>
</variable>
</family>
</family> </family>
</family> </family>
</family> </family>

View File

@ -1,2 +1,2 @@
C /etc/mailname 0644 root root - rougail.conf/etc/mailname C /etc/mailname 0644 root root - /usr/local/share/factory/etc/mailname
z /etc/mailname - - - - - z /etc/mailname - - - - -

View File

@ -0,0 +1,10 @@
%for %%service in %%services
%if %%hasattr(%%service, 'files')
%for %%file in %%service.files
%if %%file.name != %%rougail_filename and %%hasattr(%%file, 'activate') and %%file.activate == True
C %%file.name %%file.mode %%file.owner %%file.group - /usr/local/share/factory%%file.name
z %%file.name - - - - -
%end if
%end for
%end if
%end for

View File

@ -1 +1 @@
tests/templates/70container_files_symlink_multi/dest/etc/mailname /etc/mailname

View File

@ -1 +1 @@
tests/templates/70container_files_symlink_multi/dest/etc/mailname2 /etc/mailname2

View File

@ -1,4 +0,0 @@
C /etc/mailname 0644 root root - rougail.conf/etc/mailname
z /etc/mailname - - - - -
C /etc/mailname2 0644 root root - rougail.conf/etc/mailname2
z /etc/mailname2 - - - - -

View File

@ -1,4 +0,0 @@
C /etc/mailname 0644 root root - rougail.conf/etc/mailname
z /etc/mailname - - - - -
C /etc/mailname2 0644 root root - rougail.conf/etc/mailname2
z /etc/mailname2 - - - - -

View File

@ -61,9 +61,6 @@ async def test_dictionary(test_dir):
distrib_dir, distrib_dir,
tmp_dir, tmp_dir,
dest_dir, dest_dir,
dest_dir,
join(dest_dir, 'rougail.conf'),
'rougail.conf',
) )
list_templates = set() list_templates = set()
if isdir(dest_dir): if isdir(dest_dir):