]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.cli
Remove a lot of 'devicify' calls to increase speed of code.
[people/ms/network.git] / functions.cli
index 21bb3c5229e9c09a9a07bac535652fd2def6d18a..ab2c091c3c47f918ff373dc844eb0d73a3d31249 100644 (file)
@@ -33,49 +33,34 @@ function cli_config() {
 }
 
 function cli_device() {
-       local action=${1}
-       shift
+       local device=${1}
+       local action=${2}
+       shift 2
 
-       local device
-       local devices=$@
+       assert device_exists ${device}
 
-       if [ -z "${devices}" ]; then
-               devices=$(devices_get_all)
+       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"
-                       for device in ${devices}; do
-                               cli_device_discover ${device} $@
-                       done
+                       cli_device_discover ${device} $@
                        ;;
 
                show|"")
-                       for device in ${devices}; do
-                               cli_device_print ${device}
-                       done
+                       # XXX device_show needs to be implemented
+                       device_show ${device}
                        ;;
                *)
                        cli_usage device
-                       ;;                              
+                       ;;
        esac
 }
 
-function cli_device_print() {
-       local device=${1}
-
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
-
-       echo "${device}"
-       echo "  Type: $(device_get_type ${device})"
-       echo "  Addr: $(device_get_address ${device})"
-       echo
-}
-
 function cli_device_discover() {
        local device=${1}
        shift
@@ -105,8 +90,8 @@ function cli_device_discover() {
        local hook
        local out
        local ret
-       for hook in $(hooks_get_all); do
-               out=$(hook_exec ${hook} discover ${device})
+       for hook in $(hook_zone_get_all); do
+               out=$(hook_zone_exec ${hook} discover ${device})
                ret=$?
 
                [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
@@ -147,7 +132,104 @@ function cli_device_discover() {
        [ "${up}" = "1" ] || device_set_down ${device}
 }
 
+function cli_hostname() {
+       if cli_help_requested $@; then
+               cli_usage hostname
+               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_hotplug() {
+       if cli_help_requested $@; then
+               cli_usage root-hotplug
+               exit ${EXIT_OK}
+       fi
+
+       local command=${1}
+       shift
+
+       case "${command}" in
+               device)
+                       device_hotplug $@
+                       exit $?
+                       ;;
+               *)
+                       cli_usage root-hotplug
+                       exit ${EXIT_OK}
+                       ;;
+       esac
+}
+
+function cli_port() {
+       if cli_help_requested $@; then
+               cli_usage root-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_usage root-zone
+               exit ${EXIT_OK}
+       fi
+
        local action
        local zone
 
@@ -156,10 +238,28 @@ function cli_zone() {
                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|show|status|up)
+                       config|down|edit|port|status|up)
                                zone_${action} ${zone} $@
                                ;;
+                       *)
+                               error "Unrecognized argument: ${action}"
+                               cli_usage root-zone-subcommands
+                               exit ${EXIT_ERROR}
+                               ;;
                esac
        else
                action=${1}
@@ -169,8 +269,14 @@ function cli_zone() {
                        create|remove)
                                zone_${action} $@
                                ;;
-                       *)
-                               error "Unrecognized argument: '${action}'"
+                       ""|*)
+                               if [ -n "${action}" ]; then
+                                       error "Unrecognized argument: '${action}'"
+                                       echo
+                               fi
+
+                               cli_usage root-zone
+                               exit ${EXIT_ERROR}
                                ;;
                esac
        fi
@@ -186,8 +292,10 @@ function cli_start() {
 
        local zone
        for zone in ${zones}; do
-               zone_up ${zone}
+               zone_start ${zone} &
        done
+
+       wait # until everything is settled
 }
 
 function cli_stop() {
@@ -200,8 +308,10 @@ function cli_stop() {
 
        local zone
        for zone in ${zones}; do
-               zone_down ${zone}
+               zone_stop ${zone} &
        done
+
+       wait # until everything is settled
 }
 
 function cli_restart() {
@@ -218,13 +328,72 @@ function cli_restart() {
        cli_start $@
 }
 
+function cli_status() {
+       if cli_help_requested $@; then
+               cli_usage root-status
+               exit ${EXIT_OK}
+       fi
+
+       local zones=$(zones_get $@)
+
+       local zone
+       for zone in ${zones}; do
+               zone_status ${zone}
+       done
+}
+
+function cli_reset() {
+       if cli_help_requested $@; then
+               cli_usage root-reset
+               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
-       for argument in $@; do
-               if [ "${argument}" = "help" -o "${argument}" = "-h" -o "${argument}" = "--help" ]; then
+       local argument="${1}"
+
+       if [ -n "${argument}" ]; then
+               if listmatch ${argument} help -h --help; then
                        return ${EXIT_OK}
                fi
-       done
+       fi
 
        return ${EXIT_ERROR}
 }
@@ -239,11 +408,11 @@ function cli_usage() {
                        echo "  start  - ..."
                        echo "  stop   - ..."
                        echo "  restart - ..."
+                       echo "  status - ..."
                        echo
                        echo "  config - ..."
                        echo
                        echo "  device - ..."
-                       echo "  show   - ???"
                        echo "  zone   - ..."
                        echo
                        ;;
@@ -259,6 +428,16 @@ function cli_usage() {
                        echo    "    ${0} ${what#root-} DEBUG=1 ..."
                        echo
                        ;;
+               root-reset)
+                       echo    "${0}: ${what#root-} [--force | -f]"
+                       echo
+                       echo    "  This command resets the network configuration."
+                       echo
+                       echo    "  Will delete all zones and ports."
+                       echo
+                       echo -e "  ${COLOUR_RED}USE WITH CAUTION!${COLOUR_NORMAL}"
+                       echo
+                       ;;
                root-start|root-stop|root-restart)
                        echo    "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
                        echo
@@ -266,19 +445,57 @@ function cli_usage() {
                        echo    "  One can pass several parameters to only process a subset of all"
                        echo    "  available zones:"
                        echo
-                       echo -e "    ${BOLD}--local-only${NORMAL}"
+                       echo -e "    ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
+                       echo    "    Process all local zones which includes every zone without red."
+                       echo
+                       echo -e "    ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
+                       echo    "    Process all remote zones which means only the red ones."
+                       echo
+                       echo -e "    ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
+                       echo    "    Process all zones. This is the default parameter."
+                       echo
+                       echo    "    Additionally, you can pass one or more zone names which will"
+                       echo    "    be processed."
+                       echo
+                       ;;
+               root-status)
+                       echo    "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
+                       echo
+                       echo    "  This commands shows status information of all zones by default."
+                       echo    "  One can pass several parameters to only process a subset of all"
+                       echo    "  available zones:"
+                       echo
+                       echo -e "    ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
                        echo    "    Process all local zones which includes every zone without red."
                        echo
-                       echo -e "    ${BOLD}--remote-only${NORMAL}"
+                       echo -e "    ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
                        echo    "    Process all remote zones which means only the red ones."
                        echo
-                       echo -e "    ${BOLD}--all${NORMAL}"
+                       echo -e "    ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
                        echo    "    Process all zones. This is the default parameter."
                        echo
                        echo    "    Additionally, you can pass one or more zone names which will"
                        echo    "    be processed."
                        echo
                        ;;
+               root-zone)
+                       echo    "${0}: ${what#root-} <create|remove> <zone> [<type> <options...>]"
+                       echo
+                       echo    "  Create or remove a zone."
+                       echo
+                       echo -e "    ${COLOUR_BOLD}create <zone> <type> <options>${COLOUR_NORMAL}"
+                       echo    "    Create a new zone of type <type> where <zone> is an allowed"
+                       echo    "    zone name."
+                       echo
+                       echo -e "    ${COLOUR_BOLD}remove <zone>${COLOUR_NORMAL}"
+                       echo    "    Remove the zone <zone>."
+                       echo
+                       echo    "  You may also edit the configuration of the zones."
+                       echo
+                       echo -e "    ${COLOUR_BOLD}<zone> ...${COLOUR_NORMAL}"
+                       echo    "    Edit the zone <zone>."
+                       echo
+                       ;;
                usage)
                        echo
                        echo "  Run '${0} help' to get information how to use this tool."
@@ -292,3 +509,41 @@ function cli_usage() {
 
        echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
 }
+
+function cli_status_headline() {
+       local zone=${1}
+
+       local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
+       zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
+
+       echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
+}
+
+function cli_headline() {
+       echo
+       echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
+}
+
+function cli_yesno() {
+       local message="$@ [y/N] "
+       local yesno
+
+       echo
+       echo -ne "${message}"
+       read yesno
+
+       if listmatch ${yesno} y Y j J yes YES Yes; then
+               return ${EXIT_OK}
+       fi
+
+       return ${EXIT_ERROR}
+}
+
+function cli_get_key() {
+       local key="${1%%=*}"
+       echo "${key/--/}"
+}
+
+function cli_get_val() {
+       echo "${@##*=}"
+}