dumpdescriptor.in 9.22 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2010-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
# 
# {{{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 ".
36
	  "[-d] [-e] [-v clientvers] [-i <imageid> [-t]] | [-o <osid>]\n");
37 38
    exit(-1);
}
39 40 41 42 43
my $optlist    = "di:o:tev:";
my $debug      = 0;
my $dotypes    = 0;
my $export     = 0;
my $clientvers = 0;
44 45 46 47 48 49 50 51 52

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBAUDIT	= "@TBAUDITEMAIL@";
my $TBGROUP_DIR	= "@GROUPSROOT_DIR@";
my $TBPROJ_DIR	= "@PROJROOT_DIR@";
53
my $TBBASE	= "@TBBASE@";
54

55 56 57 58 59
#
# 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
#
60
my $METADATA_SERVERVERSION = 2;
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#
# 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;

# 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;
}
100 101 102
if (defined($options{"t"})) {
    $dotypes = 1;
}
103 104 105
if (defined($options{"e"})) {
    $export = 1;
}
106 107 108 109
if (defined($options{"v"})) {
    $clientvers = $options{"v"};
}

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
if (@ARGV) {
    usage();
}

if (defined($options{"i"})) {
    my $imageid = $options{"i"};
    my $image = Image->Lookup($imageid);
    if (!defined($image)) {
	fatal("No such image: $imageid");
    }
    DumpImage($image);
}
elsif (defined($options{"o"})) {
    my $osid = $options{"o"};
    my $osinfo = OSinfo->Lookup($osid);
    if (!defined($osinfo)) {
	fatal("No such osid: $osid");
    }
    DumpOS($osinfo);
}
else {
    fatal("Must supply an image or os ID");
}

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

    $xmlfields{"imagename"}    = $image->imagename();
142 143 144 145
    if (!$export) {
	$xmlfields{"pid"}      = $image->pid();
	$xmlfields{"gid"}      = $image->gid();
    }
146
    $xmlfields{"description"}  = $image->description();
147 148 149
    if (!$image->isdataset()) {
	$xmlfields{"loadpart"} = $image->loadpart();
    }
150 151
    $xmlfields{"global"}       = $image->global();
    $xmlfields{"shared"}       = $image->shared();
152 153 154 155 156 157
    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
158
	if ($clientvers == 0 && $image->IsDirPath()) {
159 160 161 162 163
	    $path .= $image->imagename() . ".ndz" .
		($image->version() ? ":" . $image->version() : "");
	}
	$xmlfields{"path"} = $path;
    }
164 165
    $xmlfields{"hash"}         = $image->hash()
    	if ($export && defined($image->hash()) && $image->hash() ne "");
166
    $xmlfields{"mbr_version"}  = $image->mbr_version();
167
    if ($clientvers > 0) {
168
	$xmlfields{"isdataset"}    = $image->isdataset();
169 170 171 172 173
	if ($clientvers > 1 && $image->isdataset()) {
	    $xmlfields{"lba_low"}  = $image->lba_low();
	    $xmlfields{"lba_high"} = $image->lba_high();
	    $xmlfields{"lba_size"} = $image->lba_size();
	}
174
    }
175

176 177
    if ($export) {
	my $imageid    = $image->imageid();
178
	my $uuid       = $image->uuid();
179 180 181 182 183 184 185 186 187 188 189
	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");
	    }
	}
	#
	# Generate a url that allows the image to be downloaded.
	#
	my $url = "$TBBASE/spewimage.php".
190
	    "?imageid=$uuid&access_key=$access_key";
191 192 193 194
	
	$xmlfields{"imagefile_url"} = $url;
    }

195 196 197 198 199 200 201 202 203 204 205 206
    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();
    }

207
    if (! ($image->ezid() || $image->isdataset())) {
208 209 210 211 212 213 214
	$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());
    }
215
    elsif (!$image->isdataset()) {
216 217 218 219 220
	my $osinfo = OSinfo->Lookup($image->imageid());
	if (!defined($osinfo)) {
	    fatal("Could not find osid for $image");
	}
	$xmlfields{"OS"}              = $osinfo->OS();
221 222
	$xmlfields{"version"}         = $osinfo->version()
	    if (defined($osinfo->version()) && $osinfo->version() ne "");
223
	$xmlfields{"op_mode"}         = $osinfo->op_mode();
224 225
	$xmlfields{"osfeatures"}      = $osinfo->osfeatures()
	    if (defined($osinfo->osfeatures()) && $osinfo->osfeatures() ne "");
226 227
	$xmlfields{"reboot_waittime"} = $osinfo->reboot_waittime()
	    if (defined($osinfo->reboot_waittime()));
228 229 230 231 232
	# 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++) {
233 234
		my $func = "part${i}_osid";
		my $foo  = $image->$func();
235 236 237 238 239 240
		if (defined($foo)) {
		    $xmlfields{"loadpart"} = $i;
		    last;
		}
	    }
	}
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	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();
	}
257
    }
258 259 260 261 262 263 264 265
    if ($dotypes) {
	my @typelist = $image->TypeList();
	foreach my $nodetype (@typelist) {
	    my $type = $nodetype->type();
	    
	    $xmlfields{"mtype_$type"} = "1";
	}
    }
266 267 268 269 270 271 272 273 274 275

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

279 280 281
	print " <attribute name=\"$key\">";
	print "<value>" . CGI::escapeHTML($val) . "</value>";
	print "</attribute>\n";
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    }
    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();
299 300
    $xmlfields{"version"}         = $osinfo->version()
    	if (defined($osinfo->version()) && $osinfo->version() ne "");
301
    $xmlfields{"path"}            = $osinfo->path()
302
	if (defined($osinfo->path()) && $osinfo->path() ne "");
303 304 305
    $xmlfields{"magic"}           = $osinfo->magic()
	if (defined($osinfo->magic()));
    $xmlfields{"op_mode"}         = $osinfo->op_mode();
306 307
    $xmlfields{"features"}        = $osinfo->osfeatures()
	if (defined($osinfo->osfeatures()) && $osinfo->osfeatures() ne "");
308
    $xmlfields{"shared"}          = $osinfo->shared();
309
    $xmlfields{"mfs"}             = $osinfo->mfs() if ($osinfo->mfs());
310 311 312 313 314 315 316 317 318 319 320 321 322 323
    $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";
324
    foreach my $key (sort keys(%xmlfields)) {
325 326
	my $val = $xmlfields{$key};

327 328 329
	print " <attribute name=\"$key\">";
	print "<value>" . CGI::escapeHTML($val) . "</value>";
	print "</attribute>\n";
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    }
    print "</osid>\n";

    return 0;
}
exit(0);

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

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