|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
from calculate.templates.template_engine import TemplateEngine, Variables,\
|
|
|
|
|
FILE, DIR, ParametersProcessor
|
|
|
|
|
from calculate.templates.template_processor import TemplateAction
|
|
|
|
|
from calculate.utils.package import PackageAtomParser, Package, PackageNotFound
|
|
|
|
|
from calculate.utils.files import write_file, read_link, read_file_lines,\
|
|
|
|
|
FilesError, join_paths,\
|
|
|
|
@ -37,24 +36,6 @@ other_parameter = other_value
|
|
|
|
|
[!section_name]
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
APPENDS_SET = TemplateAction().available_appends
|
|
|
|
|
|
|
|
|
|
vars_1 = Variables({'value_1': 'value_1', 'value_2': 'value_to_print',
|
|
|
|
|
'value_3': 5})
|
|
|
|
|
DATAVARS_MODULE = Variables({'vars_1': vars_1})
|
|
|
|
|
|
|
|
|
|
CHROOT_PATH = os.path.join(os.getcwd(), 'tests/templates/testfiles/test_root')
|
|
|
|
|
CL_CONFIG_PATH = os.path.join(CHROOT_PATH, 'var/lib/calculate/config')
|
|
|
|
|
CL_CONFIG_ARCHIVE_PATH = os.path.join(CHROOT_PATH,
|
|
|
|
|
'var/lib/calculate/config-archive')
|
|
|
|
|
|
|
|
|
|
template_engine = TemplateEngine(datavars_module=DATAVARS_MODULE,
|
|
|
|
|
appends_set=APPENDS_SET,
|
|
|
|
|
chroot_path=CHROOT_PATH)
|
|
|
|
|
|
|
|
|
|
target_path = os.path.join(CHROOT_PATH, 'etc/dir/file.conf')
|
|
|
|
|
run_target_path = os.path.join(CHROOT_PATH, 'file_to_run.py')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateExecutorError(Exception):
|
|
|
|
|
pass
|
|
|
|
@ -224,10 +205,10 @@ class TemplateWrapper:
|
|
|
|
|
# Если по этому пути что-то есть -- проверяем конфликты.
|
|
|
|
|
if os.path.exists(target_file_path):
|
|
|
|
|
for file_type, checker in self.type_checks.items():
|
|
|
|
|
if checker(target_path):
|
|
|
|
|
if checker(target_file_path):
|
|
|
|
|
self.target_type = file_type
|
|
|
|
|
break
|
|
|
|
|
self.target_is_link = os.path.islink(target_path)
|
|
|
|
|
self.target_is_link = os.path.islink(target_file_path)
|
|
|
|
|
# Если установлен параметр mirror и есть параметр source,
|
|
|
|
|
# содержащий несуществующий путь -- удаляем целевой файл.
|
|
|
|
|
if self.parameters.source is True and self.parameters.mirror:
|
|
|
|
@ -721,30 +702,31 @@ class TemplateExecutor:
|
|
|
|
|
output_path = template_object.output_path
|
|
|
|
|
|
|
|
|
|
template_format = template_object.format_class
|
|
|
|
|
print('target_path: ', template_object.target_path)
|
|
|
|
|
print('output_path: ', output_path)
|
|
|
|
|
|
|
|
|
|
# Задаемся значениями chmod и chown в зависимости от наличия или
|
|
|
|
|
# отсутствия файла.
|
|
|
|
|
# отсутствия файла, принадлежности его пакету и наличию дефолтных
|
|
|
|
|
# значений параметров.
|
|
|
|
|
try:
|
|
|
|
|
chmod = template_parameters.chmod
|
|
|
|
|
chmod = template_object.parameters.chmod
|
|
|
|
|
if not chmod:
|
|
|
|
|
chmod = self._get_file_mode(template_object.target_path)
|
|
|
|
|
elif template_object.target_type is not None:
|
|
|
|
|
chmod = self.directory_default_parameters.get('chmod',
|
|
|
|
|
False)
|
|
|
|
|
if (template_object.target_type is not None and
|
|
|
|
|
not template_object.target_without_package):
|
|
|
|
|
chmod = self._get_file_mode(template_object.target_path)
|
|
|
|
|
else:
|
|
|
|
|
chmod = self.file_default_parameters.get('chmod',
|
|
|
|
|
False)
|
|
|
|
|
|
|
|
|
|
chown = template_parameters.chown
|
|
|
|
|
chown = template_object.parameters.chown
|
|
|
|
|
if not chown:
|
|
|
|
|
chown = self._get_file_owner(template_object.target_path)
|
|
|
|
|
elif template_object.target_type is not None:
|
|
|
|
|
chown = self.directory_default_parameters.get('chown',
|
|
|
|
|
False)
|
|
|
|
|
if (template_object.target_type is not None and
|
|
|
|
|
not template_object.target_without_package):
|
|
|
|
|
chown = self._get_file_owner(template_object.target_path)
|
|
|
|
|
else:
|
|
|
|
|
chown = self.file_default_parameters.get('chown',
|
|
|
|
|
False)
|
|
|
|
|
except OSError:
|
|
|
|
|
raise TemplateExecutorError('No access to the directory: {}'.
|
|
|
|
|
format(template_object.target_path))
|
|
|
|
|
print('chmod: ', chmod)
|
|
|
|
|
print('chown: ', chown)
|
|
|
|
|
|
|
|
|
|
if template_object.format_class.EXECUTABLE or\
|
|
|
|
|
template_object.md5_matching:
|
|
|
|
@ -756,6 +738,8 @@ class TemplateExecutor:
|
|
|
|
|
output_paths.append(template_object.archive_path)
|
|
|
|
|
|
|
|
|
|
if template_object.target_type is not None and not replace:
|
|
|
|
|
# Если целевой файл есть и нет параметра replace -- используем
|
|
|
|
|
# текст целевого файла.
|
|
|
|
|
with open(input_path, 'r') as input_file:
|
|
|
|
|
input_text = input_file.read()
|
|
|
|
|
else:
|
|
|
|
@ -781,12 +765,12 @@ class TemplateExecutor:
|
|
|
|
|
# Меняем права доступа и владельца всех сохраняемых файлов,
|
|
|
|
|
# если это необходимо.
|
|
|
|
|
if chown:
|
|
|
|
|
self.chown_file(save_path, chown,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
self._chown_file(save_path, chown,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
if chmod:
|
|
|
|
|
self.chmod_file(save_path, chmod,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
self._chmod_file(save_path, chmod,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
# Убираем все ._cfg файлы.
|
|
|
|
|
if template_object.cfg_list:
|
|
|
|
@ -841,13 +825,11 @@ class TemplateExecutor:
|
|
|
|
|
# Меняем права доступа и владельца ._cfg????_ файлов, если
|
|
|
|
|
# это необходимо.
|
|
|
|
|
if chown:
|
|
|
|
|
self.chown_file(output_path, chown,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
self._chown_file(output_path, chown,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
if chmod:
|
|
|
|
|
self.chmod_file(output_file,
|
|
|
|
|
template_object.parameters.chmod,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
self._chmod_file(output_file, chmod,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
# Обновляем CL.
|
|
|
|
|
self.calculate_config_file.set_files_md5(
|
|
|
|
@ -898,12 +880,14 @@ class TemplateExecutor:
|
|
|
|
|
|
|
|
|
|
# Меняем владельца и права доступа к очищенному файлу, если нужно.
|
|
|
|
|
if template_object.chown:
|
|
|
|
|
self.chown_file(template_object.target_path,
|
|
|
|
|
template_object.parameters.chown)
|
|
|
|
|
self._chown_file(template_object.target_path,
|
|
|
|
|
template_object.parameters.chown,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
if template_object.chmod:
|
|
|
|
|
self.chmod_file(template_object.target_path,
|
|
|
|
|
template_object.parameters.chown)
|
|
|
|
|
self._chmod_file(template_object.target_path,
|
|
|
|
|
template_object.parameters.chmod,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
template_object.add_to_contents()
|
|
|
|
|
|
|
|
|
@ -915,12 +899,14 @@ class TemplateExecutor:
|
|
|
|
|
|
|
|
|
|
# Меняем права и владельца файла, на который указывает ссылка.
|
|
|
|
|
if template_object.parameters.chmod:
|
|
|
|
|
self.chmod_file(template_object.parameters.source,
|
|
|
|
|
template_object.parameters.chmod)
|
|
|
|
|
self._chmod_file(template_object.parameters.source,
|
|
|
|
|
template_object.parameters.chmod,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
if template_object.parameters.chown:
|
|
|
|
|
self.chmod_file(template_object.parameters.source,
|
|
|
|
|
template_object.parameters.chown)
|
|
|
|
|
self._chown_file(template_object.parameters.source,
|
|
|
|
|
template_object.parameters.chown,
|
|
|
|
|
check_existation=False)
|
|
|
|
|
|
|
|
|
|
template_object.add_to_contents()
|
|
|
|
|
|
|
|
|
@ -1158,7 +1144,7 @@ class TemplateExecutor:
|
|
|
|
|
'Can not chmod directory: {0}, reason: {1}'.
|
|
|
|
|
format(target_path, str(error)))
|
|
|
|
|
|
|
|
|
|
def chown_file(self, target_path, chown_value, check_existation=True):
|
|
|
|
|
def _chown_file(self, target_path, chown_value, check_existation=True):
|
|
|
|
|
'''Метод для смены владельца файла.'''
|
|
|
|
|
try:
|
|
|
|
|
if check_existation and not os.path.exists(target_path):
|
|
|
|
@ -1174,7 +1160,7 @@ class TemplateExecutor:
|
|
|
|
|
chown_value['gid']),
|
|
|
|
|
str(error)))
|
|
|
|
|
|
|
|
|
|
def chmod_file(self, target_path, chmod_value, check_existation=True):
|
|
|
|
|
def _chmod_file(self, target_path, chmod_value, check_existation=True):
|
|
|
|
|
'''Метод для смены прав доступа к директории.'''
|
|
|
|
|
try:
|
|
|
|
|
if check_existation and not os.path.exists(target_path):
|
|
|
|
@ -1215,7 +1201,7 @@ class TemplateExecutor:
|
|
|
|
|
if self.chroot_path == '/':
|
|
|
|
|
group_name = grp.getgrgid(gid).gr_name
|
|
|
|
|
else:
|
|
|
|
|
user_name = str(gid)
|
|
|
|
|
group_name = str(gid)
|
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
|
group_name = str(gid)
|
|
|
|
|
|
|
|
|
@ -1246,16 +1232,35 @@ class TemplateExecutor:
|
|
|
|
|
'ntfs'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Применение основного шаблона:
|
|
|
|
|
template_engine.process_template_from_string(template_text, FILE)
|
|
|
|
|
template_parameters = template_engine.parameters
|
|
|
|
|
template_text = template_engine.template_text
|
|
|
|
|
vars_1 = Variables({'value_1': 'value_1', 'value_2': 'value_to_print',
|
|
|
|
|
'value_3': 5})
|
|
|
|
|
DATAVARS_MODULE = Variables({'vars_1': vars_1})
|
|
|
|
|
|
|
|
|
|
CHROOT_PATH = os.path.join(os.getcwd(), 'tests/templates/testfiles/test_root')
|
|
|
|
|
CL_CONFIG_PATH = os.path.join(CHROOT_PATH, 'var/lib/calculate/config')
|
|
|
|
|
CL_CONFIG_ARCHIVE_PATH = os.path.join(CHROOT_PATH,
|
|
|
|
|
'var/lib/calculate/config-archive')
|
|
|
|
|
|
|
|
|
|
template_executor_obj = TemplateExecutor(
|
|
|
|
|
datavars_module=DATAVARS_MODULE,
|
|
|
|
|
chroot_path=CHROOT_PATH,
|
|
|
|
|
cl_config_archive=CL_CONFIG_ARCHIVE_PATH,
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH)
|
|
|
|
|
|
|
|
|
|
APPENDS_SET = template_executor_obj.available_appends
|
|
|
|
|
|
|
|
|
|
template_engine = TemplateEngine(datavars_module=DATAVARS_MODULE,
|
|
|
|
|
appends_set=APPENDS_SET,
|
|
|
|
|
chroot_path=CHROOT_PATH)
|
|
|
|
|
|
|
|
|
|
target_path = os.path.join(CHROOT_PATH, 'etc/dir/file.conf')
|
|
|
|
|
run_target_path = os.path.join(CHROOT_PATH, 'file_to_run.py')
|
|
|
|
|
|
|
|
|
|
# Применение основного шаблона:
|
|
|
|
|
template_engine.process_template_from_string(template_text, FILE)
|
|
|
|
|
template_parameters = template_engine.parameters
|
|
|
|
|
template_text = template_engine.template_text
|
|
|
|
|
|
|
|
|
|
template_executor_obj.execute_template(target_path,
|
|
|
|
|
template_parameters,
|
|
|
|
|
FILE, template_text=template_text)
|
|
|
|
@ -1265,11 +1270,6 @@ input()
|
|
|
|
|
template_engine.process_template_from_string(template_to_run, FILE)
|
|
|
|
|
template_parameters = template_engine.parameters
|
|
|
|
|
template_text = template_engine.template_text
|
|
|
|
|
template_executor_obj = TemplateExecutor(
|
|
|
|
|
datavars_module=DATAVARS_MODULE,
|
|
|
|
|
chroot_path=CHROOT_PATH,
|
|
|
|
|
cl_config_archive=CL_CONFIG_ARCHIVE_PATH,
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH)
|
|
|
|
|
template_executor_obj.execute_template(target_path,
|
|
|
|
|
template_parameters,
|
|
|
|
|
FILE, template_text=template_text)
|
|
|
|
@ -1279,11 +1279,6 @@ input()
|
|
|
|
|
template_engine.process_template_from_string(backup_template_text, FILE)
|
|
|
|
|
template_parameters = template_engine.parameters
|
|
|
|
|
template_text = template_engine.template_text
|
|
|
|
|
template_executor_obj = TemplateExecutor(
|
|
|
|
|
datavars_module=DATAVARS_MODULE,
|
|
|
|
|
chroot_path=CHROOT_PATH,
|
|
|
|
|
cl_config_archive=CL_CONFIG_ARCHIVE_PATH,
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH)
|
|
|
|
|
template_executor_obj.execute_template(target_path,
|
|
|
|
|
template_parameters,
|
|
|
|
|
FILE, template_text=template_text)
|
|
|
|
|