function zone_dir() {
local zone=${1}
- echo "${ZONE_DIR}/${zone}"
+ #assert isset zone
+
+ echo "${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
+
[[ ${zone} =~ $(zone_match) ]]
}
function zone_is_local() {
local zone=${1}
- if [[ ${zone} =~ ^red[0-9]{1,5} ]]; then
- return ${EXIT_ERROR}
- fi
- return ${EXIT_OK}
+ ! zone_is_nonlocal ${zone}
+}
+
+function zone_is_nonlocal() {
+ local zone=${1}
+
+ assert isset zone
+
+ [[ ${zone} =~ ^red[0-9]{1,5} ]]
}
function zone_get_hook() {
local zone=${1}
+ assert isset zone
+
config_get_hook $(zone_dir ${zone})/settings
}
return ${EXIT_ERROR}
fi
- if ! hook_exists ${hook}; then
+ if ! hook_zone_exists ${hook}; then
error "Hook '${hook}' does not exist."
return ${EXIT_ERROR}
fi
mkdir -p $(zone_dir ${zone})
- hook_exec ${hook} create ${zone} $@
+ # 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.
return ${EXIT_ERROR}
fi
- if ! hook_exists ${hook}; then
+ if ! hook_zone_exists ${hook}; then
error "Hook '${hook}' does not exist."
return ${EXIT_ERROR}
fi
- hook_exec ${hook} edit ${zone} $@
+ hook_zone_exec ${hook} edit ${zone} $@
}
function zone_remove() {
return ${EXIT_ERROR}
fi
- if ! hook_exists ${hook}; then
+ if ! hook_zone_exists ${hook}; then
error "Hook '${hook}' does not exist."
return ${EXIT_ERROR}
fi
- hook_exec ${hook} up ${zone} $@
+ zone_db ${zone} starting
+
+ hook_zone_exec ${hook} up ${zone} $@
+
+ zone_db ${zone} started
}
function zone_down() {
return ${EXIT_ERROR}
fi
- if ! hook_exists ${hook}; then
+ if ! hook_zone_exists ${hook}; then
error "Hook '${hook}' does not exist."
return ${EXIT_ERROR}
fi
- hook_exec ${hook} down ${zone} $@
+ zone_db ${zone} stopping
+
+ hook_zone_exec ${hook} down ${zone} $@
+
+ zone_db ${zone} stopped
}
function zone_status() {
return ${EXIT_ERROR}
fi
- if ! hook_exists ${hook}; then
+ if ! hook_zone_exists ${hook}; then
error "Hook '${hook}' does not exist."
return ${EXIT_ERROR}
fi
- hook_exec ${hook} status ${zone} $@
+ hook_zone_exec ${hook} status ${zone} $@
}
function zone_port() {
+ 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
+ add|edit|rem)
+ zone_port_${action} ${zone} $@
+ ;;
+ *)
+ error "Unrecognized argument: ${action}"
+ cli_usage root-zone-port-subcommands
+ exit ${EXIT_ERROR}
+ ;;
+ esac
+}
+
+function zone_port_add() {
local zone=${1}
shift
- if ! zone_exists ${zone}; then
- error "Zone '${zone}' does not exist."
- return ${EXIT_ERROR}
- fi
+ assert isset zone
- 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
+ assert isset hook
- if ! hook_exists ${hook}; then
- error "Hook '${hook}' does not exist."
- return ${EXIT_ERROR}
- fi
+ hook_zone_exec ${hook} port_add ${zone} $@
+}
- hook_exec ${hook} port ${zone} $@
+function zone_port_edit() {
+ zone_port_cmd edit $@
}
+function zone_port_rem() {
+ zone_port_cmd rem $@
+}
+
+function zone_port_cmd() {
+ local cmd=${1}
+ local zone=${2}
+ local port=${3}
+ shift 3
+
+ assert isset zone
+ assert isset port
+
+ local hook_zone=$(zone_get_hook ${zone})
+ local hook_port=$(port_get_hook ${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} $@
+}
+
+function zone_port_up() {
+ zone_port_cmd up $@
+}
+
+function zone_port_down() {
+ zone_port_cmd down $@
+}
+
+function zone_get_ports() {
+ local zone=${1}
+
+ assert isset zone
+
+ local port
+ for port in $(zone_dir ${zone})/ports/*; do
+ port=$(basename ${port})
+
+ if port_exists ${port}; then
+ echo "${port}"
+ fi
+ done
+}
+
+# XXX overwritten some lines below
function zone_config() {
local zone=${1}
shift
return ${EXIT_ERROR}
fi
- if ! hook_exists ${hook}; then
+ if ! hook_zone_exists ${hook}; then
error "Hook '${hook}' does not exist."
return ${EXIT_ERROR}
fi
- hook_exec ${hook} config ${zone} $@
+ 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_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() {
function zones_get_all() {
local zone
- for zone in ${ZONE_DIR}/*; do
+ for zone in $(zone_dir)/*; do
zone=$(basename ${zone})
zone_exists ${zone} || continue
echo "${zone}"
- done | sort
+ done
}
function zones_get_local() {
function zones_get_nonlocal() {
local zone
for zone in $(zones_get_all); do
- zone_is_local ${zone} || echo "${zone}"
+ zone_is_nonlocal ${zone} && echo "${zone}"
done
}
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 zone=${2}
shift 2
- local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
+ assert isset cmd
+ assert isset zone
- local hook_port
- local port
- for port in $(zone_ports_list ${zone}); do
- hook_port=$(config_get_hook $(zone_dir ${zone})/${port})
+ assert zone_exists ${zone}
+
+ local hook=$(zone_get_hook ${zone})
- hook_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
+ local port
+ for port in $(zone_get_ports ${zone}); do
+ #zone_port_cmd ${cmd} ${zone} ${port} $@
+ hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
done
}
function zone_ports_up() {
- zone_ports_cmd up $@
+ zone_ports_cmd port_up $@
}
function zone_ports_down() {
- zone_ports_cmd down $@
+ zone_ports_cmd port_down $@
+}
+
+function zone_ports_status() {
+ zone_ports_cmd port_status $@
}
function zone_configs_list() {
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
+ 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_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
+ hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
done
}
zone_configs_cmd down $@
}
+function zone_configs_status() {
+ zone_configs_cmd config_status $@
+}
+
function zone_has_ipv4() {
device_has_ipv4 $@
}
+function zone_has_ipv6() {
+ device_has_ipv6 $@
+}
+
+function zone_db() {
+ local zone=${1}
+ local action=${2}
+ shift 2
+
+ case "${action}" in
+ starting|started|stopping|stopped)
+ db_connection_update ${zone} ${action}
+ ;;
+ esac
+}
+
+function zone_is_up() {
+ local zone=${1}
+
+ device_is_up ${zone}
+}
+
+function zone_is_down() {
+ ! zone_is_up $@
+}
+
+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
+
+ echo "$(zone_dir ${zone})/settings"
+}
+
+function zone_config_read() {
+ local zone=${1}
+
+ assert isset zone
+
+ config_read $(zone_file ${zone})
+}
+
+function zone_config_write() {
+ local zone=${1}
+
+ assert isset zone
+
+ config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
+}
+
+function zone_config_set() {
+ local zone=${1}
+ shift
+ local args="$@"
+
+ assert isset zone
+
+ (
+ zone_config_read ${zone}
+
+ for arg in ${args}; do
+ eval "${arg}"
+ done
+
+ 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}"
+ )
+}