function zone_dir() {
local zone=${1}
- #assert isset zone
-
- echo "${ZONE_DIR}/zones/${zone}"
+ echo "${NETWORK_ZONE_DIR}/zones/${zone}"
}
function zone_exists() {
local zone=${1}
-
assert isset zone
[ -d "$(zone_dir ${zone})" ]
function zone_name_is_valid() {
local zone=${1}
- assert isset zone
+ # Don't accept empty strings.
+ [ -z "${zone}" ] && return ${EXIT_FALSE}
[[ ${zone} =~ $(zone_match) ]]
}
function zone_is_local() {
local zone=${1}
- ! zone_is_nonlocal ${zone}
+ [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
}
function zone_is_nonlocal() {
local zone=${1}
+ [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
+}
+
+function zone_get_hook() {
+ local zone=${1}
assert isset zone
- [[ ${zone} =~ ^red[0-9]{1,5} ]]
+ config_get_hook $(zone_dir ${zone})/settings
}
-function zone_get_hook() {
+function zone_start() {
+ # This function will bring up the zone
+ # 'asynchronously' with help of systemd.
+
local zone=${1}
+ assert zone_exists ${zone}
+
+ service_start "network@${zone}.service"
+}
+
+function zone_stop() {
+ # This function will bring down the zone
+ # 'asynchronously' with help of systemd.
+
+ local zone=${1}
+ assert zone_exists ${zone}
+
+ service_stop "network@${zone}.service"
+}
+function zone_enable() {
+ # This function will enable the zone
+ # with help of systemd.
+
+ local zone="${1}"
+ assert zone_exists "${zone}"
+
+ # Enable service for the zone
+ service_enable "network@${zone}.service"
+ local ret=$?
+
+ if [ ${ret} -eq ${EXIT_OK} ]; then
+ log INFO "Auto-start enabled for zone ${zone}"
+ return ${EXIT_OK}
+ fi
+
+ log ERROR "Could not enable zone ${zone}: ${ret}"
+ return ${ret}
+}
+
+function zone_disable() {
+ # This function will disable the zone
+ # with help of systemd.
+
+ local zone="${1}"
+ assert zone_exists "${zone}"
+
+ # Disable service for the zone
+ service_disable "network@${zone}.service"
+ local ret=$?
+
+ if [ ${ret} -eq ${EXIT_OK} ]; then
+ log INFO "Auto-start disabled for zone ${zone}"
+ return ${EXIT_OK}
+ fi
+
+ log ERROR "Could not disable zone ${zone}: ${ret}"
+ return ${ret}
+}
+
+function zone_is_enabled() {
+ local zone="${1}"
assert isset zone
- config_get_hook $(zone_dir ${zone})/settings
+ # Ask systemd if the zone is enabled.
+ if service_is_enabled "network@${zone}.service"; then
+ return ${EXIT_TRUE}
+ fi
+
+ return ${EXIT_FALSE}
}
function zone_create() {
mkdir -p $(zone_dir ${zone})
- # Create directory for ports
- mkdir -p $(zone_dir ${zone})/ports
+ # Create directories for configs and ports
+ mkdir -p $(zone_dir ${zone})/{configs,ports}
hook_zone_exec ${hook} create ${zone} $@
local ret=$?
# Maybe the zone create hook did not exit correctly.
# If this is the case we remove the created zone immediately.
if [ "${ret}" = "${EXIT_ERROR}" ]; then
- zone_remove ${zone}
+ zone_remove_now ${zone}
+ return ${EXIT_ERROR}
fi
+
+ # Automatically enable zone.
+ zone_enable "${zone}"
}
function zone_edit() {
return ${EXIT_ERROR}
fi
+ # Check if the zone is tagged for removal.
+ if zone_has_remove_tag ${zone}; then
+ error "You cannot edit a zone that is tagged for removal."
+ return ${EXIT_ERROR}
+ fi
+
local hook=$(config_get_hook $(zone_dir ${zone})/settings)
if [ -z "${hook}" ]; then
hook_zone_exec ${hook} edit ${zone} $@
}
+
function zone_remove() {
local zone=${1}
- shift
+ assert zone_exists ${zone}
- if ! zone_exists ${zone}; then
- error "Zone '${zone}' does not exist."
- return ${EXIT_ERROR}
- fi
+ # Make the zone for removal.
+ touch $(zone_dir ${zone})/.remove
+
+ log INFO "Zone '${zone}' has been tagged for removal."
+}
- # XXX Tear this down here?
+function zone_has_remove_tag() {
+ local zone=${1}
+ assert zone_exists ${zone}
+
+ [ -e "$(zone_dir ${zone})/.remove" ]
+}
+
+# This function will remove the given zone
+# RIGHT NOW. Use zone_remove to remove it
+# at the next status change.
+function zone_remove_now() {
+ local zone=${1}
+ assert zone_exists ${zone}
+
+ log INFO "Removing zone '${zone}' right now."
+
+ # Force the zone down.
+ zone_is_up ${zone} && zone_set_down ${zone}
+
+ # Disable zone.
+ zone_disable "${zone}"
rm -rf $(zone_dir ${zone})
}
return ${EXIT_ERROR}
fi
+ # Check if a zone has got the remove tag.
+ if zone_has_remove_tag ${zone}; then
+ error "Cannot bring up any zone which is to be removed."
+ return ${EXIT_ERROR}
+ fi
+
local hook=$(config_get_hook $(zone_dir ${zone})/settings)
if [ -z "${hook}" ]; then
hook_zone_exec ${hook} down ${zone} $@
zone_db ${zone} stopped
+
+ # Remove the zone, if it has got a remove tag.
+ if zone_has_remove_tag ${zone}; then
+ zone_remove_now ${zone}
+ fi
}
function zone_status() {
fi
hook_zone_exec ${hook} status ${zone} $@
+
+ # Show that the zone it to be removed soon.
+ if zone_has_remove_tag ${zone}; then
+ warning "This zone is tagged for removal."
+ fi
}
function zone_port() {
assert isset hook_zone
assert isset hook_port
- assert hook_zone_port_exists ${hook_zone} ${hook_port}
-
hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
}
done
}
+function zone_has_port() {
+ # Check, if the given port is configured
+ # in this zone.
+
+ local zone=${1}
+ local port=${2}
+ shift 2
+
+ assert isset zone
+ assert isset port
+
+ [ -e "$(zone_dir ${zone})/ports/${port}" ]
+}
+
+# XXX overwritten some lines below
function zone_config() {
local zone=${1}
shift
hook_zone_exec ${hook} config ${zone} $@
}
+function zone_config() {
+ local zone=${1}
+ local action=${2}
+ shift 2
+
+ assert isset zone
+ assert isset action
+ assert zone_exists ${zone}
+
+ # Aliases
+ case "${action}" in
+ del|delete|remove)
+ action="rem"
+ ;;
+ esac
+
+ case "${action}" in
+ create|edit|rem)
+ zone_config_${action} ${zone} $@
+ ;;
+ *)
+ error "Unrecognized argument: ${action}"
+ 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
+
+ assert isset zone
+ assert isset option
+
+ (
+ VALUE="${default}"
+ zone_config_read ${zone}
+
+ VALUE="${!option}"
+ echo "${VALUE}"
+ )
+}
+
+function zone_config_create() {
+ local zone=${1}
+ shift
+
+ assert isset zone
+
+ local hook=$(zone_get_hook ${zone})
+
+ assert isset hook
+
+ hook_zone_exec ${hook} config_create ${zone} $@
+}
+
function zone_show() {
local zone=${1}
zone_exists ${zone} || continue
echo "${zone}"
- done | sort
+ done
}
function zones_get_local() {
local zone=${1}
local port
- for port in $(zone_dir ${zone})/port.*; do
+ for port in $(zone_dir ${zone})/ports/*; do
[ -e "${port}" ] || continue
echo $(basename ${port})
- done | sort
+ done
}
function zone_ports_cmd() {
local port
for port in $(zone_get_ports ${zone}); do
- #zone_port_cmd ${cmd} ${zone} ${port} $@
hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
done
}
local zone=${1}
local config
- for config in $(zone_dir ${zone})/config.*; do
+ for config in $(zone_dir ${zone})/configs/*; do
[ -e "${config}" ] || continue
- echo $(basename ${config})
- done | sort
+ basename ${config}
+ done
}
function zone_configs_cmd() {
local hook_config
local config
for config in $(zone_configs_list ${zone}); do
- hook_config=$(config_get_hook $(zone_dir ${zone})/${config})
+ hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
done
zone_configs_cmd down $@
}
-function zone_has_ipv4() {
- device_has_ipv4 $@
+function zone_configs_status() {
+ zone_configs_cmd config_status $@
}
-function zone_has_ipv6() {
- device_has_ipv6 $@
+function zone_has_ip() {
+ device_has_ip $@
}
function zone_db() {
! zone_is_up $@
}
-function zone_get_supported_hooks() {
+function zone_get_supported_port_hooks() {
local zone=${1}
local hook=$(zone_get_hook ${zone})
hook_zone_ports_get_all ${hook}
}
+function zone_get_supported_config_hooks() {
+ local zone=${1}
+
+ local hook=$(zone_get_hook ${zone})
+
+ hook_zone_configs_get_all ${hook}
+}
+
function zone_file() {
local zone=${1}
assert isset zone
+ # Save the HOOK variable.
+ local hook="${HOOK}"
+
config_read $(zone_file ${zone})
+
+ # Restore hook.
+ HOOK="${hook}"
}
function zone_config_write() {
zone_config_write ${zone}
)
}
+
+function zone_config_get() {
+ local zone=${1}
+ local key=${2}
+
+ assert isset zone
+ assert isset key
+
+ (
+ zone_config_read ${zone}
+
+ echo "${!key}"
+ )
+}