fix: improve rougail.user_datas method

This commit is contained in:
egarette@silique.fr 2024-11-23 11:08:24 +01:00
parent c330de60e7
commit e905b24c99
1245 changed files with 5627 additions and 224 deletions

View file

@ -0,0 +1,52 @@
"""
Silique (https://www.silique.fr)
Copyright (C) 2024
distribued with GPL-2 or later license
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
from rougail.error import DictConsistencyError
from rougail.annotator.variable import Walk
from .i18n import _
class Annotator(Walk):
"""Annotate for environment"""
level = 95
def __init__(
self,
objectspace,
*args, # pylint: disable=unused-argument
) -> None:
if not objectspace.paths:
return
self.objectspace = objectspace
self.check_family()
self.check_variable()
def check_family(self):
for family in self.get_families():
if family.name != family.name.lower():
msg = _('family name must be a lowercase name when we want to use user data "environment", so "{0}" is invalid')
raise DictConsistencyError(msg.format(family.name), 200, family.xmlfiles)
def check_variable(self):
for variable in self.get_variables():
if variable.name != variable.name.lower():
msg = _('variable name must be a lowercase name when we want to use user data "environment", so "{0}" is invalid')
raise DictConsistencyError(msg.format(variable.name), 201, variable.xmlfiles)

View file

@ -1,36 +0,0 @@
"""
Cli code for Rougail-user-data-environment
Silique (https://www.silique.fr)
Copyright (C) 2024
distribued with GPL-2 or later license
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
from .data import RougailUserDataEnvironment
def run(rougailconfig,
config,
user_datas,
):
RougailUserDataEnvironment(config,
user_datas=user_datas,
rougailconfig=rougailconfig,
).run()
__all__ = ('run',)

View file

@ -21,27 +21,26 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
"rougail environment variable prefix"
rougail_environment_var = "ROUGAIL_"
def get_rougail_config(*,
backward_compatibility=True,
) -> dict:
options = """
environment:
description: Define value from the environment
description: Define values from the environment
disabled:
type: jinja
jinja: |
{% if 'environment' not in step.user_data %}
disabled
{% endif %}
mandatory:
description: Ask values only for mandatories variables without any value
negative_description: Ask values all variables
alternative_name: envm
default: false
default_environment_name:
description: Name of the default environment prefix
default: rougail
disabled:
jinja: |
{% if main_namespace %}
use namespaces
{% endif %}
"""
return {'name': 'environment',
'process': 'user data',

View file

@ -18,8 +18,7 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
from .helper import get_rougail_environment
import os
from rougail.object_model import CONVERT_OPTION
from rougail.config import RougailConfig
from tiramisu.error import ValueOptionError
@ -29,7 +28,6 @@ class RougailUserDataEnvironment:
config: 'Config',
*,
rougailconfig: RougailConfig=None,
user_datas=None,
):
# this is the tiramisu config object
self.config = config
@ -40,68 +38,55 @@ class RougailUserDataEnvironment:
user_data.append('environment')
rougailconfig['step.user_data'] = user_data
user_data = rougailconfig['step.user_data']
self.rougailconfig = rougailconfig
if 'environment' not in user_data:
raise Exception('environment is not set in step.user_data')
self.rougailconfig = rougailconfig
if user_datas:
self.errors = user_datas['errors']
self.warnings = user_datas['warnings']
else:
self.errors = []
self.warnings = []
# bash environment variables (key/value pairs)
self.rougail_environment_vars = get_rougail_environment()
self.errors = []
self.warnings = []
def run(self):
self.config.property.read_write()
self.parse(self.config.unrestraint)
self.config.property.read_only()
# about unused bash environment variables
if len(self.rougail_environment_vars):
unused_env_var_str = ", ".join(self.rougail_environment_vars.keys())
self.warnings.append("the following rougail bash environment "
"variable(s) are not used : " + unused_env_var_str)
values = self.parse()
return [{'source': 'environment',
'errors': self.errors,
'warnings': self.warnings,
'values': values,
'options': {'multi_separator': ',',
'needs_convert': True,
},
}]
def parse(self, config):
for option in config:
if option.isoptiondescription():
self.parse(option)
else:
# FIXME handle exceptions
# try except
# ValueError suivant le type
# PropertyError -> hidden ou frozen...
# pour les tests: mettre hidden à true dans le yaml
# try:
self.load_environment(option)
# except Exception as exc:
# print(exc)
def parse(self):
variables = {}
found_ns = False
for option in self.config:
if option.group_type() == "namespace":
found_ns = True
variables.update(get_rougail_environment(option.name()))
if not found_ns:
if self.rougailconfig['main_namespace'] is None:
return get_rougail_environment(self.rougailconfig['environment.default_environment_name'])
return get_rougail_environment(self.rougailconfig['main_namespace'])
return variables
def load_environment(self, option):
option_name = option.path()
# the bash variable are in upper case
option_name = option_name.upper()
# if the option is not in the bash environment, do nothing
if option_name not in self.rougail_environment_vars.keys():
return
# actually the rougail type **is not** the same thing as
# the tiramisu type, which is in `option.type`
option_type = option.information.get('type')
ismulti = option.ismulti()
# here we retrieve the type conversion function
# carefull, the coercion function 'func' could be None
# -> in this case, do nothing
type_obj = CONVERT_OPTION.get(option_type, {}).get("func")
option_bash_value = self.rougail_environment_vars.pop(option_name)
if ismulti:
# here we expect the bash option value of a multi to be coma separated:
option_bash_value = [opt.strip() for opt in option_bash_value.split(",")]
if type_obj:
option_bash_value = [type_obj(opt) for opt in option_bash_value]
elif type_obj:
option_bash_value = type_obj(option_bash_value)
try:
option.value.set(option_bash_value)
except Exception as exc:
print(exc)
def get_rougail_environment(namespace):
"""gets all the rougail environment variables and their values
:sample: {'VARINT': '5', 'VARNAME34': '58, 22', 'VARNAME2': 'tata',
'VARNAME1': 'titi', 'MYFAMILY.VARNAME3': 'spam'}
:returns: rougail environment variables as a key/value dict
"""
# first we look at all environment variables
all_envvar = os.environ
# then we filter the ROUGAIL_ environment variables
if namespace is None:
rougail_environment_var = rougail_default_environment_var.upper() + '_'
len_env = len(rougail_environment_var) + 1
root = ''
else:
rougail_environment_var = namespace.upper() + '_'
len_env = len(rougail_environment_var)
root = namespace.lower() + '.'
return {root + envvar[len_env:].lower(): envval
for envvar, envval in all_envvar.items()
if envvar.startswith(rougail_environment_var)}

View file

@ -1,44 +0,0 @@
"""
Helper file for Rougail-user-data-environment
Silique (https://www.silique.fr)
Copyright (C) 2024
distribued with GPL-2 or later license
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import os
from .config import rougail_environment_var
def get_rougail_environment():
"""gets all the rougail environment variables and their values
:sample: {'VARINT': '5', 'VARNAME34': '58, 22', 'VARNAME2': 'tata',
'VARNAME1': 'titi', 'MYFAMILY.VARNAME3': 'spam'}
:returns: rougail environment variables as a key/value dict
"""
# first we look at all environment variables
all_envvar = os.environ
# then we filter the ROUGAIL_ environment variables
rougail_envvar = [envvar.replace(rougail_environment_var, '')
for envvar in all_envvar
if envvar.startswith(rougail_environment_var)]
rougail_environment_dict = dict()
for var in rougail_envvar:
rougail_environment_dict[var] = os.environ[rougail_environment_var + var]
return rougail_environment_dict

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@
{}

View file

@ -0,0 +1 @@
{}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1 @@
ROUGAIL_VERSION="string1"

View file

@ -0,0 +1 @@
ROUGAIL_VERSION="string1"

View file

@ -0,0 +1 @@
ROUGAIL_VERSION="string1"

View file

@ -0,0 +1,3 @@
{
"rougail.version": "string1"
}

View file

@ -0,0 +1,3 @@
{
"rougail.version": "string1"
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1 @@
ROUGAIL_EMPTY="string1"

View file

@ -0,0 +1 @@
ROUGAIL_EMPTY="string1"

View file

@ -0,0 +1 @@
ROUGAIL_EMPTY="string1"

View file

@ -0,0 +1,3 @@
{
"rougail.empty": "string1"
}

View file

@ -0,0 +1,3 @@
{
"rougail.empty": "string1"
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="string1"
ROUGAIL_VAR2="string1,string2,string3"

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="string1"
ROUGAIL_VAR2="string1,string2,string3"

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,8 @@
{
"rougail.var1": "string1",
"rougail.var2": [
"string1",
"string2",
"string3"
]
}

View file

@ -0,0 +1,6 @@
{
"rougail.var1": "no",
"rougail.var2": [
"no"
]
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="string1,string2,string3"
ROUGAIL_VAR2="string1,string2,string3"

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="string1,string2,string3"
ROUGAIL_VAR2="string1,string2,string3"

View file

@ -0,0 +1,12 @@
{
"rougail.var1": [
"string1",
"string2",
"string3"
],
"rougail.var2": [
"string1",
"string2",
"string3"
]
}

View file

@ -0,0 +1,12 @@
{
"rougail.var1": [
"no",
"yes",
"maybe"
],
"rougail.var2": [
"no",
"yes",
"maybe"
]
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="domain1.lan,domain2.lan"
ROUGAIL_VAR2="domain1.lan,domain2.lan"

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="domain1.lan,domain2.lan"
ROUGAIL_VAR2="domain1.lan,domain2.lan"

View file

@ -0,0 +1 @@
ROUGAIL_VAR1="domain1.lan,domain2.lan"

View file

@ -0,0 +1,10 @@
{
"rougail.var1": [
"domain1.lan",
"domain2.lan"
],
"rougail.var2": [
"domain1.lan",
"domain2.lan"
]
}

View file

@ -0,0 +1,10 @@
{
"rougail.var1": [
"domain1.lan",
"domain2.lan"
],
"rougail.var2": [
"domain1.lan",
"domain2.lan"
]
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1 @@
ROUGAIL_VAR1="string1"

View file

@ -0,0 +1 @@
ROUGAIL_VAR1="string1"

View file

@ -0,0 +1 @@
ROUGAIL_VAR1="string1"

View file

@ -0,0 +1,3 @@
{
"rougail.var1": "string1"
}

View file

@ -0,0 +1,3 @@
{
"rougail.var1": "string1"
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1 @@
ROUGAIL_WITHOUT_TYPE="string1"

View file

@ -0,0 +1 @@
ROUGAIL_WITHOUT_TYPE="string1"

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,3 @@
{
"rougail.without_type": "string1"
}

View file

@ -0,0 +1,3 @@
{
"rougail.without_type": "non"
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,6 @@
ROUGAIL_VAR1="True"
ROUGAIL_VAR2="True"
ROUGAIL_VAR3="True"
ROUGAIL_VAR4="True"
ROUGAIL_VAR5="True"
ROUGAIL_VAR6="True"

View file

@ -0,0 +1,6 @@
ROUGAIL_VAR1="True"
ROUGAIL_VAR2="True"
ROUGAIL_VAR3="True"
ROUGAIL_VAR4="True"
ROUGAIL_VAR5="True"
ROUGAIL_VAR6="True"

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,8 @@
{
"rougail.var1": true,
"rougail.var2": true,
"rougail.var3": true,
"rougail.var4": true,
"rougail.var5": true,
"rougail.var6": true
}

View file

@ -0,0 +1,8 @@
{
"rougail.var1": true,
"rougail.var2": true,
"rougail.var3": true,
"rougail.var4": false,
"rougail.var5": false,
"rougail.var6": false
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1 @@
ROUGAIL_VARIABLE="True"

View file

@ -0,0 +1 @@
ROUGAIL_VARIABLE="True"

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,3 @@
{
"rougail.variable": true
}

View file

@ -0,0 +1,3 @@
{
"rougail.variable": true
}

View file

@ -0,0 +1,6 @@
{
"errors": [
"\"1\" est une valeur invalide pour l'option \"rougail.var6 (the sixth variable)\" de type choice, seul \"1\", \"2\" et \"3\" sont autoris\u00e9es"
],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,6 @@
ROUGAIL_VAR1="a"
ROUGAIL_VAR2="a"
ROUGAIL_VAR3="a"
ROUGAIL_VAR4=""
ROUGAIL_VAR5="a"
ROUGAIL_VAR6="1"

View file

@ -0,0 +1,6 @@
ROUGAIL_VAR1="a"
ROUGAIL_VAR2="a"
ROUGAIL_VAR3="a"
ROUGAIL_VAR4=""
ROUGAIL_VAR5="a"
ROUGAIL_VAR6="1"

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="a"
ROUGAIL_VAR2="a"

View file

@ -0,0 +1,8 @@
{
"rougail.var1": "a",
"rougail.var2": "a",
"rougail.var3": "a",
"rougail.var4": null,
"rougail.var5": "a",
"rougail.var6": 1
}

View file

@ -0,0 +1,8 @@
{
"rougail.var1": "a",
"rougail.var2": "a",
"rougail.var3": null,
"rougail.var4": null,
"rougail.var5": "a",
"rougail.var6": 1
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="string1,string2,string3"
ROUGAIL_VAR2="string1"

View file

@ -0,0 +1,2 @@
ROUGAIL_VAR1="string1,string2,string3"
ROUGAIL_VAR2="string1"

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,8 @@
{
"rougail.var1": [
"string1",
"string2",
"string3"
],
"rougail.var2": "string1"
}

View file

@ -0,0 +1,8 @@
{
"rougail.var1": [
"a",
"b",
"c"
],
"rougail.var2": "a"
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,2 @@
ROUGAIL_CUSTOM1="string1"
ROUGAIL_CUSTOM2="string1"

View file

@ -0,0 +1,2 @@
ROUGAIL_CUSTOM1="string1"
ROUGAIL_CUSTOM2="string1"

View file

@ -0,0 +1 @@
ROUGAIL_CUSTOM1="string1"

View file

@ -0,0 +1,4 @@
{
"rougail.custom1": "string1",
"rougail.custom2": "string1"
}

View file

@ -0,0 +1,4 @@
{
"rougail.custom1": "string1",
"rougail.custom2": "value"
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1,4 @@
{
"errors": [],
"warnings": []
}

View file

@ -0,0 +1 @@
ROUGAIL_VARIABLE="domain1.lan"

View file

@ -0,0 +1 @@
ROUGAIL_VARIABLE="domain1.lan"

Some files were not shown because too many files have changed in this diff Show more