From a5ebb1698882c160214013617e5285ca5904300c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 17 Jul 2010 15:16:12 +0200 Subject: [PATCH] network: Some work on configuration code. --- functions.colors | 3 ++ functions.hook | 38 ++++++++++++++- functions.zone | 66 +++++++++++++++++++++++--- header-device => header-config | 4 +- header-zone | 49 ++++++++++++------- hooks/zones/bridge.configs/ipv4-static | 17 +++---- 6 files changed, 142 insertions(+), 35 deletions(-) rename header-device => header-config (95%) diff --git a/functions.colors b/functions.colors index fa7e77d1..c0de2605 100644 --- a/functions.colors +++ b/functions.colors @@ -31,6 +31,9 @@ COLOUR_OK=${COLOUR_GREEN} COLOUR_UP=${COLOUR_GREEN} COLOUR_WARN=${COLOUR_YELLOW} +COLOUR_ENABLED=${COLOUR_GREEN} +COLOUR_DISABLED=${COLOUR_RED} + COLOUR_STP_FORWARDING=${COLOUR_GREEN} COLOUR_STP_DISCARDING=${COLOUR_RED} COLOUR_STP_LEARNING=${COLOUR_YELLOW} diff --git a/functions.hook b/functions.hook index b4ced924..d193266d 100644 --- a/functions.hook +++ b/functions.hook @@ -63,6 +63,7 @@ function config_get_hook() { local config=${1} assert isset config + assert [ -e "${config}" ] ( . ${config} @@ -137,6 +138,9 @@ function hook_zone_config_exec() { local hook_config=${2} shift 2 + assert isset hook_zone + assert isset hook_config + if ! hook_zone_exists ${hook_zone}; then error "Hook '${hook_zone}' does not exist." return ${EXIT_ERROR} @@ -168,10 +172,40 @@ function hook_zone_ports_get_all() { return ${EXIT_ERROR} fi + # If the zone hook has got no ports we exit silently + if ! hook_zone_has_ports ${hook}; then + return ${EXIT_OK} + fi + local h for h in $(hook_dir zone)/${hook}.ports/*; do h=$(basename ${h}) - ## XXX executeable? - echo "${h}" + if hook_zone_port_exists ${hook} ${h}; then + echo "${h}" + fi + done +} + +function hook_zone_configs_get_all() { + local hook=${1} + + if ! hook_exists zone ${hook}; then + error "Hook '${hook}' does not exist." + return ${EXIT_ERROR} + fi + + # If the zone hook has got no configurations we exit silently + if ! hook_zone_has_configs ${hook}; then + return ${EXIT_OK} + fi + + local h + for h in $(hook_dir zone)/${hook}.configs/*; do + h=$(basename ${h}) + if hook_zone_config_exists ${hook} ${h}; then + echo "${h}" + fi done + + return ${EXIT_OK} } diff --git a/functions.zone b/functions.zone index 1fb31d2a..770ce99d 100644 --- a/functions.zone +++ b/functions.zone @@ -98,8 +98,8 @@ 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=$? @@ -321,6 +321,7 @@ function zone_get_ports() { done } +# XXX overwritten some lines below function zone_config() { local zone=${1} shift @@ -345,6 +346,47 @@ function zone_config() { 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() { local zone=${1} @@ -437,7 +479,7 @@ function zone_ports_list() { 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}) @@ -479,7 +521,7 @@ 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}) @@ -496,7 +538,7 @@ 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 @@ -510,6 +552,10 @@ function zone_configs_down() { zone_configs_cmd down $@ } +function zone_configs_status() { + zone_configs_cmd config_status $@ +} + function zone_has_ipv4() { device_has_ipv4 $@ } @@ -540,7 +586,7 @@ function zone_is_down() { ! zone_is_up $@ } -function zone_get_supported_hooks() { +function zone_get_supported_port_hooks() { local zone=${1} local hook=$(zone_get_hook ${zone}) @@ -548,6 +594,14 @@ function zone_get_supported_hooks() { 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} diff --git a/header-device b/header-config similarity index 95% rename from header-device rename to header-config index 783ed077..47e99217 100644 --- a/header-device +++ b/header-config @@ -39,11 +39,11 @@ done function run() { case "${action}" in - add|create|rem|up|down|status) + edit|create|rem|up|down|status) _${action} $@ ;; esac - error "Device hook '${HOOK}' didn't exit properly." + error "Config hook '${HOOK}' didn't exit properly." exit ${EXIT_ERROR} } diff --git a/header-zone b/header-zone index 4d41feb4..9407c976 100644 --- a/header-zone +++ b/header-zone @@ -162,7 +162,7 @@ function _port_cmd() { assert isset hook_zone assert isset hook_port - if ! listmatch ${hook_port} $(zone_get_supported_hooks ${zone}); then + if ! listmatch ${hook_port} $(zone_get_supported_port_hooks ${zone}); then error_log "Zone '${zone}' does not support port of type '${hook_port}'." exit ${EXIT_ERROR} fi @@ -224,7 +224,25 @@ function __configcmd() { } function _config_create() { - __configcmd create $@ + local zone=${1} + local hook_config=${2} + shift 2 + + assert isset zone + assert isset hook_config + assert zone_exists ${zone} + + if ! listmatch ${hook_config} $(zone_get_supported_config_hooks ${zone}); then + error_log "Zone '${zone}' does not support configuration of type '${hook_config}'." + exit ${EXIT_ERROR} + fi + + local hook_zone=$(zone_get_hook ${zone}) + assert isset hook_zone + + hook_zone_config_exec ${hook_zone} ${hook_config} create ${zone} $@ + + exit $? } function _config_edit() { @@ -286,32 +304,29 @@ function run() { #action=${action//-/_} case "${action}" in - create|discover|down|edit|info|rem|status|up|port_add|port_rem|port_up|port_down|port_status) + # Main functions + create|discover|down|edit|info|rem|status|up) _${action} $@ ;; - port) - if ! hook_zone_has_ports ${HOOK}; then - error "Hook '${HOOK}' does not support ports." - exit ${EXIT_ERROR} - fi - - _port $@ + # Port callbacks + port_add|port_rem|port_up|port_down|port_status) + _${action} $@ ;; - config) - if ! hook_zone_has_configs ${HOOK}; then - error "Hook '${HOOK}' does not support configurations." - exit ${EXIT_ERROR} - fi - - _config $@ + # Configuration callbacks + config_create) + _${action} $@ ;; + # ppp daemon callbacks ppp-ip-pre-up|ppp-ip-up|ppp-ip-down) _${action} $@ ;; + *) + error "Unknown action: ${action}" + ;; esac error "Hook did not exit properly." diff --git a/hooks/zones/bridge.configs/ipv4-static b/hooks/zones/bridge.configs/ipv4-static index d897660b..9ea7288d 100755 --- a/hooks/zones/bridge.configs/ipv4-static +++ b/hooks/zones/bridge.configs/ipv4-static @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/header-port +. /lib/network/header-config HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY" @@ -59,7 +59,8 @@ function _create() { PREFIX=$(ipv4_mask_to_cidr ${NETMASK}) fi - config_write $(zone_dir ${zone})/config.${HOOK}.$(uuid) ${HOOK_SETTINGS} + # XXX maybe we can add some hashing to identify a configuration again + config_write $(zone_dir ${zone})/configs/${HOOK}.$(uuid) ${HOOK_SETTINGS} exit ${EXIT_OK} } @@ -73,8 +74,8 @@ function _up() { error "Zone '${zone}' doesn't exist." exit ${EXIT_ERROR} fi - - config_read $(zone_dir ${zone})/${config} + + config_read $(zone_dir ${zone})/configs/${config} if ! zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then ip addr add ${ADDRESS}/${PREFIX} dev ${zone} @@ -99,7 +100,7 @@ function _down() { exit ${EXIT_ERROR} fi - config_read $(zone_dir ${zone})/${config} + config_read $(zone_dir ${zone})/configs/${config} if zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then ip addr del ${ADDRESS}/${PREFIX} dev ${zone} @@ -118,13 +119,13 @@ function _status() { exit ${EXIT_ERROR} fi - config_read $(zone_dir ${zone})/${config} + config_read $(zone_dir ${zone})/configs/${config} printf " %10s - " "${HOOK}" if zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then - echo -ne "${COLOUR_OK} OK ${COLOUR_NORMAL}" + echo -ne "${COLOUR_ENABLED}ENABLED ${COLOUR_NORMAL}" else - echo -ne "${COLOUR_ERROR}ERROR${COLOUR_NORMAL}" + echo -ne "${COLOUR_DISABLED}DISABLED${COLOUR_NORMAL}" fi echo " - ${ADDRESS}/${PREFIX}" -- 2.39.2