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}"
hook_zone_exec ${hook} up ${zone} $@
zone_db ${zone} started
+
+ # Execute all triggers after the zone got up
+ triggers_execute_all "up" ZONE="${zone}"
}
zone_down() {
zone_db ${zone} stopped
+ # Execute all triggers after the zone went down
+ triggers_execute_all "down" ZONE="${zone}"
+
# Remove the zone, if it has got a remove tag.
if zone_has_destroy_tag "${zone}"; then
zone_destroy_now "${zone}"
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}
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
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() {
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
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="${?}"
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="${?}"
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 ]
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 $@
}
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}")
+}