From ea699552edea8032ddfbc2ef8c6e176b1010eeb4 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 28 Sep 2014 17:12:47 +0200 Subject: [PATCH] Rectify config creation --- src/functions/functions.hook | 75 ++++++------------ src/functions/functions.settings | 4 +- src/functions/functions.zone | 131 ++++++++++++++----------------- src/header-zone | 33 +++----- src/hooks/configs/ipv4-static | 3 +- 5 files changed, 98 insertions(+), 148 deletions(-) diff --git a/src/functions/functions.hook b/src/functions/functions.hook index 20a71323..0434f9fa 100644 --- a/src/functions/functions.hook +++ b/src/functions/functions.hook @@ -19,7 +19,7 @@ # # ############################################################################### -HOOK_COMMANDS_CONFIG="hook_create hook_down hook_status hook_up" +HOOK_COMMANDS_CONFIG="hook_create hook_down hook_status hook_remove hook_up" HOOK_COMMANDS_PORT="hook_create hook_down hook_edit hook_hotplug \ hook_hotplug_rename hook_info hook_status hook_up" @@ -125,6 +125,22 @@ function hook_exec() { return ${ret} } +function hook_list() { + local type="${1}" + + local dir="$(hook_dir "${type}")" + assert isset dir + + local hook + for hook in ${dir}/*; do + hook="$(basename "${hook}")" + + if hook_exists "${type}" "${hook}"; then + echo "${hook}" + fi + done +} + function config_get_hook() { local config=${1} @@ -141,61 +157,22 @@ function hook_zone_exists() { hook_exists zone $@ } -function hook_zone_config_exists() { - local hook_zone=${1} - local hook_config=${2} - - hook_exists zone "${hook_zone}.configs/${hook_config}" -} - -function hook_zone_has_configs() { - local hook=${1} - - [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ] -} - function hook_zone_exec() { hook_exec zone $@ } -function hook_zone_config_exec() { - local hook_zone=${1} - local hook_port=${2} - shift 2 - - hook_zone_exec "${hook_zone}.configs/${hook_port}" $@ -} - function hook_zone_get_all() { - local type=${1} - - local hook - for hook in $(hook_dir zone)/*; do - hook=$(basename ${hook}) - hook_zone_exists ${hook} && echo "${hook}" - done + hook_list zone } -function hook_zone_configs_get_all() { - local hook=${1} - - if ! hook_exists zone ${hook}; then - error "Hook '${hook}' does not exist." - return ${EXIT_ERROR} - fi - - # If the zone hook has got no configurations we exit silently - if ! hook_zone_has_configs ${hook}; then - return ${EXIT_OK} - fi +function hook_config_exists() { + hook_exists config $@ +} - local h - for h in $(hook_dir zone)/${hook}.configs/*; do - h=$(basename ${h}) - if hook_zone_config_exists ${hook} ${h}; then - echo "${h}" - fi - done +function hook_config_exec() { + hook_exec config $@ +} - return ${EXIT_OK} +function hook_config_get_all() { + hook_list config } diff --git a/src/functions/functions.settings b/src/functions/functions.settings index 52e519b9..ff1e9ebf 100644 --- a/src/functions/functions.settings +++ b/src/functions/functions.settings @@ -155,8 +155,8 @@ function settings_print() { function settings_check() { # If there is a function defined that is called __check # we call that function - if [ -n "$(type -t _check)" ]; then - _check || return $? + if [ -n "$(type -t hook_check)" ]; then + hook_check || return $? fi return ${EXIT_OK} diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 49194982..65296acf 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -530,68 +530,60 @@ function zone_has_port() { [ -e "$(zone_dir ${zone})/ports/${port}" ] } -# XXX overwritten some lines below function zone_config() { - local zone=${1} - shift - - if ! zone_exists ${zone}; then - error "Zone '${zone}' does not exist." - return ${EXIT_ERROR} - fi - - local hook=$(config_get_hook $(zone_dir ${zone})/settings) - - if [ -z "${hook}" ]; then - error "Config file did not provide any hook." - return ${EXIT_ERROR} - fi - - if ! hook_zone_exists ${hook}; then - error "Hook '${hook}' does not exist." - return ${EXIT_ERROR} - fi - - hook_zone_exec ${hook} config ${zone} $@ -} - -function zone_config() { - local zone=${1} - local action=${2} + local zone="${1}" + local cmd="${2}" shift 2 assert isset zone - assert isset action - assert zone_exists ${zone} + assert isset cmd + assert zone_exists "${zone}" - # Aliases - case "${action}" in - del|delete|remove) - action="rem" + case "${cmd}" in + create) + zone_config_create "${zone}" "$@" ;; - esac - - case "${action}" in - create|edit|rem) - zone_config_${action} ${zone} $@ + edit) + zone_config_edit "${zone}" "$@" + ;; + remove) + zone_config_remove "${zone}" "$@" ;; *) - error "Unrecognized argument: ${action}" + error "Unrecognized argument: ${cmd}" cli_usage root-zone-config-subcommands exit ${EXIT_ERROR} ;; esac } -function zone_config_create() { - local zone="${1}" - assert isset zone - shift +function zone_config_cmd() { + assert [ $# -gt 2 ] - local hook=$(zone_get_hook "${zone}") + local cmd="${1}" + local zone="${2}" + shift 2 + + local hook="$(zone_get_hook "${zone}")" assert isset hook - hook_zone_exec "${hook}" "config_create" "${zone}" "$@" + hook_zone_exec "${hook}" "config_${cmd}" "${zone}" "$@" +} + +function zone_config_create() { + zone_config_cmd "create" "$@" +} + +function zone_config_edit() { + zone_config_cmd "edit" "$@" +} + +function zone_config_remove() { + zone_config_cmd "remove" "$@" +} + +function zone_config_show() { + zone_config_cmd "show" "$@" } function zone_show() { @@ -723,43 +715,42 @@ function zone_ports_status() { zone_ports_cmd port_status $@ } -function zone_configs_list() { - local zone=${1} - - local config - for config in $(zone_dir ${zone})/configs/*; do - [ -e "${config}" ] || continue - - basename ${config} - done -} - function zone_configs_cmd() { - local cmd=${1} - local zone=${2} + assert [ $# -gt 2 ] + + local cmd="${1}" + local zone="${2}" shift 2 - local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings) + assert zone_exists "${zone}" - local hook_config local config - for config in $(zone_configs_list ${zone}); do - hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config}) - - hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@ + for config in $(zone_get_configs "${zone}"); do + hook_config_exec "${config}" "${cmd}" "${zone}" $@ done } function zone_configs_up() { - zone_configs_cmd up $@ + zone_configs_cmd "up" $@ } function zone_configs_down() { - zone_configs_cmd down $@ + zone_configs_cmd "down" $@ } function zone_configs_status() { - zone_configs_cmd config_status $@ + zone_configs_cmd "status" $@ +} + +function zone_configs_list() { + local zone=${1} + + local config + for config in $(zone_dir ${zone})/configs/*; do + [ -e "${config}" ] || continue + + basename ${config} + done } function zone_has_ip() { @@ -797,11 +788,7 @@ function zone_get_supported_port_hooks() { } function zone_get_supported_config_hooks() { - local zone=${1} - - local hook=$(zone_get_hook ${zone}) - - hook_zone_configs_get_all ${hook} + hook_config_get_all } function zone_file() { diff --git a/src/header-zone b/src/header-zone index b5ab3704..2120377d 100644 --- a/src/header-zone +++ b/src/header-zone @@ -174,41 +174,26 @@ function hook_config_cmd() { exit ${EXIT_ERROR} fi - if ! hook_config_exists "${hook_zone}" "${hook_config}"; then - log ERROR "Hook '${hook_config}' is not supported for zone '${zone}'." - exit ${EXIT_ERROR} - fi + #if ! hook_config_exists "${hook_zone}" "${hook_config}"; then + # log ERROR "Hook '${hook_config}' is not supported for zone '${zone}'." + # exit ${EXIT_ERROR} + #fi - hook_zone_config_exec "${hook_zone}" "${hook_config}" "${cmd}" "${zone}" "$@" + hook_config_exec "${hook_config}" "${cmd}" "${zone}" "$@" } function hook_config_create() { - local zone="${1}" - assert isset zone - - local hook_config="${2}" - assert isset hook_config - - shift 2 + assert [ $# -gt 2 ] - if ! listmatch "${hook_config}" $(zone_get_supported_config_hooks ${zone}); then - log ERROR "Zone '${zone}' does not support configuration of type '${hook_config}'." - exit ${EXIT_ERROR} - fi - - local hook_zone="$(zone_get_hook "${zone}")" - assert isset hook_zone - - hook_zone_config_exec "${hook_zone}" "${hook_config}" create "${zone}" "$@" - exit $? + hook_config_cmd "create" "$@" } function hook_config_edit() { - hook_config_cmd edit "$@" + hook_config_cmd "edit" "$@" } function hook_config_remove() { - cmd_not_implemented + hook_config_cmd "remove" "$@" } function hook_config_show() { diff --git a/src/hooks/configs/ipv4-static b/src/hooks/configs/ipv4-static index 9f9b3de3..ceec9fe7 100644 --- a/src/hooks/configs/ipv4-static +++ b/src/hooks/configs/ipv4-static @@ -34,7 +34,8 @@ function hook_check() { } function hook_create() { - local zone=${1} + local zone="${1}" + assert isset zone shift while [ $# -gt 0 ]; do -- 2.39.2