]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.zone
Cleanup code that deletes ports/zones
[people/ms/network.git] / src / functions / functions.zone
index e9e2dd44220a355881388d616166244b5ef1d613..b9d475f71745e7cbadd97bbff92bef3eb19a4228 100644 (file)
 #                                                                             #
 ###############################################################################
 
-zone_dir() {
-       local zone=${1}
-
-       echo "${NETWORK_ZONE_DIR}/zones/${zone}"
-}
-
 zone_exists() {
        local zone=${1}
        assert isset zone
 
-       [ -d "$(zone_dir ${zone})" ]
+       [ -d "${NETWORK_ZONES_DIR}/${zone}" ]
 }
 
 zone_match() {
@@ -68,7 +62,7 @@ zone_get_hook() {
        local zone=${1}
        assert isset zone
 
-       config_get_hook $(zone_dir ${zone})/settings
+       config_get_hook "${NETWORK_ZONES_DIR}/${zone}/settings"
 }
 
 zone_start() {
@@ -233,10 +227,11 @@ zone_new() {
                return ${EXIT_ERROR}
        fi
 
-       mkdir -p $(zone_dir ${zone})
-
        # Create directories for configs and ports
-       mkdir -p $(zone_dir ${zone})/{configs,ports}
+       local what
+       for what in configs ports; do
+               make_directory "${NETWORK_ZONES_DIR}/${zone}/${what}"
+       done
 
        hook_zone_exec "${hook}" "new" "${zone}" "$@"
        local ret=$?
@@ -305,7 +300,7 @@ zone_rename() {
        zone_disable "${zone}"
 
        # Rename the configuration files
-       mv -f "$(zone_dir "${zone}")" "$(zone_dir "${name}")"
+       mv -f "${NETWORK_ZONES_DIR}/${zone}" "${NETWORK_ZONES_DIR}/${name}"
 
        # Enable the zone if it was enabled before
        [ ${zone_was_enabled} -eq ${EXIT_TRUE} ] && zone_enable "${name}"
@@ -327,15 +322,19 @@ zone_destroy() {
                return ${EXIT_ERROR}
        fi
 
-       log INFO "Destroying zone ${zone}"
-
        # Force the zone down.
        zone_is_active "${zone}" && zone_stop "${zone}"
 
        # Disable zone auto-start
        zone_disable "${zone}"
 
-       rm -rf "$(zone_dir "${zone}")"
+       if ! rm -rf "${NETWORK_ZONES_DIR}/${zone}"; then
+               log ERROR "Could not destroy zone ${zone}"
+               return ${EXIT_ERROR}
+       fi
+
+       log INFO "Destroyed zone ${zone}"
+       return ${EXIT_OK}
 }
 
 zone_up() {
@@ -457,10 +456,8 @@ zone_get_ports() {
        assert isset zone
 
        local port
-       for port in $(zone_dir ${zone})/ports/*; do
-               port=$(basename ${port})
-
-               if port_exists ${port}; then
+       for port in $(list_directory "${NETWORK_ZONES_DIR}/${zone}/ports"); do
+               if port_exists "${port}"; then
                        echo "${port}"
                fi
        done
@@ -472,9 +469,7 @@ zone_get_ports_num() {
 
        local counter=0
        local port
-       for port in $(zone_dir "${zone}")/ports/*; do
-               port="$(basename "${port}")"
-
+       for port in $(list_directory "${NETWORK_ZONES_DIR}/${zone}/ports"); do
                if port_exists "${port}"; then
                        counter=$(( ${counter} + 1 ))
                fi
@@ -495,7 +490,7 @@ zone_has_port() {
        assert isset zone
        assert isset port
 
-       [ -e "$(zone_dir ${zone})/ports/${port}" ]
+       [ -e "${NETWORK_ZONES_DIR}/${zone}/ports/${port}" ]
 }
 
 zone_config() {
@@ -705,29 +700,12 @@ zone_config_convert_hid_to_id() {
        return ${EXIT_FALSE}
 }
 
-zone_show() {
-       local zone=${1}
-
-       echo "${zone}"
-       echo "  Type: $(zone_get_hook ${zone})"
-       echo
-}
-
-zones_show() {
-       local zone
-
-       for zone in $(zones_get "$@"); do
-               zone_show ${zone}
-       done
-}
-
 zones_get_all() {
        local zone
-       for zone in $(zone_dir)/*; do
-               zone=$(basename ${zone})
-               zone_exists ${zone} || continue
-
-               echo "${zone}"
+       for zone in $(list_directory "${NETWORK_ZONES_DIR}"); do
+               if zone_exists ${zone}; then
+                       echo "${zone}"
+               fi
        done
 }
 
@@ -816,12 +794,7 @@ zones_get() {
 zone_ports_list() {
        local zone=${1}
 
-       local port
-       for port in $(zone_dir ${zone})/ports/*; do
-               [ -e "${port}" ] || continue
-
-               echo $(basename ${port})
-       done
+       list_directory "${NETWORK_ZONES_DIR}/${zone}/ports"
 }
 
 zone_port_attach() {
@@ -1086,12 +1059,7 @@ zone_configs_status() {
 zone_configs_list() {
        local zone=${1}
 
-       local config
-       for config in $(zone_dir ${zone})/configs/*; do
-               [ -e "${config}" ] || continue
-
-               basename ${config}
-       done
+       list_directory "${NETWORK_ZONES_DIR}/${zone}/configs"
 }
 
 zone_config_get_new_id() {
@@ -1100,7 +1068,7 @@ zone_config_get_new_id() {
        assert [ $# -eq 1 ]
        local zone=${1}
 
-       local zone_path=$(zone_dir ${zone})
+       local zone_path="${NETWORK_ZONES_DIR}/${zone}"
        local i=0
 
        while true; do
@@ -1117,22 +1085,29 @@ zone_config_check_same_setting() {
        # with the same setting is already configured for this zone.
        # Returns True when yes and False when no.
 
-       assert [ $# -eq 4 ]
+       assert [ $# -eq 5 ]
 
        local zone=${1}
        local hook=${2}
-       local key=${3}
-       local value=${4}
+       local id=${3}
+       local key=${4}
+       local value=${5}
 
        # The key should be local for this function
        local ${key}
        local config
 
        for config in $(zone_configs_list ${zone}); do
+               # Check if the config is eqal with the config we want to edit, when continue
+               if [[ "${config}" = "${hook}.${id}" ]]; then
+                       continue
+               fi
+
                # Check if the config is from the given hook, when not continue
                if  [[ $(zone_config_get_hook "${zone}" "${config}") != ${hook} ]]; then
                        continue
                fi
+
                # Get the value of the key for a given function
                zone_config_settings_read "${zone}" "${config}" \
                 --ignore-superfluous-settings "${key}"
@@ -1191,7 +1166,7 @@ zone_config_id_is_valid() {
        local zone=${1}
        local id=${2}
 
-       local zone_path=$(zone_dir ${zone})
+       local zone_path="${NETWORK_ZONES_DIR}/${zone}"
 
        [ -f ${zone_path}/configs/*.${id} ];
 }
@@ -1271,14 +1246,6 @@ zone_get_supported_config_hooks() {
        hook_config_get_all
 }
 
-zone_file() {
-       local zone=${1}
-
-       assert isset zone
-
-       echo "$(zone_dir ${zone})/settings"
-}
-
 zone_settings_read() {
        local zone=${1}
        assert isset zone
@@ -1294,7 +1261,7 @@ zone_settings_read() {
        # Save the HOOK variable.
        local hook="${HOOK}"
 
-       settings_read "$(zone_file "${zone}")" ${args}
+       settings_read "${NETWORK_ZONES_DIR}/${zone}/settings" ${args}
 
        # Restore hook.
        HOOK="${hook}"
@@ -1310,7 +1277,7 @@ zone_settings_write() {
        fi
        list_append args ${HOOK_SETTINGS}
 
-       settings_write "$(zone_file ${zone})" ${args}
+       settings_write "${NETWORK_ZONES_DIR}/${zone}/settings" ${args}
 }
 
 zone_settings_set() {
@@ -1360,7 +1327,7 @@ zone_config_settings_read() {
                list_append args "$@"
        fi
 
-       local path="$(zone_dir "${zone}")/configs/${config}"
+       local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
        settings_read "${path}" ${args}
 }
 
@@ -1371,10 +1338,7 @@ zone_config_settings_write() {
        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
+       assert isset id
 
        local args
        if function_exists "hook_check_config_settings"; then
@@ -1382,7 +1346,7 @@ zone_config_settings_write() {
        fi
        list_append args ${HOOK_CONFIG_SETTINGS}
 
-       local path="$(zone_dir "${zone}")/configs/${hook}.${id}"
+       local path="${NETWORK_ZONES_DIR}/${zone}/configs/${hook}.${id}"
        settings_write "${path}" ${args}
 }
 
@@ -1392,7 +1356,7 @@ zone_config_settings_destroy() {
        local zone="${1}"
        local config="${2}"
 
-       local path="$(zone_dir "${zone}")/configs/${config}"
+       local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
 
        # Check if path is valid
        if [ ! -f ${path} ]; then
@@ -1404,6 +1368,39 @@ zone_config_settings_destroy() {
        rm -f "${path}"
 
 }
+
+zone_config_find_by_hook() {
+       local zone="${1}"
+       assert isset zone
+
+       local hook="${2}"
+       assert isset hook
+
+       local config
+       for config in $(zone_configs_list "${zone}"); do
+               local h="$(zone_config_get_hook "${zone}" "${config}")"
+
+               [[ "${hook}" = "${h}" ]] && echo "${config}"
+       done
+
+       return ${EXIT_OK}
+}
+
+zone_config_settings_read_by_hook() {
+       local zone="${1}"
+       assert isset zone
+
+       local hook="${2}"
+       assert isset hook
+
+       local config
+       for config in $(zone_config_find_by_hook "${zone}" "${hook}"); do
+               zone_config_settings_read "${zone}" "${config}"
+       done
+
+       return ${EXIT_OK}
+}
+
 zone_port_settings_read() {
        assert [ $# -ge 2 ]
 
@@ -1418,7 +1415,7 @@ zone_port_settings_read() {
                list_append args "$@"
        fi
 
-       local path="$(zone_dir "${zone}")/ports/${port}"
+       local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
        settings_read "${path}" ${args}
 }
 
@@ -1435,7 +1432,7 @@ zone_port_settings_write() {
        fi
        list_append args ${HOOK_PORT_SETTINGS}
 
-       local path="$(zone_dir "${zone}")/ports/${port}"
+       local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
        settings_write "${path}" ${args}
 }
 
@@ -1445,7 +1442,7 @@ zone_port_settings_remove() {
        local zone="${1}"
        local port="${2}"
 
-       local path="$(zone_dir "${zone}")/ports/${port}"
+       local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
        settings_remove "${path}"
 }