blob: 0e6b39683a1c814bc80b90a259b20b363d7c4a35 [file] [log] [blame]
Ayaka Koshibeaec629622015-01-05 20:33:29 -08001#!/bin/bash
2# -----------------------------------------------------------------------------
3# Allows a select group of commands to be sent to all ONOS instances in a cell.
4# -----------------------------------------------------------------------------
5
6set -o pipefail
7IFS=$'\n'
8
9source ogroup-opts
10
11function err() {
12 printf '%s: %s: %s\n' "$(basename $0)" "$1" "$2" >&2
13 usage >&2
14 exit 1
15}
16
17function usage() {
18cat << EOF
19
20usage: $(basename $0) <help|[command]>
21
22Sends a command to all ONOS instances in the current cell. Currently supported
23commands are: $GOPTS
24
25options:
26 [command] : A command to send to the instances.
27 help : Displays this message and exits.
28
29notes:
30 Hitting <TAB> will display the options for $(basename $0).
31
32EOF
33}
34
35# gets the utility name
36function 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
52OCIS=( $(env | sed -ne 's:OC[0-9]=\(.*\):\1 :g p' | sort -k1) )
53if [ -z "$OCIS" ]; then
54 printf "no controller instances, quitting early" >&2 && exit 0
55fi
56
57CMD_HELP=false
58while [ $# -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
83done
84
85( $CMD_HELP ) && $cmd '-h' && exit 0
86
87# TODO: verbose-mode and cleanup
88for i in ${OCIS[@]}; do
89 ${cmd} $(echo ${args[@]}) "$i" 2>/dev/null &
90done