manage_instance.in 122 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
# 
# {{{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 XML::Simple;
use Data::Dumper;
use CGI;
use POSIX ":sys_wait_h";
31
use POSIX qw(setsid strftime ceil floor);
32
use Date::Parse;
33
use JSON;
34 35 36 37 38 39

#
# Back-end script to manage APT profiles.
#
sub usage()
{
Leigh Stoller's avatar
Leigh Stoller committed
40 41
    print("Usage: manage_instance snapshot instance ".
	  "[-n node_id] [-i imagename] [-u node|all]\n");
42 43 44
    print("Usage: manage_instance consoleurl instance node\n");
    print("Usage: manage_instance terminate instance\n");
    print("Usage: manage_instance refresh instance\n");
45 46 47
    print("Usage: manage_instance reboot instance node_id ...\n");
    print("Usage: manage_instance reload instance node_id ...\n");
    print("Usage: manage_instance deletenodes instance node_id ...\n");
48
    print("Usage: manage_instance monitor instance\n");
49
    print("Usage: manage_instance lockdown instance set|clear user|admin\n");
50
    print("Usage: manage_instance panic instance set|clear\n");
51
    print("Usage: manage_instance linktest instance [-k | level]\n");
52
    print("Usage: manage_instance writecreds instance directory\n");
53
    print("Usage: manage_instance updatekeys instance [uid] \n");
54
    print("Usage: manage_instance extend instance ".
55
	  "[-M] [-m message | -f filename] hours\n");
56
    print("Usage: manage_instance denyextension instance [-m message] [filename]\n");
57
    print("Usage: manage_instance maxextension instance\n");
58
    print("Usage: manage_instance moreinfo instance [-m message] [filename]\n");
59
    print("Usage: manage_instance utilization instance\n");
60
    print("Usage: manage_instance schedterminate instance [-m message] days [filename]\n");
61
    print("Usage: manage_instance idledata instance\n");
62
    print("Usage: manage_instance openstackstats instance\n");
Leigh Stoller's avatar
Leigh Stoller committed
63
    print("Usage: manage_instance getmanifests instance\n");
64
    print("Usage: manage_instance warn instance\n");
65
    print("Usage: manage_instance applyextensionpolicy instance\n");
66 67
    exit(-1);
}
68
my $optlist     = "dt:s";
69
my $debug       = 0;
70
my $silent      = 0;
71
my $webtask_id;
72
my $webtask;
73 74
my $this_user;
my $geniuser;
75 76 77 78

#
# Configure variables
#
79 80 81 82 83 84 85
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $PROTOUSER      = "elabman";
my $SUDO           = "/usr/local/bin/sudo";
my $MANAGEINSTANCE = "$TB/bin/manage_instance";
my $WAP            = "$TB/sbin/wap";
my $TBACCT         = "$TB/sbin/tbacct";
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

#
# 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;
105
use libEmulab;
106
use libtestbed;
107 108 109 110
use User;
use Project;
use APT_Profile;
use APT_Instance;
111
use APT_Geni;
112
use APT_Utility;
113 114
use GeniXML;
use GeniHRN;
115 116 117
use Genixmlrpc;
use GeniResponse;
use GeniSlice;
118
use GeniImage;
119
use GeniUser;
120
use WebTask;
121
use EmulabFeatures;
122 123 124

# Protos
sub fatal($);
125
sub UserError($);
126
sub DoSnapshot();
127
sub DoConsole();
128
sub DoTerminate();
129
sub DoSchedTerminate();
130
sub DoExtend();
131
sub DoDenyOrMoreInfo($);
132
sub DoRefresh();
133
sub DoReboot();
134
sub DoReload();
135
sub DoLockdown();
136
sub DoPanic();
137
sub DoManifests();
138
sub DoLinktest();
139
sub DoUpdateKeys();
140
sub DoDeleteNodes();
141
sub DoUtilization();
142
sub DoIdleData();
143
sub DoOpenstack();
144 145
sub DoCheckAutoApprove();
sub CheckAutoApprove($$);
146
sub DoMaxExtension();
147
sub DoMaxExtensionInternal($$);
148
sub DoApplyExtensionPolicy();
149
sub WriteCredentials();
150
sub StartMonitor();
151
sub StartMonitorInternal(;$);
152
sub DoImageTrackerStuff($$$$$$$);
153
sub DoWarn();
154
sub DoDelete();
155
sub DenyExtensionInternal($);
156
sub ExtendInternal($$$$$);
157 158
sub CallMethodOnAggregates($$$@);
sub ResponseErrorMessage($$);
159 160 161 162 163 164 165 166 167 168 169 170

#
# 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{"t"})) {
    $webtask_id = $options{"t"};
}
171 172 173
if (defined($options{"d"})) {
    $debug++;
}
174 175 176
if (defined($options{"s"})) {
    $silent = 1;
}
177
if (@ARGV < 2) {
178 179
    usage();
}
180
my $action   = shift(@ARGV);
181 182
my $token    = shift(@ARGV);
my $instance = APT_Instance->Lookup($token);
183
if (!defined($instance)) {
184
    $instance = APT_Instance->LookupBySlice($token);
185
}
186 187 188 189 190 191 192 193 194 195 196
#
# Grab the webtask object.
#
if (defined($webtask_id)) {
    $webtask = WebTask->Lookup($webtask_id);
    if (!defined($webtask)) {
	fatal("Could not lookup/create webtask for profile");
    }
    $webtask->AutoStore(1);
}

197 198 199 200 201 202 203 204 205 206
if (getpwuid($UID) eq "nobody") {
    $this_user = User->ImpliedUser();
}
else  {
    $this_user = User->ThisUser();
}
# If a guest user, we will not have an actual user, which is okay.
if (defined($this_user)) {
    $geniuser = GeniUser->CreateFromLocal($this_user);
}
207

208 209 210 211 212 213 214
if (!defined($instance)) {
    # Lets not make this a fatal error; when coming from the web interface,
    # we do not need to send email. 
    UserError("No such instance $token");
}
my $uuid = $instance->uuid();

215
if ($action eq "snapshot") {
216 217
    DoSnapshot();
}
218 219 220
if ($action eq "extend") {
    DoExtend();
}
221
elsif ($action eq "denyextension") {
222 223 224 225
    DoDenyOrMoreInfo("deny")
}
elsif ($action eq "moreinfo") {
    DoDenyOrMoreInfo("info")
226
}
227
elsif ($action eq "consoleurl") {
228 229
    DoConsole()
}
230 231 232
elsif ($action eq "terminate") {
    DoTerminate()
}
233 234 235
elsif ($action eq "warn") {
    DoWarn()
}
236 237 238
elsif ($action eq "schedterminate") {
    DoSchedTerminate()
}
239 240 241
elsif ($action eq "refresh") {
    DoRefresh()
}
242 243 244
elsif ($action eq "reboot") {
    DoReboot()
}
245 246 247
elsif ($action eq "reload") {
    DoReload()
}
248 249 250
elsif ($action eq "monitor") {
    StartMonitor()
}
251 252 253
elsif ($action eq "lockdown") {
    DoLockdown()
}
254 255 256
elsif ($action eq "panic") {
    DoPanic()
}
257 258 259
elsif ($action eq "linktest") {
    DoLinktest()
}
260 261 262
elsif ($action eq "updatekeys") {
    DoUpdateKeys()
}
263 264 265
elsif ($action eq "writecreds") {
    WriteCredentials()
}
266 267 268
elsif ($action eq "getmanifests") {
    DoManifests()
}
269 270 271
elsif ($action eq "deletenodes") {
    DoDeleteNodes()
}
272 273 274
elsif ($action eq "utilization") {
    DoUtilization()
}
275 276 277
elsif ($action eq "idledata") {
    DoIdleData()
}
278 279 280
elsif ($action eq "openstackstats") {
    DoOpenstack()
}
281 282 283
elsif ($action eq "maxextension") {
    DoMaxExtension()
}
284 285 286
elsif ($action eq "checkautoapprove") {
    DoCheckAutoApprove()
}
287 288 289
elsif ($action eq "applyextensionpolicy") {
    DoApplyExtensionPolicy()
}
290 291 292
elsif ($action eq "delete") {
    DoDelete()
}
293 294 295
else {
    usage();
}
296 297 298 299 300 301 302
exit(0);

#
# Take a snapshot. Implies a single node instance, for now.
#
sub DoSnapshot()
{
303 304
    my $errmsg;
    my $logfile;
305
    my $errcode        = -1;
306
    my $exitcode       = -1;
307 308
    my $needunlock     = 0;
    my $old_status     = $instance->status();
309 310
    my $node_id;
    my $imagename;
Leigh Stoller's avatar
Leigh Stoller committed
311
    my $description;
312
    my $cloneprofile;
313
    my $update_profile;
314 315
    my $copyback_uuid;
    my $copyback_urn;
316 317 318
    my $swebtask;
    my $nosnapshot     = 0;
    my $mustnotexist   = 0;
319
    my $wholedisk      = 0;
320
    my $update_prepare = 0;
321 322
    my $doversions = 0;
    my $usetracker = 0;
323
    my $operation = "image-only"; # Default to just snapshot.
324

Leigh Stoller's avatar
Leigh Stoller committed
325
    my $optlist = "n:i:u:Uc:O:SseD:";
326 327 328 329 330 331 332 333 334 335
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"n"})) {
	$node_id = $options{"n"};
    }
    if (defined($options{"i"})) {
	$imagename = $options{"i"};
    }
336 337 338
    if (defined($options{"c"})) {
	$cloneprofile = $options{"c"};
    }
339 340 341 342 343 344
    if (defined($options{"u"})) {
	$update_profile = $options{"u"};
	if ($update_profile !~ /^(node|all)$/) {
	    usage();
	}
    }
345 346 347
    if (defined($options{"U"})) {
	$update_prepare = 1;
    }
Leigh Stoller's avatar
Leigh Stoller committed
348 349 350
    if (defined($options{"D"})) {
	$description = ReadFile($options{"D"});
    }
351 352 353
    if (defined($options{"s"})) {
	$nosnapshot = 1;
    }
354 355 356 357 358
    if (defined($options{"e"})) {
	$wholedisk = 1;
	# Must be a brand new image for wholedisk. 
	$mustnotexist = 1;
    }
359 360 361 362 363 364 365 366 367 368 369 370 371 372
    if (defined($options{"S"})) {
	$nosnapshot = 1;
	$mustnotexist = 1;
    }
    if (defined($options{"O"})) {
	$operation = $options{"O"};
	if ($operation !~
	    /^(update-profile|copy-profile|new-profile|image-only)$/) {
	    usage();
	}
	if ($operation eq "update-profile") {
	    $update_profile = "node";
	}
    }
373 374 375 376 377 378
    if (defined($cloneprofile) && defined($update_profile)) {
	fatal("Not allowed to update profile when cloning a profile");
    }
    if (defined($cloneprofile) && !defined($imagename)) {
	fatal("Must supply image name when cloning a profile");
    }
379 380 381 382 383 384 385
    if ($old_status ne "ready") {
	fatal("Instance must be in the ready state to take a snapshot");
    }
    my $slice = $instance->GetGeniSlice();
    if (!defined($slice)) {
	fatal("No slice for quick VM: $uuid");
    }
386
    
387
    #
388
    # Might be a clone (manage_profile).
389
    #
390
    my $sliver_urn;
391 392
    my $aggregate;
    my $node;
393 394 395 396 397 398 399 400
    my $profile;

    if (defined($cloneprofile)) {
	$profile = APT_Profile->Lookup($cloneprofile);
    }
    else {
	$profile = APT_Profile->Lookup($instance->profile_id());
    }
401
    if (!defined($profile)) {
402 403
	fatal("Could not lookup profile for " .
	      (defined($cloneprofile) ? "cloning" : "snapshot"));
404
    }
405 406
    my $project = Project->Lookup($profile->pid_idx());
    if (!defined($project)) {
407
	fatal("Could not lookup project for $profile");
408 409
    }
    
410
    #
411
    # Sanity checks. 
412
    #
413
    my @aggs = $instance->AggregateList();
414 415
    if (! @aggs) {
	fatal("No slivers for instance!");
416
    }
417
    if (!defined($node_id)) {
418
	# We snapshot the one node in the instance.
419 420 421 422 423 424 425 426 427
	if (@aggs != 1) {
	    fatal("Too many aggregates (> 1) to snapshot");
	}
	my ($agg) = @aggs;
	my $manifest = GeniXML::Parse($agg->manifest());
	if (! defined($manifest)) {
	    fatal("Could not parse manifest for $agg");
	}
	my @nodes = GeniXML::FindNodes("n:node", $manifest)->get_nodelist();
428 429 430
	if (@nodes != 1) {
	    fatal("Too many nodes (> 1) to snapshot");
	}
431
	($node)     = @nodes;
432
	$sliver_urn = GeniXML::GetSliverId($node);
433 434
	$node_id    = GeniXML::GetVirtualId($node);
	$aggregate  = $agg;
435 436 437
	# Profile Snapshot, always use the profile name. Clone passes in name.
	if (!defined($imagename)) {
	    $imagename = $profile->name();
438
	}
439
    }
440
    else {
441 442
	my $nodecount = 0;
	
443 444 445 446 447 448 449
	# Find the node in its manifest.
	foreach my $agg (@aggs) {
	    my $manifest = GeniXML::Parse($agg->manifest());
	    if (! defined($manifest)) {
		fatal("Could not parse manifest for $agg");
	    }
	    foreach my $ref (GeniXML::FindNodes("n:node",
450 451 452
					$manifest)->get_nodelist(),
			     GeniXML::FindNodesNS("n:vhost", $manifest,
					$GeniXML::EMULAB_NS)->get_nodelist()) {
453 454
		$nodecount++;
		
455 456 457
		my $client_id   = GeniXML::GetVirtualId($ref);
		my $manager_urn = GetManagerId($ref);
		my $urn          = GeniXML::GetSliverId($ref);
458 459 460

		# No sliver urn or a different aggregate.
		next
461 462 463
		    if (! (defined($urn) &&
			   defined($manager_urn) &&
			   $manager_urn eq $agg->aggregate_urn()));
464 465 466 467 468 469 470

		if ($node_id eq $client_id) {
		    $node = $ref;
		    $sliver_urn = $urn;
		    $aggregate = $agg;
		    last;
		}
471 472 473 474
	    }
	}
	if (!defined($sliver_urn)) {
	    fatal("Could not find node '$node_id' in manifest");
475
	}
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	#
	# So, we want Profile snapshot above (of a single node profile) and
	# Node snapshot in a single node profile to behave the same wrt the
	# image name, so look at the nodecount to see if need to append the
	# nodeid to the imagename. 
	#
	if (!defined($imagename)) {
	    $imagename = $profile->name();
	    if ($nodecount > 1) {
		$imagename .= "." . $node_id;
	    }
	}
    }
    #
    # Make sure a valid imagename. This a local test of course, but this
    # only works on IG aggregates anyway.
    #
    if (! TBcheck_dbslot($imagename, "images",
			 "imagename", TBDB_CHECKDBSLOT_ERROR)) {
495 496 497
	$errmsg   = "Invalid imagename: " . TBFieldErrorString() . "\n";
	$errcode  = GENIRESPONSE_ERROR;
	$exitcode = 1;
498
	goto uerror;
499 500
    }
    
501
    #
502 503 504
    # Instruct the remote cluster to copy the image back to its origin,
    # but we need to ask the IMS for uuid of the image that is running,
    # so we can tell the cluster, which then tells the origin cluster.
505 506
    # We also need to know what the new URN of the image will be, for
    # updating the profile. 
507 508 509 510 511 512
    #
    if (GetSiteVar("protogeni/use_imagetracker") &&	
	EmulabFeatures->FeatureEnabled("APT_UseImageTracker",
					   $this_user, $project)) {
	$usetracker = 1;

513 514 515 516
	#
	# When cloning, we use the URN returned by the cluster; it is
	# the origin of the new image.
	#
517 518
	if (!defined($cloneprofile)) {
	    my $rval = DoImageTrackerStuff($aggregate, $node, $project,
519
					   $imagename,
520 521 522 523 524 525 526
					   \$copyback_uuid, \$copyback_urn,
					   \$errmsg);
	    if ($rval) {
		if ($rval < 0) {
		    fatal($errmsg);
		}
		else {
527 528
		    $errcode  = GENIRESPONSE_ERROR;
		    $exitcode = 1;
529 530 531
		    goto uerror;
		}
	    }
532 533 534 535
	}
    }
    if (0) {
	fatal("$copyback_uuid, $copyback_urn\n");
536 537
    }

538
    #
539 540 541
    # We are not going to allow this if the instance is on a different
    # cluster then where the image was originally created, since otherwise
    # the image provenancewill look like spaghetti. 
542
    #
543
    if (defined($update_profile)) {
544 545
	my $diskref = GeniXML::GetDiskImage($node);
	if (defined($diskref)) {
546
	    my $authority = $aggregate->GetGeniAuthority();
547
	    my $image_url = GeniXML::GetText("url", $diskref);
548
	    if (defined($image_url) && !$usetracker) {
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
		require URI;

		# Get the hostname for the image URL.
		my $uri = URI->new($image_url);
		if (!defined($uri)) {
		    fatal("Could not parse $image_url");
		}
		my $image_host = $uri->host();

		# Get the hostname for the authority.
		$uri = URI->new($authority->url());
		if (!defined($uri)) {
		    fatal("Could not parse authority URL");
		}
		my $authority_host = $uri->host();

		# Compare domains.
		$image_host =~ s/^([^.]+\.)//;
		$authority_host =~ s/^([^.]+\.)//;
	
		if ($image_host ne $authority_host) {
		    $errmsg  = "Not allowed to take a snapshot on this cluster";
571 572
		    $errcode  = GENIRESPONSE_ERROR;
		    $exitcode = 1;
573
		    goto uerror;
574 575 576
		}
	    }
	}
577 578 579 580
	# Do this here to avoid output to logfile.
	$doversions =
	    EmulabFeatures->FeatureEnabled("APT_ProfileVersions",
					   $this_user, $project);
581 582
    }
    if ($slice->Lock()) {
583
	$errmsg  = "Experiment is busy, please try again later.";
584 585
	$errcode = GENIRESPONSE_BUSY;
	$exitcode = 1;
586
	goto uerror;
587 588
    }
    $needunlock = 1;
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    if (!$nosnapshot) {
	#
	# Grab the webtask, but only after we have it locked, since we are
	# going to modify it. This is a different webtask then the one we
	# got on the command line. The command line webtask is for reporting
	# he results of the command, while the instance webtask is used to
	# report ongoing status of the imaging operation (to the web UI).
	#
	$swebtask = $instance->webtask();
	# We reuse this so clear it.
	$swebtask->Reset();
	$swebtask->AutoStore(1);
	# These are for the web server (imaging status).
	$swebtask->aggregate_urn($aggregate->aggregate_urn());
	$swebtask->client_id($node_id);
	$swebtask->operation($operation) if (defined($operation));
	$swebtask->imagename($imagename);
	
	$instance->SetStatus("imaging");
	$aggregate->SetStatus("imaging");
    }
611

612 613 614
    # Shorten default timeout
    Genixmlrpc->SetTimeout(60);
    
615 616
    #
    # This returns pretty fast, and then the imaging takes place in
617
    # the background at the aggregate. 
618
    #
619
    my $response =
620
	$aggregate->CreateImage($sliver_urn, $imagename,
621
				$update_prepare, $copyback_uuid,
Leigh Stoller's avatar
Leigh Stoller committed
622 623
				undef, $nosnapshot, $mustnotexist, $wholedisk,
				$description);
624
    if ($response->code() != GENIRESPONSE_SUCCESS) {
625 626 627 628
	$errcode = $response->code();
	($exitcode,$errmsg) = ResponseErrorMessage($aggregate, $response);
	# Important to tell web user about these.
	if ($response->code() == GENIRESPONSE_NOSPACE ||
Leigh Stoller's avatar
Leigh Stoller committed
629
	    $response->code() == GENIRESPONSE_FORBIDDEN || 
630 631 632
	    $response->code() == GENIRESPONSE_ALREADYEXISTS) {
	    $exitcode = 1;
	}
633 634 635 636
	if (!$nosnapshot) {
	    $instance->SetStatus($old_status);
	    $aggregate->SetStatus($old_status);
	}
637
	goto uerror;
638 639 640
    }
    my ($image_urn, $image_url,
	$version_urn, $version_url) = @{ $response->value() };
641 642 643 644
    #
    # For version zero, kill the version number.
    #
    if (!defined($version_urn) || $version_urn =~ /:0$/) {
645 646 647
	$version_urn = $image_urn;
	$version_url = $image_url
    }
648
    my $image_name;
649

650 651 652 653 654 655
    # When using the image server we use URNs. 
    if ($usetracker) {
	# DoImageTrackerStuff determined that we use whatever the cluster
	# tells us, cause it is the home of the image.
	if (!defined($copyback_urn)) {
	    $image_name = $version_urn;
Leigh Stoller's avatar
Leigh Stoller committed
656 657
	}
	else {
658
	    $image_name = $copyback_urn;
Leigh Stoller's avatar
Leigh Stoller committed
659
	}
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    }
    elsif ($aggregate->OnLocalCluster()) {
	$image_name = $version_urn;
    }
    else {
	$image_name = $version_url;
    }
    
    if ($nosnapshot) {
	if (defined($webtask)) {
	    $webtask->image_urn($version_urn);
	    $webtask->image_url($version_url);
	    $webtask->image_name($image_name);
	}
	print "$version_urn,$version_url,$image_name\n";
	$slice->UnLock();
	exit(0);
    }
    if (defined($swebtask)) {
	$swebtask->image_urn($version_urn);
	$swebtask->image_url($version_url);
681
	$swebtask->image_name($image_name);
682

Leigh Stoller's avatar
Leigh Stoller committed
683 684 685
	# We tell the web interface that the image has to be copied
	# back,
	if (defined($copyback_uuid)) {
686
	    $swebtask->copyback_uuid($copyback_uuid);
Leigh Stoller's avatar
Leigh Stoller committed
687
	}
688
    }
689
    print "$version_urn,$version_url,$image_name\n";
690 691 692 693 694

    #
    # Exit and leave child to poll.
    #
    if (! $debug) {
695 696 697 698
        $logfile = TBMakeLogname("snapshot");
	
	if (my $childpid = TBBackGround($logfile)) {
	    # Parent exits normally, web interface watches.
699 700 701 702 703
	    exit(0);
	}
	# Let parent exit;
	sleep(2);
    }
704 705
    # Bind the process id. This is important when the caller is
    # manage_profile, doing a clone.
706 707
    $webtask->SetProcessID($PID)
	if (defined($webtask));
708 709 710 711

    #
    # Poll for a reasonable amount of time.
    #
712
    my $seconds  = 1500;
713
    my $interval = 15;
714
    my $ready    = 0;
715
    my $sliver_ready = 0;
716
    my $failed   = 0;
717

718
    while ($seconds > 0) {
719 720
	sleep($interval);
	$seconds -= $interval;
721
    
722
	my $response = $aggregate->SliceStatus();
723
	if ($response->code() != GENIRESPONSE_SUCCESS &&
724
	    $response->code() != GENIRESPONSE_NETWORK_ERROR &&
725
	    $response->code() != GENIRESPONSE_SERVER_UNAVAILABLE &&
726 727 728 729 730 731
	    $response->code() != GENIRESPONSE_BUSY) {
	    $errmsg = "Sliverstatus failed: ". $response->output() . "\n";
	    $failed = 1;
	    last;
	}
	next
732
	    if ($response->code() == GENIRESPONSE_BUSY ||
733
		$response->code() == GENIRESPONSE_SERVER_UNAVAILABLE ||
734
		$response->code() == GENIRESPONSE_NETWORK_ERROR);
735

736
	my $blob = $response->value();
737
	# This is the per-aggregate status, we always set this for web UI.
738
	$aggregate->UpdateSliverStatusAll($blob->{'details'});
739
	
740 741 742 743
	if ($blob->{'status'} eq "failed") {
	    $failed = 1;
	    last;
	}
744 745 746 747
	elsif ($blob->{'status'} eq "ready") {
	    $sliver_ready = 1;
	}
	
748 749 750
	#
	# We are watching for the image status to report ready or failed.
	#
751
	$response = $aggregate->ImageInfo($image_urn);
752
	if ($response->code() != GENIRESPONSE_SUCCESS &&
753
	    $response->code() != GENIRESPONSE_NETWORK_ERROR &&
754
	    $response->code() != GENIRESPONSE_SERVER_UNAVAILABLE &&
755 756 757 758 759 760
	    $response->code() != GENIRESPONSE_BUSY) {
	    $errmsg = "Imageinfo failed: ". $response->output() . "\n";
	    $failed = 1;
	    last;
	}
	next
761
	    if ($response->code() == GENIRESPONSE_BUSY ||
762
		$response->code() == GENIRESPONSE_SERVER_UNAVAILABLE ||
763
		$response->code() == GENIRESPONSE_NETWORK_ERROR);
764

765
	my $imageblob = $response->value();
766
	if (defined($swebtask)) {
767 768 769 770 771 772 773 774 775
	    my %blobcopy = %{ $imageblob };

	    #
	    # If the image is ready, but needs to be copied back to
	    # its origin, hold of ready till later. We will wait for
	    # the copyback to finish, see below.
	    #
	    if ($imageblob->{'status'} eq "ready" && defined($copyback_uuid)) {
		$blobcopy{'status'} = "copying";
Leigh Stoller's avatar
Leigh Stoller committed
776
	    }
777 778
	    # This is also being updated by the event system.
	    $instance->UpdateImageStatus(\%blobcopy);
779
	}
780
	if ($imageblob->{'status'} eq "ready") {
781 782 783
	    $ready = 1;
	    last;
	}
784
	elsif ($imageblob->{'status'} eq "failed") {
785 786 787
	    $failed = 1;
	    last;
	}
788
    }
789
    # Cause of image status events.
790 791
    $swebtask->Refresh()
	if (defined($swebtask));
792
    
793 794 795 796
    if ($failed) {
	$errmsg = "Imaging failed"
	    if (!defined($errmsg));
	goto bad;
797
    }
798 799
    elsif (!$ready) {
	$errmsg  = "Imaging timed out";
800
	$errcode = -2;
801 802
	goto bad;
    }
Leigh Stoller's avatar
Leigh Stoller committed
803 804 805 806 807 808
    #
    # We cannot change a geni-lib script profile, so no need to do this.
    # But we can change a portal converted profile.
    #
    elsif (defined($update_profile) &&
	   (!defined($profile->script()) || $profile->portal_converted())) {
809
	#
Leigh Stoller's avatar
Leigh Stoller committed
810 811
	# New image. Might not have changed if the cluster is not doing
	# image versions.
812
	#
813
	# DoImageTrackerStuff determined that we use whatever the cluster
814
	# tells us, cause it is the home of the image.
Leigh Stoller's avatar
Leigh Stoller committed
815 816
	#
	my $newimage = $version_urn
817
	    if ($usetracker && !defined($copyback_urn));
Leigh Stoller's avatar
Leigh Stoller committed
818 819 820
	# And fall back to url
	$newimage = $version_url
	    if (!defined($newimage));
821

Leigh Stoller's avatar
Leigh Stoller committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	#
	# Okay, is the rspec going to change. This is kinda annoying to
	# figure out, so lets just run the code in impotent mode and have
	# it tell us the number of nodes that will get updated.
	#
	my $changed = 
	    $profile->UpdateDiskImage($node_id, $newimage,
				      ($update_profile eq "all" ? 1 : 0), 1);

	if ($changed > 0) {
	    #
	    # Create a new version of the profile and update the rspec
	    # to reflect the new image version. 
	    #
	    if ($doversions) {
		$profile = $profile->NewVersion($this_user);
		if (!defined($profile)) {
		    print STDERR "Could not create new profile version\n";
		    $webtask->Exited(70)
			if (defined($webtask));
		    exit(1);
		}
	    }
	    $profile->UpdateDiskImage($node_id, $newimage,
				      ($update_profile eq "all" ? 1 : 0), 0);
	    $profile->InsertImageRecords();
	    #
	    # For a portal converted profile, we need to regen the script.
	    #
	    if ($profile->portal_converted()) {
		if ($profile->Convert2Genilib() != 0) {
		    fatal("Could not convert rspec to geni-lib");
		}
	    }
	}
857
    }
858
    $instance->SetStatus("ready");
859
    $aggregate->SetStatus("ready");
Leigh Stoller's avatar
Leigh Stoller committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    
    #
    # If there is a copyback_uuid, we want to wait for that to finish.
    #
    if (defined($copyback_uuid)) {
	#
	# We know the copyback is done when the IMS has the info.
	#
	my $copied  = 0;
	$seconds  = 1000;

	while ($seconds > 0) {
	    sleep($interval);
	    $seconds -= $interval;

	    #
	    # It would clearly be more more efficient to just look in
	    # the IMS database. 
	    #
	    Genixmlrpc->SetContext(APT_Geni::GeniContext());
	    my $blob = GeniImage::GetImageData($copyback_urn, \$errmsg);
	    Genixmlrpc->SetContext(undef);
	    # We get back undefined if the image is not posted yet.
	    if (defined($blob)) {
		$copied = 1;
		last;
	    }
	    sleep($interval);
	}
	# Tell the web interface. 
	if (!$copied) {
	    $errmsg  = "Failed to copy image back to its origin cluster";
	    $errcode = 1;
	    goto bad;
	}
895 896
	elsif (defined($swebtask)) {
	    $swebtask->image_status("ready");
Leigh Stoller's avatar
Leigh Stoller committed
897 898
	}
    }
899
    $swebtask->Exited(0);
900 901 902
    $webtask->Exited(0)
	if (defined($webtask));
    $slice->UnLock();
903
    
904 905 906 907 908 909 910
    if (defined($logfile)) {
	if (-s $logfile) {
	    SENDMAIL($TBOPS,
		     "Instance Snapshot Complete",
		     "Finished taking snapshot of $instance.\n",
		     $TBOPS, undef, $logfile);
	}
911 912
	unlink($logfile);
    }
913 914 915 916 917
    if (!$sliver_ready) {
	#
	# Image is ready, but sliver is not. Start a monitor so that
	# web interface is updated.
	#
Leigh Stoller's avatar
Leigh Stoller committed
918
	StartMonitorInternal();
919
    }
920
    exit(0);
921
  bad:
922
    if (!$sliver_ready) {
923
	#
924 925
	# Image is ready, but sliver is not. Start a monitor so that
	# web interface is updated.
926
	#
Leigh Stoller's avatar
Leigh Stoller committed
927
	StartMonitorInternal();
928
    }
929
    $instance->SetStatus("ready");
930
    $aggregate->SetStatus("ready");
931
    if (defined($logfile)) {
932 933 934 935 936
	SENDMAIL($TBOPS,
		 "Snapshot failed",
		 "Error taking snapshot of $instance:\n\n".
		 "$errmsg\n",
		 $TBOPS, undef, $logfile);
937 938
	unlink($logfile);
    }
939 940 941 942 943 944
  uerror:
    print STDERR "$errmsg\n";
    if (defined($errmsg) && defined($webtask)) {
	$webtask->Exited($errcode);
	$webtask->output($errmsg);
    }
945
    # For display in the imaging modal.
Leigh Stoller's avatar
Leigh Stoller committed
946 947 948 949
    if (defined($errmsg) && defined($swebtask)) {
	$swebtask->Exited($errcode);
	$swebtask->output($errmsg);
    }
950 951 952
    $slice->UnLock()
	if ($needunlock);

Leigh Stoller's avatar
Leigh Stoller committed
953
    exit($exitcode);
954
}
955

956
sub DoImageTrackerStuff($$$$$$$)
957
{
958
    my ($aggregate, $node, $project, $newname, $puuid, $purn, $perrmsg) = @_;
959 960 961 962 963 964 965 966 967 968 969 970 971 972
    my $node_id = GeniXML::GetVirtualId($node);
    my $errmsg;

    #
    # If we do not have a diskinfo section, we will use the URN we get back
    # from the cluster (it is a snapshot of the default image).
    #
    my $diskinfo = GeniXML::GetDiskImage($node);
    return 0
	if (!defined($diskinfo));

    #
    # This one needs more thought, it might be a URL.
    #
973 974 975 976 977
    my $image_token = GeniXML::GetText("name", $diskinfo);
    if (!defined($image_token)) {
	$image_token = GeniXML::GetText("url", $diskinfo);
	return 0
	    if (!defined($image_token));
978
    }
979 980 981
    if (GeniHRN::IsValid($image_token)) {
	my ($auth,$ospid) = GeniHRN::ParseImage($image_token);
	if (!defined($ospid)) {
982
	    $$perrmsg = "Invalid image urn: $image_token";
983 984 985 986
	    return 1;
	}
    }
    
987
    Genixmlrpc->SetContext(APT_Geni::GeniContext());
988
    my $blob = GeniImage::GetImageData($image_token, \$errmsg);
989 990 991
    Genixmlrpc->SetContext(undef);
    
    if (!defined($blob)) {
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	if (GeniHRN::IsValid($image_token)) {
	    #
	    # See if this is for a system image (emulab-ops). If it is,
	    # and the domain is not the MS, then retry with a MS URN.
	    #
	    # This is sorta temporary; at some point there will not be any
	    # profiles using the URNs that are not in the image tracker.
	    # Of course a user is free to set the URN to anything the want,
	    # which is why I expect this code to be here for a while. 
	    #
	    my $urn;
	    my $hrn = GeniHRN->new($image_token);
	    my ($auth,$ospid,$os,$vers) = $hrn->ParseImage();
	    if ($ospid eq TBOPSPID() && $auth ne "emulab.net") {
		$urn = GeniHRN::GenerateImage("emulab.net",
						 TBOPSPID(), $os, $vers);
1008 1009 1010
		Genixmlrpc->SetContext(APT_Geni::GeniContext());
		$blob = GeniImage::GetImageData($urn, \$errmsg);
		Genixmlrpc->SetContext(undef);
1011 1012 1013
	    }
	}
	if (!defined($blob)) {
1014 1015 1016
	    $$perrmsg = "Could not get info from the image server for ".
		"$image_token:\n" . $errmsg;
	    return 1;
1017
	}
1018 1019
    }
    #
1020
    # System Image? We use the URN we get back from CreateImage().
1021 1022 1023
    # The cluster will be the origin for the new image.
    #
    return 0
1024 1025 1026 1027
	if (exists($blob->{'issystem'}) && $blob->{'issystem'});
    # Image aliases are also considered system images.
    return 0
	if (exists($blob->{'isimagealias'}) && $blob->{'isimagealias'});
1028

1029
    my $image_urn     = $blob->{'urn'};
1030 1031 1032 1033
    my $copyback_uuid = $blob->{'version_uuid'};
    my $copyback_urn  = $image_urn;

    my $hrn = GeniHRN->Parse($image_urn);
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    if (!defined($hrn)) {
	$$perrmsg = "Could not parse $image_urn\n";
	return -1;
    }
    if (!$hrn->IsImage()) {
	$$perrmsg = "$image_urn is not an image URN\n";
	return -1;
    }
    my $projhrn = GeniHRN->Parse($blob->{'project_urn'});
    if (!defined($projhrn)) {
	$$perrmsg = "Could not parse " . $blob->{'project_urn'} . "\n";
	return -1;
    }

    #
    # Whenever we cross projects, we expect the cluster to create
    # a new image. We use the URN that is returned.
    #
    if (lc($projhrn->subauth()) ne lc($project->pid())) {
	# We use the URN we get back from CreateSliver().
	return 0;
    }
1056 1057 1058 1059

    #
    # What happens if the user is doing a snapshot on the cluster where
    # the image lives? The copyback (import) makes no sense in that case,
1060
    # We can use the URN the cluster returns.
1061
    #
1062
    if (lc($hrn->domain()) eq lc($aggregate->domain())) {
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	return 0;
    }

    #
    # Different cluster
    #
    # Is this node currently running a different image then what
    # the new name will be? Is so, then we expect the cluster to
    # start a new image and we use the URN it returns.
    #
    if ($newname ne $hrn->osname()) {
1074
	return 0;
1075 1076 1077 1078 1079
    }

    #
    # If we are going to update the profile, we need to know what to
    # change the image urn to, and that depends on what version the
1080 1081 1082 1083 1084
    # image is currently at, AT THE ORIGIN CLUSTER. The urn we get back
    # from the snapshotting cluster is not what we care about, we need
    # a urn for the origin cluster. But that depends on what version the
    # origin cluster is at (the highest numbered version). But if we are
    # doing a snapshot of an earlier version, we cannot generate the
1085
    # version here, we have to ask what it will be. 
1086 1087
    #
    if ($blob->{'isversioned'}) {
1088
	$copyback_urn = GeniHRN::GenerateImage($hrn->authority(),
1089 1090
					       $hrn->ospid(),
					       $hrn->osname(),
1091
					       $blob->{'maxversion'} + 1);
1092 1093 1094 1095 1096 1097
    }
    $$puuid = $copyback_uuid;
    $$purn  = $copyback_urn;
    return 0;
}

1098 1099 1100 1101 1102
#
# Ask the console URL for a node in an instance.
#
sub DoConsole()
{
1103
    my $errmsg;
1104
    usage()
1105 1106
	if (!@ARGV);
    my $node_id = shift(@ARGV);
1107

1108 1109 1110 1111 1112
    #
    # Sanity check to make sure the node is really in the rspec, since
    # we need its sliver urn to ask for the console url.
    #
    my $sliver_urn;
1113
    my $sliver;
1114
    foreach my $obj ($instance->AggregateList()) {
1115 1116 1117 1118
	my $manifest = GeniXML::Parse($obj->manifest());
	if (! defined($manifest)) {
	    fatal("Could not parse manifest for $obj");
	}
1119 1120 1121
	my @nodes = (GeniXML::FindNodes("n:node", $manifest)->get_nodelist(),
		     GeniXML::FindNodesNS("n:vhost", $manifest,
					  $GeniXML::EMULAB_NS)->get_nodelist());
1122
	foreach my $node (@nodes) {
1123 1124 1125
	    my $client_id   = GeniXML::GetVirtualId($node);
	    my $urn         = GeniXML::GetSliverId($node);
	    my $manager_urn = GetManagerId($node);
1126 1127 1128

	    # No sliver urn or a different aggregate.
	    next
1129 1130 1131
		if (! (defined($urn) &&
		       defined($manager_urn) &&
		       $manager_urn eq $obj->aggregate_urn()));
1132 1133

	    if ($node_id eq $client_id) {
1134
		$sliver_urn = $urn;
1135 1136
		$sliver = $obj;
	    }
1137 1138 1139 1140 1141
	}
    }
    if (!defined($sliver_urn)) {
	fatal("Could not find node '$node_id' in manifest");
    }
1142 1143 1144 1145 1146 1147 1148
    if ($sliver->GetAptAggregate()->CheckStatus(\$errmsg)) {
	print STDERR "$errmsg\n";
	if (defined($webtask)) {
	    $webtask->output($errmsg);
	    $webtask->Exited(GENIRESPONSE_SERVER_UNAVAILABLE);
	}
	exit(1);
1149
    }
1150 1151
    my $response = $sliver->ConsoleInfo($sliver_urn);

1152
    if ($response->code() == GENIRESPONSE_RPCERROR) {
1153
	print STDERR "RPC Error: " . $response->error() . "\n";
1154
	if (defined($webtask)) {
1155
	    $webtask->output($response->error());
1156 1157
	    $webtask->Exited($response->code());
	}
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	exit(-1);
    }
    if ($response->code() == GENIRESPONSE_NETWORK_ERROR ||
	$response->code() == GENIRESPONSE_SERVER_UNAVAILABLE) {
	print STDERR "RPC Error: " . $response->error() . "\n";
	if (defined($webtask)) {
	    $webtask->output($response->error());
	    $webtask->Exited($response->code());
	}
	exit(1);
1168
    }
1169 1170 1171 1172
    if ($response->code() == GENIRESPONSE_UNAVAILABLE) {
	print STDERR "Server says there is no console for $node_id\n";
	if (defined($webtask)) {
	    $webtask->output("Sorry, $node_id does not have a console line");
1173 1174
	    $webtask->Exited($response->code());
	}
1175
	exit(1);
1176 1177 1178 1179 1180
    }
    if ($response->code() == GENIRESPONSE_SEARCHFAILED) {
	print STDERR "Server says $node_id has been deallocated\n";
	if (defined($webtask)) {
	    $webtask->output("Sorry, $node_id has been deallocated");
1181 1182
	    $webtask->Exited($response->code());
	}
1183
	exit(1);
1184
    }
1185 1186 1187 1188 1189 1190
    if ($response->code() == GENIRESPONSE_FORBIDDEN) {
	print STDERR "Server says access to $node_id console is forbidden\n";
	if (defined($webtask)) {
	    $webtask->output("Sorry, access to $node_id console is forbidden");
	    $webtask->Exited($response->code());
	}
1191
	exit(1);
1192
    }
1193 1194 1195 1196 1197
    if ($response->code() == XMLRPC_NO_SUCH_METHOD) {
	print STDERR "Server does not support console access\n";
	if (defined($webtask)) {
	    $webtask->output("Sorry, server does not support console access");
	    $webtask->Exited(GENIRESPONSE_NOT_IMPLEMENTED);
1198
	}
1199 1200 1201 1202
	exit(1);
    }
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	fatal($response->error());
1203 1204 1205
    }
    my $url;
    my $pswd;
1206
    my $logurl;
1207 1208 1209 1210 1211
	
    if (ref($response->value())) {
	$url  = $response->value()->{'url'};
	$pswd = $response->value()->{'password'}
	    if (exists($response->value()->{'password'}));
1212 1213 1214 1215
	$logurl = $response->value()->{'logurl'}
	    if (exists($response->value()->{'logurl'}));

	print Dumper($response->value());
1216 1217 1218
    }
    else {
	$url = $response->value();
1219
    }
1220
    if (defined($webtask)) {
1221
	if ($response->code()) {
1222
	    $webtask->output($response->error());
1223 1224 1225 1226
	}
	else {
	    $webtask->url($url);
	    $webtask->password($pswd) if (defined($pswd));
1227
	    $webtask->logurl($logurl) if (defined($logurl));
1228
	}
1229
	$webtask->Exited($response->code());
1230
	exit($response->code());
1231 1232 1233
    }
    # For command line operation too.
    if ($response->code()) {
1234
	fatal($response->error());
1235
    }
1236 1237
    print $url . "\n";
    print $pswd . "\n" if (defined($pswd));
1238
    print $logurl . "\n" if (defined($logurl));
1239 1240
    exit(0);
}
1241

1242 1243 1244 1245 1246 1247
#
# Terminate
#
sub DoTerminate()
{
    my $errmsg;
1248 1249
    my $errcode;
    my $exitcode = 1;
1250
    my $logfile;
1251 1252
    my $takelock = 0;
    my $expired  = $RECORDHISTORY_TERMINATED;
1253

1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    my $optlist = "eL";
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"e"})) {
	$expired = $RECORDHISTORY_EXPIRED;
    }
    if (defined($options{"L"})) {
	$takelock = 1;
1264
    }
1265 1266 1267
    
    my $slice = $instance->GetGeniSlice();
    if (!defined($slice)) {
1268 1269 1270 1271
	#
	# No slice (typically) means we never got far enough to the
	# get the sliver created on the backend cluster.
	#
1272 1273 1274 1275 1276 1277
	goto killit;
    }
    #
    # Lock the slice in case it is doing something else, like taking
    # a disk image.
    #
1278
    # When told to take the lock, we take it and go.
1279 1280 1281 1282 1283
    #
    if ($takelock) {
	$slice->TakeLock();
    }
    elsif ($slice->Lock()) {
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
	#
	# A special case is if the slice is provisioning. This means the
	# user is giving up on it, and we want to tell the aggregate to
	# kill it. Not all aggregates are going to allow this, so need
	# to be able to deal with that.
	#
	if ($instance->status() ne "provisioned") {
	    fatal("Slice is busy, cannot lock it");
	}
	if (!$instance->canceled()) {
	    print "Marking instance canceled\n";
	    $instance->MarkCanceled();
	}
	sleep(1);
	# We have an obvious race here since we do not have the lock.
	exit(0);
1300 1301 1302 1303
    }
    my $old_status = $instance->status();
    $instance->SetStatus("terminating");

1304 1305 1306 1307 1308 1309 1310 1311
    #
    # If deferred, then no reason to save this in the history, it
    # was never started.
    #
    if ($old_status eq "deferred") {
	goto killit;
    }

1312 1313 1314
    #
    # Exit and let caller poll for status.
    #
1315
    if (!$debug) {
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
        $logfile = TBMakeLogname("terminate");
	
	if (my $childpid = TBBackGround($logfile)) {
	    my $status = 0;
	    #
	    # Wait a couple of seconds to see if there is going to be an
	    # immediate error. Then return and let it continue to run. This
	    # allows the web server to see quick errors. Later errors will
	    # have to be emailed. 
	    #
1326
	    sleep(5);
1327 1328 1329 1330 1331 1332 1333
	    my $foo = waitpid($childpid, &WNOHANG);
	    if ($foo) {
		$status = $? >> 8;
	    }
	    exit($status);
	}
    }
1334

1335
    # Skip terminated aggregates, since we retry later on failure.
1336
    # Also skip deferred aggregates, these were never setup.
1337 1338 1339
    my @agglist;
    foreach my $agg ($instance->AggregateList()) {
	push(@agglist, $agg)
1340 1341
	    if ($agg->status() ne "terminated" &&
		$agg->status() ne "deferred");
1342 1343
    }
    my $response;
1344

1345 1346 1347 1348 1349
    $errcode = CallMethodOnAggregates("Terminate", 10, \$response, @agglist);
				      
    if ($errcode) {
	$exitcode = -1;
	$errmsg   = $response;
1350 1351
	goto bad;
    }
1352

1353
    #
1354
    # Look at all the responses, update the status. 
1355
    #
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
    my @responses = @{$response};
    foreach my $agg (@agglist) {
	my $response = shift(@responses);
	my $code = $response->code();

	# SEARCHFAILED is success too
	if ($code == GENIRESPONSE_SUCCESS ||
	    $code == GENIRESPONSE_SEARCHFAILED) {
	    $agg->SetStatus("terminated");
	    next;
	}
	($exitcode,$errmsg) = ResponseErrorMessage($agg, $response);
	
	#
	# These will typically clear up at some point so abort
	# sending any email.
	#
	if ($code == GENIRESPONSE_BUSY ||
	    $code == GENIRESPONSE_SERVER_UNAVAILABLE ||
	    $code == GENIRESPONSE_NETWORK_ERROR) {
	    if (defined($logfile)) {
		unlink($logfile);
		$logfile = undef;
	    }
	    $errcode  = $code;
	    $exitcode = 1;
	}
	else {
	    # We are going to send mail below, so exit with positive status
	    # so web interface does not send email.
	    $errcode  = $code;
	    $exitcode = 1;
1388 1389
	}
    }
1390 1391 1392 1393 1394
    # Will have to try again later.
    goto bad
	if ($errcode);

    $instance->SetStatus("terminated");
1395
    $slice->Delete();
1396
    $instance->RecordHistory($expired);
1397 1398 1399 1400 1401
  killit:
    $instance->Delete();
    unlink($logfile)
	if (defined($logfile));
    exit(0);
1402
    
1403 1404 1405
  bad:
    print STDERR $errmsg . "\n";
    $instance->SetStatus($old_status);
1406
    if (defined($logfile)) {
1407 1408
	my $instance_name = $instance->name();
	my $slice_uuid    = $slice->uuid();
1409 1410
	SENDMAIL($TBOPS,
		 "Unable to terminate instance $uuid",
1411 1412
		 "Name: $instance_name\n".
		 "Slice: $slice_uuid\n\n".
1413
		 "$errmsg\n",
1414 1415
		 $TBOPS, undef, $logfile)
	    if (!$silent);