panic.in 5.51 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
Leigh Stoller's avatar
Leigh Stoller committed
4
# Copyright (c) 2000-2011 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
use English;
use Getopt::Std;
use POSIX qw(isatty setsid);

#
# Press the panic button. Also invoked from web interface.
#
sub usage()
{
    print(STDERR
Leigh Stoller's avatar
Leigh Stoller committed
18 19
	  "Usage: panic [-l level] <pid> <eid>\n".
	  "       panic -r <pid> <eid>\n".
Leigh Stoller's avatar
Leigh Stoller committed
20
	  "switches and arguments:\n".
Leigh Stoller's avatar
Leigh Stoller committed
21 22
	  "-l level - Level 1; reboot nodes into the admin MFS\n".
	  "         - Level 2; disable the control network\n".
Leigh Stoller's avatar
Leigh Stoller committed
23 24 25 26 27
	  "-r       - Reset panic state (admin people only)\n".
	  "<pid>    - The project the experiment belongs to\n".
	  "<eid>    - The experiment name (id)\n");
    exit(-1);
}
Leigh Stoller's avatar
Leigh Stoller committed
28
my $optlist = "rl:";
Leigh Stoller's avatar
Leigh Stoller committed
29
my $reset   = 0;
Leigh Stoller's avatar
Leigh Stoller committed
30
my $level   = 1;
Leigh Stoller's avatar
Leigh Stoller committed
31

32
sub fatal($);
Leigh Stoller's avatar
Leigh Stoller committed
33
sub DoIt();
Leigh Stoller's avatar
Leigh Stoller committed
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 66 67 68 69 70

#
# 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;
Leigh Stoller's avatar
Leigh Stoller committed
71
use Firewall;
72 73
use Experiment;
use User;
Leigh Stoller's avatar
Leigh Stoller committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

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.
#
92
my %options = ();
Leigh Stoller's avatar
Leigh Stoller committed
93 94 95 96 97 98 99 100 101
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}
if (defined($options{"r"})) {
    $reset = 1;
}
Leigh Stoller's avatar
Leigh Stoller committed
102 103 104 105 106
if (defined($options{"l"})) {
    $level = 1;
    usage()
	if ($level < 1 || $level > 2);
}
Leigh Stoller's avatar
Leigh Stoller committed
107

108 109 110
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
Leigh Stoller's avatar
Leigh Stoller committed
111
}
112 113 114 115 116 117
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
118
}
119 120
my $pid = $experiment->pid();
my $eid = $experiment->eid();
Leigh Stoller's avatar
Leigh Stoller committed
121 122

#
123 124
# 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
125
#
126 127
if ($UID && !$this_user->IsAdmin() &&
    !$experiment->AccessCheck($this_user, TB_EXPT_MODIFY)) {
Leigh Stoller's avatar
Leigh Stoller committed
128
    die("*** $0:\n".
Leigh Stoller's avatar
Leigh Stoller committed
129
	"    You do not have permission for this experiment!\n");
Leigh Stoller's avatar
Leigh Stoller committed
130 131 132 133 134
}

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

138 139 140 141
# 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
142 143 144 145 146 147 148 149

#
# 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. 
#
150 151
$experiment->LockTables() == 0
    or fatal("Could not lock experiment tables for $pid/$eid!");
Leigh Stoller's avatar
Leigh Stoller committed
152 153 154 155 156 157

#
# Called from user (via web interface).
#
if ($reset) {
    ExitWithStatus(1, "Experiment $pid/$eid is not paniced!\n")
Leigh Stoller's avatar
Leigh Stoller committed
158 159 160 161
	if ($experiment->state() ne EXPTSTATE_PANICED &&
	    $experiment->paniced());

    $level = $experiment->paniced();
Leigh Stoller's avatar
Leigh Stoller committed
162 163 164
}
else {
    ExitWithStatus(1, "Experiment $pid/$eid is not active!\n")
165 166 167
	if (! ($experiment->state() eq EXPTSTATE_ACTIVE ||
	       $experiment->state() eq EXPTSTATE_ACTIVATING ||
	       $experiment->state() eq EXPTSTATE_SWAPPING));
Leigh Stoller's avatar
Leigh Stoller committed
168 169 170
}

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

Leigh Stoller's avatar
Leigh Stoller committed
176 177 178 179
# Force level 2 for firewalled experiments.
if ($experiment->IsFirewalled()) {
    $level = 2
	if (!$reset);
Leigh Stoller's avatar
Leigh Stoller committed
180 181 182
}

#
Leigh Stoller's avatar
Leigh Stoller committed
183 184
# XXX - At this point a failure is going to leave things in an
# inconsistent state.
Leigh Stoller's avatar
Leigh Stoller committed
185
#
Leigh Stoller's avatar
Leigh Stoller committed
186 187 188 189
if (Firewall::Panic($experiment, $level,
		    ($reset ?
		     Firewall::PANIC_RESET() : Firewall::PANIC_PANIC()))) {
    fatal("Failure in Firewall::Panic()");
Leigh Stoller's avatar
Leigh Stoller committed
190
}
191
$experiment->Unlock();
Leigh Stoller's avatar
Leigh Stoller committed
192
print "Panic Button has been ". ($reset ? "cleared" : "pressed") . "\n";
Leigh Stoller's avatar
Leigh Stoller committed
193 194 195 196 197 198 199

#
# Send email notification to user *and* to tbops.
#
SENDMAIL("$user_name <$user_email>",
	 "Panic Button ". ($reset ? "Cleared" : "Pressed") .
	    " for Experiment $pid/$eid",
200
	 "$user_uid has " . ($reset ? "cleared" : "pressed") .
Leigh Stoller's avatar
Leigh Stoller committed
201 202
	   " the panic button for experiment $pid/$eid",
	 "$user_name <$user_email>",
203
	 "Cc:  $swapper_name <$swapper_email>\n".
Leigh Stoller's avatar
Leigh Stoller committed
204
	 "Bcc: $TBOPS");
Leigh Stoller's avatar
Leigh Stoller committed
205
exit(0);
Leigh Stoller's avatar
Leigh Stoller committed
206 207 208 209 210 211 212 213 214 215

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",
216
	     "$user_uid ". ($reset ? "cleared" : "pressed") .
Leigh Stoller's avatar
Leigh Stoller committed
217 218 219 220
	       " the panic button for experiment $pid/$eid,\n".
	     "BUT there was a failure!\n\n".
	     "$mesg\n",
	     "$user_name <$user_email>",
221
	     "Cc:  $swapper_name <$swapper_email>\n".
Leigh Stoller's avatar
Leigh Stoller committed
222 223 224 225 226
	     "Bcc: $TBOPS");

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