]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.zone
Split port hooks in to (create|remove|up|down) actions
[people/stevee/network.git] / src / functions / functions.zone
index 75ee70ebacaabf38898cc02b6712d00629c90b3a..bf156dc9c7d6696e2c8086e7029928c54e89b701 100644 (file)
@@ -81,6 +81,28 @@ function zone_start() {
        service_start "network@${zone}.service"
 }
 
+function zone_start_auto() {
+       local zone="${1}"
+       assert zone_exists "${zone}"
+
+       # If the zone has already been started, we
+       # will reload it so the current configuration
+       # is re-applied.
+       if zone_is_active "${zone}"; then
+               zone_reload "${zone}"
+               return ${?}
+
+       # If the zone is still down, but in auto-start mode,
+       # we will start it.
+       elif zone_is_enabled "${zone}"; then
+               zone_start "${zone}"
+               return ${?}
+       fi
+
+       # Otherwise, nothing will be done.
+       return ${EXIT_OK}
+}
+
 function zone_stop() {
        # This function will bring down the zone
        # 'asynchronously' with help of systemd.
@@ -91,6 +113,22 @@ function zone_stop() {
        service_stop "network@${zone}.service"
 }
 
+function zone_reload() {
+       local zone="${1}"
+       assert zone_exists "${zone}"
+
+       service_reload "network@${zone}.service"
+}
+
+function zone_hotplug_event() {
+       local zone="${1}"
+       assert isset zone
+
+       hotplug_assert_in_hotplug_event
+
+       zone_cmd "hotplug" "${zone}"
+}
+
 function zone_enable() {
        # This function will enable the zone
        # with help of systemd.
@@ -143,6 +181,38 @@ function zone_is_enabled() {
        return ${EXIT_FALSE}
 }
 
+function zone_is_active() {
+       local zone="${1}"
+       assert isset zone
+
+       if service_is_active "network@${zone}.service"; then
+               return ${EXIT_TRUE}
+       fi
+
+       return ${EXIT_FALSE}
+}
+
+function zone_is_enabled_or_active() {
+       local zone="${1}"
+       assert isset zone
+
+       zone_is_enabled "${zone}" || zone_is_active "${zone}"
+}
+
+function zone_cmd() {
+       local cmd="${1}"
+       local port="${2}"
+       shift 2
+
+       assert isset cmd
+       assert isset zone
+
+       local hook="$(zone_get_hook ${zone})"
+       assert isset hook
+
+       hook_exec zone "${hook}" "${cmd}" "${zone}" $@
+}
+
 function zone_create() {
        local zone=${1}
        local hook=${2}
@@ -197,8 +267,7 @@ function zone_edit() {
                return ${EXIT_ERROR}
        fi
 
-       local hook=$(config_get_hook $(zone_dir ${zone})/settings)
-
+       local hook="$(zone_get_hook "${zone}")"
        if [ -z "${hook}" ]; then
                error "Config file did not provide any hook."
                return ${EXIT_ERROR}
@@ -263,8 +332,7 @@ function zone_up() {
                return ${EXIT_ERROR}
        fi
 
-       local hook=$(config_get_hook $(zone_dir ${zone})/settings)
-
+       local hook="$(zone_get_hook "${zone}")"
        if [ -z "${hook}" ]; then
                error "Config file did not provide any hook."
                return ${EXIT_ERROR}
@@ -291,8 +359,7 @@ function zone_down() {
                return ${EXIT_ERROR}
        fi
 
-       local hook=$(config_get_hook $(zone_dir ${zone})/settings)
-
+       local hook="$(zone_get_hook "${zone}")"
        if [ -z "${hook}" ]; then
                error "Config file did not provide any hook."
                return ${EXIT_ERROR}
@@ -316,27 +383,27 @@ function zone_down() {
 }
 
 function zone_status() {
-       local zone=${1}
+       local zone="${1}"
+       assert isset zone
        shift
 
-       if ! zone_exists ${zone}; then
+       if ! zone_exists "${zone}"; then
                error "Zone '${zone}' does not exist."
                return ${EXIT_ERROR}
        fi
 
-       local hook=$(config_get_hook $(zone_dir ${zone})/settings)
-
+       local hook="$(zone_get_hook "${zone}")"
        if [ -z "${hook}" ]; then
                error "Config file did not provide any hook."
                return ${EXIT_ERROR}
        fi
 
-       if ! hook_zone_exists ${hook}; then
+       if ! hook_zone_exists "${hook}"; then
                error "Hook '${hook}' does not exist."
                return ${EXIT_ERROR}
        fi
 
-       hook_zone_exec ${hook} status ${zone} $@
+       hook_zone_exec "${hook}" "status" "${zone}" "$@"
 
        # Show that the zone it to be removed soon.
        if zone_has_remove_tag ${zone}; then
@@ -458,6 +525,24 @@ function zone_get_ports() {
        done
 }
 
+function zone_get_ports_num() {
+       local zone="${1}"
+       assert isset zone
+
+       local counter=0
+       local port
+       for port in $(zone_dir "${zone}")/ports/*; do
+               port="$(basename "${port}")"
+
+               if port_exists "${port}"; then
+                       counter=$(( ${counter} + 1 ))
+               fi
+       done
+
+       echo "${counter}"
+       return ${EXIT_OK}
+}
+
 function zone_has_port() {
        # Check, if the given port is configured
        # in this zone.
@@ -472,88 +557,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_option() {
-       local zone=${1}
-       local option=${2}
-       local default=${3}
-       shift 2
+function zone_config_cmd() {
+       assert [ $# -gt 2 ]
 
-       assert isset zone
-       assert isset option
+       local cmd="${1}"
+       local zone="${2}"
+       shift 2
 
-       (
-               VALUE="${default}"
-               zone_config_read ${zone}
+       local hook="$(zone_get_hook "${zone}")"
+       assert isset hook
 
-               VALUE="${!option}"
-               echo "${VALUE}"
-       )
+       hook_zone_exec "${hook}" "config_${cmd}" "${zone}" "$@"
 }
 
 function zone_config_create() {
-       local zone=${1}
-       shift
-
-       assert isset zone
+       zone_config_cmd "create" "$@"
+}
 
-       local hook=$(zone_get_hook ${zone})
+function zone_config_edit() {
+       zone_config_cmd "edit" "$@"
+}
 
-       assert isset hook
+function zone_config_remove() {
+       zone_config_cmd "remove" "$@"
+}
 
-       hook_zone_exec ${hook} config_create ${zone} $@
+function zone_config_show() {
+       zone_config_cmd "show" "$@"
 }
 
 function zone_show() {
@@ -673,6 +730,14 @@ function zone_ports_cmd() {
        done
 }
 
+function zone_ports_create() {
+       zone_ports_cmd "port_create" $@
+}
+
+function zone_ports_remove() {
+       zone_ports_cmd "port_remove" $@
+}
+
 function zone_ports_up() {
        zone_ports_cmd port_up $@
 }
@@ -685,43 +750,61 @@ 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 [ $# -ge 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})
+       for config in $(zone_configs_list "${zone}"); do
+               local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
+               assert isset config_hook
 
-               hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
+               hook_config_exec "${config_hook}" "${cmd}" "${zone}" "${config}" $@
        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_config_get_hook() {
+       assert [ $# -eq 2 ]
+
+       local zone="${1}"
+       assert isset zone
+
+       local config="${2}"
+       assert isset config
+
+       local HOOK
+       zone_config_settings_read "${zone}" "${config}" \
+               --ignore-superfluous-settings HOOK
+
+       print "${HOOK}"
 }
 
 function zone_has_ip() {
@@ -759,11 +842,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() {
@@ -774,7 +853,7 @@ function zone_file() {
        echo "$(zone_dir ${zone})/settings"
 }
 
-function zone_config_read() {
+function zone_settings_read() {
        local zone=${1}
 
        assert isset zone
@@ -782,21 +861,21 @@ function zone_config_read() {
        # Save the HOOK variable.
        local hook="${HOOK}"
 
-       config_read $(zone_file ${zone})
+       settings_read $(zone_file ${zone})
 
        # Restore hook.
        HOOK="${hook}"
 }
 
-function zone_config_write() {
+function zone_settings_write() {
        local zone=${1}
 
        assert isset zone
 
-       config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
+       settings_write $(zone_file ${zone}) ${HOOK_SETTINGS}
 }
 
-function zone_config_set() {
+function zone_settings_set() {
        local zone=${1}
        shift
        local args="$@"
@@ -804,17 +883,17 @@ function zone_config_set() {
        assert isset zone
 
        (
-               zone_config_read ${zone}
+               zone_settings_read ${zone}
 
                for arg in ${args}; do
                        eval "${arg}"
                done
        
-               zone_config_write ${zone}
+               zone_settings_write ${zone}
        )
 }
 
-function zone_config_get() {
+function zone_settings_get() {
        local zone=${1}
        local key=${2}
 
@@ -822,8 +901,62 @@ function zone_config_get() {
        assert isset key
 
        (
-               zone_config_read ${zone}
+               zone_settings_read ${zone}
 
                echo "${!key}"
        )
 }
+
+function zone_config_settings_read() {
+       assert [ $# -gt 2 ]
+
+       local zone="${1}"
+       local config="${2}"
+       shift 2
+
+       local path="$(zone_dir "${zone}")/configs/${config}"
+       settings_read "${path}" "$@"
+}
+
+function zone_config_settings_write() {
+       assert [ $# -gt 2 ]
+
+       local zone="${1}"
+       local config="${2}"
+       shift 2
+
+       local path="$(zone_dir "${zone}")/configs/${config}"
+       settings_write "${path}" "$@"
+}
+
+function zone_port_settings_read() {
+       assert [ $# -gt 2 ]
+
+       local zone="${1}"
+       local port="${2}"
+       shift 2
+
+       local path="$(zone_dir "${zone}")/ports/${port}"
+       settings_read "${path}" "$@"
+}
+
+function zone_port_settings_write() {
+       assert [ $# -gt 2 ]
+
+       local zone="${1}"
+       local port="${2}"
+       shift 2
+
+       local path="$(zone_dir "${zone}")/ports/${port}"
+       settings_write "${path}" "$@"
+}
+
+function zone_port_settings_remove() {
+       assert [ $# -eq 2 ]
+
+       local zone="${1}"
+       local port="${2}"
+
+       local path="$(zone_dir "${zone}")/ports/${port}"
+       settings_remove "${path}"
+}