]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/header-zone
hooks: Import zone default settings, too
[people/ms/network.git] / src / header-zone
index eaebd1b684f512daabaffce5e06c4e107cdf9dcc..7ad3e39e5e7bf1033e8439e61f6bb28c3354acc6 100644 (file)
 #                                                                             #
 ###############################################################################
 
-function hook_info() {
+hook_info() {
        echo "HOOK=\"${HOOK}\""
 }
 
-function hook_create() {
+hook_hotplug() {
+       # If the hook does not handle the hotplug event, it
+       # must return EXIT_NOT_HANDLED.
+       exit ${EXIT_NOT_HANDLED}
+}
+
+hook_new() {
        local zone="${1}"
        assert isset zone
        shift
 
-       config_read $(zone_dir ${zone})/settings
+       local ${HOOK_SETTINGS}
 
-       hook_parse_cmdline $@
+       # Import all default variables
+       hook_set_defaults
+
+       if ! hook_parse_cmdline "$@"; then
+               return ${EXIT_ERROR}
+       fi
 
-       config_write $(zone_dir ${zone})/settings ${HOOK_SETTINGS}
+       if ! zone_settings_write "${zone}"; then
+               log ERROR "Could not write settings for zone ${zone}"
+               return ${EXIT_ERROR}
+       fi
 
        exit ${EXIT_OK}
 }
 
-function hook_edit() {
-       hook_create $@
+hook_edit() {
+       local zone="${1}"
+       assert isset zone
+       shift
+
+       if ! zone_settings_read "${zone}"; then
+               log ERROR "Could not read settings for zone ${zone}"
+               return ${EXIT_ERROR}
+       fi
+
+       if ! hook_parse_cmdline "$@"; then
+               return ${EXIT_ERROR}
+       fi
+
+       if ! zone_settings_write "${zone}"; then
+               log ERROR "Could not write settings for zone ${zone}"
+               return ${EXIT_ERROR}
+       fi
+
+       exit ${EXIT_OK}
+
 }
 
-function hook_remove() {
+hook_remove() {
        cmd_not_implemented
 }
 
-function hook_status() {
+hook_status() {
        local zone="${1}"
        assert isset zone
 
@@ -56,36 +89,25 @@ function hook_status() {
        exit ${STATUS_DOWN}
 }
 
-function hook_up() {
+hook_up() {
        cmd_not_implemented
 }
 
-function hook_down() {
+hook_down() {
        cmd_not_implemented
 }
 
-function hook_discover() {
+hook_discover() {
        # This hook does not support a discovery
        exit ${DISCOVER_NOT_SUPPORTED}
 }
 
-# The default help function.
-function hook_help() {
-       # If no man page has been configured, we print an error message.
-       if [ -z "${HOOK_MANPAGE}" ]; then
-               error "There is no help available for hook '${HOOK}'. Exiting."
-               exit ${EXIT_ERROR}
-       fi
-
-       cli_show_man ${HOOK_MANPAGE}
-}
-
 # Do nothing
-function hook_parse_cmdline() {
+hook_parse_cmdline() {
        return ${EXIT_OK}
 }
 
-function hook_port() {
+hook_port() {
        local zone="${1}"
        assert isset zone
 
@@ -95,7 +117,7 @@ function hook_port() {
        local ret
        case "${action}" in
                add|create|edit|rem|show)
-                       hook_port_${action} "${zone}" $@
+                       hook_port_${action} "${zone}" "$@"
                        ret=$?
                        ;;
                *)
@@ -107,35 +129,57 @@ function hook_port() {
        exit ${ret}
 }
 
-function hook_port_add() {
+hook_port_attach() {
        return ${EXIT_NOT_SUPPORTED}
 }
 
-function hook_port_edit() {
+hook_port_detach() {
        return ${EXIT_NOT_SUPPORTED}
 }
 
-function hook_port_remove() {
+hook_port_edit() {
        return ${EXIT_NOT_SUPPORTED}
 }
 
-function hook_port_show() {
-       cmd_not_implemented
+hook_port_status() {
+       return ${EXIT_NOT_SUPPORTED}
 }
 
-function hook_port_status() {
-       return ${EXIT_NOT_SUPPORTED}
+hook_default_port_create() {
+       assert [ $# -ge 2 ]
+
+       local zone="${1}"
+       local port="${2}"
+
+       port_create "${port}"
+}
+
+hook_port_create() {
+       hook_default_port_create "$@"
+}
+
+hook_default_port_remove() {
+       assert [ $# -ge 2 ]
+
+       local zone="${1}"
+       local port="${2}"
+
+       port_remove "${port}"
+}
+
+hook_port_remove() {
+       hook_default_port_remove "$@"
 }
 
-function hook_port_up() {
+hook_port_up() {
        cmd_not_implemented
 }
 
-function hook_port_down() {
+hook_port_down() {
        cmd_not_implemented
 }
 
-function hook_config() {
+hook_config() {
        local zone="${1}"
        assert isset zone
 
@@ -145,7 +189,7 @@ function hook_config() {
 
        local ret
        case "${action}" in
-               create|edit|rem|show)
+               new|destroy|edit|show)
                        hook_config_${action} "${zone}" "$@"
                        exit $?
                        ;;
@@ -156,7 +200,7 @@ function hook_config() {
        esac
 }
 
-function hook_config_cmd() {
+hook_config_cmd() {
        local cmd="${1}"
        assert isset cmd
 
@@ -174,54 +218,83 @@ 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
+hook_config_new() {
+       assert [ $# -ge 2 ]
 
-       local hook_config="${2}"
-       assert isset hook_config
+       hook_config_cmd "new" "$@"
+}
+
+hook_config_destroy() {
+       assert [ $# -eq 2 ]
+       local zone=${1}
+       # The id must be the id and not the hid.
+       local id=${2}
 
        shift 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}
+       # 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="$(zone_get_hook "${zone}")"
-       assert isset hook_zone
+       local hook=$(zone_config_get_hook_from_id ${zone}  ${id})
+       assert isset hook
 
-       hook_zone_config_exec "${hook_zone}" "${hook_config}" create "${zone}" "$@"
-       exit $?
-}
+       # First we bring the hook down
+       hook_config_cmd "down" "${zone}" "${hook}" "${hook}.${id}"
+
+       # If a hook_destroy function is implemented in the hook this function will be executed.
+       # If not a empty defined in header-config is executed.
+       if ! hook_config_cmd "destroy" "${zone}" "${hook}" "${hook}.${id}" "$@"; then
+
+               # A better error message should printed inside the hook.
+               # We will not bring the config up because we do not know if it is safe or if some parts are already destroyed.
+               log ERROR "Could not destroy config with the follwoing id: ${id}"
+               return ${EXIT_ERROR}
+       fi
 
-function hook_config_edit() {
-       hook_config_cmd edit "$@"
+       # Now we delete the config of the zone
+       zone_config_settings_destroy "${zone}" "${hook}.${id}"
 }
 
-function hook_config_remove() {
-       cmd_not_implemented
+hook_config_edit() {
+       assert [ $# -ge 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}" "$@"
 }
 
-function hook_config_show() {
+hook_config_show() {
        cmd_not_implemented
 }
 
-function hook_ppp_write_config() {
+hook_ppp_write_config() {
        cmd_not_implemented
 
        # Arguments: <zone> <filename>
 }
 
-function hook_ppp_ip_pre_up() {
+hook_ppp_ip_pre_up() {
        local zone="${1}"
        assert isset zone
        shift
@@ -235,7 +308,7 @@ function hook_ppp_ip_pre_up() {
        exit $?
 }
 
-function hook_ppp_ipv4_up() {
+hook_ppp_ipv4_up() {
        local zone="${1}"
        assert isset zone
        shift
@@ -249,7 +322,7 @@ function hook_ppp_ipv4_up() {
        exit $?
 }
 
-function hook_ppp_ipv4_down() {
+hook_ppp_ipv4_down() {
        local zone="${1}"
        assert isset zone
        shift
@@ -263,7 +336,7 @@ function hook_ppp_ipv4_down() {
        exit $?
 }
 
-function hook_ppp_ipv6_up() {
+hook_ppp_ipv6_up() {
        local zone="${1}"
        assert isset zone
        shift
@@ -277,7 +350,7 @@ function hook_ppp_ipv6_up() {
        exit $?
 }
 
-function hook_ppp_ipv6_down() {
+hook_ppp_ipv6_down() {
        local zone="${1}"
        assert isset zone
        shift