This functions is needed to implement the new id feature described in #11405
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index f321c2d..b44d3b5 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -992,6 +992,24 @@ zone_configs_list() { done }
+zone_config_get_new_id() { + # This functions returns the next free id for a zone + + assert [ $# -eq 1 ] + local zone=${1} + + local zone_path=$(zone_dir ${zone}) + local i=0 + + while true; do + if [ ! -f ${zone_path}/configs/*.${i} ]; then + echo "${i}" + return ${EXIT_OK} + fi + (( i++ )) + done +} + zone_config_get_hook() { assert [ $# -eq 2 ]
This function is needed to implement the id feature described in #11405
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index b44d3b5..fcc7bfa 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -1048,6 +1048,19 @@ zone_config_hook_is_configured() { return ${EXIT_FALSE} }
+zone_config_id_is_valid() { + # This function checks if a given id is valid for a zone + # Return True when yes and false when no + + assert [ $# -eq 2 ] + local zone=${1} + local id=${2} + + local zone_path=$(zone_dir ${zone}) + + [ -f ${zone_path}/configs/*.${id} ]; +} + zone_has_ip() { device_has_ip $@ }
This function is needed to implement the id feature described in #11405
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index fcc7bfa..90d7e11 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -1061,6 +1061,26 @@ zone_config_id_is_valid() { [ -f ${zone_path}/configs/*.${id} ]; }
+zone_config_get_hook_from_id() { + # Returns the hook for a given id + assert [ $# -eq 2 ] + local zone=${1} + local id=${2} + + local config + for config in $(zone_configs_list "${zone}"); do + if [[ ${config} == *.${id} ]]; then + local config_hook="$(zone_config_get_hook "${zone}" "${config}")" + assert isset config_hook + print "${config_hook}" + return "${EXIT_OK}" + fi + done + + # If we get here the zone has no config with the given id + return ${EXIT_ERROR} +} + zone_has_ip() { device_has_ip $@ }
When we write a config for the frist time a unique id is generated and appended to the filename. So it is possible to identify a config clearly.
The variable config is rename to hook because this function takes now the name of the hook and the id. The name of the config is no more suitable. If no id is passed we generate one. This should only happen when we write the file for the first time.
Fixes: #11405
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 90d7e11..441bf5f 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -1216,8 +1216,13 @@ zone_config_settings_write() { assert [ $# -ge 2 ]
local zone="${1}" - local config="${2}" - shift 2 + local hook="${2}" + local id=${3} + + if ! isset id; then + id=$(zone_config_get_new_id ${zone}) + log DEBUG "ID for the config is: ${id}" + fi
local args if function_exists "hook_check_config_settings"; then @@ -1225,7 +1230,7 @@ zone_config_settings_write() { fi list_append args ${HOOK_CONFIG_SETTINGS}
- local path="$(zone_dir "${zone}")/configs/${config}" + local path="$(zone_dir "${zone}")/configs/${hook}.${id}" settings_write "${path}" ${args} }
With the new id function this function gets a zone name and a id, but hook_config_cmd needs also the name of the hook.
So this function now calls zone_config_get_hook_from_id to get the hook and calls then hook_config_cmd with the correct argument order.
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/header-zone | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/src/header-zone b/src/header-zone index c445d55..fdd6e51 100644 --- a/src/header-zone +++ b/src/header-zone @@ -206,7 +206,22 @@ hook_config_new() { }
hook_config_destroy() { - hook_config_cmd "destroy" "$@" + assert [ $# -eq 2 ] + local zone=${1} + # The id must be the id and not the hid. + local id=${2} + + shift 2 + + # Check if we get a valid id + if ! zone_config_id_is_valid ${zone} ${id}; then + log ERROR "ID: ${id} is not a valid id for zone ${zone}" + fi + + local hook=$(zone_config_get_hook_from_id ${zone} ${id}) + assert isset hook + + hook_config_cmd "destroy" "${zone}" "${hook}" "${hook}.${id}" "$@" }
hook_config_edit() {
With the new id function this function gets a zone name and a id, but hook_config_cmd needs also the name of the hook.
So this function now calls zone_config_get_hook_from_id to get the hook and calls then hook_config_cmd with the correct argument order.
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/header-zone | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/src/header-zone b/src/header-zone index fdd6e51..2e3fa09 100644 --- a/src/header-zone +++ b/src/header-zone @@ -225,7 +225,22 @@ hook_config_destroy() { }
hook_config_edit() { - hook_config_cmd "edit" "$@" + assert [ $# -eq 2 ] + local zone=${1} + # The id must be the id and not the hid. + local id=${2} + + shift 2 + + # Check if we get a valid id + if ! zone_config_id_is_valid ${zone} ${id}; then + log ERROR "ID: ${id} is not a valid id for zone ${zone}" + fi + + local hook=$(zone_config_get_hook_from_id ${zone} ${id}) + assert isset hook + + hook_config_cmd "edit" "${zone}" "${hook}" "${hook}.${id}" "$@" }
hook_config_show() {
The syntax to edit a config is now
network zone upl0 config <id> edit
similar to the syntax if a zone is edited.
The cmd variable is setted to the content of ${1}, because we need the content in this variable if the ${id} is not valid, to print a nice error message.
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 441bf5f..776b63b 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -556,9 +556,18 @@ zone_config() { zone_config_edit "${zone}" "$@" ;; *) - error "Unrecognized argument: ${cmd}" - cli_usage root-zone-config-subcommands - exit ${EXIT_ERROR} + # Check is we get a valid id + # TODO This could be also a valid hid + local id=${cmd} + + if zone_config_id_is valid ${zone} ${id} && [[ ${1} == "edit" ]]; then + shift 1 + zone_config_edit "${zone}" "${id}""$@" + else + error "Unrecognized argument: ${cmd}" + cli_usage root-zone-config-subcommands + exit ${EXIT_ERROR} + fi ;; esac }
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 776b63b..d121225 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -550,7 +550,12 @@ zone_config() { zone_config_new "${zone}" "$@" ;; destroy) - zone_config_destroy "${zone}" "$@" + local id=${1} + if zone_config_id_is_valid ${zone} ${id}; then + zone_config_destroy "${zone}" "$@" + else + log ERROR "${id} is not a valid id" + fi ;; edit) zone_config_edit "${zone}" "$@"