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)
if isinstance(values, dict):
values = list(values.values())
family.family = self.make_group_from_elts(elttype,
family.family = self.make_group_from_elts(service_name,
elttype,
values,
f'services.{service_name}.{eltname}',
)
@ -138,6 +139,7 @@ class ServiceAnnotator:
self.space.services.service = families
def make_group_from_elts(self,
service_name,
name,
elts,
path,
@ -156,7 +158,11 @@ class ServiceAnnotator:
# try to launch _update_xxxx() function
update_elt = '_update_' + elt_name
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'):
c_name = elt.source
@ -256,13 +262,29 @@ class ServiceAnnotator:
result_elts.append({'elt_name': name, 'elt': elt})
return result_elts
def _update_override(self, *args):
self._update_file(*args)
def _update_override(self,
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,
file_,
index,
service_path,
service_name,
):
if not hasattr(file_, 'file_type') or file_.file_type == "UnicodeOption":
if not hasattr(file_, 'source'):

View file

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

View file

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

View file

@ -3,7 +3,7 @@
<services>
<service name="test">
<override name="test.service"/>
<override/>
</service>
</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 name="overrides" doc="overrides">
<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">
<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 doc="source" multi="False" name="source" type="string">
<value>test.service</value>

View file

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

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 name="overrides" doc="overrides">
<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">
<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 doc="source" multi="False" name="source" type="string">
<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>
</variable>
</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>

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

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,
tmp_dir,
dest_dir,
dest_dir,
join(dest_dir, 'rougail.conf'),
'rougail.conf',
)
list_templates = set()
if isdir(dest_dir):