common-env.sh 6.91 KB
Newer Older
1
#!/bin/sh
Mike Hibler's avatar
Mike Hibler committed
2 3 4 5 6 7
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#

8 9 10 11 12
#
# Simple shell script to get some environment variables useful for pelab
# shell scripts
#

13 14 15 16 17 18
#
# 'Header guard'
#
if [ "$COMMON_ENV_LOADED" != "yes" ]; then
COMMON_ENV_LOADED="yes"

19 20 21 22 23
#
# Find out the OS we're running on
#
UNAME=`uname -s`

24 25 26 27 28 29 30 31 32 33 34
#
# Yow.  The BSD shell requires setting trapsasync (-T) to allow us to
# receive signals when in a wait.
#
if [ "$UNAME" = "FreeBSD" ]; then
    set -o trapsasync
    SH="/bin/sh -T"
else
    SH=/bin/sh
fi

35 36 37
#
# Different binary directories for FreeBSD/Linux
#
38
if [ "$UNAME" = "Linux" ]; then
39
    BIN_PATH="/usr/bin"
40
elif [ "$UNAME" = "FreeBSD" ]; then
41 42 43 44 45 46
    BIN_PATH="/usr/local/bin"
else
    echo "Unable to determine operating system"
    exit -1
fi

47 48 49 50 51 52 53 54 55
#
# Locations of some Emulab-specific files
#
EMUVAR="/var/emulab"
EMUBOOT="$EMUVAR/boot"
NICKFILE="$EMUBOOT/nickname"
RCPLAB="$EMUBOOT/rc.plab"
HOSTSFILE="/etc/hosts"
IFCONFIG="/sbin/ifconfig"
56
PERL="/usr/bin/perl"
57 58
PYTHON="${BIN_PATH}/python"
SUDO="${BIN_PATH}/sudo"
59 60
MKDIR="/bin/mkdir"
CHMOD="/bin/chmod"
61
SU="/bin/su"
62

63
SYNC="/usr/local/etc/emulab/emulab-sync"
64 65
SYNCTIMO=120

66
if [ "$UNAME" = "Linux" ]; then
67
    GREP="/bin/grep"
68 69
elif [ "$UNAME" = "FreeBSD" ]; then
    GREP="/usr/bin/grep"
70 71 72 73
else
    echo "Unable to determine operating system"
    exit -1
fi
74 75 76 77 78

#
# Some 'constants' by convention. 
#
export PELAB_LAN=elabc
79
export EPLAB_LAN=plabc
80

81 82 83 84 85
#
# Name of the barrier for indicating all stubs are ready
#
export STUB_BARRIER=stubsready

86 87 88
#
# Node/experiment info
#
89
export NICKNAME=`cat $NICKFILE`
90
export HOSTNAME=`echo $NICKNAME | cut -d. -f1`;
91 92 93
export EXPERIMENT=`echo $NICKNAME | cut -d. -f2`;
export PROJECT=`echo $NICKNAME | cut -d. -f3`;

94 95 96 97 98 99 100

#
# Important directories
#
SCRIPT_LOCATION=`dirname $0`
export BASE="${SCRIPT_LOCATION}/../";
export STUB_DIR="${BASE}/stub/";
101
export MAGENT_DIR="${BASE}/magent/";
102 103
export NETMON_DIR="${BASE}/libnetmon/";
export MONITOR_DIR="${BASE}/monitor/";
104
export DBMONITOR_DIR="${BASE}/dbmonitor/";
105
export IPERFD_DIR="${BASE}/iperfd/";
106
export TMPDIR="/var/tmp/";
107 108 109
export LOGDIR="/local/logs/"

#
Mike Hibler's avatar
Mike Hibler committed
110 111
# Temproary files we use.
# We put them in /local/logs so they become part of the fossil record.
112
#
Mike Hibler's avatar
Mike Hibler committed
113 114
export IPMAP="/local/logs/ip-mapping.txt"
export INITCOND="/local/logs/initial-conditions.txt"
115 116

#
Mike Hibler's avatar
Mike Hibler committed
117
# Important scripts/libraries
118 119 120
#
export NETMOND="netmond"
export STUBD="stubd"
121
export MAGENT="magent"
122
export MONITOR="monitor.py"
123
export DBMONITOR="dbmonitor.pl"
124
export IPERFD="iperfd"
125
export GENIPMAP="gen-ip-mapping.pl"
Mike Hibler's avatar
Mike Hibler committed
126
export GENINITCOND="init-elabnodes.pl"
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
export NETMON_LIB="libnetmon.so"

#
# Where are we running?
#
if [ -e "$RCPLAB" ]; then
    export RUNNING_ON="plab"
    export ON_PLAB="yes"
    export ON_ELAB=""
else
    export RUNNING_ON="elab"
    export ON_PLAB=""
    export ON_ELAB="yes"
fi

142 143 144 145 146 147 148 149 150 151
#
# Find out if we're in a vserver - can we see the init process?
#
ps --pid 1 > /dev/null
if [ $? == 1 ]; then
    export IN_VSERVER="yes"
else
    export IN_VSERVER=""
fi

Mike Hibler's avatar
Mike Hibler committed
152 153 154 155 156 157 158 159 160 161
#
# Are we the "master" (sync server) node?
#

if `$SYNC -m`; then
    export ON_MASTER="yes"
else
    export ON_MASTER=""
fi

162 163 164 165 166 167 168 169 170 171
#
# Make a handy variable for running as root (ie. invoke sudo if necessary)
#
if [ "$EUID" != "0" ]; then
    export AS_ROOT="$SUDO"
else
    export AS_ROOT=""
fi

#
172 173 174 175
# How big is this experiment? Counts the number of planetlab nodes. IF there
# are real planetlab nodes in this experiment, count them instead of the number
# of fake planetlab nodes - this allows us to remove planetlab nodes that fail
# from the experiment, and *maybe* everything else will just work.
176
#
177
export PEER_PAIRS=`$GREP -q planet /etc/hosts && $GREP -E -c 'planet-.*-realinternet ' /etc/hosts || $GREP -E -c 'plab-.*-plabc ' /etc/hosts`
178 179 180 181 182 183 184 185 186 187
export PEERS=`expr $PEER_PAIRS \* 2`

#
# Make the logdir if it doesn't exist
#
if [ ! -d $LOGDIR ]; then
    $AS_ROOT $MKDIR -p $LOGDIR
    $AS_ROOT $CHMOD 777 $LOGDIR
fi

188 189 190 191
#
# IP addresses and interface names
#
ifacename() {
192
    LINKIP=$1
193 194 195 196 197 198 199 200 201
    IFACENAME=`$IFCONFIG -a | $PERL -e "while(<>) { if (/^(eth\d+)/) { \\\$if = \\\$1; } if (/$LINKIP/) { print \"\\\$if\n\"; exit 0; }} exit 1;"`

    echo $IFACENAME
}

lookup_ip_host()
{
    HOST=$1
    LINK=$2
202
    IPADDR=`grep "${HOST}-${LINK}[ ]" $HOSTSFILE | cut -f1`
203 204 205 206 207 208 209 210 211 212 213 214
    echo $IPADDR
}

get_iface_addr() {
    IFACE=$1
    IFACEADDR=`$IFCONFIG -a | $PERL -e "while(<>) { if (/^$IFACE/) { \\\$found = 1; } if (\\\$found && /addr:(\d+\.\d+\.\d+\.\d+)/) { print \"\\\$1\n\"; exit 0; }} exit 1;"`
    echo $IFACEADDR
}

#
# $HOST_ROLE should be set by the calling script
#
215
if [ "$HOST_ROLE" = "monitor" ]; then
216 217
    export PELAB_IP=`lookup_ip_host $HOSTNAME $PELAB_LAN`
    export PELAB_IFACE=`ifacename $PELAB_IP`
218 219
elif [ "$HOST_ROLE" = "stub" ]; then
    if [ "$ON_ELAB" = "yes" ]; then
220
        export PLAB_IP=`lookup_ip_host $HOSTNAME $EPLAB_LAN`
221 222 223 224 225 226 227 228
        #
        # We could still be in a vserver
        #
        if [ "$IN_VSERVER" = "yes" ]; then
            export PLAB_IFACE="vnet"
        else
            export PLAB_IFACE=`ifacename $PLAB_IP`
        fi
229 230 231 232 233 234 235
    else
        # On real planetlab
        export PLAB_IFACE="vnet"
        export PLAB_IP=`get_iface_addr $PLAB_IFACE`
    fi
fi

236 237 238 239 240 241 242 243
#
# Function for waiting on a barrier sync
# I'd rather automatically determine the number of peer pairs, but that
# looks too hard for now.
#
barrier_wait()
{
    BARRIER=$1
Mike Hibler's avatar
Mike Hibler committed
244 245

    if [ $ON_MASTER ]; then
246
        WAITERS=`expr $PEERS - 1`
247 248 249
        echo "Waiting up to $SYNCTIMO seconds for $WAITERS clients"
        sync_timeout $SYNCTIMO $SYNC -n $BARRIER -i $WAITERS
	_rval=$?
Mike Hibler's avatar
Mike Hibler committed
250
    else
251 252 253 254 255 256 257 258 259 260 261 262 263
	#
	# XXX on planetlab, lookup of the syncserver name can
	# fail transiently.  Try a couple more times, waiting in between.
	#
	for i in 1 2 3; do
	    $SYNC -n $BARRIER 
	    _rval=$?
	    if [ $_rval -ne 68 ]; then
	        break
	    fi
	    echo "sync on $BARRIER failed, trying again..."
	    sleep 10
	done
264
    fi
265 266

    return $_rval
267 268
}

269 270 271 272 273 274 275 276 277 278 279 280
#
# Log the stdout and stderr of the given process to the logdir
# Runs the program in the background and returns its pid
#
log_output_background()
{
    PROGNAME=$1
    CMD=$2
    $CMD 1> ${LOGDIR}/${PROGNAME}.stdout 2> ${LOGDIR}/${PROGNAME}.stderr &
    echo $!
}

281 282 283 284 285 286 287 288 289 290
sync_watchdog()
{
    TIMO=$1
    SYNCDPID=$2

    sleep $TIMO
    echo '*** HUPing syncd'
    $AS_ROOT kill -HUP $SYNCDPID
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
#
# If $SYNC command doesn't return within the indicated timeout period,
# HUP the syncserver to force everyone out of a barrier.
#
sync_timeout()
{
    TIMO=$1
    shift
    CMDSTR=$*

    if [ -r /var/run/syncd.pid ]; then
        SYNCDPID=`cat /var/run/syncd.pid`
    else
        SYNCDPID=""
    fi

    # fire off the command
    $CMDSTR & CMDPID=$!

    # and a watchdog
    if [ -n "$SYNCDPID" ]; then
312
        sync_watchdog $TIMO $SYNCDPID & DOGPID=$!
313 314 315 316 317 318 319 320
    fi

    # wait for the command to finish or be terminated
    wait $CMDPID
    RVAL=$?

    # nuke the watchdog
    if [ -n "$SYNCDPID" ]; then
321
        kill -9 $DOGPID >/dev/null 2>&1
322 323 324 325 326 327
    fi

    # and return the result
    return $RVAL
}

328 329 330 331
#
# Become the 'vuser'
#

332
fi # End of header guard