]> git.ipfire.org Git - people/arne_f/network.git/commitdiff
network: Some work on configuration code.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 17 Jul 2010 13:16:12 +0000 (15:16 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 17 Jul 2010 13:16:12 +0000 (15:16 +0200)
functions.colors
functions.hook
functions.zone
header-config [moved from header-device with 95% similarity]
header-zone
hooks/zones/bridge.configs/ipv4-static

index fa7e77d1f3b28eb25bf9a9464c302f00ff1a67c7..c0de2605d6fa8266428346b5bfca2f199615f1d7 100644 (file)
@@ -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}
index b4ced9248006b107f600c6d63f6bb2864ad7f347..d193266d14857844c01c5bce64e91f17267fc32c 100644 (file)
@@ -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}
 }
index 1fb31d2a9938de3117eb7470d6d4036c5dd04d47..770ce99d5a7a95e3f401ed815332b1601ead7427 100644 (file)
@@ -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}
 
similarity index 95%
rename from header-device
rename to header-config
index 783ed077ed57ad420a2456dda542a2eb33de8556..47e9921785da779c3d7044f327c430027d1c880f 100644 (file)
@@ -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}
 }
index 4d41feb45e7aa5c0a29cacc5334dd9fcaf0f16d4..9407c976f4d03993b5ae5fdf3739f32bc06cfa23 100644 (file)
@@ -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."
index d897660b2cdcdfa5b9ed208537be8f9fd6efc1dd..9ea7288d133dc246097cf0dd18136e358286f159 100755 (executable)
@@ -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}"