manage_instance.in 18.9 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2015 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 close);
32 33 34 35 36 37

#
# Back-end script to manage APT profiles.
#
sub usage()
{
38 39 40 41 42
    print("Usage: manage_instance snapshot instance [imagename node_id]\n");
    print("Usage: manage_instance consoleurl instance node\n");
    print("Usage: manage_instance extend instance seconds\n");
    print("Usage: manage_instance terminate instance\n");
    print("Usage: manage_instance refresh instance\n");
43 44
    exit(-1);
}
45
my $optlist     = "dt:";
46 47
my $debug       = 0;
my $webtask_id;
48
my $webtask;
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

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $QUICKVM     = "$TB/sbin/protogeni/quickvm";

#
# 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;
75
use libtestbed;
76 77 78 79
use User;
use Project;
use APT_Profile;
use APT_Instance;
80
use APT_Geni;
81 82
use GeniXML;
use GeniHRN;
83 84 85
use Genixmlrpc;
use GeniResponse;
use GeniSlice;
86
use WebTask;
87
use EmulabFeatures;
88 89 90 91

# Protos
sub fatal($);
sub DoSnapshot();
92
sub DoConsole();
93 94 95
sub DoTerminate();
sub DoExtend();
sub DoRefresh();
96 97 98 99 100 101 102 103 104 105 106 107

#
# 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"};
}
108 109 110 111
if (defined($options{"d"})) {
    $debug++;
}
if (@ARGV < 2) {
112 113
    usage();
}
114
my $action   = shift(@ARGV);
115 116
my $uuid     = shift(@ARGV);
my $instance = APT_Instance->Lookup($uuid);
117 118 119
if (!defined($instance)) {
    $instance = APT_Instance->LookupBySlice($uuid);
}
120 121 122 123
if (!defined($instance)) {
    fatal("No such instance $uuid");
}

124
if ($action eq "snapshot") {
125 126
    DoSnapshot();
}
127 128 129 130
if ($action eq "extend") {
    DoExtend();
}
elsif ($action eq "consoleurl") {
131 132
    DoConsole()
}
133 134 135 136 137 138 139 140 141
elsif ($action eq "terminate") {
    DoTerminate()
}
elsif ($action eq "refresh") {
    DoRefresh()
}
else {
    usage();
}
142 143 144 145 146 147 148
exit(0);

#
# Take a snapshot. Implies a single node instance, for now.
#
sub DoSnapshot()
{
149 150 151 152 153 154 155 156 157 158 159 160 161
    my $errmsg;
    my $logfile;
    my $errcode        = 1;
    my $needunlock     = 0;
    my $old_status     = $instance->status();
    
    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");
    }
162 163
    my $authority = $instance->GetGeniAuthority();
    
Leigh B Stoller's avatar
Leigh B Stoller committed
164 165 166 167 168 169 170 171
    # The web interface (and in the future the xmlrpc interface) sets this.
    my $this_user = User->ImpliedUser();
    if (! defined($this_user)) {
	$this_user = User->ThisUser();
	if (!defined($this_user)) {
	    fatal("You ($UID) do not exist!");
	}
    }
172
    
173 174
    #
    # If we get an imagename on the command line, the caller is
175 176 177
    # saying it is responsible (clone). If we do not get one, we
    # create the name and update the underlying profile with the new
    # image urn.
178
    #
179
    my $project;
180
    my $imagename;
181 182
    my $node_id;
    my $sliver_urn;
183 184
    my $update_profile = 0;
    
185
    my $profile = APT_Profile->Lookup($instance->profile_id());
186 187 188 189 190
    if (!defined($profile)) {
	fatal("Could not lookup profile for instance");
    }
    if (@ARGV) {
	$imagename = shift(@ARGV);
191 192 193
	if (@ARGV) {
	    $node_id = shift(@ARGV);
	}
194 195 196 197
    }
    else {
	$imagename = $profile->name();
	$update_profile = 1;
198 199 200 201
	$project = Project->Lookup($profile->pid_idx());
	if (!defined($project)) {
	    fatal("Could not lookup project for profile");
	}
202 203 204 205 206 207 208 209 210
    }
    
    #
    # Sanity check to make sure there is just one node. 
    #
    my $manifest = GeniXML::Parse($instance->manifest());
    if (! defined($manifest)) {
	fatal("Could not parse manifest");
    }
211
    my $node;
212
    my @nodes = GeniXML::FindNodes("n:node", $manifest)->get_nodelist();
213 214 215 216
    if (!defined($node_id)) {
	if (@nodes != 1) {
	    fatal("Too many nodes (> 1) to snapshot");
	}
217
	($node)     = @nodes;
218
	$sliver_urn = GeniXML::GetSliverId($node);
219
    }
220
    else {
221
	foreach $node (@nodes) {
222 223 224 225 226 227 228 229
	    my $client_id = GeniXML::GetVirtualId($node);
	    if ($node_id eq $client_id) {
		$sliver_urn = GeniXML::GetSliverId($node);
		last;
	    }
	}
	if (!defined($sliver_urn)) {
	    fatal("Could not find node '$node_id' in manifest");
230 231 232 233 234 235
	}
    }
    #
    # Create the webtask object.
    #
    if (defined($webtask_id)) {
236 237 238
	$webtask = WebTask->LookupOrCreate($instance->uuid(), $webtask_id);
	# Convenient.
	$webtask->AutoStore(1);
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
    #
    # Really, a snapshot and not a clone. We are not going to allow this
    # if the instance in on a different cluster then where the image was
    # originally created, since otherwise the image provenance will look
    # like spaghetti.
    #
    if ($update_profile) {
	my $diskref = GeniXML::GetDiskImage($node);
	if (defined($diskref)) {
	    my $image_url = GeniXML::GetText("url", $diskref);
	    if (defined($image_url)) {
		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";
		    $errcode = 1;
		    goto bad;
		}
	    }
	}
    }
    if ($slice->Lock()) {
	fatal("Slice is busy, cannot lock it");
    }
    $needunlock = 1;
    
285 286 287 288 289 290 291 292 293 294 295 296 297 298
    my $geniuser  = $instance->GetGeniUser();
    my $context   = APT_Geni::GeniContext();
    if (! (defined($geniuser) && defined($authority) &&
	   defined($slice) && defined($context))) {
	$errmsg = "Internal error getting instance objects";
	goto bad;
    }
    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    if (! (defined($speaksfor_credential) && defined($slice_credential))) {
	$errmsg = "Internal error getting credentials";
	goto bad;
    }
    $instance->SetStatus("imaging");
299

300 301 302 303
    my $args = {
	"slice_urn"   => $slice->urn(),
	"imagename"   => $imagename,
	"sliver_urn"  => $sliver_urn,
304
	"global"      => 1,
305 306 307
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString()],
    };
308 309
    #
    # This returns pretty fast, and then the imaging takes place in
310
    # the background at the aggregate. 
311
    #
312 313 314 315 316 317
    my $response = Genixmlrpc::CallMethod($authority->url(),
					  $context, "CreateImage", $args);
    if (!defined($response)) {
	$errmsg = "Internal error creating image";
	$instance->SetStatus($old_status);
	goto bad;
318
    }
319 320 321 322 323 324 325 326 327 328 329 330 331 332
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	$errmsg = "Could not create image: " . $response->output() . "\n";
	$instance->SetStatus($old_status);
	goto bad;
    }
    my ($image_urn, $image_url,
	$version_urn, $version_url) = @{ $response->value() };
    if (!defined($version_urn)) {
	$version_urn = $image_urn;
	$version_url = $image_url
    }
    if (defined($webtask)) {
	$webtask->image_urn($version_urn);
	$webtask->image_url($version_url);
333 334
    }
    else {
335
	print "$image_urn,$image_url\n";
336 337 338 339 340 341
    }

    #
    # Exit and leave child to poll.
    #
    if (! $debug) {
342 343 344 345
        $logfile = TBMakeLogname("snapshot");
	
	if (my $childpid = TBBackGround($logfile)) {
	    # Parent exits normally, web interface watches.
346 347 348 349 350
	    exit(0);
	}
	# Let parent exit;
	sleep(2);
    }
351 352 353 354 355 356 357 358
    # Bind the process id. This is important when the caller is
    # manage_profile, doing a clone.
    $webtask->SetProcessID($PID)
	if (defined($webtask));

    #
    # Poll for a reasonable amount of time.
    #
359 360
    my $seconds  = 1200;
    my $interval = 5;
361 362 363 364 365 366 367
    my $ready    = 0;
    my $failed   = 0;
    # do not want this in the args for calls below.
    delete($args->{'sliver_urn'});
    delete($args->{'imagename'});
    # But we need this;
    $args->{'image_urn'} = $image_urn;
368

369
    while ($seconds > 0) {
370 371
	sleep($interval);
	$seconds -= $interval;
372 373 374 375 376 377 378 379 380 381 382 383 384
    
	my $response =
	    Genixmlrpc::CallMethod($authority->url(),
				   $context, "SliverStatus", $args);

	if ($response->code() != GENIRESPONSE_SUCCESS &&
	    $response->code() != GENIRESPONSE_BUSY) {
	    $errmsg = "Sliverstatus failed: ". $response->output() . "\n";
	    $failed = 1;
	    last;
	}
	next
	    if ($response->code() == GENIRESPONSE_BUSY);
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 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
	my $blob = $response->value();
	if (defined($webtask)) {
	    #
	    # Need to find the sliver inside the blob.
	    #
	    my $sliverblob = $blob->{'details'}->{$sliver_urn};
	    if (defined($sliverblob)) {
		$webtask->state($sliverblob->{'state'});
		$webtask->rawstate($sliverblob->{'rawstate'});
	    }
	}
	if ($blob->{'status'} eq "failed") {
	    $failed = 1;
	    last;
	}
	#
	# We are watching for the image status to report ready or failed.
	#
	$response =
	    Genixmlrpc::CallMethod($authority->url(),
				   $context, "ImageInfo", $args);

	if ($response->code() != GENIRESPONSE_SUCCESS &&
	    $response->code() != GENIRESPONSE_BUSY) {
	    $errmsg = "Imageinfo failed: ". $response->output() . "\n";
	    $failed = 1;
	    last;
	}
	next
	    if ($response->code() == GENIRESPONSE_BUSY);

	$blob = $response->value();
	if (defined($webtask)) {
	    $webtask->image_size($blob->{'size'}) 	
		if (exists($blob->{'size'}));
	    $webtask->image_status($blob->{'status'})
		if (exists($blob->{'status'}));
	}
	if ($blob->{'status'} eq "ready") {
	    $ready = 1;
	    last;
	}
	elsif ($blob->{'status'} eq "failed") {
	    $failed = 1;
	    last;
	}
432
    }
433 434 435 436 437
    if ($failed) {
	$errmsg = "Imaging failed"
	    if (!defined($errmsg));
	$errcode = 1;
	goto bad;
438
    }
439 440 441 442 443 444 445 446 447 448 449 450
    elsif (!$ready) {
	$errmsg  = "Imaging timed out";
	$errcode = 60;
	goto bad;
    }
    elsif ($update_profile) {
	#
	# If successful, we create a new version of the profile and
	# update the rspec to reflect the new image version. Note
	# that we expect the CM is doing image versioning, so do not
	# bother to check if the image version is actually new.
	#
451 452 453 454 455
	my $doversions =
	    EmulabFeatures->FeatureEnabled("APT_ProfileVersions",
					   $this_user, $project);

	if ($doversions) {
456 457 458 459 460 461 462
	    $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);
	    }
463
	}
464
	$profile->UpdateDiskImage($version_url);
465
    }
466 467 468 469 470 471 472 473
    $instance->SetStatus("ready");
    # We garbage collect these later, so anyone waiting has a chance
    # to see the exit status
    $webtask->Exited(0)
	if (defined($webtask));
    $slice->UnLock();
    unlink($logfile)
	if (defined($logfile));
474
    exit(0);
475 476 477 478 479 480
  bad:
    print STDERR "$errmsg\n";
    if (defined($errmsg)) {
	$webtask->Exited($errcode);
	$webtask->output($errmsg);
    }
481 482
    $slice->UnLock()
	if ($needunlock);
483 484 485 486 487 488 489 490 491 492
    if (defined($logfile)) {
	$instance->Brand()->SendEmail($instance->Brand()->OpsEmailAddress(),
				      "Snapshot failed",
				      "Error taking snapshot of $instance:\n\n".
				      "$errmsg\n",
				      $instance->Brand()->OpsEmailAddress(),
				      undef, $logfile);
	unlink($logfile);
    }
    exit($errcode);
493
}
494 495 496 497 498 499

#
# Ask the console URL for a node in an instance.
#
sub DoConsole()
{
500
    usage()
501 502
	if (!@ARGV);
    my $node_id = shift(@ARGV);
503 504 505 506 507 508 509

    if (defined($webtask_id)) {
	$webtask = WebTask->LookupOrCreate(undef, $webtask_id);
	if (!defined($webtask)) {
	    fatal("Could not lookup/create webtask for $webtask_id");
	}
    }
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    
    #
    # 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;
    my $manifest = GeniXML::Parse($instance->manifest());
    if (! defined($manifest)) {
	fatal("Could not parse manifest");
    }
    my @nodes = GeniXML::FindNodes("n:node", $manifest)->get_nodelist();
    foreach my $node (@nodes) {
	my $client_id = GeniXML::GetVirtualId($node);
	if ($node_id eq $client_id) {
	    $sliver_urn = GeniXML::GetSliverId($node);
	}
    }
    if (!defined($sliver_urn)) {
	fatal("Could not find node '$node_id' in manifest");
    }
530 531 532
    my $response = $instance->ConsoleURL($sliver_urn);
    if (!defined($response)) {
	fatal("RPC Error calling ConsoleURL");
533
    }
534 535 536 537 538 539 540 541 542 543 544 545 546
    if (defined($webtask)) {
	$webtask->output($response->output());
	$webtask->code($response->code());
	$webtask->value($response->value())
	    if (! $response->code());
	$webtask->Exited($response->code());
	exit(0);
    }
    # For command line operation too.
    if ($response->code()) {
	fatal($response->output());
    }
    print $response->value() . "\n";
547 548
    exit(0);
}
549

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
#
# Terminate
#
sub DoTerminate()
{
    my $errmsg;
    my $logfile;
    
    my $slice = $instance->GetGeniSlice();
    if (!defined($slice)) {
	goto killit;
    }
    #
    # Lock the slice in case it is doing something else, like taking
    # a disk image.
    #
    if ($slice->Lock()) {
	fatal("Slice is busy, cannot lock it");
    }
    # So we can communicate errors back to the web interface.
    if (defined($webtask_id)) {
	$webtask = WebTask->LookupOrCreate($instance->uuid(), $webtask_id);
	if (!defined($webtask)) {
	    $slice->UnLock();
	    fatal("Could not lookup/create webtask for $webtask_id");
	}
	$webtask->AutoStore(1);
    }
    my $old_status = $instance->status();
    $instance->SetStatus("terminating");

    #
    # Exit and let caller poll for status.
    #
    if (defined($webtask) && !$debug) {
        $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. 
	    #
	    sleep(3);
	    my $foo = waitpid($childpid, &WNOHANG);
	    if ($foo) {
		$status = $? >> 8;
	    }
	    exit($status);
	}
    }
    my $response = $instance->Terminate();
    if (!defined($response)) {
	$errmsg = "RPC Error calling Terminate";
	goto bad;
    }

    # SEARCHFAILED is success.
    if ($response->code() != GENIRESPONSE_SUCCESS &&
	$response->code() != GENIRESPONSE_SEARCHFAILED) {
	if ($response->code() == GENIRESPONSE_BUSY) {
	    $errmsg = "Slice was busy for too long; try again later?";
	    goto bad;
	}
	$errmsg = "Could not delete slice: ". $response->output();
	goto bad;
    }
    $slice->Delete();
    $instance->RecordHistory();
  killit:
    $instance->Delete();
    $webtask->Exited(0)
	if (defined($webtask));
    unlink($logfile)
	if (defined($logfile));
    exit(0);
  bad:
    print STDERR $errmsg . "\n";
    $instance->SetStatus($old_status);
    $slice->UnLock();
    if (defined($webtask)) {
	$webtask->output($errmsg);
	$webtask->Exited(1);
    }
    if (defined($logfile)) {
	$instance->Brand()->SendEmail($instance->Brand()->OpsEmailAddress(),
				      "Unable to terminate instance $uuid",
				      "Error terminating $instance:\n\n".
				      "$errmsg\n",
				      $instance->Brand()->OpsEmailAddress(),
				      undef, $logfile);
	unlink($logfile);
    }
    exit(1);
}

#
# Extend.
#
sub DoExtend()
{
    usage()
	if (!@ARGV);
    
    my $seconds = shift(@ARGV);
    if ($seconds !~ /^\d*$/) {
	usage();
    }

    if ($instance->status() eq "failed") {
	fatal("Cannot extend failed instance!");
    }

    my $slice = $instance->GetGeniSlice();
    if (!defined($slice)) {
	fatal("No slice for instance!");
    }
    #
    # Lock the slice in case it is doing something else, like taking
    # a disk image.
    #
    if ($slice->Lock()) {
	fatal("Slice is busy, cannot lock it");
    }
    # Save in case of error.
    my $oldexpires = $slice->expires();

    # Need to update slice before creating new credential. 
    $slice->AddToExpiration($seconds);
    my $new_expires = $slice->ExpirationGMT();
    
    my $response = $instance->Extend($new_expires);
    if (!defined($response)) {
	$slice->UnLock();
	fatal("Internal error calling Extend");
    }
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	# Reset back to original expiration, sorry.
	$slice->SetExpiration($oldexpires);
	$slice->UnLock();
	# This is something the user should see.
	if ($response->code() == GENIRESPONSE_REFUSED) {
	    print STDERR $response->output() . "\n";
	    # For web interface.
	    exit(1);
	}
	fatal("Failed to extend slice: ". $response->output())
    }
    $slice->UnLock();
    exit(0);
}

#
# Refresh; ask the aggregate for status and set the instance status
# accordingly.
#
sub DoRefresh()
{
    my $errmsg;
    
    my $slice = $instance->GetGeniSlice();
    if (!defined($slice)) {
	print STDERR "No slice for instance\n";
	goto killit;
    }
    #
    # Lock the slice in case it is doing something else, like taking
    # a disk image.
    #
    if ($slice->Lock()) {
	fatal("Slice is busy, cannot lock it");
    }

    my $response = $instance->SliceStatus();
    if (!defined($response)) {
	$errmsg = "RPC Error calling SliceStatus";
	goto bad;
    }

    if ($response->code() != GENIRESPONSE_SUCCESS) {
	if ($response->code() == GENIRESPONSE_SEARCHFAILED) {
	    print STDERR "Slice is gone; killing instance";
	    $slice->Delete();
	}
	if ($response->code() == GENIRESPONSE_BUSY) {
	    $errmsg = "Slice was busy; try again later";
	    goto bad;
	}
	$errmsg = "Could not get status: ". $response->output();
	goto bad;
    }
    my $blob = $response->value();
    if ($blob->{'status'} eq "ready") {
	$instance->SetStatus("ready");
    }
    elsif ($blob->{'status'} eq "failed") {
	$instance->SetStatus("failed");
    }
    $slice->UnLock();
    exit(0);
  killit:
    $instance->RecordHistory();
    $instance->Delete();
    exit(0);
  bad:
    $slice->UnLock();
    print STDERR $errmsg . "\n";
    exit(1);
}

762 763 764 765
sub fatal($)
{
    my ($mesg) = @_;

766 767 768 769
    if (defined($webtask)) {
	$webtask->output($mesg);
	$webtask->code(-1);
    }
770 771 772 773 774 775 776 777 778 779 780 781 782 783
    print STDERR "*** $0:\n".
	         "    $mesg\n";
    # Exit with negative status so web interface treats it as system error.
    exit(-1);
}

sub escapeshellarg($)
{
    my ($str) = @_;

    $str =~ s/[^[:alnum:]]/\\$&/g;
    return $str;
}