Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ----------------------------------------------------------------------------- |
| 3 | # Starts or interacts with mininet in a remote screen session. |
| 4 | # ----------------------------------------------------------------------------- |
| 5 | |
| 6 | [ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1 |
| 7 | . $ONOS_ROOT/tools/build/envDefaults |
| 8 | |
Thomas Vachuska | 534f476 | 2016-08-30 16:25:13 -0700 | [diff] [blame] | 9 | export MAX_WAIT=${ONOS_MN_TIMEOUT:-90} |
Thomas Vachuska | 2527a0f | 2015-07-14 11:38:21 -0700 | [diff] [blame] | 10 | |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 11 | cmd="$1" && shift |
| 12 | log="screenlog.0" |
| 13 | remote="$ONOS_USER@$OCN" |
Terje Mikal Mjelde | fbaf055 | 2018-03-09 09:14:03 +0100 | [diff] [blame^] | 14 | screenversion=$(ssh $remote screen -v | sed -n -e 's/^Screen version \([0-9]*\.[0-9]*\).*$/\1/p') |
| 15 | # note: Screen version check for >4.04 may be inaccurate - The only sure thing is that v4.01 |
| 16 | # requires logname to NOT be given while in v4.05 it is optional (as long as -L is the |
| 17 | # last parameter on the command line). |
| 18 | [[ $screenversion > "4.04" ]] && screenlog=$log |
| 19 | mininet="ssh -t -t $remote screen -L $screenlog -S mininet" |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 20 | |
| 21 | case $cmd in |
| 22 | send) |
| 23 | $mininet -X "stuff \"$@\\n\"" 2>/dev/null |
| 24 | ;; |
| 25 | |
| 26 | sendAndExpect) |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 27 | cmd="" |
| 28 | for a in $*; do shift; if [ "$a" = "--expect" ]; then break; fi; cmd="$cmd $a"; done |
| 29 | $mininet -X "stuff \"$cmd\\n\"" 2>/dev/null |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 30 | onos-mininet expect "$@" |
| 31 | ;; |
| 32 | |
| 33 | wait) |
| 34 | ssh $remote " |
Thomas Vachuska | 2527a0f | 2015-07-14 11:38:21 -0700 | [diff] [blame] | 35 | let count=0 |
| 36 | sleep 1 && while test ! -f $log; do if test \$count -ge $MAX_WAIT; then exit 1; fi; sleep 1; let count=count+1; done |
| 37 | while ! (tail -n1 $log | egrep -q '^mininet>'); do if [ \$count -ge $MAX_WAIT ]; then exit 1; fi; sleep 1; done |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 38 | sleep ${1-:1} |
| 39 | " |
| 40 | ;; |
| 41 | |
| 42 | expect) |
| 43 | aux=/tmp/mininet.$$.log |
| 44 | ssh $remote " |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 45 | sleep 1 |
| 46 | if [ ! -f $log ]; then exit 1; fi; |
Thomas Vachuska | 2527a0f | 2015-07-14 11:38:21 -0700 | [diff] [blame] | 47 | let count=0 |
| 48 | while ! (tail -n1 $log | egrep -q '^mininet>'); do if test \$count -ge $MAX_WAIT; then exit 1; fi; sleep 1; let count=count+1; done |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 49 | tac $log | awk '{ print \$0; } /^mininet>/ { if (on) { exit 0; } on=1; }' | tac > $aux |
| 50 | cat $aux |
| 51 | set -x |
| 52 | egrep \"$@\" $aux |
| 53 | " |
| 54 | ;; |
| 55 | |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 56 | attach) |
| 57 | $mininet -x |
| 58 | ;; |
| 59 | |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 60 | start) |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 61 | ssh $remote "rm -f $log; echo logfile flush 1 > ~/.screenrc" |
| 62 | ( |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 63 | $mininet "$@" |
| 64 | scp $remote:$log /tmp/mininet.log |
| 65 | ssh $remote rm -f $log |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 66 | ) & |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 67 | ;; |
| 68 | |
| 69 | stop) |
| 70 | $mininet -X "stuff \"^C\\n\"" 2>/dev/null && \ |
| 71 | $mininet -X "stuff \"^C\\n\"" 2>/dev/null && \ |
| 72 | $mininet -X "stuff \"exit\\n\"" 2>/dev/null |
Thomas Vachuska | e112535 | 2016-11-09 14:06:51 -0800 | [diff] [blame] | 73 | ssh -t -t $remote sudo mn -c |
| 74 | |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 75 | ;; |
Thomas Vachuska | 4d5310c | 2016-07-14 12:48:39 -0700 | [diff] [blame] | 76 | cleanup) |
| 77 | ssh -t -t $remote sudo mn -c |
Thomas Vachuska | a8e125a | 2016-08-19 10:46:31 -0700 | [diff] [blame] | 78 | ssh -t -t $remote "screen -list | grep mininet | cut -d. -f1 | tr -d '\t' | xargs kill -9; screen -wipe; exit 0" |
Thomas Vachuska | 4d5310c | 2016-07-14 12:48:39 -0700 | [diff] [blame] | 79 | ;; |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 80 | esac |