Ayaka Koshibe | aec62962 | 2015-01-05 20:33:29 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ----------------------------------------------------------------------------- |
| 3 | # Allows a select group of commands to be sent to all ONOS instances in a cell. |
| 4 | # ----------------------------------------------------------------------------- |
| 5 | |
| 6 | set -o pipefail |
| 7 | IFS=$'\n' |
| 8 | |
| 9 | source ogroup-opts |
| 10 | |
| 11 | function err() { |
| 12 | printf '%s: %s: %s\n' "$(basename $0)" "$1" "$2" >&2 |
| 13 | usage >&2 |
| 14 | exit 1 |
| 15 | } |
| 16 | |
| 17 | function usage() { |
| 18 | cat << EOF |
| 19 | |
| 20 | usage: $(basename $0) <help|[command]> |
| 21 | |
| 22 | Sends a command to all ONOS instances in the current cell. Currently supported |
| 23 | commands are: $GOPTS |
| 24 | |
| 25 | options: |
| 26 | [command] : A command to send to the instances. |
| 27 | help : Displays this message and exits. |
| 28 | |
| 29 | notes: |
| 30 | Hitting <TAB> will display the options for $(basename $0). |
| 31 | |
| 32 | EOF |
| 33 | } |
| 34 | |
| 35 | # gets the utility name |
| 36 | function getcmd() { |
| 37 | # check that utility can be run in "batch-mode" |
| 38 | local isgopt=false |
| 39 | for c in $(printf '%s' "$GOPTS" | tr ' ' $'\n'); do |
| 40 | [ "$c" = "$1" ] && isgopt=true && break |
| 41 | done |
| 42 | if $isgopt ; then |
| 43 | printf 'onos-%s' "$1" |
| 44 | else |
| 45 | err 'unsupported command' "$1" |
| 46 | fi |
| 47 | } |
| 48 | |
| 49 | # early sanity check for instances/arguments |
| 50 | [ -z "$1" ] && usage && exit 0 |
| 51 | |
| 52 | OCIS=( $(env | sed -ne 's:OC[0-9]=\(.*\):\1 :g p' | sort -k1) ) |
| 53 | if [ -z "$OCIS" ]; then |
| 54 | printf "no controller instances, quitting early" >&2 && exit 0 |
| 55 | fi |
| 56 | |
| 57 | CMD_HELP=false |
| 58 | while [ $# -gt 0 ]; do |
| 59 | case "$1" in |
| 60 | 'help') |
| 61 | usage && exit 0 |
| 62 | ;; |
| 63 | '-'?) |
| 64 | err 'invalid flag' "$1" && exit 1 |
| 65 | ;; |
| 66 | *) |
| 67 | cmd=$(getcmd $1) || exit 1 |
| 68 | shift |
| 69 | # grab flags aimed at the utility being called. |
| 70 | argv=( $@ ) |
| 71 | args=() |
| 72 | for i in "${!argv[@]}"; do |
| 73 | # 'help' is a parameter for us; '-h' is for the command |
| 74 | [ "${argv[$i]}" = 'help' ] && break |
| 75 | [ "${argv[$i]}" = '-h' ] && CMD_HELP=true |
| 76 | args[$i]="${argv[$i]}" |
| 77 | shift |
| 78 | done |
| 79 | continue |
| 80 | ;; |
| 81 | esac |
| 82 | shift |
| 83 | done |
| 84 | |
| 85 | ( $CMD_HELP ) && $cmd '-h' && exit 0 |
| 86 | |
| 87 | # TODO: verbose-mode and cleanup |
| 88 | for i in ${OCIS[@]}; do |
| 89 | ${cmd} $(echo ${args[@]}) "$i" 2>/dev/null & |
| 90 | done |