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 |
Thomas Vachuska | a7b2404 | 2018-05-15 12:45:47 -0700 | [diff] [blame^] | 19 | [[ $screenversion > "4.05" ]] && screenlog="-Logfile $log" |
Terje Mikal Mjelde | fbaf055 | 2018-03-09 09:14:03 +0100 | [diff] [blame] | 20 | mininet="ssh -t -t $remote screen -L $screenlog -S mininet" |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 21 | |
| 22 | case $cmd in |
| 23 | send) |
| 24 | $mininet -X "stuff \"$@\\n\"" 2>/dev/null |
| 25 | ;; |
| 26 | |
| 27 | sendAndExpect) |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 28 | cmd="" |
| 29 | for a in $*; do shift; if [ "$a" = "--expect" ]; then break; fi; cmd="$cmd $a"; done |
| 30 | $mininet -X "stuff \"$cmd\\n\"" 2>/dev/null |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 31 | onos-mininet expect "$@" |
| 32 | ;; |
| 33 | |
| 34 | wait) |
| 35 | ssh $remote " |
Thomas Vachuska | 2527a0f | 2015-07-14 11:38:21 -0700 | [diff] [blame] | 36 | let count=0 |
| 37 | sleep 1 && while test ! -f $log; do if test \$count -ge $MAX_WAIT; then exit 1; fi; sleep 1; let count=count+1; done |
Ray Milkey | 922db30 | 2018-04-02 08:54:51 -0700 | [diff] [blame] | 38 | while ! (tail -n1 $log | egrep -q '^[ ]*[0]*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] | 39 | sleep ${1-:1} |
| 40 | " |
| 41 | ;; |
| 42 | |
| 43 | expect) |
| 44 | aux=/tmp/mininet.$$.log |
| 45 | ssh $remote " |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 46 | sleep 1 |
| 47 | if [ ! -f $log ]; then exit 1; fi; |
Thomas Vachuska | 2527a0f | 2015-07-14 11:38:21 -0700 | [diff] [blame] | 48 | let count=0 |
Ray Milkey | 922db30 | 2018-04-02 08:54:51 -0700 | [diff] [blame] | 49 | while ! (tail -n1 $log | egrep -q '^[ ]*[0]*mininet>'); do if test \$count -ge $MAX_WAIT; then exit 1; fi; sleep 1; let count=count+1; done |
| 50 | tac $log | awk '{ print \$0; } /^[ ]*[0]*mininet>/ { if (on) { exit 0; } on=1; }' | tac > $aux |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 51 | cat $aux |
| 52 | set -x |
| 53 | egrep \"$@\" $aux |
| 54 | " |
| 55 | ;; |
| 56 | |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 57 | attach) |
| 58 | $mininet -x |
| 59 | ;; |
| 60 | |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 61 | start) |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 62 | ssh $remote "rm -f $log; echo logfile flush 1 > ~/.screenrc" |
| 63 | ( |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 64 | $mininet "$@" |
| 65 | scp $remote:$log /tmp/mininet.log |
| 66 | ssh $remote rm -f $log |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 67 | ) & |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 68 | ;; |
| 69 | |
| 70 | stop) |
| 71 | $mininet -X "stuff \"^C\\n\"" 2>/dev/null && \ |
| 72 | $mininet -X "stuff \"^C\\n\"" 2>/dev/null && \ |
| 73 | $mininet -X "stuff \"exit\\n\"" 2>/dev/null |
Thomas Vachuska | e112535 | 2016-11-09 14:06:51 -0800 | [diff] [blame] | 74 | ssh -t -t $remote sudo mn -c |
| 75 | |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 76 | ;; |
Thomas Vachuska | 4d5310c | 2016-07-14 12:48:39 -0700 | [diff] [blame] | 77 | cleanup) |
| 78 | ssh -t -t $remote sudo mn -c |
Thomas Vachuska | a8e125a | 2016-08-19 10:46:31 -0700 | [diff] [blame] | 79 | 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] | 80 | ;; |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 81 | esac |