dumpdescriptor.in 12.5 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2010-2016 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
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34
#
use English;
use strict;
use Getopt::Std;
use CGI;
use Data::Dumper;

#
# Dump an EZ descriptor out.
#
sub usage()
{
35
    print("Usage: dumpdescriptor ".
Leigh B Stoller's avatar
Leigh B Stoller committed
36
	  "[-d] [-e] [-v clientvers] [-i <imageid> [-t]] | [-o <osid>]\n");
37 38
    exit(-1);
}
Leigh B Stoller's avatar
Leigh B Stoller committed
39 40 41 42 43
my $optlist    = "di:o:tev:";
my $debug      = 0;
my $dotypes    = 0;
my $export     = 0;
my $clientvers = 0;
44
my $argumentid;
45 46 47 48 49 50 51 52 53

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBAUDIT	= "@TBAUDITEMAIL@";
my $TBGROUP_DIR	= "@GROUPSROOT_DIR@";
my $TBPROJ_DIR	= "@PROJROOT_DIR@";
54
my $TBBASE	= "@TBBASE@";
55 56 57
my $PGENISUPPORT= @PROTOGENI_SUPPORT@;
my $OURDOMAIN   = "@OURDOMAIN@";
my $WITHPROVENANCE= @IMAGEPROVENANCE@;
58

Leigh B Stoller's avatar
Leigh B Stoller committed
59 60 61 62 63
#
# When fetching the metadata, we now tell the server what client
# version of the software we are so it gives something we can handle.
# Be sure to update this if you change the version in image_import
#
64
my $METADATA_SERVERVERSION = 2;
Leigh B Stoller's avatar
Leigh B Stoller committed
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#
# 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 libdb;
use libtestbed;
use User;
use Project;
use Image;
use OSinfo;
87 88
use EmulabFeatures;
$EmulabFeatures::verbose = 0;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

# Protos
sub fatal($);
sub DumpImage($);
sub DumpOS($);

#
# 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 = 1;
}
106 107 108
if (defined($options{"t"})) {
    $dotypes = 1;
}
109 110 111
if (defined($options{"e"})) {
    $export = 1;
}
Leigh B Stoller's avatar
Leigh B Stoller committed
112 113 114 115
if (defined($options{"v"})) {
    $clientvers = $options{"v"};
}

116 117 118 119 120
if (@ARGV) {
    usage();
}

if (defined($options{"i"})) {
121 122
    $argumentid = $options{"i"};
    my $image = Image->Lookup($argumentid);
123
    if (!defined($image)) {
124
	fatal("No such image: $argumentid");
125 126 127 128
    }
    DumpImage($image);
}
elsif (defined($options{"o"})) {
129 130
    $argumentid = $options{"o"};
    my $osinfo = OSinfo->Lookup($argumentid);
131
    if (!defined($osinfo)) {
132
	fatal("No such osid: $argumentid");
133 134 135 136 137 138 139 140 141 142 143 144 145 146
    }
    DumpOS($osinfo);
}
else {
    fatal("Must supply an image or os ID");
}

sub DumpImage($)
{
    my ($image) = @_;
    
    # Array of string values to print. 
    my %xmlfields = ();

147 148 149
    # For version info.
    my @imagelist = ();

150
    $xmlfields{"imagename"}    = $image->imagename();
151 152 153 154
    if (!$export) {
	$xmlfields{"pid"}      = $image->pid();
	$xmlfields{"gid"}      = $image->gid();
    }
155
    $xmlfields{"description"}  = $image->description();
156 157 158
    if (!$image->isdataset()) {
	$xmlfields{"loadpart"} = $image->loadpart();
    }
159 160
    $xmlfields{"global"}       = $image->global();
    $xmlfields{"shared"}       = $image->shared();
Leigh B Stoller's avatar
Leigh B Stoller committed
161 162 163 164 165 166
    if (defined($image->path()) && $image->path() ne "") {
	#
	# Old clients cannot handle directory based paths, so give them
	# a filename instead.
	#
	my $path = $image->path();
Leigh B Stoller's avatar
Leigh B Stoller committed
167
	if ($clientvers == 0 && $image->IsDirPath()) {
Leigh B Stoller's avatar
Leigh B Stoller committed
168 169 170 171 172
	    $path .= $image->imagename() . ".ndz" .
		($image->version() ? ":" . $image->version() : "");
	}
	$xmlfields{"path"} = $path;
    }
173 174
    $xmlfields{"hash"}         = $image->hash()
    	if ($export && defined($image->hash()) && $image->hash() ne "");
175
    $xmlfields{"mbr_version"}  = $image->mbr_version();
176
    if ($export && $clientvers > 0) {
177
	$xmlfields{"isdataset"}    = $image->isdataset();
178 179 180 181 182
	if ($clientvers > 1 && $image->isdataset()) {
	    $xmlfields{"lba_low"}  = $image->lba_low();
	    $xmlfields{"lba_high"} = $image->lba_high();
	    $xmlfields{"lba_size"} = $image->lba_size();
	}
183
    }
184

185
    if ($export) {
186
	my $url;
187
	my $imageid    = $image->imageid();
188
	my $uuid       = $image->uuid();
189
	my $image_uuid = $image->image_uuid();
190 191 192 193 194 195 196 197
	my $access_key = $image->access_key();
	if (!defined($access_key) || $access_key eq "") {
	    $access_key = TBGenSecretKey();
	    if ($image->Update({'access_key' => $access_key})) {
		fatal("Could not initialize access key");
	    }
	}
	#
198 199 200 201 202 203 204
	# Generate a url that allows the image to be downloaded. This is a
	# little confusing cause of the ways the caller can request an
	# image.  If using the image uuid (which means, whatever the
	# current version is right now), then return a URL that is not
	# version specific. Otherwise, return URL to the actual image
	# version. The messiness is how to determine that we want the
	# version specific or not, from the script arguments.
205
	#
206 207 208 209 210 211 212 213
	if ($argumentid =~ /:/) {
	    $url = "$TBBASE/spewimage.php".
		"?imageid=$uuid&access_key=$access_key";
	}
	else {
	    $url = "$TBBASE/spewimage.php".
		"?imageid=$image_uuid&access_key=$access_key";
	}
214
	$xmlfields{"imagefile_url"} = $url;
215 216 217 218 219 220 221 222 223 224 225 226 227

	if ($clientvers > 2) {
	    #
	    # If this is a nonlocal image, send along where it came from.
	    # This allows the caller to trace back to the origin.
	    #
	    if (!$image->IsLocal()) {
		$xmlfields{'metadata_url'} = $image->metadata_url();
	    }
	    # Tell the caller if we have delta/full images.
	    $xmlfields{'havedelta'} = $image->HaveDeltaImage();
	    $xmlfields{'havefull'}  = $image->HaveFullImage();

228 229 230 231
	    # Tell the caller some extras for debugging image tracking (IMS).
	    $xmlfields{'origin_uuid'} = $image_uuid;
	    $xmlfields{'origin_name'} = $image->versname();
	    
232 233 234 235 236 237 238
	    if ($PGENISUPPORT) {
		# And the URN is required for image tracking.
		require GeniHRN;
		
		$xmlfields{'origin_urn'}  =
		    GeniHRN::Generate($OURDOMAIN, "authority", "cm");
	    }
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
	    #
	    # Send along the history so that the caller can get all
	    # the versions, which is important for delta based images.
	    #
	    if ($clientvers > 3) {
		#
		# Send the non-version metadata url so that the other side
		# can have it for when an experiment wants whatever the
		# latest version is. This allows different users to ask for
		# different versions or just latest version, and have them
		# come from the same image, instead of making new independent
		# shadow images.
		#
		$xmlfields{'image_metadata_url'} = $image->LocalURL();
		$xmlfields{'metadata_url'}       = $image->LocalVersionURL();
		
		$xmlfields{'image_version'}  = $image->version();
		$xmlfields{"deltahash"}      = $image->deltahash()
		    if ($export && defined($image->deltahash()) &&
			$image->deltahash() ne "");

		#
		# We need to send the parent info too, for the delta chain
		# to be constructed. 
		#
		if (defined($image->parent_imageid())) {
		    $xmlfields{'parent_imageid'} = $image->parent_imageid();
		    $xmlfields{'parent_version'} = $image->parent_version();
		}
		
		if ($image->version()) {
		    my @tmp;
		    
		    # This returns a reverse ordered list.
		    if ($image->AllVersions(\@tmp)) {
			fatal("Could not get image version list");
		    }
		    #
		    # But just the history prior to this image.
		    #		
		    foreach my $im (@tmp) {
			next
			    if ($im->version() >= $image->version());
			
			$imagelist[$im->version()] = $im;
		    }
		}
	    }
287
	}
288 289
    }

290 291 292 293 294 295 296 297 298 299 300 301
    sub MapOS($) {
	my ($osid) = @_;
	return "none"
	    if (!defined($osid));
	
	my $osinfo = OSinfo->Lookup($osid);
	if (!defined($osinfo)) {
	    fatal("Could not find osid $osid");
	}
	return $osinfo->pid() . "," . $osinfo->osname();
    }

302
    if (! ($image->ezid() || $image->isdataset())) {
303 304 305 306 307 308 309
	$xmlfields{"loadlength"}   = $image->loadlength();
	$xmlfields{"part1_osid"}   = MapOS($image->part1_osid());
	$xmlfields{"part2_osid"}   = MapOS($image->part2_osid());
	$xmlfields{"part3_osid"}   = MapOS($image->part3_osid());
	$xmlfields{"part4_osid"}   = MapOS($image->part4_osid());
	$xmlfields{"default_osid"} = MapOS($image->default_osid());
    }
310
    elsif (!$image->isdataset()) {
311 312 313 314 315
	my $osinfo = OSinfo->Lookup($image->imageid());
	if (!defined($osinfo)) {
	    fatal("Could not find osid for $image");
	}
	$xmlfields{"OS"}              = $osinfo->OS();
316 317
	$xmlfields{"version"}         = $osinfo->version()
	    if (defined($osinfo->version()) && $osinfo->version() ne "");
318
	$xmlfields{"op_mode"}         = $osinfo->op_mode();
319 320
	$xmlfields{"osfeatures"}      = $osinfo->osfeatures()
	    if (defined($osinfo->osfeatures()) && $osinfo->osfeatures() ne "");
321 322
	$xmlfields{"reboot_waittime"} = $osinfo->reboot_waittime()
	    if (defined($osinfo->reboot_waittime()));
323 324 325 326 327
	# whole disk images are described differently in EZ format
	if ($image->loadpart() == 0 && $image->loadlength() == 4) {
	    $xmlfields{"wholedisk"} = 1;
	    # find the partition which has an osid defined
	    for (my $i = 1; $i <= 4; $i++) {
328 329
		my $func = "part${i}_osid";
		my $foo  = $image->$func();
330 331 332 333 334 335
		if (defined($foo)) {
		    $xmlfields{"loadpart"} = $i;
		    last;
		}
	    }
	}
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	if (defined($osinfo->nextosid()) && $osinfo->nextosid()) {
	    my $nextosinfo = OSinfo->Lookup($osinfo->nextosid());
	    if (!defined($nextosinfo)) {
		fatal("Could not look up nextosid for $osinfo");
	    }
	    $xmlfields{"nextosid"} =
		$nextosinfo->pid() . "," . $nextosinfo->osname();
	}
	if (defined($osinfo->def_parentosid()) && $osinfo->def_parentosid()) {
	    my $nextosinfo = OSinfo->Lookup($osinfo->def_parentosid());
	    if (!defined($nextosinfo)) {
		fatal("Could not look up def_parentosid for $osinfo");
	    }
	    $xmlfields{"def_parentosid"} =
		$nextosinfo->pid() . "," . $nextosinfo->osname();
	}
352
    }
353 354 355 356 357 358 359 360
    if ($dotypes) {
	my @typelist = $image->TypeList();
	foreach my $nodetype (@typelist) {
	    my $type = $nodetype->type();
	    
	    $xmlfields{"mtype_$type"} = "1";
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
361 362 363 364 365 366 367 368 369 370

    #
    # Old sites cannot handle a version element.
    #
    if ($clientvers > 0) {
	print "<image metadata_version=\"${METADATA_SERVERVERSION}\">\n";
    }
    else {
	print "<image>\n";
    }
371
    foreach my $key (sort keys(%xmlfields)) {
372 373
	my $val = $xmlfields{$key};

374 375 376
	print " <attribute name=\"$key\">";
	print "<value>" . CGI::escapeHTML($val) . "</value>";
	print "</attribute>\n";
377
    }
378 379 380 381 382 383 384 385 386 387
    if (@imagelist) {
	foreach my $im (@imagelist) {
	    my $version = $im->version();
	    my $url     = $im->LocalVersionURL();

	    print " <version_history name='$version'>";
	    print CGI::escapeHTML($url);
	    print "</version_history>\n";
	}
    }
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    print "</image>\n";

    return 0;
}

sub DumpOS($)
{
    my ($osinfo) = @_;
    
    # Array of string values to print. 
    my %xmlfields = ();

    $xmlfields{"description"}     = $osinfo->description();
    $xmlfields{"osname"}          = $osinfo->osname();
    $xmlfields{"pid"}             = $osinfo->pid();
    $xmlfields{"OS"}              = $osinfo->OS();
404 405
    $xmlfields{"version"}         = $osinfo->version()
    	if (defined($osinfo->version()) && $osinfo->version() ne "");
406
    $xmlfields{"path"}            = $osinfo->path()
407
	if (defined($osinfo->path()) && $osinfo->path() ne "");
408 409 410
    $xmlfields{"magic"}           = $osinfo->magic()
	if (defined($osinfo->magic()));
    $xmlfields{"op_mode"}         = $osinfo->op_mode();
411 412
    $xmlfields{"features"}        = $osinfo->osfeatures()
	if (defined($osinfo->osfeatures()) && $osinfo->osfeatures() ne "");
413
    $xmlfields{"shared"}          = $osinfo->shared();
414
    $xmlfields{"mfs"}             = $osinfo->mfs() if ($osinfo->mfs());
415 416 417 418 419 420 421 422 423 424 425 426 427 428
    $xmlfields{"mustclean"}       = $osinfo->mustclean();
    $xmlfields{"reboot_waittime"} = $osinfo->reboot_waittime()
	if (defined($osinfo->reboot_waittime()));

    if (defined($osinfo->nextosid()) && $osinfo->nextosid()) {
	my $nextosinfo = OSinfo->Lookup($osinfo->nextosid());
	if (!defined($nextosinfo)) {
	    fatal("Could not look up nextosid for $osinfo");
	}
	$xmlfields{"nextosid"} =
	    $nextosinfo->pid() . "," . $nextosinfo->osname();
    }

    print "<osid>\n";
429
    foreach my $key (sort keys(%xmlfields)) {
430 431
	my $val = $xmlfields{$key};

432 433 434
	print " <attribute name=\"$key\">";
	print "<value>" . CGI::escapeHTML($val) . "</value>";
	print "</attribute>\n";
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    }
    print "</osid>\n";

    return 0;
}
exit(0);

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

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