support of variables's file_name

This commit is contained in:
Emmanuel Garette 2019-12-22 11:04:39 +01:00
parent 3881cb7e98
commit 1ce16ad05d
10 changed files with 44 additions and 27 deletions

View file

@ -11,8 +11,8 @@ import logging
from typing import Dict from typing import Dict
from subprocess import call from subprocess import call
from os import listdir, unlink, mkdir from os import listdir, unlink, makedirs
from os.path import basename, join, split, isfile, isdir from os.path import dirname, basename, join, split, isfile, isdir
from tempfile import mktemp from tempfile import mktemp
@ -114,14 +114,16 @@ class CheetahTemplate(ChtTemplate):
context, context,
eosfunc: Dict, eosfunc: Dict,
config: Config, config: Config,
current_container: str): current_container: str,
destfilename):
"""Initialize Creole CheetahTemplate """Initialize Creole CheetahTemplate
""" """
ChtTemplate.__init__(self, file=filename, ChtTemplate.__init__(self, file=filename,
searchList=[context, eosfunc, {'is_defined' : IsDefined(context), searchList=[context, eosfunc, {'is_defined' : IsDefined(context),
'creole_client' : CreoleClient(config), 'creole_client' : CreoleClient(config),
'current_container':CreoleGet(current_container), 'current_container':CreoleGet(current_container),
}]) 'rougail_filename': destfilename
}])
class CreoleLeader: class CreoleLeader:
@ -326,11 +328,13 @@ class CreoleTemplateEngine:
# full path of the destination file # full path of the destination file
log.info(_(f"Cheetah processing: '{destfilename}'")) log.info(_(f"Cheetah processing: '{destfilename}'"))
try: try:
cheetah_template = CheetahTemplate(join(self.tmp_dir, filevar['source']), cheetah_template = CheetahTemplate(join(self.tmp_dir,
filevar['source']),
self.creole_variables_dict, self.creole_variables_dict,
self.eosfunc, self.eosfunc,
self.config.config.copy(), self.config.config.copy(),
container) container,
destfilename)
data = str(cheetah_template) data = str(cheetah_template)
except CheetahNotFound as err: except CheetahNotFound as err:
varname = err.args[0][13:-1] varname = err.args[0][13:-1]
@ -386,15 +390,18 @@ class CreoleTemplateEngine:
log.info(_("Instantiating file '{filename}'")) log.info(_("Instantiating file '{filename}'"))
container_dir = join(self.dest_dir, container_dir = join(self.dest_dir,
container) container)
if not isdir(container_dir): filenames = filevar['name']
mkdir(container_dir) if not isinstance(filenames, list):
destfilename = join(container_dir, filenames = [filenames]
filevar['source']) for filename in filenames:
self.process(destfilename, destfilename = join(container_dir,
filevar, filename[1:])
container) makedirs(dirname(destfilename), exist_ok=True)
self.change_properties(destfilename, self.process(destfilename,
filevar) filevar,
container)
self.change_properties(destfilename,
filevar)
def instance_files(self, def instance_files(self,
container=None): container=None):

View file

@ -8,7 +8,7 @@
description="Reconfigurer le serveur" description="Reconfigurer le serveur"
image="backup.svg" image="backup.svg"
url="extra.test.calc_url" url="extra.test.calc_url"
url_type="SymLinkOption"> url_type="variable">
<input>Reconfigurer</input> <input>Reconfigurer</input>
<profile>ead_admin</profile> <profile>ead_admin</profile>
<ewtapp>ead</ewtapp> <ewtapp>ead</ewtapp>

View file

@ -2,7 +2,7 @@
<creole> <creole>
<containers> <containers>
<container name='test' id='23'> <container name='test' id='23'>
<file name='file_name' name_type="SymLinkOption" source="mailname"/> <file name='file_name' name_type="variable" source="mailname"/>
</container> </container>
</containers> </containers>
<variables> <variables>

View file

@ -4,7 +4,7 @@
<containers> <containers>
<container name='test' id='23'> <container name='test' id='23'>
<fstab name='path_fstab' name_type="SymLinkOption"/> <fstab name='path_fstab' name_type="variable"/>
</container> </container>
</containers> </containers>

View file

@ -5,7 +5,7 @@
<containers> <containers>
<container name='test' id='23'> <container name='test' id='23'>
<service_restriction service='nut'> <service_restriction service='nut'>
<ip interface='auto' netmask='nut_monitor_netmask' netmask_type='SymLinkOption' ip_type='SymLinkOption'>nut_monitor_host</ip> <ip interface='auto' netmask='nut_monitor_netmask' netmask_type='variable' ip_type='variable'>nut_monitor_host</ip>
</service_restriction> </service_restriction>
</container> </container>

View file

@ -5,7 +5,7 @@
<containers> <containers>
<container name='test' id='23'> <container name='test' id='23'>
<service_restriction service='nut'> <service_restriction service='nut'>
<ip interface='auto' netmask='nut_monitor_netmask' netmask_type='SymLinkOption' ip_type='SymLinkOption'>nut_monitor_host</ip> <ip interface='auto' netmask='nut_monitor_netmask' netmask_type='variable' ip_type='variable'>nut_monitor_host</ip>
</service_restriction> </service_restriction>
</container> </container>

View file

@ -2,7 +2,7 @@
<creole> <creole>
<containers> <containers>
<container name='test' id='23'> <container name='test' id='23'>
<file name='file_name' name_type="SymLinkOption"/> <file name='file_name' name_type="variable"/>
</container> </container>
</containers> </containers>
<variables> <variables>

View file

@ -1 +0,0 @@
oui

View file

@ -18,6 +18,16 @@ def test_dir(request):
return request.param return request.param
def find_files(dirname, root, files):
for filename in listdir(dirname):
abs_dirname = join(dirname, filename)
root_file = root + [filename]
if isdir(join(dirname, filename)):
find_files(abs_dirname, root_file, files)
else:
files.add(join(*root_file))
def test_dictionary(test_dir): def test_dictionary(test_dir):
test_dir = join(template_dirs, test_dir) test_dir = join(template_dirs, test_dir)
tmp_dir = join(test_dir, 'tmp') tmp_dir = join(test_dir, 'tmp')
@ -45,13 +55,15 @@ def test_dictionary(test_dir):
dest_dir) dest_dir)
if isdir(join(dest_dir, 'test')): if isdir(join(dest_dir, 'test')):
list_templates = set(listdir(join(dest_dir, 'test'))) list_templates = set()
find_files(join(dest_dir), [], list_templates)
else: else:
list_templates = set() list_templates = set()
list_results = set(listdir(join(test_dir, 'result'))) list_results = set()
find_files(join(test_dir, 'result'), [], list_results)
assert list_templates == list_results assert list_templates == list_results
for result in listdir(join(test_dir, 'result')): for result in list_results:
template_file = join(dest_dir, 'test', result) template_file = join(dest_dir, result)
if not isfile(template_file): if not isfile(template_file):
raise Exception(f'{template_file} is not generated') raise Exception(f'{template_file} is not generated')
with open(join(test_dir, 'result', result), 'r') as fh: with open(join(test_dir, 'result', result), 'r') as fh: