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" |
DongRyeol Cha | 5c0a9f0 | 2018-05-17 14:32:55 +0900 | [diff] [blame^] | 14 | remote_scp="$ONOS_USER@[$OCN]" |
Terje Mikal Mjelde | fbaf055 | 2018-03-09 09:14:03 +0100 | [diff] [blame] | 15 | screenversion=$(ssh $remote screen -v | sed -n -e 's/^Screen version \([0-9]*\.[0-9]*\).*$/\1/p') |
| 16 | # note: Screen version check for >4.04 may be inaccurate - The only sure thing is that v4.01 |
| 17 | # requires logname to NOT be given while in v4.05 it is optional (as long as -L is the |
| 18 | # last parameter on the command line). |
| 19 | [[ $screenversion > "4.04" ]] && screenlog=$log |
Thomas Vachuska | a7b2404 | 2018-05-15 12:45:47 -0700 | [diff] [blame] | 20 | [[ $screenversion > "4.05" ]] && screenlog="-Logfile $log" |
Terje Mikal Mjelde | fbaf055 | 2018-03-09 09:14:03 +0100 | [diff] [blame] | 21 | mininet="ssh -t -t $remote screen -L $screenlog -S mininet" |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 22 | |
| 23 | case $cmd in |
| 24 | send) |
| 25 | $mininet -X "stuff \"$@\\n\"" 2>/dev/null |
| 26 | ;; |
| 27 | |
| 28 | sendAndExpect) |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 29 | cmd="" |
| 30 | for a in $*; do shift; if [ "$a" = "--expect" ]; then break; fi; cmd="$cmd $a"; done |
| 31 | $mininet -X "stuff \"$cmd\\n\"" 2>/dev/null |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 32 | onos-mininet expect "$@" |
| 33 | ;; |
| 34 | |
| 35 | wait) |
| 36 | ssh $remote " |
Thomas Vachuska | 2527a0f | 2015-07-14 11:38:21 -0700 | [diff] [blame] | 37 | let count=0 |
| 38 | 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] | 39 | 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] | 40 | sleep ${1-:1} |
| 41 | " |
| 42 | ;; |
| 43 | |
| 44 | expect) |
| 45 | aux=/tmp/mininet.$$.log |
| 46 | ssh $remote " |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 47 | sleep 1 |
| 48 | if [ ! -f $log ]; then exit 1; fi; |
Thomas Vachuska | 2527a0f | 2015-07-14 11:38:21 -0700 | [diff] [blame] | 49 | let count=0 |
Ray Milkey | 922db30 | 2018-04-02 08:54:51 -0700 | [diff] [blame] | 50 | 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 |
| 51 | 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] | 52 | cat $aux |
| 53 | set -x |
| 54 | egrep \"$@\" $aux |
| 55 | " |
| 56 | ;; |
| 57 | |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 58 | attach) |
| 59 | $mininet -x |
| 60 | ;; |
| 61 | |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 62 | start) |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 63 | ssh $remote "rm -f $log; echo logfile flush 1 > ~/.screenrc" |
| 64 | ( |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 65 | $mininet "$@" |
DongRyeol Cha | 5c0a9f0 | 2018-05-17 14:32:55 +0900 | [diff] [blame^] | 66 | scp $remote_scp:$log /tmp/mininet.log |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 67 | ssh $remote rm -f $log |
Thomas Vachuska | e76f653 | 2015-07-08 09:40:53 -0700 | [diff] [blame] | 68 | ) & |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 69 | ;; |
| 70 | |
| 71 | stop) |
| 72 | $mininet -X "stuff \"^C\\n\"" 2>/dev/null && \ |
| 73 | $mininet -X "stuff \"^C\\n\"" 2>/dev/null && \ |
| 74 | $mininet -X "stuff \"exit\\n\"" 2>/dev/null |
Thomas Vachuska | e112535 | 2016-11-09 14:06:51 -0800 | [diff] [blame] | 75 | ssh -t -t $remote sudo mn -c |
| 76 | |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 77 | ;; |
Thomas Vachuska | 4d5310c | 2016-07-14 12:48:39 -0700 | [diff] [blame] | 78 | cleanup) |
| 79 | ssh -t -t $remote sudo mn -c |
Thomas Vachuska | a8e125a | 2016-08-19 10:46:31 -0700 | [diff] [blame] | 80 | 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] | 81 | ;; |
Thomas Vachuska | 0a4c270 | 2015-07-06 08:43:41 -0700 | [diff] [blame] | 82 | esac |