imageid_defs.php 35.1 KB
Newer Older
1 2
<?php
#
Leigh Stoller's avatar
Leigh Stoller committed
3
# Copyright (c) 2006-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
# 
# {{{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
#
include_once("osinfo_defs.php");	# For SpitOSIDLink() below.

26 27 28 29 30 31
# Default architectures.
$image_architectures		= array();
$image_architectures["i386"]	= "i386";
$image_architectures["x86_64"]	= "x86_64";
$image_architectures["aarch64"]	= "aarch64";

32 33 34 35
class Image
{
    var	$image;
    var $types;
36 37
    var $group;
    var $project;
38 39 40 41

    #
    # Constructor by lookup on unique ID
    #
42
    function Image($id, $version = NULL) {
43 44 45
	if (is_null($version)) {
	    list($id,$version) = preg_split('/:/', $id);
	}
46 47
	$safe_id = addslashes($id);

48 49
	if (is_null($version)) {
	    $query_result =
50 51
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid,".
                            "   i.locked as image_locked ".
52
			    "  from images as i ".
53 54 55 56 57 58 59 60
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid and v.version=i.version ".
			    "where i.imageid='$safe_id'");
	}
	else {
	    # This will get deleted images, but that is okay.
	    $safe_version = addslashes($version);
	    $query_result =
61 62
	        DBQueryWarn("select i.*,v.*,i.uuid as image_uuid,".
                            "  i.locked as image_locked".
63 64 65
			    "  from image_versions as v ".
			    "left join images as i on ".
			    "     i.imageid=v.imageid ".
66 67 68
			    "where v.imageid='$safe_id' and ".
			    "      v.version='$safe_version'");
	}
69 70 71 72 73 74 75

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->image = NULL;
	    return;
	}
	$this->image = mysql_fetch_array($query_result);

76 77 78
	# Load lazily;
	$this->group      = null;
	$this->project    = null;
79 80 81 82 83 84 85 86
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->image);
    }

    # Lookup by imageid
87
    function Lookup($id, $version = NULL) {
88
	$foo = new Image($id,$version);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

	if (! $foo->IsValid())
	    return null;

	return $foo;
    }

    # Lookup by imagename in a project
    function LookupByName($project, $name) {
	$pid       = $project->pid();
	$safe_name = addslashes($name);
	
	$query_result =
	    DBQueryFatal("select imageid from images ".
			 "where pid='$pid' and imagename='$safe_name'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	return Image::Lookup($row["imageid"]);
    }

112 113 114 115 116 117 118 119 120 121 122 123 124 125
    # Look for most recent unreleased version.
    function LookupUnreleased() {
	global $DOPROVENANCE;
	if (!$DOPROVENANCE) {
	    return $this;
	}
	$imageid   = $this->imageid();
	
	$query_result =
	    DBQueryFatal("select version from image_versions ".
			 "where imageid='$imageid' and released=0 and ".
			 "      deleted is null ".
			 "order by version desc limit 1");

Leigh Stoller's avatar
Leigh Stoller committed
126 127
	# If no unreleased version, just return $this. Might mean that
	# provenance is not turned on for the project cause of a feature.
128
	if (mysql_num_rows($query_result) == 0) {
Leigh Stoller's avatar
Leigh Stoller committed
129
	    return $this;
130 131 132 133 134
	}
	$row = mysql_fetch_array($query_result);
	return Image::Lookup($imageid, $row["version"]);
    }

135
    function LookupByUUID($uuid, $version = NULL) {
136 137
	$safe_uuid = addslashes($uuid);

138 139 140 141 142
	#
	# First look to see if the uuid is for the image itself,
	# which means current version. Otherwise look for a
	# version with the uuid.
	#
143
	$query_result =
144 145
	    DBQueryFatal("select i.imageid,i.version from images as i ".
			 "where i.uuid='$safe_uuid'");
146 147 148 149 150 151 152 153 154 155 156 157
	if (mysql_num_rows($query_result)) {
	    if (!is_null($version)) {
		#
		# Specific version, but using the image UUID.
		# Note that you cannot lookup a deleted image this way.
		# Must have the version specific UUID.
		#
		$row = mysql_fetch_array($query_result);
		return Image::Lookup($row["imageid"], $version);
	    }
	}
	else {
158 159 160
	    $query_result =
		DBQueryWarn("select imageid,version from image_versions ".
			    "where uuid='$safe_uuid' and ".
Leigh Stoller's avatar
Leigh Stoller committed
161
			    "      deleted is null");
162
	}
163
	if (!$query_result || mysql_num_rows($query_result) == 0) {
164 165 166
	    return null;
	}
	$row = mysql_fetch_array($query_result);
167
	return Image::Lookup($row["imageid"], $row["version"]);
168 169
    }
    
170 171 172 173 174 175 176
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

177 178 179
	$imageid    = $this->imageid();
	$version    = $this->version();
	$image_uuid = $this->image_uuid();
180
	
181
	$query_result =
182 183 184 185 186 187 188
            DBQueryWarn("select i.*,v.*,i.uuid as image_uuid,".
                        "  i.locked as image_locked".
                        "  from image_versions as v ".
                        "left join images as i on ".
                        "     i.imageid=v.imageid ".
                        "where v.imageid='$safe_id' and ".
                        "      v.version='$safe_version'");
189 190 191 192 193
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->imageid = NULL;
	    return -1;
	}
194
	$this->image = mysql_fetch_array($query_result);
195 196 197
	return 0;
    }

198
    #
199 200
    # Class function to create a new image descriptor.
    #
201 202
    function NewImageId($ez, $imagename, $args, $creator, $group,
			$target, &$errors) {
203 204 205 206 207
	global $suexec_output, $suexec_output_array;

        #
        # Generate a temporary file and write in the XML goo.
        #
Russ Fish's avatar
Russ Fish committed
208
	$xmlname = tempnam("/tmp", $ez ? "newimageid_ez" : "newimageid");
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
	    $errors[] = "Transient error(1); please try again later.";
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
	    $errors[] = "Transient error(2); please try again later.";
	    return null;
	}

	# Add these. Maybe caller should do this?
	$args["imagename"] = $imagename;

	fwrite($fp, "<image>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</image>\n");
	fclose($fp);
	chmod($xmlname, 0666);
232
	$opt = ($target ? "-t " . escapeshellarg($target) : "");
233

234
	$script = "webnewimageid" . ($ez ? "_ez" : "");
235
	$retval = SUEXEC($creator->uid(),
236
			 $group->pid() . "," . $group->unix_gid(),
237
			 "$script $opt $xmlname",
238 239 240 241
			 SUEXEC_ACTION_IGNORE);

	if ($retval) {
	    if ($retval < 0) {
242
		$errors[] = "Transient error(3, $retval); please try again later.";
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		# unlink($xmlname);
		if (count($suexec_output_array)) {
		    for ($i = 0; $i < count($suexec_output_array); $i++) {
			$line = $suexec_output_array[$i];
			if (preg_match("/^([-\w]+):\s*(.*)$/",
				       $line, $matches)) {
			    $errors[$matches[1]] = $matches[2];
			}
			else
			    $errors[] = $line;
		    }
		}
		else
259
		    $errors[] = "Transient error(4, $retval); please try again later.";
260 261 262 263 264 265 266 267 268 269 270 271
	    }
	    return null;
	}

        #
        # Parse the last line of output. Ick.
        #
	unset($matches);
	
	if (!preg_match("/^IMAGE\s+([^\/]+)\/(\d+)\s+/",
			$suexec_output_array[count($suexec_output_array)-1],
			$matches)) {
272
	    $errors[] = "Transient error(5); please try again later.";
273 274 275 276 277 278
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    return null;
	}
	$image = $matches[2];
	$newimage = image::Lookup($image);
	if (! $newimage) {
279
	    $errors[] = "Transient error(6); please try again later.";
280 281 282 283 284 285 286 287 288 289
	    TBERROR("Could not lookup new image $image", 0);
	    return null;
	}

	# Unlink this here, so that the file is left behind in case of error.
	# We can then create the image by hand from the xmlfile, if desired.
	unlink($xmlname);
	return $newimage; 
    }

290 291 292 293 294
    #
    # Flip global bit. If making it global, turn off shared.
    # Also, if an EZ image, flip the bit on the os_info entry too.
    #
    function SetGlobal($mode) {
295 296
	$imageid  = $this->imageid();
	$version  = $this->version();
297 298 299
	$mode     = ($mode ? 1 : 0);
	$extra    = ($mode ? ",shared=0" : "");

300 301
	DBQueryFatal("update image_versions set global='$mode' $extra ".
		     "where imageid='$imageid' and version='$version'");
302 303

	if ($this->ezid()) {
304 305
	    DBQueryFatal("update os_info_versions set shared='$mode' ".
			 "where osid='$imageid' and vers='$version'");
306 307 308 309
	}
	return 0;
    }

310 311
    #
    # Class function to edit an image descriptor.
312 313 314 315 316 317 318 319 320 321
    #
    function EditImageid($image, $args, &$errors) {
	global $suexec_output, $suexec_output_array;

        #
        # Generate a temporary file and write in the XML goo.
        #
	$xmlname = tempnam("/tmp", "editimageid");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
322
	    $errors[] = "Transient error(1); please try again later.";
323 324 325 326
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
327
	    $errors[] = "Transient error(2); please try again later.";
328 329 330 331 332
	    return null;
	}

	# Add these. Maybe caller should do this?
	$args["imageid"] = $image->imageid();
333
	$args["version"] = $image->version();
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

	fwrite($fp, "<image>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</image>\n");
	fclose($fp);
	chmod($xmlname, 0666);

	$retval = SUEXEC("nobody", "nobody", "webeditimageid $xmlname",
			 SUEXEC_ACTION_IGNORE);

	if ($retval) {
	    if ($retval < 0) {
350
		$errors[] = "Transient error(3, $retval); please try again later.";
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		# unlink($xmlname);
		if (count($suexec_output_array)) {
		    for ($i = 0; $i < count($suexec_output_array); $i++) {
			$line = $suexec_output_array[$i];
			if (preg_match("/^([-\w]+):\s*(.*)$/",
				       $line, $matches)) {
			    $errors[$matches[1]] = $matches[2];
			}
			else
			    $errors[] = $line;
		    }
		}
		else
367
		    $errors[] = "Transient error(4, $retval); please try again later.";
368 369 370 371 372 373 374 375
	    }
	    return null;
	}

	# There are no return value(s) to parse at the end of the output.

	# Unlink this here, so that the file is left behind in case of error.
	# We can then create the image by hand from the xmlfile, if desired.
376
	unlink($xmlname);
377 378 379
	return true;
    }

380 381 382 383 384 385 386 387 388 389 390 391
    #
    # Equality test.
    #
    function SameImage($image) {
	return $image->imageid() == $this->imageid();
    }

    # accessors
    function field($name) {
	return (is_null($this->image) ? -1 : $this->image[$name]);
    }
    function imagename()	{ return $this->field("imagename"); }
392
    function version()		{ return $this->field("version"); }
393
    function architecture()	{ return $this->field("architecture"); }
394 395
    function pid()		{ return $this->field("pid"); }
    function gid()		{ return $this->field("gid"); }
396 397
    function pid_idx()		{ return $this->field("pid_idx"); }
    function gid_idx()		{ return $this->field("gid_idx"); }
398
    function imageid()		{ return $this->field("imageid"); }
399 400
    function parent_imageid()	{ return $this->field("parent_imageid"); }
    function parent_version()	{ return $this->field("parent_version"); }
401
    function image_uuid()	{ return $this->field("image_uuid"); }
402
    function uuid()		{ return $this->field("uuid"); }
403 404
    function creator()		{ return $this->field("creator"); }
    function creator_idx()	{ return $this->field("creator_idx"); }
405
    function creator_urn()	{ return $this->field("creator_urn"); }
406
    function created()		{ return $this->field("created"); }
407
    function deleted()		{ return $this->field("deleted"); }
408 409 410 411 412 413 414
    function description()	{ return $this->field("description"); }
    function loadpart()		{ return $this->field("loadpart"); }
    function loadlength()	{ return $this->field("loadlength"); }
    function part1_osid()	{ return $this->field("part1_osid"); }
    function part2_osid()	{ return $this->field("part2_osid"); }
    function part3_osid()	{ return $this->field("part3_osid"); }
    function part4_osid()	{ return $this->field("part4_osid"); }
415 416 417 418
    function part1_vers()	{ return $this->field("part1_vers"); }
    function part2_vers()	{ return $this->field("part2_vers"); }
    function part3_vers()	{ return $this->field("part3_vers"); }
    function part4_vers()	{ return $this->field("part4_vers"); }
419
    function default_osid()	{ return $this->field("default_osid"); }
420
    function default_vers()	{ return $this->field("default_vers"); }
421 422 423 424 425 426
    function path()		{ return $this->field("path"); }
    function magic()		{ return $this->field("magic"); }
    function ezid()		{ return $this->field("ezid"); }
    function shared()		{ return $this->field("shared"); }
    function isglobal()		{ return $this->field("global"); }
    function updated()		{ return $this->field("updated"); }
427 428
    function updater()		{ return $this->field("updater"); }
    function updater_urn()	{ return $this->field("updater_urn"); }
429
    function mbr_version()	{ return $this->field("mbr_version"); }
430
    function hash()		{ return $this->field("hash"); }
431 432
    function metadata_url()	{ return $this->field("metadata_url"); }
    function imagefile_url()	{ return $this->field("imagefile_url"); }
433
    function logfileid()	{ return $this->field("logfileid"); }
434
    function noexport()		{ return $this->field("noexport"); }
435
    function ready()		{ return $this->field("ready"); }
436
    function isdataset()	{ return $this->field("isdataset"); }
437
    function nodelta()		{ return $this->field("nodelta"); }
438
    function released()		{ return $this->field("released"); }
439
    function notes()		{ return $this->field("notes"); }
440 441 442 443 444
    function locked()		{ return $this->field("image_locked"); }
    function size()		{ return $this->field("size"); }
    function lba_low()		{ return $this->field("lba_low"); }
    function lba_high()		{ return $this->field("lba_high"); }
    function lba_size()		{ return $this->field("lba_size"); }
Leigh Stoller's avatar
Leigh Stoller committed
445
    function nodetypes()	{ return $this->field("nodetypes"); }
446 447 448 449

    # Return the DB data.
    function DBData()		{ return $this->image; }
    # and the types array
450
    function Types()		{ return $this->TypeList(); }
451

452 453 454
    # Concat id/vers.
    function versid() { return $this->imageid() . ":" . $this->version(); }

455 456 457 458 459 460 461 462
    #
    # Access Check, determines if $user can access $this record.
    # 
    function AccessCheck($user, $access_type) {
	global $TB_IMAGEID_READINFO;
	global $TB_IMAGEID_MODIFYINFO;
	global $TB_IMAGEID_DESTROY;
	global $TB_IMAGEID_ACCESS;
463
	global $TB_IMAGEID_EXPORT;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	global $TB_IMAGEID_MIN;
	global $TB_IMAGEID_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_LOCALROOT;
	$mintrust = $TB_IMAGEID_READINFO;

	if ($access_type < $TB_IMAGEID_MIN || $access_type > $TB_IMAGEID_MAX) {
	    TBERROR("Invalid access type $access_type!", 1);
	}

        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}

	$shared = $this->shared();
	$global = $this->isglobal();
484
	$imageid= $this->imageid();
485 486 487
	$pid    = $this->pid();
	$gid    = $this->gid();
	$uid    = $user->uid();
488 489 490
	$uid_idx= $user->uid_idx();
	$pid_idx= $user->uid_idx();
	$gid_idx= $user->uid_idx();
491 492

        #
493
        # Global ImageIDs can be read by anyone but written with permission.
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
        # 
	if ($global) {
	    if ($access_type == $TB_IMAGEID_READINFO) {
		return 1;
	    }
	}

        #
        # Otherwise must have proper trust in the project.
        # 
	if ($access_type == $TB_IMAGEID_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
            #
            # Shared imageids are readable by anyone in the project.
            #
	    if ($shared)
		$gid = $pid;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	if (TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	    return 1;
	}
        # No point in looking further; never allowed.
	if ($access_type == $TB_IMAGEID_EXPORT) {
	    return 0;
	}
	
	#
	# Look in the image permissions. First look for a user permission,
	# then look for a group permission.
	#
	$query_result = 
	    DBQueryFatal("select allow_write from image_permissions ".
			 "where imageid='$imageid' and ".
			 "      permission_type='user' and ".
			 "      permission_idx='$uid_idx'");
	
	if (mysql_num_rows($query_result)) {
	    $row  = mysql_fetch_array($query_result);

            # Only allowed to read.
	    if ($access_type == $TB_IMAGEID_READINFO ||
		$access_type == $TB_IMAGEID_ACCESS)
		return 1;
	}
	$trust_none = TBDB_TRUSTSTRING_NONE;
	$query_result = 
	    DBQueryFatal("select allow_write from group_membership as g ".
			 "left join image_permissions as p on ".
			 "     p.permission_type='group' and ".
			 "     p.permission_idx=g.gid_idx ".
			 "where g.uid_idx='$uid_idx' and ".
			 "      p.imageid='$imageid' and ".
			 "      trust!='$trust_none'");

	if (mysql_num_rows($query_result)) {
            # Only allowed to read.
	    if ($access_type == $TB_IMAGEID_READINFO ||
		$access_type == $TB_IMAGEID_ACCESS)
		return 1;
	}
	return 0;
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
    #
    # Load the project object for an experiment.
    #
    function Project() {
	$pid_idx = $this->pid_idx();

	if ($this->project)
	    return $this->project;

	$this->project = Project::Lookup($pid_idx);
	if (! $this->project) {
	    TBERROR("Could not lookup project $pid_idx!", 1);
	}
	return $this->project;
    }
    #
    # Load the group object for an experiment.
    #
    function Group() {
	$gid_idx = $this->gid_idx();

	if ($this->group)
	    return $this->group;

	$this->group = Group::Lookup($gid_idx);
	if (! $this->group) {
	    TBERROR("Could not lookup group $gid_idx!", 1);
	}
	return $this->group;
    }

593
    function Show($showperms = 0) {
594
	global $TBBASE, $DOPROVENANCE;
595
	
596 597
	$imageid	= $this->imageid();
	$imagename	= $this->imagename();
598
	$version	= $this->version();
599 600 601 602 603 604 605 606 607
	$pid		= $this->pid();
	$gid		= $this->gid();
	$description	= $this->description();
	$loadpart	= $this->loadpart();
	$loadlength	= $this->loadlength();
	$part1_osid	= $this->part1_osid();
	$part2_osid	= $this->part2_osid();
	$part3_osid	= $this->part3_osid();
	$part4_osid	= $this->part4_osid();
608 609 610 611
	$part1_vers	= $this->part1_vers();
	$part2_vers	= $this->part2_vers();
	$part3_vers	= $this->part3_vers();
	$part4_vers	= $this->part4_vers();
612
	$default_osid	= $this->default_osid();
613
	$default_vers	= $this->default_vers();
614 615 616 617
	$path		= $this->path();
	$shared		= $this->shared();
	$globalid	= $this->isglobal();
	$creator	= $this->creator();
618
	$creator_urn	= $this->creator_urn();
619
	$created	= $this->created();
620
	$updated	= $this->updated();
621 622
	$updater	= $this->updater();
	$updater_urn	= $this->updater_urn();
623
	$uuid           = $this->uuid();
624
	$image_uuid     = $this->image_uuid();
625
	$mbr_version    = $this->mbr_version();
626
	$hash           = $this->hash();
627
	$notes          = $this->notes();
628 629
	$isdataset      = $this->isdataset();
        
630 631 632 633 634 635 636
	#
	# An imported image has a metadata_url, and at the moment I
	# do want to worry about exporting an imported image.
	#
	$imagefile_url  = $this->imagefile_url();
	$metadata_url   = $this->metadata_url();

637 638 639 640 641 642
	if (!$description)
	    $description = "&nbsp;";
	if (!$path)
	    $path = "&nbsp;";
	if (!$created)
	    $created = "N/A";
643 644
	if (!strcmp($notes, ""))
	    $notes = "&nbsp;";
645 646 647 648 649 650 651 652 653 654 655
    
        #
        # Generate the table.
        #
	echo "<table align=center border=2 cellpadding=2 cellspacing=2>\n";

	echo "<tr>
                <td>Image Name: </td>
                <td class=\"left\">$imagename</td>
              </tr>\n";

656 657 658 659 660 661 662
	echo "<tr>
                <td>Description: </td>
                <td class=left>\n";
	echo "$description";
	echo "   </td>
 	      </tr>\n";

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	echo "<tr>
                <td>Project: </td>
                <td class=\"left\">
                  <a href='showproject.php3?pid=$pid'>$pid</a></td>
              </tr>\n";

	echo "<tr>
                  <td>Group: </td>
                  <td class=\"left\">
                    <a href='showgroup.php3?pid=$pid&gid=$gid'>$gid</a></td>
              </tr>\n";
    
	echo "<tr>
                <td>Created: </td>
                <td class=left>$created</td>
 	      </tr>\n";

680 681 682 683 684 685 686 687 688 689 690 691
	echo "<tr>
                <td>Creator: </td>
                <td class=left>$creator</td>
     	      </tr>\n";

	if ($creator_urn) {
	    echo "<tr>
                    <td>Creator URN: </td>
                    <td class=left>$creator_urn</td>
         	  </tr>\n";
	}
	    
692 693 694 695 696
	if ($updated) {
	    echo "<tr>
                    <td>Updated: </td>
                    <td class=left>$updated</td>
     	          </tr>\n";
697 698 699 700 701 702 703 704 705 706
	    echo "<tr>
                    <td>Updated By: </td>
                    <td class=left>$updater</td>
     	          </tr>\n";
	    if ($updater_urn) {
		echo "<tr>
                        <td>Updater URN: </td>
                        <td class=left>$updater_urn</td>
         	          </tr>\n";
	    }
707
	}
708 709 710 711 712 713 714 715
	$deleted = $this->deleted();
	if (isset($deleted)) {
	    
	    echo "<tr>
                    <td><font color=red>Deleted: </font></td>
                    <td class=left><font color=red>$deleted</font></td>
     	          </tr>\n";
	}
716

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	#
	# Find the last time this image was used. 
	#
	$usage_result =
	    DBQueryFatal("select FROM_UNIXTIME(stamp) as lastused ".
			 "  from image_history ".
			 "where action='os_setup' and imageid='$imageid' ".
			 "order by stamp desc limit 1");
	if (mysql_num_rows($usage_result)) {
	    $urow = mysql_fetch_array($usage_result);
	    $lastused = $urow['lastused'];

	    echo "<tr>
                    <td>Last Used: </td>
                    <td class=\"left\">$lastused</td>
                  </tr>\n";
	}
734

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
        if ($isdataset) {
            echo "<tr>
                    <td>Dataset?: </td>
                    <td class=\"left\">Yes</td>
                  </tr>\n";
        }
        else {
            echo "<tr>
                    <td>Load Partition: </td>
                    <td class=\"left\">$loadpart</td>
                  </tr>\n";
            
            echo "<tr>
                    <td>Load Length: </td>
                    <td class=\"left\">$loadlength</td>
                  </tr>\n";
        }
752 753 754 755 756

	if ($part1_osid) {
	    echo "<tr>
                     <td>Partition 1 OS: </td>
                     <td class=\"left\">";
757
	    SpitOSIDLink($part1_osid, $part1_vers);
758 759 760 761 762 763 764 765
	    echo "   </td>
                  </tr>\n";
	}

	if ($part2_osid) {
	    echo "<tr>
                     <td>Partition 2 OS: </td>
                     <td class=\"left\">";
766
	    SpitOSIDLink($part2_osid, $part2_vers);
767 768 769 770 771 772 773 774
	    echo "   </td>
                  </tr>\n";
	}

	if ($part3_osid) {
	    echo "<tr>
                     <td>Partition 3 OS: </td>
                     <td class=\"left\">";
775
	    SpitOSIDLink($part3_osid, $part3_vers);
776 777 778 779 780 781 782 783
	    echo "   </td>
                  </tr>\n";
	}

	if ($part4_osid) {
	    echo "<tr>
                     <td>Partition 4 OS: </td>
                     <td class=\"left\">";
784
	    SpitOSIDLink($part4_osid, $part4_vers);
785 786 787 788 789 790 791 792
	    echo "   </td>
                  </tr>\n";
	}

	if ($default_osid) {
	    echo "<tr>
                     <td>Boot OS: </td>
                     <td class=\"left\">";
793
	    SpitOSIDLink($default_osid, $default_vers);
794 795 796 797 798 799 800 801 802 803 804
	    echo "   </td>
                  </tr>\n";
	}

	echo "<tr>
                <td>Filename: </td>
                <td class=left>\n";
	echo "$path";
	echo "  </td>
              </tr>\n";

805
        if (!$isdataset) {
806 807 808 809 810 811 812
            if ($this->architecture()) {
                echo "<tr>
                        <td>Architecture: </td>
                        <td class=left>" . $this->architecture();
                echo "  </td>
                     </tr>\n";
            }
813 814 815 816 817 818 819 820 821 822
            echo "<tr>
                      <td>Types: </td>
                      <td class=left>\n";
            echo "&nbsp;";
            foreach ($this->Types() as $type) {
                echo "$type &nbsp; ";
            }
            echo "  </td>
                  </tr>\n";
        }
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

	echo "<tr>
                <td>Shared?: </td>
                <td class=left>\n";
	if ($shared)
	    echo "Yes";
	else
	    echo "No";
    
	echo "  </td>
              </tr>\n";

	echo "<tr>
                <td>Global?: </td>
                <td class=left>\n";

839 840
	$globalflip = ($globalid ? 0 : 1);
	$globalval  = ($globalid ? "Yes" : "No");
841 842
	echo "$globalval (<a href='toggle.php?imageid=$imageid".
	    "&type=imageglobal&value=$globalflip'>Toggle</a>)";
843 844 845 846
	echo "  </td>
              </tr>\n";

	echo "<tr>
847 848
                <td>Internal ID (Vers): </td>
                <td class=left>$imageid ($version)</td>
849 850
              </tr>\n";

851 852 853 854
	if ($this->parent_imageid()) {
	    $p_imageid   = $this->parent_imageid();
	    $p_version   = $this->parent_version();
	    $p_image     = Image::Lookup($p_imageid, $p_version);
855 856 857 858 859 860
	    # On an elabinelab we will not have the previous version.
	    # if it came in at creation time.
	    if ($p_image) {
		$p_imagename = $p_image->imagename();
		$p_url       = CreateURL("showimageid", $p_image,
					 "version", $p_version);
861
	    
862 863 864 865 866 867
		echo "<tr>
                        <td>Derived from: </td>
                        <td class=left><a href='$p_url'>
                          ${p_imagename}:${p_version}</a></td>
                     </tr>\n";
	    }
868
	}
869 870 871 872 873 874 875 876 877 878 879 880 881 882
	if ($this->version() > 0 &&
	    (is_null($this->parent_imageid()) ||
	     ($this->parent_imageid() &&
	      $this->parent_imageid() != $this->imageid()))) {
	    $p_version   = $this->version() - 1;
	    $p_url       = CreateURL("showimageid", $this,
				     "version", $p_version);
	    echo "<tr>
                    <td>Previous Vers: </td>
                    <td class=left>
                        <a href='$p_url'>${imagename}:${p_version}</a></td>
                  </tr>\n";
	}

883 884 885 886 887 888 889 890 891
	# Look for an unreleased version of this image.
	$unreleased = $this->LookupUnreleased();
	if ($unreleased && $unreleased->version() != $this->version()) {
	    $u_version = $unreleased->version();
	    $u_url     = CreateURL("showimageid", $this, "version", $u_version);

	    echo "<tr>
                    <td>Unreleased Vers: </td>
                    <td class=left>
Leigh Stoller's avatar
Leigh Stoller committed
892
                         <a href='$u_url'>${imagename}:${u_version}</a></td>
893 894 895 896 897
                  </tr>\n";
	}
	if ($DOPROVENANCE) {
	    $released = $this->released();
	    $ready    = $this->ready();
898
	    $isdelta  = $this->size() ? 0 : 1;
899
	    $nodelta  = $this->nodelta();
900 901
	    
	    echo "<tr>
902 903
                    <td>Rdy/Rel/Delta/NoD: </td>
                    <td class=left>$ready/$released/$isdelta/$nodelta</td>
904 905
                  </tr>\n";
	}
906

907 908 909 910 911
	echo "<tr>
                <td>MBR Version: </td>
                <td class=left>$mbr_version</td>
              </tr>\n";

912 913 914 915 916 917 918
	# Until I change the schema.
	if ($mbr_version == 99) {
	    echo "<tr>
                    <td>XEN Package: </td>
                    <td class=left>Yes</td>
                  </tr>\n";
	}
919 920
	if ($this->ezid()) {
	    $doesxen = 0;
921
	    $osinfo = $this->OSinfo();
922 923 924 925 926 927 928 929 930 931 932 933 934
	    if ($osinfo && $osinfo->def_parentosid()) {
		$parentosinfo = OSinfo::Lookup($osinfo->def_parentosid());
		if ($parentosinfo &&
		    $parentosinfo->FeatureSupported("xen-host")) {
		    $doesxen = 1;
		}
	    }
	    $xenval  = ($doesxen ? "Yes" : "No");
	    $xenflip = ($doesxen ? 0 : 1);

	    echo "<tr>
                  <td>XEN Capable?:</td>
   	          <td class=left>
935 936
                     $xenval (<a href='toggle.php?imageid=$imageid".
		          "&type=imagedoesxen&value=$xenflip'>Toggle</a>
937 938 939 940 941 942 943
                      if you know this image can run
               as a XEN guest. More info
               <a target=_blank
                  href='https://wiki.emulab.net/wiki/Emulab/wiki/xen'>here</a>)
              </td>
             </tr>\n";
	}
944

945 946 947 948 949 950 951
	if ($hash) {
	    echo "<tr>
                    <td>SHA1 Hash: </td>
                    <td class=left>$hash</td>
                  </tr>\n";
	}

952
	echo "<tr>
953
                <td>Version UUID: </td>
954 955 956
                <td class=left>$uuid</td>
              </tr>\n";

957
	echo "<tr>
958 959 960 961 962 963
                <td>Image UUID: </td>
                <td class=left>$image_uuid</td>
              </tr>\n";

	if ($metadata_url) {
            echo "<tr>
964 965 966
                <td>Metadata URL: </td>
                <td class=left><a href='$metadata_url'>https:// ...</a></td>
              </tr>\n";
967 968 969 970 971 972 973 974 975 976 977 978 979
        }
        else {
	    $version_url = "$TBBASE/image_metadata.php?uuid=$uuid";
	    $image_url   = "$TBBASE/image_metadata.php?uuid=$image_uuid";
            echo "<tr>
                     <td>Version URL: </td>
                     <td class=left><a href='$version_url'>https:// ...</a></td>
                  </tr>\n";
            echo "<tr>
                     <td>ImageURL: </td>
                     <td class=left><a href='$image_url'>https:// ...</a></td>
                  </tr>\n";
	}
980

981 982
        echo "<tr>
                 <td>Notes: </td>
983 984 985 986
                 <td class=left>
                     <textarea rows=4 cols=60 readonly>" .
			    str_replace("\r", "", $notes) .
	              "</textarea></td>
987 988
              </tr>\n";

989 990 991 992 993 994 995
	if ($imagefile_url) {
	    echo "<tr>
                   <td>Image File URL: </td>
                   <td class=left><a href='$imagefile_url'>https:// ...</a></td>
                  </tr>\n";
	}

996 997 998 999 1000
	#
	# Show who all can access this image outside the project.
	#
	if ($showperms) {
	    $query_result =
1001
		DBQueryFatal("select distinct * from image_permissions ".
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
			     "where imageid='$imageid' ".
			     "order by permission_type,permission_id");
	    if (mysql_num_rows($query_result)) {
		echo "<tr>
                      <td align=center colspan=2>
                      External permissions
                      </td>
                  </tr>\n";

		while ($row = mysql_fetch_array($query_result)) {
		    $perm_type = $row['permission_type'];
		    $perm_idx  = $row['permission_idx'];
		    $writable  = $row['allow_write'];

		    if ($writable) {
			$writable = "(read/write)";
		    }
		    else {
			$writable = "(read only)";
		    }

		    if ($perm_type == "user") {
			$user = User::Lookup($perm_idx);
			if (isset($user)) {
			    $uid = $user->uid();
			    echo "<tr>
                                    <td>User: </td>
                                    <td class=left>$uid $writable</td>
                                  </tr>\n";
			}
		    }
		    elseif ($perm_type == "group") {
			$group = Group::Lookup($perm_idx);
			if (isset($group)) {
			    $pid = $group->pid();
			    $gid = $group->gid();
			    echo "<tr>
                                    <td>Group: </td>
                                    <td class=left>$pid/$gid $writable</td>
                                  </tr>\n";
			}
		    }
		}
	    }
	}

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	echo "</table>\n";
    }

    #
    # See if an image is inuse.
    #
    function InUse() {
	$imageid = $this->imageid();

	$query_result1 =
	    DBQueryFatal("select * from current_reloads ".
			 "where image_id='$imageid'");
	$query_result2 =
	    DBQueryFatal("select * from scheduled_reloads ".
			 "where image_id='$imageid'");
	$query_result3 =
	    DBQueryFatal("select * from node_type_attributes ".
			 "where attrkey='default_imageid' and ".
			 "      attrvalue='$imageid' limit 1");

	if (mysql_num_rows($query_result1) ||
	    mysql_num_rows($query_result2) ||
	    mysql_num_rows($query_result3)) {
	    return 1;
	}
	return 0;
    }
1075

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    function TypeList() {
	$imageid = $this->imageid();
        $version = $this->version();
        $result  = array();

        #
        # Deleted images stash a list in the descriptor.
        #
        $deleted = $this->deleted();
        if (isset($deleted)) {
            $nodetypes = $this->nodetypes();
            if (isset($nodetypes)) {
                foreach (preg_split("/,/", $this->nodetypes()) as $type) {
                    $result[] = $type;
                }
            }
            return $result;
        }
        #
        # If there is an architecture set in the image, we use that to
        # find the matching types in the node_types table. This overrides
        # anything found in the osidtoimageid.
        #
        $arch = $this->architecture();
        if (isset($arch)) {
            $query_result =
                DBQueryFatal("select distinct nt.type from images as i ".
                             # The image architecture could be a short list.
                             "inner join node_types as nt on ".
                             "   FIND_IN_SET(nt.architecture,i.architecture) ".
                             "where i.imageid='$imageid'");

            $osinfo = OSinfo::Lookup($imageid, $version);
            if ($osinfo && $osinfo->def_parentosid()) {
                $result[] = "pcvm";
            }
        }
        else {
            $query_result =
                DBQueryFatal("select distinct type from osidtoimageid ".
                             "where imageid='$imageid'");
        }
        while ($row = mysql_fetch_array($query_result)) {
            $type = $row['type'];
            $result[] = $type;
        }
	return $result;
    }

1125 1126 1127 1128 1129 1130 1131
    function GetLogfile() {
	$this->Refresh();
	
	if ($this->logfileid()) 
	    return Logfile::Lookup($this->logfileid());
	return null;
    }
1132 1133 1134

    function DoesXen($does) {
	$imageid = $this->imageid();
1135
	$version = $this->version();
1136 1137
	
	if ($does) {
1138 1139 1140 1141 1142 1143 1144 1145
            if (TBSiteVarExists("general/default_xen_parentosid")) {
                $xenname = TBGetSiteVar("general/default_xen_parentosid");
            }
            else {
                $xenname = "emulab-ops,XEN43-64-STD";
            }
	    list($pid,$osname) = preg_split('/,/', $xenname);
	    $parentosinfo = OSinfo::LookupByName($pid,$osname);
1146
	    if (!$parentosinfo) {
1147
                return -1;
1148 1149 1150
	    }
	    $parentosid = $parentosinfo->osid();

1151 1152
	    DBQueryFatal("update os_info_versions set ".
			 "    def_parentosid='$parentosid' ".
Leigh Stoller's avatar
Leigh Stoller committed
1153
			 "where osid='$imageid' and vers='$version'");
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	    DBQueryFatal("replace into os_submap set ".
			 "  osid='$imageid', parent_osid='$parentosid'");
	    DBQueryFatal("replace into osidtoimageid set ".
			 " osid='$imageid', type='pcvm', imageid='$imageid'");
	}
	else {
	    DBQueryFatal("delete from osidtoimageid ".
			 "where osid='$imageid' and type='pcvm'");
	    DBQueryFatal("delete from os_submap ".
			 "where osid='$imageid'");
1164 1165
	    DBQueryFatal("update os_info_versions set def_parentosid=NULL ".
			 "where osid='$imageid' and vers='$version'");
1166 1167 1168
	}
	return 0;
    }
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$pid = $this->pid();
	$imagename = $this->imagename();
	$imageid = $this->imageid();
	$version = $this->version();
	
	$html = "<font size=+1>Image <b>".
	    "<a href='showproject.php3?pid=$pid'>$pid</a>/".
1181 1182
	    "<a href='showimageid.php3?imageid=$imageid&version=$version'>".
	    "$imagename</a>".
1183 1184 1185 1186
	    "</b></font>\n";

	return $html;
    }
1187 1188 1189
    function OSinfo() {
	return OSinfo::Lookup($this->imageid(), $this->version());
    }
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

    function URL() {
        global $TBBASE;
	$uuid           = $this->uuid();
        $image_uuid     = $this->image_uuid();

        return "$TBBASE/image_metadata.php?uuid=$image_uuid";
    }
    function VersionURL() {
        global $TBBASE;
	$uuid           = $this->uuid();
        $image_uuid     = $this->image_uuid();

        return "$TBBASE/image_metadata.php?uuid=$uuid";
    }
1205
}