panic.in 5.78 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
7
use strict;
Leigh Stoller's avatar
Leigh Stoller committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use English;
use Getopt::Std;
use POSIX qw(isatty setsid);

#
# Press the panic button. Also invoked from web interface.
#
sub usage()
{
    print(STDERR
	  "Usage: panicbutton [-r] <pid> <eid>\n".
	  "switches and arguments:\n".
	  "-r       - Reset panic state (admin people only)\n".
	  "<pid>    - The project the experiment belongs to\n".
	  "<eid>    - The experiment name (id)\n");
    exit(-1);
}
my $optlist = "r";
my $reset   = 0;

28
sub fatal($);
Leigh Stoller's avatar
Leigh Stoller committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

#
# Exit codes are important; they tell the web page what has happened so
# it can say something useful to the user. Fatal errors are mostly done
# with die(), but expected errors use this routine. At some point we will
# use the DB to communicate the actual error.
#
# $status < 0 - Fatal error. Something went wrong we did not expect.
# $status = 0 - Termination is proceeding in the background. Notified later.
# $status > 0 - Expected error. User not allowed for some reason. 
# 
sub ExitWithStatus($$)
{
    my ($status, $message) = @_;
    
    if ($status < 0) {
	die("*** $0:\n".
	    "    $message\n");
    }
    else {
	print STDERR "$message\n";
    }
    exit($status);
}

#
# Configure variables
#
my $TB     = "@prefix@";
my $TBOPS  = "@TBOPSEMAIL@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
66 67
use Experiment;
use User;
Leigh Stoller's avatar
Leigh Stoller committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

my $snmpit    = "$TB/bin/snmpit";
    
#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
86
my %options = ();
Leigh Stoller's avatar
Leigh Stoller committed
87 88 89 90 91 92 93 94 95 96
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}
if (defined($options{"r"})) {
    $reset = 1;
}

97 98 99
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
Leigh Stoller's avatar
Leigh Stoller committed
100
}
101 102 103 104 105 106
my $user_uid = $this_user->uid();

# Slowly convert to using Experiment module.
my $experiment = Experiment->Lookup($ARGV[0], $ARGV[1]);
if (!defined($experiment)) {
    tbdie("Could not lookup experiment object!")
Leigh Stoller's avatar
Leigh Stoller committed
107
}
108 109
my $pid = $experiment->pid();
my $eid = $experiment->eid();
Leigh Stoller's avatar
Leigh Stoller committed
110 111 112 113

#
# See if the experiment is firewalled. Error if not.
#
114
if (!$experiment->IsFirewalled()) {
Leigh Stoller's avatar
Leigh Stoller committed
115
    die("*** $0:\n".
116
	"    $experiment is not firewalled!\n");
Leigh Stoller's avatar
Leigh Stoller committed
117 118 119
}

#
120 121
# Verify that this person is allowed to press the panic button.
# Note that any script down the line has to do an admin check also. 
Leigh Stoller's avatar
Leigh Stoller committed
122
#
123 124
if ($UID && !$this_user->IsAdmin() &&
    !$experiment->AccessCheck($this_user, TB_EXPT_MODIFY)) {
Leigh Stoller's avatar
Leigh Stoller committed
125
    die("*** $0:\n".
126
	"    You do not have permission to end this experiment!\n");
Leigh Stoller's avatar
Leigh Stoller committed
127 128 129 130 131
}

#
# Get email info for user.
#
132 133
my $user_name  = $this_user->name();
my $user_email = $this_user->email();
Leigh Stoller's avatar
Leigh Stoller committed
134

135 136 137 138
# Get email for current swapper.
my $swapper        = $experiment->GetSwapper();
my $swapper_name   = $swapper->name();
my $swapper_email  = $swapper->email();
Leigh Stoller's avatar
Leigh Stoller committed
139 140 141 142 143 144 145 146

#
# We have to protect against trying to end an experiment that is currently
# in the process of being terminated. We use a "wrapper" state (actually
# a timestamp so we can say when termination was requested) since
# terminating consists of a couple of different experiment states down inside
# the tb scripts. 
#
147 148
$experiment->LockTables() == 0
    or fatal("Could not lock experiment tables for $pid/$eid!");
Leigh Stoller's avatar
Leigh Stoller committed
149 150 151 152 153 154

#
# Called from user (via web interface).
#
if ($reset) {
    ExitWithStatus(1, "Experiment $pid/$eid is not paniced!\n")
155
	if ($experiment->state() ne EXPTSTATE_PANICED);
Leigh Stoller's avatar
Leigh Stoller committed
156 157 158
}
else {
    ExitWithStatus(1, "Experiment $pid/$eid is not active!\n")
159 160 161
	if (! ($experiment->state() eq EXPTSTATE_ACTIVE ||
	       $experiment->state() eq EXPTSTATE_ACTIVATING ||
	       $experiment->state() eq EXPTSTATE_SWAPPING));
Leigh Stoller's avatar
Leigh Stoller committed
162 163 164
}

#
165
# Change experiment state and lock it. Unlock tables at same time.
166
#
167
$experiment->Lock(($reset ? EXPTSTATE_ACTIVE : EXPTSTATE_PANICED), 1) == 0
168
    or fatal("Could not lock $experiment");
Leigh Stoller's avatar
Leigh Stoller committed
169 170 171 172 173 174 175 176 177

#
# XXX - At this point a failure is going to leave things in an
# inconsistent state. Be sure to call fatal() only since we are
# going into the background, and we have to send email since no
# one is going to see printed error messages (output goes into the
# log file, which will be sent along in the email). 
#

178 179 180 181
#
# Get firewall node and port info
#
my ($firewall, $port);
182 183
if ($experiment->FirewallAndPort(\$firewall, \$port) != 0) {
    fatal("Could not determine firewall port for $experiment");
Leigh Stoller's avatar
Leigh Stoller committed
184 185 186 187 188 189 190 191 192 193
}

#
# Call snmpit.
#
if ($reset) {
    system("$snmpit -e ${firewall}:${port}");
    if ($?) {
	fatal("snmpit exited with $?!");
    }
194
    $experiment->SetPanicBit(0);
Leigh Stoller's avatar
Leigh Stoller committed
195 196 197 198 199 200 201
    print "Panic situation has been cleared!\n";
}
else {
    system("$snmpit -d ${firewall}:${port}");
    if ($?) {
	fatal("snmpit exited with $?!");
    }
202
    $experiment->SetPanicBit(1);
Leigh Stoller's avatar
Leigh Stoller committed
203 204
    print "Panic Button has been pressed!\n";
}
205
$experiment->Unlock();
Leigh Stoller's avatar
Leigh Stoller committed
206 207 208 209 210 211 212

#
# Send email notification to user *and* to tbops.
#
SENDMAIL("$user_name <$user_email>",
	 "Panic Button ". ($reset ? "Cleared" : "Pressed") .
	    " for Experiment $pid/$eid",
213
	 "$user_uid has " . ($reset ? "cleared" : "pressed") .
Leigh Stoller's avatar
Leigh Stoller committed
214 215
	   " the panic button for experiment $pid/$eid",
	 "$user_name <$user_email>",
216
	 "Cc:  $swapper_name <$swapper_email>\n".
Leigh Stoller's avatar
Leigh Stoller committed
217 218 219 220 221 222 223 224 225 226 227 228 229
	 "Bcc: $TBOPS");

exit 0;

sub fatal($)
{
    my($mesg) = $_[0];
    
    #
    # Send a message to the testbed list. Append the logfile.
    #
    SENDMAIL("$user_name <$user_email>",
	     "Panic Button Failure for Experiment $pid/$eid",
230
	     "$user_uid ". ($reset ? "cleared" : "pressed") .
Leigh Stoller's avatar
Leigh Stoller committed
231 232 233 234
	       " the panic button for experiment $pid/$eid,\n".
	     "BUT there was a failure!\n\n".
	     "$mesg\n",
	     "$user_name <$user_email>",
235
	     "Cc:  $swapper_name <$swapper_email>\n".
Leigh Stoller's avatar
Leigh Stoller committed
236 237 238 239 240
	     "Bcc: $TBOPS");

    die("*** $0:\n".
	"    $mesg\n");
}