manage_expsettings.in 10.8 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2018 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# 
# {{{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;

#
# Back-end script to manage various experiment settings.
#
sub usage()
{
    print("Usage: manage_expsettings lockdown experiment [-f] set|clear\n".
	  "       manage_expsettings swappable experiment [-f] set|clear [reason]\n".
	  "       manage_expsettings autoswap experiment [-f] set hours\n".
	  "       manage_expsettings autoswap experiment clear\n");
    exit(-1);
}
41
my $optlist     = "dsn";
42 43
my $debug       = 0;
my $silent      = 0;
44
my $impotent    = 0;
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 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
my $this_user;

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

#
# 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 libEmulab;
use libtestbed;
use User;
use Experiment;
use Reservation;

# Protos
sub fatal($);
sub UserError($);
sub DoLockdown();
sub DoAutoswap();
sub DoSwappable();
sub ReadReason($$$);
sub CheckReason($$);
sub Notify($);

#
# 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++;
}
99 100 101
if (defined($options{"n"})) {
    $impotent = 1;
}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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
if (defined($options{"s"})) {
    $silent = 1;
}
if (@ARGV < 2) {
    usage();
}
my $action     = shift(@ARGV);
my $token      = shift(@ARGV);
my $experiment = Experiment->Lookup($token);
if (!defined($experiment)) {
    fatal("No such experiment $token");
}
my $pid = $experiment->pid();
my $eid = $experiment->eid();

#
# Have to watch for being invoked from the web interface.
#
if (getpwuid($UID) eq "nobody") {
    $this_user = User->ImpliedUser();
}
else {
    $this_user = User->ThisUser();
}
if (!defined($this_user)) {
    fatal("Who are you?");
}
my $this_uid = $this_user->uid();

if ($action eq "lockdown") {
    DoLockdown();
}
elsif ($action eq "autoswap") {
    DoAutoswap();
}
elsif ($action eq "swappable") {
    DoSwappable();
}
else {
    usage();
}
exit(0);

#
# Experiment lockdown.
#
sub DoLockdown()
{
    my $errmsg;
    my $errcode = -1;
    my $force   = 0;
    my $current = $experiment->lockdown();

    if (!$this_user->IsAdmin()) {
	UserError("Only Administrators can toggle the lockdown flag");
    }
    my $optlist = "f";
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"f"})) {
	$force = 1;
    }
    usage()
	if (@ARGV != 1);
    my $which = shift(@ARGV);

    fatal("Must specify either 'set' or 'clear'")
	if ($which !~ /^(set|clear)$/);

    if ($experiment->Lock()) {
	$errmsg = "Experiment is busy, cannot lock it. Please try again later";
	$errcode = 1;
	goto bad;
    }
    if ($which eq "clear") {
	if ($experiment->Update({"lockdown" => 0})) {
	    $errmsg  = "Could not update database";
	    $errcode = -1;
	    goto bad;
	}
    }
    else {
	if ($experiment->state() eq EXPTSTATE_SWAPPED()) {
	    if ($experiment->Update({"lockdown" => 1})) {
		$errmsg  = "Could not update database";
		$errcode = -1;
		goto bad;
	    }
	}
	else {
194 195
	    if (Reservation->Lockdown($experiment,
				      \$errmsg, $impotent, $force)) {
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
		$errcode = 1;
		$errmsg  = "Could not lockdown experiment"
		    if (!defined($errmsg));
		goto bad;
	    }
	}
    }
    if (!$silent) {
	my $new = $experiment->lockdown();
	Notify("lockdown: $current --> $new\n") if ($current != $new);
    }
    $experiment->Unlock();
    exit(0);
    
  bad:
    $experiment->Unlock();
    print STDERR $errmsg . "\n";
    exit($errcode);
}

#
# Experiment autoswap.
#
sub DoAutoswap()
{
    my $errmsg;
    my $errcode = -1;
    my $force   = 0;
    my $current = $experiment->autoswap();
    my $current_timeout = $experiment->autoswap_timeout();
    my $timeout;
    my $autoswap_max = GetSiteVar("general/autoswap_max");
    
    my $optlist = "f";
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"f"})) {
	$force = 1;
    }
    usage()
	if (@ARGV < 1);
    my $which = shift(@ARGV);
    fatal("Must specify either 'set' or 'clear'")
	if ($which !~ /^(set|clear)$/);

    if (@ARGV) {
	my $hours = shift(@ARGV);
	fatal("Hours must be an integer greater then 0")
	    if ($hours !~ /^\d+$/ || $hours <= 0);
	# Note that timeouts are in hours in the UI, but in minutes in the DB.
	$timeout = $hours * 60;
    }
    else {
	$timeout = $current_timeout;
    }

    if ($which eq "clear" && !$this_user->IsAdmin()) {
	UserError("Only Administrators can disable autoswap");
    }

    if ($experiment->Lock()) {
	$errmsg = "Experiment is busy, cannot lock it. Please try again later";
	$errcode = 1;
	goto bad;
    }
    if ($which eq "set") {
	my $current_timeout = $experiment->autoswap_timeout();
265 266 267 268 269 270 271 272 273 274 275 276 277

	#
	# If the experiment is not running, then this operation is
	# trivial; just update the database since there is no effect
	# on the reservation system. 
	#
	if ($experiment->state() eq EXPTSTATE_SWAPPED()) {
	    if ($experiment->Update({"autoswap" => 1,
				     "autoswap_timeout" => $timeout})) {
		$errmsg  = "Could not update database";
		$errcode = -1;
		goto bad;
	    }
278 279
	}
	#
280 281
	# Otherwise we ask the reservation system to do it.
	# 
282
	# This is a bit problematic; this experiment might have already
283
	# have put the system into overbook, but we cannot really tell
284 285 286 287 288
	# that (that it is this experiment). We only know when making a
	# change puts the system into overbook. So go ahead and do it and
	# if the change is rejected, we can report that.
	#
	if (Reservation->AutoSwapTimeout($experiment, $timeout, 
289
					 \$errmsg, $impotent, $force)) {
290 291 292 293 294
	    $errcode = 1;
	    $errmsg  = "Could not change autoswap timeout for experiment"
		if (!defined($errmsg));
	    goto bad;
	}
295
	
296 297 298 299 300 301 302 303 304 305 306
    }
    else {
	if ($experiment->state() eq EXPTSTATE_SWAPPED()) {
	    if ($experiment->Update({"autoswap" => 0})) {
		$errmsg  = "Could not update database";
		$errcode = -1;
		goto bad;
	    }
	}
	else {
	    if (Reservation->DisableAutoSwap($experiment,
307
					     \$errmsg, $impotent, $force)) {
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
		$errcode = 1;
		$errmsg  = "Could not disable autoswap for experiment"
		    if (!defined($errmsg));
		goto bad;
	    }
	}
	#
	# Its fine to set the timeout when disabling autoswap, does not
	# mean much until we turn autoswap back on.
	#
	if (defined($timeout)) {
	    if ($experiment->Update({"autoswap_timeout" => $timeout})) {
		$errmsg  = "Could not update database";
		$errcode = -1;
		goto bad;
	    }
	}
    }
    if (!$silent) {
	my $new = $experiment->autoswap();
	my $new_timeout = $experiment->autoswap_timeout();

	if ($current != $new || $current_timeout != $new_timeout) {
	    Notify("autoswap: $current --> $new\n".
		   "autoswap_timeout: $current_timeout --> $new_timeout\n");
	}
    }
    $experiment->Unlock();
    exit(0);
    
  bad:
    $experiment->Unlock();
    print STDERR $errmsg . "\n";
    exit($errcode);
}

#
# Experiment Swappable
#
sub DoSwappable()
{
    my $errmsg;
    my $errcode = -1;
    my $force   = 0;
    my $current = $experiment->swappable();
    my $reason  = "ADMIN";
    
    my $optlist = "fm:";
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"f"})) {
	$force = 1;
    }
    usage()
	if (@ARGV < 1);
    my $which = shift(@ARGV);

    fatal("Must specify either 'set' or 'clear'")
	if ($which !~ /^(set|clear)$/);

    if (@ARGV) {
	$reason = shift(@ARGV);
    }
    elsif (defined($options{"m"})) {
	if (ReadReason($options{"m"}, \$reason, \$errmsg)) {
	    goto bad;
	}
    }
    if (CheckReason($reason, \$errmsg)) {
	goto bad;
    }

    if ($experiment->Lock()) {
	$errmsg = "Experiment is busy, cannot lock it. Please try again later";
	$errcode = 1;
	goto bad;
    }
    if ($which eq "set") {
	if ($experiment->Update({"swappable" => 1})) {
	    $errmsg  = "Could not update database";
	    $errcode = -1;
	    goto bad;
	}
    }
    else {
	if ($experiment->state() eq EXPTSTATE_SWAPPED()) {
	    if ($experiment->Update({"swappable" => 0})) {
		$errmsg  = "Could not update database";
		$errcode = -1;
		goto bad;
	    }
	}
	else {
	    if (Reservation->DisableSwapping($experiment,
404
					     \$errmsg, $impotent, $force)) {
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
		$errcode = 1;
		$errmsg  = "Could not disable swappable for experiment"
		    if (!defined($errmsg));
		goto bad;
	    }
	}
    }
    if (defined($reason)) {
	$experiment->Update({"noswap_reason" => $reason});
    }
    if (!$silent) {
	my $new = $experiment->swappable();
	Notify("swappable: $current --> $new\n") if ($current != $new);
    }
    $experiment->Unlock();
    exit(0);
    
  bad:
    $experiment->Unlock();
    print STDERR $errmsg . "\n";
    exit($errcode);
}

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

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    # Exit with negative status so web interface treats it as system error.
    exit(-1);
}

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

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(1);
}

#
# Read a reason from a file and verify.
#
sub ReadReason($$$)
{
    my ($filename, $ref, $perr) = @_;
    my $reason = "";

    if ($filename eq "-") {
	while (<STDIN>) {
	    $reason .= $_;
	}
	$$ref = $reason;
	return 0;
    }
    if (! -e $filename) {
	$$perr = "$filename does not exist";
	return -1;
    }
    if (!open(MSG, $filename)) {
	$$perr = "Could not open $filename: $!";
	return -1;
    }
    while (<MSG>) {
	$reason .= $_;
    }
    close(MSG);
    $$ref = $reason;
    return 0;
}

#
# Verify reason with DB regex
#
sub CheckReason($$)
{
    my ($reason, $perr) = @_;

    if (!TBcheck_dbslot($reason, "default", "fulltext",
			TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	$$perr = TBFieldErrorString();
	return -1;
    }
    return 0;
}

#
# Notify the audit list of the change.
#
sub Notify($)
{
    my ($message) = @_;

    libtestbed::SENDMAIL($TBAUDIT, "Experiment Settings Change",
			 "$this_uid has changed settings for $pid/$eid:\n\n".
			 $message, $TBOPS);
}