From: Michael Tremer Date: Wed, 6 Jun 2012 16:09:11 +0000 (+0000) Subject: Move network specific command line code to network file. X-Git-Url: http://git.ipfire.org/?p=people%2Fstevee%2Fnetwork.git;a=commitdiff_plain;h=9111eb7203b0b38d35405a68262fad364df40257 Move network specific command line code to network file. --- diff --git a/functions.cli b/functions.cli index 053a55f4..3866495c 100644 --- a/functions.cli +++ b/functions.cli @@ -19,411 +19,6 @@ # # ############################################################################### -function cli_config() { - if cli_help_requested $@; then - cli_show_man network-config - exit ${EXIT_OK} - fi - - if [ -n "${1}" ]; then - config_set $@ - network_config_write - else - network_config_print - fi -} - -function cli_device() { - local device=${1} - local action=${2} - shift 2 - - assert device_exists ${device} - - if zone_exists ${device} || port_exists ${device}; then - error "The device '${device}' has already been configured." - error "You cannot do a device action." - return ${EXIT_ERROR} - fi - - case "${action}" in - discover) - echo "# XXX need to implement --raw here" - cli_device_discover ${device} $@ - ;; - - show|"") - # XXX device_show needs to be implemented - device_show ${device} - ;; - *) - cli_show_man network-device - ;; - esac -} - -function cli_device_discover() { - local device=${1} - shift - - local device_type=$(device_get_type ${device}) - if [ "${device_type}" != "real" ]; then - return ${EXIT_OK} - fi - - local raw - - while [ $# -gt 0 ]; do - case "${1}" in - --raw) - raw=1 - ;; - esac - shift - done - - local up - device_is_up ${device} && up=1 - device_set_up ${device} - - enabled raw || echo "${device}" - - local hook - local out - local ret - for hook in $(hook_zone_get_all); do - out=$(hook_zone_exec ${hook} discover ${device}) - ret=$? - - [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue - - if enabled raw; then - case "${ret}" in - ${DISCOVER_OK}) - echo "${hook}: OK" - local line - while read line; do - echo "${hook}: ${line}" - done <<<"${out}" - ;; - - ${DISCOVER_ERROR}) - echo "${hook}: FAILED" - ;; - esac - else - case "${ret}" in - ${DISCOVER_OK}) - echo " ${hook} was successful." - local line - while read line; do - echo " ${line}" - done <<<"${out}" - ;; - - ${DISCOVER_ERROR}) - echo " ${hook} failed." - ;; - esac - fi - done - - echo # New line - - [ "${up}" = "1" ] || device_set_down ${device} -} - -function cli_hostname() { - if cli_help_requested $@; then - cli_show_man network - exit ${EXIT_OK} - fi - - local hostname=${1} - - if [ -n "${hostname}" ]; then - config_hostname ${hostname} - log INFO "Hostname was set to '${hostname}'." - log INFO "Changes do only take affect after reboot." - exit ${EXIT_OK} - fi - - echo "$(config_hostname)" - exit ${EXIT_OK} -} - -function cli_port() { - if cli_help_requested $@; then - cli_show_man network-port - exit ${EXIT_OK} - fi - - local action - local port - - if port_exists ${1}; then - port=${1} - action=${2} - shift 2 - - # Action aliases - case "${action}" in - start) - action="up" - ;; - stop) - action="down" - ;; - show) - action="status" - ;; - esac - - case "${action}" in - edit|up|down|status) - port_${action} ${port} $@ - ;; - *) - error "Unrecognized argument: ${action}" - exit ${EXIT_ERROR} - ;; - esac - else - action=${1} - shift - - case "${action}" in - create|destroy) - port_${action} $@ - ;; - *) - error "Unrecognized argument: ${action}" - exit ${EXIT_ERROR} - ;; - esac - fi -} - -function cli_zone() { - if cli_help_requested $@; then - cli_show_man network-zone - exit ${EXIT_OK} - fi - - local action - local zone - - if zone_name_is_valid ${1}; then - zone=${1} - action=${2} - shift 2 - - # Action aliases - case "${action}" in - start) - action="up" - ;; - stop) - action="down" - ;; - show) - action="status" - ;; - esac - - case "${action}" in - config|down|edit|port|status|up) - zone_${action} ${zone} $@ - ;; - *) - error "Unrecognized argument: ${action}" - cli_show_man network-zone - exit ${EXIT_ERROR} - ;; - esac - else - action=${1} - shift - - case "${action}" in - create) - zone_${action} $@ - ;; - remove) - cli_zone_remove $@ - ;; - list-hooks) - cli_list_hooks zone $@ - ;; - ""|*) - if [ -n "${action}" ]; then - error "Unrecognized argument: '${action}'" - echo - fi - - cli_show_man network-zone - exit ${EXIT_ERROR} - ;; - esac - fi -} - -function cli_list_hooks() { - local type=${1} - shift - - if cli_help_requested $@; then - cli_show_man network-zone - exit ${EXIT_OK} - fi - - local hook_dir=$(hook_dir ${type}) - local hook - - for hook in ${hook_dir}/*; do - hook=$(basename ${hook}) - if hook_exists ${type} ${hook}; then - echo "${hook}" - fi - done | sort -u -} - -# Removes a zone either immediately, if it is currently down, -# or adds a tag that the removal will be done when the zone -# is brought down the next time. -function cli_zone_remove() { - if cli_help_requested $@; then - cli_show_man network-zone - exit ${EXIT_OK} - fi - - local zone=${1} - assert zone_exists ${zone} - - if zone_is_up ${zone}; then - echo "Zone '${zone}' is up and will be removed when it goes down the next time." - zone_remove ${zone} - else - echo "Removing zone '${zone}' now..." - zone_remove_now ${zone} - fi - - exit ${EXIT_OK} -} - -function cli_start() { - if cli_help_requested $@; then - cli_show_man network - exit ${EXIT_OK} - fi - - local zones=$(zones_get $@) - - local zone - for zone in ${zones}; do - zone_start ${zone} & - done - - wait # until everything is settled -} - -function cli_stop() { - if cli_help_requested $@; then - cli_show_man network - exit ${EXIT_OK} - fi - - local zones=$(zones_get $@) - - local zone - for zone in ${zones}; do - zone_stop ${zone} & - done - - wait # until everything is settled -} - -function cli_restart() { - if cli_help_requested $@; then - cli_show_man network - exit ${EXIT_OK} - fi - - cli_stop $@ - - # Give the system some time to calm down - sleep ${TIMEOUT_RESTART} - - cli_start $@ -} - -function cli_status() { - if cli_help_requested $@; then - cli_show_man network - exit ${EXIT_OK} - fi - - # When dumping status information, the debug - # mode clutters the console which is not what we want. - # Logging on the console is disabled for a short time. - local log_disable_stdout=${LOG_DISABLE_STDOUT} - LOG_DISABLE_STDOUT="true" - - local zones=$(zones_get $@) - - local zone - for zone in ${zones}; do - zone_status ${zone} - done - - # Reset logging. - LOG_DISABLE_STDOUT=${log_disable_stdout} -} - -function cli_reset() { - if cli_help_requested $@; then - cli_show_man network - exit ${EXIT_OK} - fi - - warning_log "Will reset the whole network configuration!!!" - - # Force mode is disabled by default - local force=0 - - while [ $# -gt 0 ]; do - case "${1}" in - --force|-f) - force=1 - ;; - esac - shift - done - - # If we are not running in force mode, we ask the user if he does know - # what he is doing. - if ! enabled force; then - if ! cli_yesno "Do you really want to reset the whole network configuration?"; then - exit ${EXIT_ERROR} - fi - fi - - local zone - for zone in $(zones_get --all); do - zone_remove ${zone} - done - - local port - for port in $(ports_get --all); do - port_remove ${port} - done - - # Re-run the initialization functions - init_run - - exit ${EXIT_OK} -} - function cli_help_requested() { local argument="${1}" diff --git a/network b/network index 16eb0874..d11d2ac7 100755 --- a/network +++ b/network @@ -35,6 +35,411 @@ done . /usr/lib/network/functions +function cli_config() { + if cli_help_requested $@; then + cli_show_man network-config + exit ${EXIT_OK} + fi + + if [ -n "${1}" ]; then + config_set $@ + network_config_write + else + network_config_print + fi +} + +function cli_device() { + local device=${1} + local action=${2} + shift 2 + + assert device_exists ${device} + + if zone_exists ${device} || port_exists ${device}; then + error "The device '${device}' has already been configured." + error "You cannot do a device action." + return ${EXIT_ERROR} + fi + + case "${action}" in + discover) + echo "# XXX need to implement --raw here" + cli_device_discover ${device} $@ + ;; + + show|"") + # XXX device_show needs to be implemented + device_show ${device} + ;; + *) + cli_show_man network-device + ;; + esac +} + +function cli_device_discover() { + local device=${1} + shift + + local device_type=$(device_get_type ${device}) + if [ "${device_type}" != "real" ]; then + return ${EXIT_OK} + fi + + local raw + + while [ $# -gt 0 ]; do + case "${1}" in + --raw) + raw=1 + ;; + esac + shift + done + + local up + device_is_up ${device} && up=1 + device_set_up ${device} + + enabled raw || echo "${device}" + + local hook + local out + local ret + for hook in $(hook_zone_get_all); do + out=$(hook_zone_exec ${hook} discover ${device}) + ret=$? + + [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue + + if enabled raw; then + case "${ret}" in + ${DISCOVER_OK}) + echo "${hook}: OK" + local line + while read line; do + echo "${hook}: ${line}" + done <<<"${out}" + ;; + + ${DISCOVER_ERROR}) + echo "${hook}: FAILED" + ;; + esac + else + case "${ret}" in + ${DISCOVER_OK}) + echo " ${hook} was successful." + local line + while read line; do + echo " ${line}" + done <<<"${out}" + ;; + + ${DISCOVER_ERROR}) + echo " ${hook} failed." + ;; + esac + fi + done + + echo # New line + + [ "${up}" = "1" ] || device_set_down ${device} +} + +function cli_hostname() { + if cli_help_requested $@; then + cli_show_man network + exit ${EXIT_OK} + fi + + local hostname=${1} + + if [ -n "${hostname}" ]; then + config_hostname ${hostname} + log INFO "Hostname was set to '${hostname}'." + log INFO "Changes do only take affect after reboot." + exit ${EXIT_OK} + fi + + echo "$(config_hostname)" + exit ${EXIT_OK} +} + +function cli_port() { + if cli_help_requested $@; then + cli_show_man network-port + exit ${EXIT_OK} + fi + + local action + local port + + if port_exists ${1}; then + port=${1} + action=${2} + shift 2 + + # Action aliases + case "${action}" in + start) + action="up" + ;; + stop) + action="down" + ;; + show) + action="status" + ;; + esac + + case "${action}" in + edit|up|down|status) + port_${action} ${port} $@ + ;; + *) + error "Unrecognized argument: ${action}" + exit ${EXIT_ERROR} + ;; + esac + else + action=${1} + shift + + case "${action}" in + create|destroy) + port_${action} $@ + ;; + *) + error "Unrecognized argument: ${action}" + exit ${EXIT_ERROR} + ;; + esac + fi +} + +function cli_zone() { + if cli_help_requested $@; then + cli_show_man network-zone + exit ${EXIT_OK} + fi + + local action + local zone + + if zone_name_is_valid ${1}; then + zone=${1} + action=${2} + shift 2 + + # Action aliases + case "${action}" in + start) + action="up" + ;; + stop) + action="down" + ;; + show) + action="status" + ;; + esac + + case "${action}" in + config|down|edit|port|status|up) + zone_${action} ${zone} $@ + ;; + *) + error "Unrecognized argument: ${action}" + cli_show_man network-zone + exit ${EXIT_ERROR} + ;; + esac + else + action=${1} + shift + + case "${action}" in + create) + zone_${action} $@ + ;; + remove) + cli_zone_remove $@ + ;; + list-hooks) + cli_list_hooks zone $@ + ;; + ""|*) + if [ -n "${action}" ]; then + error "Unrecognized argument: '${action}'" + echo + fi + + cli_show_man network-zone + exit ${EXIT_ERROR} + ;; + esac + fi +} + +# Removes a zone either immediately, if it is currently down, +# or adds a tag that the removal will be done when the zone +# is brought down the next time. +function cli_zone_remove() { + if cli_help_requested $@; then + cli_show_man network-zone + exit ${EXIT_OK} + fi + + local zone=${1} + assert zone_exists ${zone} + + if zone_is_up ${zone}; then + echo "Zone '${zone}' is up and will be removed when it goes down the next time." + zone_remove ${zone} + else + echo "Removing zone '${zone}' now..." + zone_remove_now ${zone} + fi + + exit ${EXIT_OK} +} + +function cli_list_hooks() { + local type=${1} + shift + + if cli_help_requested $@; then + cli_show_man network-zone + exit ${EXIT_OK} + fi + + local hook_dir=$(hook_dir ${type}) + local hook + + for hook in ${hook_dir}/*; do + hook=$(basename ${hook}) + if hook_exists ${type} ${hook}; then + echo "${hook}" + fi + done | sort -u +} + +function cli_start() { + if cli_help_requested $@; then + cli_show_man network + exit ${EXIT_OK} + fi + + local zones=$(zones_get $@) + + local zone + for zone in ${zones}; do + zone_start ${zone} & + done + + wait # until everything is settled +} + +function cli_stop() { + if cli_help_requested $@; then + cli_show_man network + exit ${EXIT_OK} + fi + + local zones=$(zones_get $@) + + local zone + for zone in ${zones}; do + zone_stop ${zone} & + done + + wait # until everything is settled +} + +function cli_restart() { + if cli_help_requested $@; then + cli_show_man network + exit ${EXIT_OK} + fi + + cli_stop $@ + + # Give the system some time to calm down + sleep ${TIMEOUT_RESTART} + + cli_start $@ +} + +function cli_status() { + if cli_help_requested $@; then + cli_show_man network + exit ${EXIT_OK} + fi + + # When dumping status information, the debug + # mode clutters the console which is not what we want. + # Logging on the console is disabled for a short time. + local log_disable_stdout=${LOG_DISABLE_STDOUT} + LOG_DISABLE_STDOUT="true" + + local zones=$(zones_get $@) + + local zone + for zone in ${zones}; do + zone_status ${zone} + done + + # Reset logging. + LOG_DISABLE_STDOUT=${log_disable_stdout} +} + +function cli_reset() { + if cli_help_requested $@; then + cli_show_man network + exit ${EXIT_OK} + fi + + warning_log "Will reset the whole network configuration!!!" + + # Force mode is disabled by default + local force=0 + + while [ $# -gt 0 ]; do + case "${1}" in + --force|-f) + force=1 + ;; + esac + shift + done + + # If we are not running in force mode, we ask the user if he does know + # what he is doing. + if ! enabled force; then + if ! cli_yesno "Do you really want to reset the whole network configuration?"; then + exit ${EXIT_ERROR} + fi + fi + + local zone + for zone in $(zones_get --all); do + zone_remove ${zone} + done + + local port + for port in $(ports_get --all); do + port_remove ${port} + done + + # Re-run the initialization functions + init_run + + exit ${EXIT_OK} +} + # Process the given action case "${action}" in init)