Image.pm.in 75.5 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2017 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
#
package Image;

use strict;
27
use Carp;
28
use Exporter;
29
use vars qw(@ISA @EXPORT $AUTOLOAD);
30 31 32 33 34 35

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use libdb;
36
use EmulabConstants;
37
use libtestbed;
38
use WebTask;
39 40
use English;
use Data::Dumper;
41
use File::Basename;
42 43 44 45 46 47 48 49 50
use overload ('""' => 'Stringify');

# Configure variables
my $TB		  = "@prefix@";
my $BOSSNODE      = "@BOSSNODE@";
my $CONTROL	  = "@USERNODE@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBAUDIT       = "@TBAUDITEMAIL@";
my $TBBASE        = "@TBBASE@";
51
my $WWWHOST       = "@WWWHOST@";
52
my $OURDOMAIN     = "@OURDOMAIN@";
53
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
54
my $DOPROVENANCE  = @IMAGEPROVENANCE@;
55
my $TBWWW         = "@TBWWW@";
Leigh Stoller's avatar
Leigh Stoller committed
56 57
my $PROJROOT      = "@PROJROOT_DIR@";
my $GROUPROOT     = "@GROUPSROOT_DIR@";
58 59 60 61 62

# Cache of instances to avoid regenerating them.
my %images    = ();
my $debug      = 0;

63 64 65 66 67 68 69
# Concat id/vers.
sub versid($)
{
    my ($self) = @_;

    return $self->imageid() . ":" . $self->version();
}
70 71 72 73 74 75
sub versname($)
{
    my ($self) = @_;

    return $self->pid() . "/" . $self->imagename() . ":" . $self->version();
}
76 77 78 79 80
sub urn($)
{
    my ($self) = @_;

    return undef
81
	if (! $PGENISUPPORT);    
82 83

    require GeniHRN;
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    my $pid    = $self->pid();
    my $gid    = $self->gid();
    my $name   = $self->imagename();
    my $domain = $OURDOMAIN;

    if ($self->isdataset()) {
	$domain .= ":${pid}";
	$domain .= ":${gid}" if ($pid ne $gid);
	
	return GeniHRN::Generate($domain, "imdataset", $name);
    }
    else {
	return GeniHRN::Generate($domain, "image", $pid . "//" . $name);
    }
98
}    
99

100 101 102 103 104 105 106 107 108 109
# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    return system($command);
}

110 111 112
sub BlessRow($$)
{
    my ($class, $row) = @_;
113

114 115 116
    my $self           = {};
    my $imageid        = $row->{"imageid"};
    $self->{'IMAGE'}   = $row;
117
    $self->{'HASH'}    = {};
118 119

    bless($self, $class);
120 121 122 123 124

    # Grab the webtask. For images these are transient.
    if (defined($self->webtask_id())) {
	$self->{'WEBTASK'} = WebTask->Lookup($self->webtask_id());
    }
125 126 127
    return $self;
}

128
#
129 130
# Lookup by idx or pid,imagename[:version] depending on the args. We always 
# return highest numbered version on this path, if no version specified.
131
#
132
sub Lookup($$;$$)
133
{
134
    my ($class, $arg1, $arg2, $arg3) = @_;
135 136

    #
137 138
    # A single arg is either an index or "pid,imagename[:version]" or
    # "pid/imagename[:version]" string.
139 140 141
    #
    if (!defined($arg2)) {
	if ($arg1 =~ /^(\d*)$/) {
142
	    my $result =
143 144
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
145
			    "  from images as i ".
146 147 148 149 150 151 152
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid and v.version=i.version ".
			    "where i.imageid='$arg1'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
153
	}
154
	elsif ($arg1 =~ /^(\d*):(\d*)$/) {
Leigh Stoller's avatar
Leigh Stoller committed
155 156 157 158
	    #
	    # This will get deleted images, but that is okay.
	    #
	    my $result =
159 160
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
Leigh Stoller's avatar
Leigh Stoller committed
161 162 163 164 165 166 167 168
			    "  from image_versions as v ".
			    "left join images as i on ".
			    "     v.imageid=i.imageid ".
			    "where v.imageid='$1' and v.version='$2'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
169
	}
170
	elsif ($arg1 =~ /^([-\w]*),([-\w\.\+]*)$/ ||
171 172
		$arg1 =~ /^([-\w]*)\/([-\w\.\+]*)$/) {
	    my $result =
173 174
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
175
			    "  from images as i ".
176 177 178 179 180 181 182 183 184 185 186
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid and v.version=i.version ".
			    "where i.pid='$1' and i.imagename='$2'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^([-\w]*),([-\w\.\+]*):(\d*)$/ ||
		$arg1 =~ /^([-\w]*)\/([-\w\.\+]*):(\d*)$/) {
	    my $result =
187 188
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
189
			    "  from images as i ".
190 191
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid ".
192
			    "where i.pid='$1' and i.imagename='$2' and ".
193
			    "      v.version='$3'");
194 195 196 197
	    return undef
		if (!$result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref())
198
	}
Leigh Stoller's avatar
Leigh Stoller committed
199
	elsif ($arg1 =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
200 201 202 203 204
	    #
	    # First look to see if the uuid is for the image itself,
	    # which means current version. Otherwise look for a
	    # version with the uuid.
	    #
Leigh Stoller's avatar
Leigh Stoller committed
205
	    my $result =
206 207
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
208 209 210 211 212 213 214 215
			    "  from images as i ".
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid and v.version=i.version ".
			    "where i.uuid='$arg1'");
	    return undef
		if (!$result);
	    return BlessRow($class, $result->fetchrow_hashref())
		if ($result->numrows);
Leigh Stoller's avatar
Leigh Stoller committed
216

217
	    $result =
218 219
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
220 221 222 223 224
			    "  from image_versions as v ".
			    "left join images as i on ".
			    "     v.imageid=i.imageid ".
			    "where v.uuid='$arg1' and ".
			    "      v.deleted is null");
Leigh Stoller's avatar
Leigh Stoller committed
225 226 227
	    return undef
		if (! $result || !$result->numrows);

228
	    return BlessRow($class, $result->fetchrow_hashref());
Leigh Stoller's avatar
Leigh Stoller committed
229
	}
230 231
	return undef;
    }
232 233
    elsif (!defined($arg3)) {
	if ($arg1 =~ /^\d+$/ && $arg2 =~ /^\d+$/) {
234 235 236
	    #
	    # This will get deleted images, but that is okay.
	    #
237
	    my $result =
238 239
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
240 241
			    "  from image_versions as v ".
			    "left join images as i on ".
242
			    "     v.imageid=i.imageid ".
243
			    "where v.imageid='$arg1' and v.version='$arg2'");
244 245
	    return undef
		if (! $result || !$result->numrows);
246

247 248 249 250
	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^([-\w\.\+]*):(\d+)$/) {
	    my $result =
251 252
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
253
			    "  from images as i ".
254 255 256 257 258 259
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid ".
			    "where i.pid='$arg1' and i.imagename='$1' and ".
			    "      v.version='$2'");
	    return undef
		if (! $result || !$result->numrows);
260

261 262 263 264
	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^[-\w\.\+]*$/) {
	    my $result =
265 266
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
267
			    "  from images as i ".
268 269 270 271 272
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid and v.version=i.version ".
			    "where i.pid='$arg1' and i.imagename='$arg2'");
	    return undef
		if (! $result || !$result->numrows);
273

274 275 276
	    return BlessRow($class, $result->fetchrow_hashref());
	}
	return undef;
277
    }
278 279 280 281
    else {
	if ($arg1 =~ /^[-\w]*$/ &&
	    $arg2 =~ /^[-\w\.\+]*$/ && $arg3 =~ /^\d+$/) {
	    my $result =
282 283
		DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
			    "   i.metadata_url as image_metadata_url ".
284
			    "  from images as i ".
285 286 287
			    "left join image_versions as v on ".
			    "     v.imageid=i.imageid ".
			    "where i.pid='$arg1' and i.imagename='$arg2' and ".
288
			    "      v.version='$arg3'");
289 290
	    return undef
		if (!$result || !$result->numrows);
291

292 293 294 295
	    return BlessRow($class, $result->fetchrow_hashref());
	}
    }
    return undef;
296
}
297 298 299 300 301 302 303 304 305

AUTOLOAD {
    my $self  = $_[0];
    my $type  = ref($self) or croak "$self is not an object";
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    # A DB row proxy method call.
    if (exists($self->{'IMAGE'}->{$name})) {
306 307 308 309
	# Allow update.
	if (scalar(@_) == 2) {
	    $self->{'IMAGE'}->{$name} = $_[1];
	}
310 311
	return $self->{'IMAGE'}->{$name};
    }
312 313 314 315 316 317 318 319 320
    # Or it is for a local storage slot.
    if ($name =~ /^_.*$/) {
	if (scalar(@_) == 2) {
	    return $self->{'HASH'}->{$name} = $_[1];
	}
	elsif (exists($self->{'HASH'}->{$name})) {
	    return $self->{'HASH'}->{$name};
	}
    }
321
    print STDERR Dumper($self);
322 323 324
    carp("No such slot '$name' field in class $type");
    return undef;
}
Leigh Stoller's avatar
Leigh Stoller committed
325 326 327 328 329 330 331 332
sub field($$) {
    my ($self, $name) = @_;
    
    if (exists($self->{'IMAGE'}->{$name})) {
	return $self->{'IMAGE'}->{$name};
    }
    return undef;
}
333 334 335 336 337 338 339 340 341 342 343 344 345 346
sub fieldExists($$) {
    my ($self, $name) = @_;

    return 1
	if (exists($self->{'IMAGE'}->{$name}));
    return 0;
}
sub fieldSet($$$) {
    my ($self, $name, $value) = @_;

    $self->{'IMAGE'}->{$name} = $value;
    return $value;
}
sub isImageAlias($)	{ return 0; }
347 348 349 350 351

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

352 353 354
    $self->{'IMAGE'}    = undef;
    $self->{'HASH'}     = undef;
    $self->{'WEBTASK'}  = undef;
355
}
356
sub webtask($)  { return $_[0]->{'WEBTASK'}; }
357 358 359 360 361 362 363

#
# Lookup by URL.
#
sub LookupByURL($$)
{
    my ($class, $url) = @_;
364
    my ($pid,$imagename);
365 366

    return undef
367
	if (! ($url =~ /^(ftp|http|https|file):/));
368 369

    my $safe_url = DBQuoteSpecial($url);
370 371 372 373

    #
    # First check for a non version specific URL (latest version).
    #
374
    my $query_result =
375 376
	DBQueryWarn("select pid,imagename from images ".
		    "where metadata_url=$safe_url");
377
    return undef
378
	if (!$query_result);
379

380 381 382 383 384
    if ($query_result->numrows) {
	my ($pid,$imagename) = $query_result->fetchrow_array();
	
	return Image->Lookup($pid, $imagename);
    }
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    
    #
    # Then a version specific URL.
    #
    $query_result =
	DBQueryWarn("select pid,imagename,version from image_versions ".
		    "where metadata_url=$safe_url and deleted is null");
    return undef
	if (!$query_result);

    if ($query_result->numrows) {
	my ($pid,$imagename,$version) = $query_result->fetchrow_array();
	
	return Image->Lookup($pid, $imagename, $version);
    }
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    else {
	#
	# If the URL points back to ourself, find the image it refers
	# to. Later phases will catch misuse of the URL (not allowed,
	# does not exist).
	#
	require URI;
	require URI::QueryParam;
	
	my $uri = URI->new($url);
	return undef
	    if (!defined($uri));
	return undef
	    if ($uri->host() ne $WWWHOST);
	my $uuid = $uri->query_param("uuid");
	return undef
	    if (!defined($uuid) || $uuid eq "");

	return Image->Lookup($uuid);
    }
    return undef;
421
}
422 423 424 425 426 427 428
# Image is local if the metadata_url is null.
sub IsLocal($)
{
    my ($self) = @_;

    return (defined($self->metadata_url()) ? 0 : 1);
}
429

430 431 432 433 434 435 436 437 438 439
#
# Lookup a "system" osid, meaning one in the emulab-ops project.
#
sub LookupByName($$)
{
    my ($class, $imagename) = @_;

    return Image->Lookup(TBOPSPID(), $imagename);
}

440 441 442 443 444 445 446 447 448 449 450
#
# Lookup by Origin UUID.
#
sub LookupByOriginUUID($$$)
{
    my ($class, $pid, $uuid) = @_;
    my $safe_uuid = DBQuoteSpecial($uuid);
    my $safe_pid = DBQuoteSpecial($pid);

    my $query_result =
	DBQueryWarn("select imageid from image_versions ".
451
		    "where pid=$safe_pid and ".
452 453 454 455 456 457 458 459
		    "      origin_uuid=$safe_uuid and deleted is null");
    return undef
	if (!$query_result || !$query_result->numrows);

    my ($imageid) = $query_result->fetchrow_array();
    return Image->Lookup($imageid);
}

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
#
# Lookup by Origin URN.
#
sub LookupByOriginURN($$)
{
    my ($class, $urn) = @_;

    return undef
	if (! $PGENISUPPORT);    

    require GeniHRN;
    return undef
	if (!GeniHRN::IsValid($urn));
    
    my $safe_urn = DBQuoteSpecial($urn);

    my $query_result =
	DBQueryWarn("select imageid,version from image_versions ".
		    "where origin_urn=$safe_urn and deleted is null");
    return undef
	if (!$query_result || !$query_result->numrows);

    my ($imageid,$version) = $query_result->fetchrow_array();
    return Image->Lookup($imageid, $version);
}

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
#
# Lookup by Authority URN. Only for datasets right now.
#
sub LookupByAuthorityURN($$)
{
    my ($class, $urn) = @_;

    return undef
	if (! $PGENISUPPORT);    

    require GeniHRN;
    return undef
	if (!GeniHRN::IsValid($urn));
    
    my $safe_urn = DBQuoteSpecial($urn);

    my $query_result =
	DBQueryWarn("select distinct imageid from image_versions ".
		    "where authority_urn=$safe_urn and deleted is null");
    return undef
	if (!$query_result || !$query_result->numrows);

    my ($imageid) = $query_result->fetchrow_array();
    return Image->Lookup($imageid);
}

512 513 514 515 516 517 518
#
# Return a list of all images of the given format for the given pid.
# If format is NULL, return all formats. If pid is NULL, return for all pids.
# List is names of the form "pid/imagename".
#
sub ListAll($$$)
{
519
    my ($class, $format, $pid) = @_;
520 521 522 523 524 525 526 527 528
    my @result = ();

    my $clause = "";
    if (defined($format) && $format =~ /^([-\w]+)$/) {
	$clause .= ($clause ? " and" : "where");
	$clause .= " format='$1'";
    }
    if (defined($pid) && $pid =~ /^([-\w]+)$/) {
	$clause .= ($clause ? " and" : "where");
529
	$clause .= " i.pid='$1'";
530 531 532
    }

    my $query_result =
533 534 535 536
	DBQueryWarn("select distinct i.pid,i.imagename from images as i ".
		    "left join image_versions as v on ".
		    "     v.imageid=i.imageid and v.version=i.version ".
		    "$clause order by i.pid,i.imagename");
537 538 539 540 541 542 543 544
    if ($query_result) {
	while (my ($pid,$name) = $query_result->fetchrow_array()) {
	    push(@result, "$pid/$name");
	}
    }
    return @result;
}

545 546 547 548 549 550 551 552 553 554 555 556 557 558
#
# Return a list of all versions of all images of the given format for the
# given pid.
#
# If format is NULL, return all formats. If pid is NULL, return for all pids.
# List is names of the form "pid/imagename:version".
#
sub ListAllVersions($$$)
{
    my ($class, $format, $pid) = @_;
    my @result = ();

    my $clause = "";
    if (defined($format) && $format =~ /^([-\w]+)$/) {
Mike Hibler's avatar
Mike Hibler committed
559
	$clause .= "and v.format='$1' ";
560 561
    }
    if (defined($pid) && $pid =~ /^([-\w]+)$/) {
Mike Hibler's avatar
Mike Hibler committed
562
	$clause .= "and v.pid='$1' ";
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    }

    my $query_result =
	DBQueryWarn("select v.pid,v.imagename,v.version from ".
		    "images as i, image_versions as v where ".
		    "i.imageid=v.imageid and v.deleted is null ".
		    "$clause order by v.pid,v.imagename,v.version");
    if ($query_result) {
	while (my ($pid,$name,$vers) = $query_result->fetchrow_array()) {
	    push(@result, "$pid/$name:$vers");
	}
    }
    return @result;
}

578 579 580 581 582 583 584 585 586 587 588
#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

    return -1
	if (! ref($self));

    my $imageid = $self->imageid();
589
    my $version = $self->version();
590

591
    my $query_result =
592 593
	DBQueryWarn("select i.*,v.*,i.uuid as image_uuid, ".
		    "   i.metadata_url as image_metadata_url ".
594 595 596 597
		    "  from image_versions as v ".
		    "left join images as i on ".
		    "     v.imageid=i.imageid ".
		    "where v.imageid='$imageid' and v.version='$version'");
598 599 600 601 602 603 604 605
    return -1
	if (!$query_result || !$query_result->numrows);

    $self->{'IMAGE'} = $query_result->fetchrow_hashref();

    return 0;
}

606 607 608 609
#
# Create a new os_info. This installs the new record in the DB,
# and returns an instance. There is some bookkeeping along the way.
#
610
sub Create($$$$$$$$)
611
{
612 613
    my ($class, $project, $group, $creator,
	$imagename, $imageid, $argref, $usrerr_ref) = @_;
614
    require OSinfo;
615 616 617 618 619

    return undef
	if (ref($class) || !ref($project));

    my $isadmin = $creator->IsAdmin();
620 621
    my $isdataset = (exists($argref->{"isdataset"}) ?
		     $argref->{"isdataset"} : 0);
622 623
    my $architecture = $argref->{'architecture'}
        if (defined($argref->{'architecture'}));
624 625 626 627 628 629

    # We may ignore particular partN_osid's by deleting them.
    my @arg_slots = grep(/^part[1-4]_osid$/, keys(%{$argref}));

    # Pass-through a bunch of required slots, ignoring any extras
    # and stuff we handle explicitly.
630 631 632 633 634 635 636
    if (!$isdataset) {
	foreach my $key ("loadpart", "loadlength", "default_osid") {
	    if (!exists($argref->{$key})) {
		$$usrerr_ref = "Error: $key missing in Image->Create!";
		return undef;
	    }
	    push(@arg_slots, $key);
637 638 639
	}
    }
    # Pass-through optional slots, otherwise the DB default is used.
640
    foreach my $key ("path", "shared", "global", "ezid", "mbr_version",
641
		     "metadata_url", "imagefile_url", "released",
642 643
		     "isdataset", "lba_size", "lba_low", "lba_high",
		     "origin_uuid", "origin_urn", "origin_name") {
644 645 646 647 648 649 650 651 652 653 654 655 656 657
	if (exists($argref->{$key})) {
	    push(@arg_slots, $key);
	}
    }

    my $pid     = $project->pid();
    my $pid_idx = $project->pid_idx();
    my $gid     = $group->gid();
    my $gid_idx = $group->gid_idx();
    my $uid     = $creator->uid();
    my $uid_idx = $creator->uid_idx();

    #
    # The pid/imageid has to be unique, so lock the table for the check/insert.
658
    # We lock the os_info tables too, for the call into Lookup.
659
    #
660 661 662
    DBQueryWarn("lock tables images write, image_versions write, ".
		"            os_info as o read, os_info_versions as v read, ".
		"            emulab_indicies write")
663 664 665 666 667 668 669 670 671 672 673
	or return undef;

    my $query_result =
	DBQueryWarn("select imagename from images ".
		    "where pid_idx='$pid_idx' and imagename='$imagename'");

    if ($query_result->numrows) {
	DBQueryWarn("unlock tables");
	$$usrerr_ref = "Error: IMAGE $imagename in project $pid already exists!";
	return undef;
    }
674 675 676 677 678 679
    # The metadata_url also has to be unique.
    if (exists($argref->{'metadata_url'})) {
	my $metadata_url = $argref->{'metadata_url'};
	my $safe_url = DBQuoteSpecial($metadata_url);
	
	my $query_result =
680 681
	    DBQueryWarn("select imagename from image_versions ".
			"where metadata_url=$safe_url and deleted is null");
682 683 684 685 686 687 688

	if ($query_result->numrows) {
	    DBQueryWarn("unlock tables");
	    $$usrerr_ref = "Error: IMAGE $metadata_url already exists!";
	    return undef;
	}
    }
689

690 691
    # We allow providing an image descriptor index, so newimageid_ez can use
    # the same name, description, and index for both the OS and Image it makes.
692
    if (defined($imageid) && $imageid > 0 ) {
693 694 695 696 697 698 699 700 701 702
	# Make sure the OS descriptor for it already exists.
	if (!OSinfo->Lookup($imageid)) {
	    DBQueryWarn("unlock tables");
	    $$usrerr_ref = "Error: Missing OSinfo $imagename/$imageid in $pid!";
	    return undef;
	}
    } else {
	#
	# Grab unique ID. Table already locked.
	# 
703
	$imageid  = TBGetUniqueIndex("next_osid", undef, 1);
704
    }
705 706 707 708 709 710 711
    #
    # The image UUID refers to the image, current version. Then each
    # version has its own uuid so it can be referred to. 
    #
    my $image_uuid   = NewUUID();
    my $version_uuid = NewUUID();
    #
712 713
    # Some fields special cause of quoting.
    #
714
    my $desc  = "''";
715 716 717
    if (exists($argref->{'description'})) {
	$desc = DBQuoteSpecial($argref->{'description'});
    }
718 719 720 721
    my $bquery = "imagename='$imagename'";
    $bquery .= ",imageid='$imageid'";
    $bquery .= ",pid='$pid',pid_idx='$pid_idx'";
    $bquery .= ",gid='$gid',gid_idx='$gid_idx'";
722

723
    # image_versions include all the images stuff.
724 725
    my $query = "insert into image_versions set $bquery, ".
	join(",", map("$_='" . $argref->{$_} . "'", @arg_slots));
726
    # except for this.
727 728 729
    $bquery .= ",architecture='$architecture'"
	if (defined($architecture));

730
    $query .= ",creator='$uid',creator_idx='$uid_idx'";
731
    $query .= ",uuid='$version_uuid'";
732
    $query .= ",created=now()";
Leigh Stoller's avatar
Leigh Stoller committed
733 734
    $query .= ",description=$desc";
    $query .= ",hash=" . DBQuoteSpecial($argref->{'hash'})
735 736 737
	if (exists($argref->{'hash'}) && $argref->{'hash'} ne '');
    $query .= ",deltahash=" . DBQuoteSpecial($argref->{'deltahash'})
	if (exists($argref->{'deltahash'}) && $argref->{'deltahash'} ne '');
738 739 740 741 742 743
    $query .= ",size=" . DBQuoteSpecial($argref->{'size'})
	if (exists($argref->{'size'}));
    $query .= ",deltasize=" . DBQuoteSpecial($argref->{'deltasize'})
	if (exists($argref->{'deltasize'}));
    $query .= ",notes=" . DBQuoteSpecial($argref->{'notes'})
	if (exists($argref->{'notes'}) && $argref->{'notes'} ne '');
744 745 746 747 748 749

    #
    # If this is set in the environment, we use it. This allows
    # us to track actual geni users, since all of that happens
    # as geniuser. We do not bother to set this for local users.
    #
750 751
    if (exists($ENV{'REALGENIURN'}) && $ENV{'REALGENIURN'} ne "") {
	$query .= ",creator_urn=". DBQuoteSpecial($ENV{'REALGENIURN'});
752
    }
753 754

    # Create the main entry:
755
    if (! DBQueryWarn("insert into images set $bquery,uuid='$image_uuid'")) {
756 757 758 759 760
	DBQueryWarn("unlock tables");
	tberror("Error inserting new images record for $pid/$imagename!");
	return undef;
    }
    # And the other entry.
761
    if (! DBQueryWarn($query)) {
762
	DBQueryWarn("delete from images where uuid='$image_uuid'");
763 764 765 766
	DBQueryWarn("unlock tables");
	tberror("Error inserting new images record for $pid/$imagename!");
	return undef;
    }
767
    DBQueryWarn("unlock tables");
768 769 770 771 772

    my $image = Image->Lookup($imageid);
    # Create the osidtoimageid mapping.  Admins have an option to do it or not.
    my $makedefault = exists($argref->{"makedefault"}) && 
	$argref->{"makedefault"} eq "1";
773 774

    if (!$isdataset && !defined($architecture) && (!$isadmin || $makedefault)){
775
	#
776 777 778
	# Dig out the mtypes we want to turn on. The caller has already
	# sanity checked them to make sure the types actually exist, and
	# that there are nodes matching that type.
779
	#
780 781 782 783 784
	my @map_updates = ();
	foreach my $key (grep(/^mtype_/, keys(%{$argref}))) {
	    if ($argref->{$key} eq "1") {
		$key =~ s/^mtype_//;
		push(@map_updates, $key);
785 786 787
	    }
	}

788
	my @osid_array = ();
789
	for (my $i = 1; $i <= 4; $i++) {
790 791
	    my $func = "part${i}_osid";
	    my $foo  = $image->$func();
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	    if (defined($foo)) {
		push(@osid_array, $foo);
	    }
	}

	for (my $i = 0; $i <= $#map_updates; $i++) {
	    for (my $j = 0; $j <= $#osid_array; $j++) {
		 my $query = "INSERT INTO osidtoimageid ".
			     "       (osid, type, imageid) ".
			     "VALUES ('$osid_array[$j]', ".
			     "        '$map_updates[$i]', ".
			     "        '$imageid')";
		 ##print "$query\n";
		 if (! DBQueryWarn($query)) {
		     tberror("Error inserting new ostoimageid mapping for ".
			     "$pid/$imagename!");
		     return undef;
		 }
	    }
	}
    }
813 814 815 816 817 818
    return $image;
}

#
# Clone an image descriptor from the DB, bumping the version number
#
819 820
# XXX A lot of stuff in here gets skipped for datasets.
#
821
sub NewVersion($$$$)
822
{
823
    my ($self, $creator, $baseimage, $usrerr_ref) = @_;
824 825
    my $osid        = $self->imageid();
    my $version     = $self->version();
826
    my $isdataset   = $self->isdataset();
827
    my $tableid     = int(rand(10000000));
828
    my $typelist    = "";
829 830 831
    my $ostablename = "os_info_versions" . $tableid;
    my $imtablename = "image_versions"  . $tableid;

832 833 834
    #
    # This should not happen but lets make sure.
    #
835 836 837 838 839 840 841
    # We cannot make this test cause of image deletion; we might have
    # deleted the head version. But this code below assumes we are
    # making a copy of the highest numbered version of the image. I think
    # we can change this, but do not want to attempt that now. Needs more
    # thought
    #
    if (0 && defined($self->deleted())) {
842 843 844 845 846 847 848 849
	print STDERR "Not allowed to clone deleted image $self\n";
	return undef;
    }
    #
    # We should always be called with the latest version. $baseimage is
    # what we are actually basing the new image on.
    #
    if (!$self->IsNewest()) {
850 851 852
	print STDERR "Image::NewVersion: $self is not the head version\n";
	return undef;
    }
853
    
Leigh Stoller's avatar
Leigh Stoller committed
854 855 856 857 858 859 860 861 862
    #
    # Grab the current type list. Do this before table locking.
    #
    if (!$isdataset) {
	my @typelist = $self->TypeList();
	$typelist = join(",", @typelist)
	    if (@typelist);
    }

863 864 865 866 867
    #
    # The pid/imageid has to be unique, so lock the table for the check/insert.
    #
    DBQueryWarn("lock tables images write, image_versions write, ".
		"            os_info write, os_info_versions write, ".
Leigh Stoller's avatar
Leigh Stoller committed
868 869
		"            emulab_indicies write, osidtoimageid read, ".
		"            node_types read")
870 871 872 873 874 875 876 877 878
	or return undef;

    my $query_result =
	DBQueryWarn("create temporary table $imtablename ".
		    "select * from image_versions ".
		    "where imageid='$osid' and version='$version'");
    goto bad
	if (!$query_result);

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    if (!$isdataset) {
	$query_result =
	    DBQueryWarn("create temporary table $ostablename ".
			"select * from os_info_versions ".
			"  where osid='$osid' and vers='$version'");
	goto bad
	    if (!$query_result);

	#
	# Update the type list in the image being cloned. Better to do this
	# when the types are changed, but this works too since no one uses
	# this until the image is deleted.
	#
	DBQueryWarn("update image_versions set nodetypes='$typelist' ".
		    "where imageid='$osid' and version='$version'");
    }
895

896 897 898 899
    #
    # Now reset a few things in each table.
    #
    my $clone_vers  = $self->version() + 1;
900
    my $updater_urn = "";
901 902 903 904 905
    #
    # If this is set in the environment, we use it. This allows
    # us to track actual geni users, since all of that happens
    # as geniuser. We do not bother to set this for local users.
    #
906 907
    if (exists($ENV{'REALGENIURN'}) && $ENV{'REALGENIURN'} ne "") {
	$updater_urn = ",updater_urn=". DBQuoteSpecial($ENV{'REALGENIURN'});
908
    }
909

910 911 912 913 914
    #
    # Figure out which partition needs to be changed. EZ images only
    # at this time.
    #
    my $part_vers = "";
915 916 917 918 919 920 921
    if (!$isdataset) {
	for (my $i = 1; $i <= 4; $i++) {
	    my $func = "part${i}_osid";
	    if (defined($self->$func())) {
		$part_vers = "part${i}_vers='${clone_vers}',";
		last;
	    }
922 923 924 925 926 927 928 929 930
	}
    }
    my $uid     = $creator->uid();
    my $uid_idx = $creator->uid_idx();

    #
    # The parent of this image might be this image, if taking a snapshot
    # of a node running the image. But we also need to support making a
    # version of the image, from a node running some other image. We still
931 932 933 934 935
    # bump the version number, but the parent pointers are different.
    #
    # In one case, the base image is undef, which indicates a new version
    # not based on anything; we use this from the image_import with
    # update path. Not sure it is the correct approach yet.
936 937
    #
    my ($parent_imageid, $parent_version);
938 939 940 941

    if (!defined($baseimage)) {
	$parent_imageid = "NULL";
	$parent_version = "NULL";
942 943
    }
    else {
944 945 946 947 948 949 950 951 952 953
	if ($self->imageid() == $baseimage->imageid()) {
	    $parent_imageid = $self->imageid();
	    $parent_version = $self->version();
	}
	else {
	    $parent_imageid = $baseimage->imageid();
	    $parent_version = $baseimage->version();
	}
	$parent_imageid = "'$parent_imageid'";
	$parent_version = "'$parent_version'";
954 955 956 957 958 959
    }

    #
    # Fix up the path by appending the version number.
    #
    my $path = $self->path();
960 961 962 963 964 965 966
    if (!$self->IsDirPath()) {
	if ($path =~ /^(.*):\d+$/) {
	    $path = $1 . ":${clone_vers}";
	}
	else {
	    $path .= ":${clone_vers}";
	}
967
    }
968 969 970
    
    if (!$isdataset) {
	DBQueryWarn("update $ostablename set ".
971
		    "  uuid=uuid(),deleted=null, ".
972 973 974 975 976 977
		    "  vers='$clone_vers',".
		    "  parent_osid=$parent_imageid,".
		    "  parent_vers=$parent_version ".
		    "where osid='$osid'")
	    or goto bad;
    }
978
    DBQueryWarn("update $imtablename set ".
979
		"  uuid=uuid(),ready=0,path='$path',released=0,deleted=null, ".
980 981
		"  $part_vers default_vers='$clone_vers', ".
		"  version='$clone_vers',last_used=NULL, ".
982
		"  created=now(),nodetypes='$typelist', ".
983 984
		"  parent_imageid=$parent_imageid,".
		"  parent_version=$parent_version, ".
985
		"  origin_neednotify=0,origin_needupdate=0, ".
986
		"  origin_uuid=NULL,origin_name=NULL,credential_string=NULL, ".
987
		"  hash=null,deltahash=null,size=0,deltasize=0, ".
988
		"  updater='$uid',updater_idx='$uid_idx' $updater_urn ".
989 990 991 992 993
		"where imageid='$osid'")
	or goto bad;

    #
    # And insert into the real table. At this point we will be
994
    # inconsistent if we crash before the commit is done.
995
    #
996 997 998 999 1000 1001 1002
    if (!$isdataset) {
	DBQueryWarn("insert into os_info_versions ".
		    "select * from $ostablename")
	    or goto bad;
    }
    if (! DBQueryWarn("insert into image_versions ".
		      "select * from $imtablename")) {
1003
	DBQueryWarn("delete from os_info_versions ".
1004 1005
		    "where osid='$osid' and vers='$clone_vers'")
	    if (!$isdataset);
1006 1007 1008 1009 1010 1011
	DBQueryWarn("delete from image_versions ".
		    "where imageid='$osid' and version='$clone_vers'");
	goto bad;
    }
    
    DBQueryWarn("unlock tables");
1012
    return Image->Lookup($osid, $clone_vers);
1013
  bad:
1014
    DBQueryWarn("unlock tables");
1015 1016
    return undef;
}
1017

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
#
# For a newly created image, set the provenance pointers back to
# the image from which it was derived.
#
sub SetProvenance($$)
{
    my ($self, $base) = @_;
    my $parent_imageid = $base->imageid();
    my $parent_version = $base->version();

    $self->Update({"parent_imageid" => $parent_imageid,
		   "parent_version" => $parent_version})
	== 0 or return -1;

    return 0;
1033 1034
}

1035 1036 1037 1038
#
# Highest number version, rather then what is deemed most recent by the
# images table.
#
1039 1040
# We must include deleted images here.
#
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
sub LookupMostRecent($)
{
    my ($self) = @_;

    my $imageid = $self->imageid();

    my $query_result =
	DBQueryWarn("select version from image_versions ".
		    "where imageid='$imageid' order by version desc limit 1");
    return undef
	if (!$query_result);

    my ($version) = $query_result->fetchrow_array();
    return Image->Lookup($imageid, $version);
}

1057 1058 1059
#
# Return a list of all image versions.
#
1060
sub AllVersions($$;$)
1061
{
1062
    my ($self, $pref, $deleted)  = @_;
1063 1064
    my @result  = ();
    my $imageid = $self->imageid();
1065
    $deleted = 0 if (!defined($deleted));
1066 1067 1068

    my $query_result =
	DBQueryWarn("select version from image_versions ".
1069 1070 1071
		    "where imageid='$imageid' ".
		    ($deleted ? "" : "and deleted is null ") .
		    "order by version desc");
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    return -1
	if (!$query_result);
    
    while (my ($version) = $query_result->fetchrow_array()) {
	my $image = Image->Lookup($imageid, $version);
	if (!defined($image)) {
	    print STDERR "Could not lookup image $imageid,$version\n";
	    return -1;
	}
	push(@result, $image);
    }
    @$pref = @result;
    return 0;
}

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 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
#
# Release an image; all this does is make the highest numbered version
# in the image_versions table, the default version. It does this by
# updating the version number in the images table. An unreleased version
# can be used by explicitly giving the version number, but unqualified
# use always get the version that is set in the images table. 
#
sub Release($)
{
    my ($self) = @_;

    DBQueryWarn("lock tables images write, image_versions write, ".
		"            os_info write, os_info_versions write")
	or return -1;

    if (!$self->ready()) {
	print STDERR "Image::Release: $self is not marked ready!\n";
	goto bad;
    }
    my $imageid = $self->imageid();
    my $version = $self->version();

    #
    # Only the "head" version can be released.
    #
    my $query_result =
	DBQueryWarn("select max(version) from image_versions ".
		    "where imageid='$imageid'");
    goto bad
	if (!$query_result || !$query_result->numrows);

    my ($head) = $query_result->fetchrow_array();
    if ($head != $self->version()) {
	print STDERR "Image::Release: not the head version of $self\n";
	goto bad;
    }
    return -1
	if (! (DBQueryWarn("update os_info set version='$version' ".
			   "where osid='$imageid'") &&
	       DBQueryWarn("update images set version='$version' ".
			   "where imageid='$imageid'") &&
	       DBQueryWarn("update image_versions set released=1 ".
			   "where imageid='$imageid' and version='$version'")));
    DBQueryWarn("unlock tables");
    return 0;
  bad:
    DBQueryWarn("unlock tables");
    return -1;
}

1137 1138 1139 1140 1141 1142 1143 1144 1145
#
# Worker class method to edit image descriptor.
# Assumes most argument checking was done elsewhere.
#
sub EditImageid($$$$)
{
    my ($class, $image, $argref, $usrerr_ref) = @_;
    my %mods;
    my $noreport;
1146
    require NodeType;
1147 1148 1149 1150 1151 1152 1153 1154

    my $imageid = $image->imageid();

    #
    # Now update the rest of the information in the DB.
    # (Others above already did their own updates.)
    #
    my %updates;
1155 1156
    foreach my $col ("description", "path", "mbr_version", "hash",
		     "notes") {
1157 1158 1159 1160 1161 1162
	# Copy args we want so that others can't get through.
	if (exists($argref->{$col})) {
	    $updates{$col} = $mods{$col} = $argref->{$col};
	}
    }
    my $redo_map = 0;
1163 1164 1165 1166 1167
    my @map_updates;
    
    if (exists($argref->{"architecture"})) {
	# This is in the images table, so done separately.
	$mods{"architecture"} = $argref->{"architecture"};
1168
    }
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    else {
	# See mtype_$type args below.

	#
	# Need a list of node types. We join this over the nodes table so that
	# we get a list of just the nodes that are currently in the testbed, not
	# just in the node_types table.
	#
	my $types_result =
	    DBQueryWarn("select distinct n.type from nodes as n ".
			"left join node_type_attributes as a on a.type=n.type ".
			"where a.attrkey='imageable' and ".
			"      a.attrvalue!='0'");
	my @mtypes_array;
	while (my ($type) = $types_result->fetchrow_array()) {
	    push(@mtypes_array, $type);
	}
	#
	# Special hack to allow pcvm type -- see SetupReload in os_setup
	#
	push @mtypes_array, "pcvm";

	foreach my $type (@mtypes_array) {
	    # Remember when we get one of the mtype_$type args.  These aren't DB
	    # columns to update, but instead control re-creating the rows in the
	    # osidtoimageid table for this imageid, below.
	    my $mtype = "mtype_$type";
	    if (exists($argref->{$mtype})) {
		my $value = $argref->{$mtype};
		##printf "argref->{$mtype} %s\n", $value;
		$mods{$mtype} = $value;
		if ($value eq "1") {
		    push(@map_updates, $type);
		    $redo_map = 1;
		}
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	    }
	}
    }

    if (!keys %mods) {
	if (!$noreport) {
	    # Warn the user that the submit button was pressed with no effect.
	    $$usrerr_ref = "Submit: Nothing changed";
	    return undef;
	}
    }

1216
    if (keys(%mods) || $redo_map) {
1217
	DBQueryWarn("lock tables images write, image_versions write, ".
Leigh Stoller's avatar
Leigh Stoller committed
1218
		    "     images as i write, image_versions as v write, ".
1219 1220
		    "     os_info write, os_info_versions write, ".
		    "     osidtoimageid write");
1221 1222 1223 1224
    }
    if (keys %updates) {
	if ($image->Update(\%updates)) {
	    $$usrerr_ref = "DB: Error updating the images table";
1225
	    DBQueryWarn("unlock tables");
1226 1227 1228
	    return undef;
	}
    }
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    if (exists($argref->{"architecture"})) {
	my $arch = $argref->{"architecture"};

	if (!DBQueryWarn("update images set architecture='$arch' ".
			 "where imageid='$imageid'")) {
	    $$usrerr_ref = "DB: Error updating the images table";
	    DBQueryWarn("unlock tables");
	    return undef;
	}
	if (0 &&
	    NodeType->LookupArchitectureTypes($arch) &&
	    !DBQueryWarn("delete from osidtoimageid ".
			      "where imageid='$imageid'")) {
	    $$usrerr_ref = "DB: Error updating the osidtoimageid table";
	    DBQueryWarn("unlock tables");
	    return undef;
	}
    }    
1247 1248 1249 1250 1251
    if ($redo_map) {
	#
	# Update the osidtoimageid table too.
	# 
	# Must delete old entries first.
1252
	DBQueryWarn("delete from osidtoimageid ".
1253 1254 1255 1256
		     "where imageid='$imageid'");

	my @osid_array;
	for (my $i = 1; $i <= 4; $i++) {
1257 1258
	    my $func = "part${i}_osid";
	    my $foo  = $image->$func();
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	    if (defined($foo)) {
		push(@osid_array, $foo);
	    }
	}
	for (my $i = 0; $i <= $#map_updates; $i++) {
	    for (my $j = 0; $j <= $#osid_array; $j++) {
		 my $query = "REPLACE INTO osidtoimageid ".
			     "       (osid, type, imageid) ".
			     "VALUES ('$osid_array[$j]', ".
			     "        '$map_updates[$i]', ".
			     "        '$imageid')";
		 ##print "$query\n";
1271
		 DBQueryWarn($query);
1272 1273 1274
	    }
	}
    }
1275
    if (keys(%mods) || $redo_map) {
1276
	DBQueryWarn("unlock tables");
1277 1278 1279 1280 1281
    }

    return 1;
}

1282 1283 1284 1285 1286 1287 1288 1289 1290
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $pid       = $self->pid();
    my $imageid   = $self->imageid();
1291
    my $version   = $self->version();
1292 1293
    my $imagename = $self->imagename();

1294
    return "[Image $imageid:$version $pid,$imagename]";
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
}

#
# Return the internal DB rowref. Used for code that has not been converted.
#
sub DBData($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    return $self->{'IMAGE'}
}

#
# Perform some updates ...
#
1313
sub Update($;$)
1314
{
1315 1316 1317
    my ($self, $argref, $allvers) = @_;
    $allvers = 0
	if (!defined($allvers));
1318 1319 1320 1321 1322 1323

    # Must be a real reference. 
    return -1
	if (! ref($self));

    my $imageid = $self->imageid();
1324
    my $version = $self->version();
1325

1326 1327
    my $query = 
	join(",", map("$_=" . DBQuoteSpecial($argref->{$_}), keys(%{$argref})));
1328

1329 1330
    $query .= " where imageid='$imageid'";
    $query .= " and version='$version'" if (!$allvers);
1331 1332

    return -1
1333 1334
	if (! DBQueryWarn("update image_versions set $query"));
	    
1335 1336 1337
    return Refresh($self);
}

1338
#
1339
# Delete an image,
1340
#
1341
sub Delete($;$)
1342
{
1343
    my ($self, $purge) = @_;
1344 1345 1346 1347 1348

    # Must be a real reference. 
    return -1
	if (! ref($self));

1349 1350 1351 1352
    $purge = 0
	if (!defined($purge));
    
    my $imageid   = $self->imageid();
1353
    my $uuid      = $self->image_uuid();
Leigh Stoller's avatar
Leigh Stoller committed
1354 1355
    # Do this before table locking.
    my @typelist  = $self->TypeList();
1356

1357 1358 1359
    DBQueryWarn("lock tables images write, image_versions write, ".
		"            os_info write, os_info_versions write, ".
		"            osidtoimageid write, os_submap write, ".
1360
		"            image_permissions write, node_types read, ".
Leigh Stoller's avatar
Leigh Stoller committed
1361 1362 1363
		"            web_tasks write, ".
		"            image_notifications write, ".
		"            image_updates write")
1364 1365
	or return -1;

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
    #
    # If provenance is on and not purging, then want to stash the
    # current type list so we can recall it later. 
    #
    if (!$purge && $DOPROVENANCE && !defined($self->nodetypes())) {
	if (@typelist) {
	    my $typelist = join(",", @typelist);
	    DBQueryWarn("update image_versions set nodetypes='$typelist' ".
			"where imageid='$imageid'")
	}
    }

1378 1379 1380 1381 1382 1383 1384
    #
    # If this is an EZ imageid, then delete the corresponding OSID too.
    #
    DBQueryWarn("delete from osidtoimageid where imageid='$imageid'")
	or goto bad;
    DBQueryWarn("delete from images where imageid='$imageid'")
	or goto bad;
1385 1386
    DBQueryWarn("delete from web_tasks where object_uuid='$uuid'")
	or goto bad;
1387 1388 1389 1390
    DBQueryWarn("delete from image_notifications where imageid='$imageid'")
	or goto bad;
    DBQueryWarn("delete from image_updates where imageid='$imageid'")
	or goto bad;
1391 1392
    $self->webtask()->Delete()
	if (defined($self->webtask()));
1393
    
1394
    if ($purge || !$DOPROVENANCE) {
1395
	goto bad
Leigh Stoller's avatar
Leigh Stoller committed
1396 1397
	    if (! DBQueryWarn("delete from image_versions ".
			      "where imageid='$imageid'"));
1398 1399 1400
    }
    else {
	# Set deleted on all of the versions.
1401
	DBQueryWarn("update image_versions set deleted=now() ".
1402 1403 1404
		    "where imageid='$imageid'")
	    or goto bad;
    }
1405
    if ($self->ezid()) {
1406 1407 1408
	# Cannot be a child or parent if I be dead.
	DBQueryWarn("delete from os_submap where ".
		    "  osid='$imageid' or parent_osid='$imageid'")
1409
	    or goto bad;
1410 1411 1412 1413 1414 1415 1416 1417 1418
	# Ditto.
	DBQueryWarn("update os_info_versions set def_parentosid=null ".
		    "  where def_parentosid='$imageid' and deleted is null")
	    or goto bad;
	# Ditto
	DBQueryWarn("update os_info_versions set nextosid=null ".
		    "  where nextosid='$imageid' and deleted is null")
	    or goto bad;
	
1419 1420
	DBQueryWarn("delete from os_info where osid='$imageid'")
	    or goto bad;
1421
	if ($purge || !$DOPROVENANCE) {
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	    DBQueryWarn("delete from os_info_versions where osid='$imageid'")
		or goto bad;
	}
	else {
	    # Set deleted on all of the versions.
	    DBQueryWarn("update os_info_versions set deleted=now() ".
			"where osid='$imageid'")
		or goto bad;
	}
	# Kill off the imageid permissions.
	DBQueryWarn("delete from image_permissions where imageid='$imageid'")
	    or goto bad;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    }
    DBQueryWarn("unlock tables")
	or return -1;

    return 0;

  bad:
    DBQueryWarn("unlock tables");
    return -1;
}

1445
#
1446 1447
# Mark a version as deleted. Since we are allowing the head version to
# be deleted, we might have to reset the current image/osinfo pointers.
1448 1449
#
sub DeleteVersion($)
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
{
    my ($self) = @_;
    my $imageid   = $self->imageid();
    my $version   = $self->version();

    DBQueryWarn("lock tables images write, image_versions write, ".
		"            os_info write, os_info_versions write")
	or return -1;

    #
    # Find the new head version; the highest numbered version that
    # is not deleted,
    #
    my $query_result =
	DBQueryWarn("select max(version) from image_versions ".
		    "where imageid='$imageid' and deleted is null and ".
		    "      version!='$version'");
    goto bad
	if (!$query_result);

    if (!$query_result->numrows) {
	print STDERR "Image::DeleteVersion: Cannot find a new head version\n";
	goto bad;
    }
    my ($head) = $query_result->fetchrow_array();
    
    goto bad
	if (! (DBQueryWarn("update image_versions set deleted=now() ".
			   "where imageid='$imageid' and version='$version'") &&
	       DBQueryWarn("update os_info_versions set deleted=now() ".
			   "where osid='$imageid' and vers='$version'") &&
	       DBQueryWarn("update os_info set version='$head' ".
			   "where osid='$imageid'") &&
	       DBQueryWarn("update images set version='$head' ".
			   "where imageid='$imageid'")));

    DBQueryWarn("unlock tables")
	or return -1;
    return 0;

  bad:
    DBQueryWarn("unlock tables");
    return -1;
}

#
# Purge a version of an image. This is not to be used, except when there
# is an error during clone, and we want to undo the creation of a new version.
#
sub PurgeVersion($)
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
{
    my ($self) = @_;

    my $imageid   = $self->imageid();

    DBQueryWarn("lock tables images write, image_versions write, ".
		"            os_info write, os_info_versions write")
	or return -1;

    #
1510
    # We do not allow the purge if it is not the "head" version of the
1511
    # image_versions. Maybe later.
1512 1513
    #
    my $query_result =
1514 1515
	DBQueryWarn("select max(version) from image_versions ".
		    "where imageid='$imageid'");
1516
    goto bad
1517 1518 1519 1520
	if (!$query_result || !$query_result->numrows);

    my ($version) = $query_result->fetchrow_array();
    if ($version != $self->version()) {
1521
	print STDERR "Image::PurgeVersion: not the head version of $self\n";
1522 1523 1524 1525 1526 1527 1528
	goto bad;
    }

    goto bad
	if (! (DBQueryWarn("delete from image_versions ".
			   "where imageid='$imageid' and version='$version'") &&
	       DBQueryWarn("delete from os_info_versions ".
1529
			   "where osid='$imageid' and vers='$version'")));
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539

    DBQueryWarn("unlock tables")
	or return -1;
    return 0;

  bad:
    DBQueryWarn("unlock tables");
    return -1;
}

1540
#
1541 1542 1543 1544 1545
# Load the project object for an image
#
sub GetProject($)
{
    my ($self) = @_;
1546
    require Project;
1547 1548 1549 1550 1551 1552 1553 1554

    # Must be a real reference. 
    return undef
	if (! ref($self));

    my $project = Project->Lookup($self->pid_idx());
    
    if (! defined($project)) {
1555
	print("*** WARNING: Could not lookup project object for $self!\n");
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	return undef;
    }
    return $project;
}

#
# Load the group object for an image
#
sub GetGroup($)
{
    my ($self) = @_;
1567
    require Group;
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581

    # Must be a real reference. 
    return undef
	if (! ref($self));

    my $group = Group->Lookup($self->gid_idx());
    
    if (! defined($group)) {
	print("*** WARNING: Could not lookup group object for $self!", 1);
	return undef;
    }
    return $group;
}

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
#
# Load the creator object for an image
#
sub GetCreator($)
{
    my ($self) = @_;
    require User;

    my $user = User->Lookup($self->creator_idx());
    
    if (! defined($user)) {
	print("*** WARNING: Could not lookup user object for $self!\n");
	return undef;
    }
    return $user;
}

1599 1600 1601
#
# Check permissions. Note that root may ask permission, which comes in
# as an undef user.
1602 1603 1604 1605
#
sub AccessCheck($$$)
{
    my ($self, $user, $access_type) = @_;
1606
    require Group;
1607 1608

    # Must be a real reference. 
1609
    return 0
1610 1611
	if (! ref($self));

1612 1613 1614 1615
    if ($access_type < TB_IMAGEID_MIN || $access_type > TB_IMAGEID_MAX) {
	print "*** Invalid access type $access_type!\n";
	return 0;
    }
1616 1617 1618
    my $isadmin = ((defined($user) && $user->IsAdmin()) ||
		   ($UID == 0 || $UID eq "root") ? 1 : 0);
	
1619 1620
    # Admins and root do whatever they want.
    return 1
1621
	if ($isadmin);
1622

1623 1624 1625 1626 1627 1628 1629 1630 1631
    my $mintrust;
    
    #
    # Global ImageIDs can be read by anyone.
    #
    if ($self->global()) {
	if ($access_type == TB_IMAGEID_READINFO) {
	    return 1;
	}
1632 1633 1634
	if ($access_type == TB_IMAGEID_EXPORT && $isadmin) {
	    return 1;
	}
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
    }

    my $project = $self->GetProject();
    return 0
	if (!defined($project));
    my $group = $self->GetGroup();
    return 0
	if (!defined($group));

    #
    # Otherwise must have proper trust in the group
    #
    if ($access_type == TB_IMAGEID_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
        #
        # Shared imageids are readable by anyone in the project.
        #
	if ($self->shared()) {
	    $group = $project->GetProjectGroup();
	}
    }
1656 1657 1658 1659 1660 1661 1662 1663 1664
    elsif ($access_type == TB_IMAGEID_EXPORT) {
	#
	# Owner or root in the project.
	#
	return 1
	    if ($user->uid_idx() == $self->creator_idx());

	$mintrust = PROJMEMBERTRUST_GROUPROOT;
    }
1665 1666 1667 1668 1669 1670 1671 1672 1673
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

    #
    # Either proper permission in the group, or group_root in the project.
    # This lets group_roots muck with other people's experiments, including
    # those in groups they do not belong to.
    #
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    return 1
	if (TBMinTrust($group->Trust($user), $mintrust) ||
	    TBMinTrust($project->Trust($user), PROJMEMBERTRUST_GROUPROOT));

    # No point in looking further; never allowed.
    return 0
	if ($access_type == TB_IMAGEID_EXPORT);

    #
    # Look in the image permissions. First look for a user permission,
    # then look for a group permission.
    #
    my $uid_idx = $user->uid_idx();
    my $imageid = $self->imageid();
    
    my $query_result = 
	DBQueryWarn("select allow_write from image_permissions ".
		    "where imageid='$imageid' and ".
		    "      permission_type='user' and ".
		    "      permission_idx='$uid_idx'");
    return 0
	if (!$query_result);
    
    if ($query_result->numrows) {
	# READINFO is read-only access to the image and its contents.
	return 1
	    if ($access_type == TB_IMAGEID_READINFO());
	
	if ($access_type == TB_IMAGEID_ACCESS()) {
	    my ($allow_write) = $query_result->fetchrow_array();

	    return 1
		if ($allow_write);
	}
    }
1709
    my $trust_none = $Group::MemberShip::TRUSTSTRING_NONE;
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
    $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 ($query_result->numrows) {
	# READINFO is read-only access to the image and its contents.
	return 1
	    if ($access_type == TB_IMAGEID_READINFO());
	
	if ($access_type == TB_IMAGEID_ACCESS()) {
	    my ($allow_write) = $query_result->fetchrow_array();

	    return 1
		if ($allow_write);
	}
    }
    return 0;
1732 1733
}

1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
#
# LockTables simply locks the given tables, and then refreshes the
# instance (thereby getting the data from the DB after the tables are locked).
#
sub LockTables($;$)
{
    my ($self, $spec) = @_;

    # Must be a real reference. 
    return -1
	if (! ref($self));

    $spec = "images write"
	if (!defined($spec));
    
    DBQueryWarn("lock tables $spec")
	or return -1;
	
    return $self->Refresh();
}
sub UnLockTables($)
{
    my ($self) = @_;

    # Must be a real reference. 
    return -1
	if (! ref($self));

    DBQueryWarn("unlock tables")
	or return -1;
    return 0;
}

1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
#
# Get the disk offset at which the image will be loaded.
# For a "whole disk" (loadpart == 0) image, it is always 0.
# For a "partition" image, it depends on the MBR type of the image.
#
# Returns -1 if the offset could not be determined (e.g., unknown MBR).
#
sub GetDiskOffset($)
{
    my ($self) = @_;

    # XXX these should come out of the DB or not exist at all!
    my %mbr = (
1780 1781 1782
	"1" => [0,   63,  6281415, 12562830, 12819870],
	"2" => [0,   63, 12305853, 24611643, 26712000],
	"3" => [0, 2048, 33556480, 39847936, 46139392]
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
    );

    # Must be a real reference. 
    return -1
	if (! ref($self));

    return -1
	if (! exists($mbr{$self->mbr_version()}));

    return -1
	if ($self->loadpart() > 4);

    return $mbr{$self->mbr_version()}[$self->loadpart()];
}

1798 1799 1800