]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.zone
zone: new function zone_config_get_hook_from_id
[people/ms/network.git] / src / functions / functions.zone
index c46cae94efedcd3941337cd91f45e52312c86f16..90d7e115275e99b318e530440111a6fc63a5c4f8 100644 (file)
@@ -284,6 +284,46 @@ zone_edit() {
        hook_zone_exec ${hook} edit ${zone} $@
 }
 
+zone_rename() {
+       assert [ $# -eq 2 ]
+
+       local zone="${1}"
+       local name="${2}"
+
+       assert zone_exists "${zone}"
+       assert not zone_has_destroy_tag "${zone}"
+       assert not zone_exists "${name}"
+
+       # The zone must be shut down before, is then renamed and
+       # potentially brought up again
+
+       # Save if the zone is running right now
+       zone_is_active "${zone}"
+       local zone_was_active="${?}"
+
+       # Save if the zone is enabled (i.e. auto-start)
+       zone_is_enabled "${zone}"
+       local zone_was_enabled="${?}"
+
+       # Stop the zone
+       zone_stop "${zone}"
+
+       # Disable the zone
+       zone_disable "${zone}"
+
+       # Rename the configuration files
+       mv -f "$(zone_dir "${zone}")" "$(zone_dir "${name}")"
+
+       # Enable the zone if it was enabled before
+       [ ${zone_was_enabled} -eq ${EXIT_TRUE} ] && zone_enable "${name}"
+
+       # Start the zone if it was up before
+       [ ${zone_was_active} -eq ${EXIT_TRUE} ] && zone_start "${name}"
+
+       log INFO "Zone ${zone} was renamed to ${name}"
+       return ${EXIT_OK}
+}
+
 
 zone_destroy() {
        local zone="${1}"
@@ -420,6 +460,35 @@ zone_status() {
        fi
 }
 
+zone_identify() {
+       assert [ $# -ge 1 ]
+
+       local zone="${1}"
+       shift
+
+       assert zone_exists "${zone}"
+
+       log INFO "Identifying zone ${zone}"
+       local pids
+
+       local pid
+       local port
+       for port in $(zone_get_ports "${zone}"); do
+               # Identify all the ports
+               port_identify "${port}" --background $@
+
+               # Save the PIDs of the subprocesses
+               list_append pids "$(cmd_background_get_pid)"
+       done
+
+       # Wait until all port_identfy processes have finished
+       for pid in ${pids}; do
+               cmd_background_result "${pid}"
+       done
+
+       return ${EXIT_OK}
+}
+
 zone_get_ports() {
        local zone=${1}
 
@@ -477,15 +546,15 @@ zone_config() {
        assert zone_exists "${zone}"
 
        case "${cmd}" in
-               create)
-                       zone_config_create "${zone}" "$@"
+               new)
+                       zone_config_new "${zone}" "$@"
+                       ;;
+               destroy)
+                       zone_config_destroy "${zone}" "$@"
                        ;;
                edit)
                        zone_config_edit "${zone}" "$@"
                        ;;
-               remove)
-                       zone_config_remove "${zone}" "$@"
-                       ;;
                *)
                        error "Unrecognized argument: ${cmd}"
                        cli_usage root-zone-config-subcommands
@@ -507,16 +576,27 @@ zone_config_cmd() {
        hook_zone_exec "${hook}" "config_${cmd}" "${zone}" "$@"
 }
 
-zone_config_create() {
-       zone_config_cmd "create" "$@"
+zone_config_new() {
+       local zone="${1}"
+       shift
+
+       # Create a new configuration, but exit when that was
+       # not successful.
+       zone_config_cmd "new" "${zone}" "$@" || return ${?}
+
+       # If the config could be created, we will try to bring
+       # it up if the zone is up, too.
+       if zone_is_up "${zone}"; then
+               zone_configs_up "${zone}"
+       fi
 }
 
-zone_config_edit() {
-       zone_config_cmd "edit" "$@"
+zone_config_destroy() {
+       zone_config_cmd "destroy" "$@"
 }
 
-zone_config_remove() {
-       zone_config_cmd "remove" "$@"
+zone_config_edit() {
+       zone_config_cmd "edit" "$@"
 }
 
 zone_config_show() {
@@ -549,6 +629,26 @@ zones_get_all() {
        done
 }
 
+zones_get_next_free() {
+       # This function return the next free zones.
+       # Example net0 upl0 upl1 are configured so the next free zones are:
+       # net1 upl2
+       local i
+       local zone_name
+       for zone_name in ${VALID_ZONES}; do
+               i=0
+
+               while true; do
+                       local zone="${zone_name}${i}"
+                       if ! zone_exists ${zone}; then
+                               echo "${zone}"
+                               break
+                       fi
+                       i=$(( i + 1 ))
+               done
+       done
+}
+
 zones_get_local() {
        local zone
        for zone in $(zones_get_all); do
@@ -649,6 +749,11 @@ zone_port_attach() {
        local hook="$(zone_get_hook "${zone}")"
        assert isset hook
 
+       # Make the port briefly flash if supported
+       if device_exists ${port}; then
+               port_identify "${port}" --background
+       fi
+
        hook_zone_exec "${hook}" "port_attach" "${zone}" "${port}" "$@"
        local ret="${?}"
 
@@ -712,6 +817,9 @@ zone_port_detach() {
        local hook=$(zone_get_hook "${zone}")
        assert isset hook
 
+       # Make the port briefly flash if supported
+       port_identify "${port}" --background
+
        hook_zone_exec "${hook}" "port_detach" "${zone}" "${port}" "$@"
        local ret="${?}"
 
@@ -884,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 ]
 
@@ -900,6 +1026,61 @@ zone_config_get_hook() {
        print "${HOOK}"
 }
 
+zone_config_hook_is_configured() {
+       # Checks if a zone has already at least one config with the given hook.
+       # Returns True when yes and False when no
+
+       assert [ $# -eq 2 ]
+       local zone=${1}
+       local hook=${2}
+
+       local config
+       for config in $(zone_configs_list "${zone}"); do
+               local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
+               assert isset config_hook
+               if [[ ${hook} == ${config_hook} ]]; then
+                       return ${EXIT_TRUE}
+               fi
+
+       done
+
+       # If we get here the zone has no config with the given hook
+       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_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 $@
 }
@@ -1092,3 +1273,18 @@ zone_port_settings_remove() {
        local path="$(zone_dir "${zone}")/ports/${port}"
        settings_remove "${path}"
 }
+
+zone_get_color() {
+       # This function return the color of a zone
+       assert [ $# -eq 1 ]
+
+       local name=${1}
+       color_read "zone" ${name}
+}
+
+zone_get_description_title() {
+       assert [ $# -eq 1 ]
+
+       local name=${1}
+       description_title_read $(description_format_filename "zone" "${name}")
+}