waitexp 2.18 KB
Newer Older
Russ Fish's avatar
Russ Fish committed
1 2 3
#! /bin/sh
#
# Copyright (c) 2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Russ Fish's avatar
Russ Fish committed
23 24 25
#

# waitexp - Wait for an experiment to be in the "active" state.
26 27
#    Required first two args are the project and experiment names.
#    Optional third arg is another state to wait for.
Russ Fish's avatar
Russ Fish committed
28 29 30 31

pid=$1
eid=$2
if [ -z "$pid" -o -z "$eid" ]; then
32
    echo "usage: $0 <pid> <eid> [<state>]"
Russ Fish's avatar
Russ Fish committed
33 34
    exit 1
fi
35 36
goal_state=$3
if [ -z $goal_state ]; then goal_state="active"; fi
Russ Fish's avatar
Russ Fish committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

max=600  # Max number of seconds to wait (10 minutes).
msg=60   # Give a message every so often (seconds)
sec=10   # Check every few seconds.
hdr=true

i=0; while [ $i -lt $(($max/$msg)) ]
do
    i=$(($i + 1))
    j=0; while [ $j -lt $(($msg/$sec)) ]
    do
	j=$(($j + 1))

	sql="select state from experiments where pid='$pid' and eid='$eid'"
	state=`echo "$sql" | mysql tbdb | tail +2`

53 54
	# Done if in the desired state now.
	if [ "$state" = "$goal_state" ]; then
Russ Fish's avatar
Russ Fish committed
55 56 57 58 59 60 61 62
	    echo ""
	    echo "$pid/$eid is $state."
	    echo ""
	    exit 0

	# Starting to wait.
	elif [ $hdr ]; then
	    echo ""
63
	    echo "*** $pid/$eid is $state.  Waiting for it to become $goal_state."
Russ Fish's avatar
Russ Fish committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	    hdr=

	# Extra message if inactive but the state changes.
	elif [ "$ostate" != "$state" ]; then
	    echo "$pid/$eid $state"
	fi
	ostate=$state

	# Dots for progress between messages.
	echo -n .
	sleep $sec
    done

    # Show the inactive state.
    echo "$pid/$eid $state"
done

# Failed!
82
echo "*** Giving up waiting for $pid/$eid to become $goal_state. ***"
Russ Fish's avatar
Russ Fish committed
83 84
echo ""
exit 1