powder_shutdown.in 6.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2019 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
use English;
use strict;
use Getopt::Std;
use Data::Dumper;
use Date::Parse;

#
# Powder Panic Button
#
sub usage()
{
35
    print STDERR "Usage: powder-panic [-d] [-b] [-n]\n";
36 37
    print STDERR "Options:\n";
    print STDERR " -d  - Turn on debugging\n";
38
    print STDERR " -b  - Batch mode, do not ask for confirmation\n";
39 40 41
    print STDERR " -n  - Dry run mode, just show what would be done\n";
    exit(-1);
}
42
my $optlist     = "dnt:b";
43 44
my $debug       = 0;
my $impotent    = 0;
45
my $batch       = 0;
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
my $logfile;
my $webtask_id;
my $webtask;

#
# Configure variables
#
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $OURDOMAIN	   = "@OURDOMAIN@";
my $MAINSITE       = @TBMAINSITE@;
my $MYURN	   = "urn:publicid:IDN+${OURDOMAIN}+authority+cm";
my $MANAGEINSTANCE = "$TB/bin/manage_instance";

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

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

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use EmulabConstants;
use emdb;
use emutil;
use WebTask;
use Brand;
use libtestbed;
use APT_Geni;
use APT_Aggregate;
use APT_Utility;
use APT_Instance;

# Protos
sub fatal($);
sub notify($);

#
# Only at the Mothership
#
if (!$MAINSITE) {
    print "No powder here, too warm.\n";
    exit(0);
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug++;
}
109 110 111
if (defined($options{"b"})) {
    $batch++;
}
112 113 114 115 116 117 118 119 120 121 122
if (defined($options{"n"})) {
    $impotent++;
}
if (defined($options{"t"})) {
    $webtask_id = $options{"t"};
    $webtask = WebTask->Lookup($webtask_id);
    if (!defined($webtask)) {
	fatal("Could not lookup/create webtask");
    }
    $webtask->AutoStore(1);
}
123 124 125 126 127 128 129 130 131 132 133 134 135
usage()
    if (@ARGV);

if (!$batch) {
    print "Are you sure you want to power off all Powder experiments? " .
	"[yes/no] ";

    my $response = <STDIN>;
    if ($response !~ /^yes$/i) {
	print "Aborting ...\n";
	exit(1);
    }
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

#
# Grab the list of instances to kill off.
#
my @killme = ();

my $query_result =
    DBQueryFatal("select uuid from apt_instances");
while (my ($uuid) = $query_result->fetchrow_array()) {
    my $instance = APT_Instance->Lookup($uuid);
    next
	if (!defined($instance));

    # This will not be set until the instance is actually paniced.
    next
	if ($instance->paniced());

    #
    # Look at the list of aggregates in use, if any of them are flagged
    # with panicpoweroff, then do it.
    #
    my $killit = 0;
    foreach my $agg ($instance->AggregateList()) {
	my $aptagg = $agg->GetAptAggregate();
    
	next
	    if ($agg->status() eq "deferred");

	#
	# Skip anything that is not ready. Need to handle imaging.
	#
	next
	    if ($agg->status() ne "ready");

	if ($aptagg->panicpoweroff()) {
	    push(@killme, $instance);
	    last;
	}
	#
	# Need to look at local experiments too ... 
	#
	if ($aptagg->urn() eq $MYURN) {
	    #
	    # Find the local experiment, and look at the list of nodes
	    # to see if any needs to be powered off during a panic. 
	    #
	    my $experiment = $instance->LocalExperiment();
	    if (!defined($experiment)) {
		print STDERR
		    "Could not lookup local Experiment for $instance\n";
		next;
	    }
	    my @nodelist = $experiment->NodeList(0);
	    my $bad = 0;
	    foreach my $node (@nodelist) {
		my $nodetype = $node->NodeTypeInfo();
		if ($nodetype && $nodetype->GetAttribute("panicpoweroff")) {
		    $bad = 0;
		    last;
		}
	    }
	    if ($bad) {
		push(@killme, $instance);
		last;
	    }
	}
    }
}
#
# Tell the web interface what instances are going to shutdown.
#
if (defined($webtask)) {
    my @listing = map {$_->uuid()} @killme;
    $webtask->instanceList(\@listing);
}
if (!@killme) {
    print "Nothing to panic about, take a chill pill\n";
    if (defined($webtask)) {
	$webtask->Exited(0);
    }
    exit(0);
}

#
# Go to background and report later.
#
if (! ($debug || $impotent)) {
    $logfile = TBMakeLogname("powder-panic");
	
    if (my $childpid = TBBackGround($logfile)) {
	print "Backgounding, see progress in $logfile\n";
	if (defined($webtask)) {
	    $webtask->Exited(0);
	}
	exit(0);
    }
    notify("Powder panic button pressed!\n" . "Track progress in $logfile");
}

while (@killme) {
    my @todo = @killme;
    @killme = ();

    foreach my $instance (@todo) {
	my $uuid = $instance->uuid();
	
	$instance->Refresh();
	next
	    if ($instance->paniced());
    
	if ($impotent) {
	    print "Would put $instance into panic mode\n";
	    next;
	}
	print "Setting quarantine mode (with power off) on $instance\n";
	system("$MANAGEINSTANCE panic $uuid -f -p set");
	if ($?) {
	    print "Will try again later\n";
	    push(@killme, $instance);
	}
    }
    if (@killme) {
	sleep(60);
    }
}
notify("All experiments are in panic mode");
if (defined($webtask)) {
    $webtask->Exited(0);
}
exit(0);

sub fatal($)
{
    my ($mesg) = @_;

    if (defined($webtask)) {
 	$webtask->output($mesg);
	$webtask->Exited(-1);
    }
    notify($mesg);
    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}

sub notify($)
{
    my ($mesg) = @_;
    
    SENDMAIL($TBOPS,
	     "Powder Panic Notification",
	     $mesg,
	     $TBOPS);
}