imagerelease.in 7.83 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2014-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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use English;
use strict;
use Getopt::Std;
use Data::Dumper;
use File::stat;
use File::Basename;

#
# Release an image by changing the current version in the versions table
# to point to it. We first copy the image file over to boss, and after
# we call imagevalidate to make sure the DB is consistent.
#
sub usage()
{
38
    print("Usage: imagerelease [-q] [-f] [-m] <imageid>\n" .
39
	  "Options:\n".
40 41
	  "       -q      Turn on quiet mode\n".
	  "       -f      Force even if it make no sense.\n".
42 43
	  "       -m      With force, just mark as ready/released.\n".
	  "       -n      Impotent mode; just show what would be done.\n");
44 45
    exit(-1);
}
46
my $optlist    = "dqfmn";
47 48
my $debug      = 0;
my $quiet      = 0;
49
my $force      = 0;
50
my $impotent   = 0;
51
my $markready  = 0;
52 53 54 55 56 57 58

#
# Configure variables
#
my $TB		  = "@prefix@";
my $FSNODE	  = "@FSNODE@";
my $PROJROOT      = "@PROJROOT_DIR@";
59
my $SYSIMAGEDIR   = "$TB/images";
60
my $IMAGEVALIDATE = "$TB/sbin/imagevalidate";
61
my $SSH		  = "/usr/bin/ssh";
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
my $SCP		  = "/usr/bin/scp";
my $SUDO	  = "/usr/local/bin/sudo";

# Protos
sub fatal($);

#
# Untaint the path
#
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use EmulabConstants;
use User;
85
use OSImage;
86 87 88 89 90 91 92 93 94 95 96 97

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"q"})) {
    $quiet = 1;
}
98 99 100
if (defined($options{"n"})) {
    $impotent = 1;
}
101 102
if (defined($options{"f"})) {
    $force = 1;
103 104 105
    if (defined($options{"m"})) {
	$markready = 1;
    }
106
}
107 108 109 110 111 112 113 114 115 116 117 118 119
usage()
    if (@ARGV != 1);
my $imageid = $ARGV[0];

my ($user,$user_uid);
if ($UID) {
    $user = User->ThisUser();
    if (!defined($user)) {
	fatal("You ($UID) do not exist!");
    }
    $user_uid = $user->uid();
}

120
my $image = OSImage->Lookup($imageid);
121
if (!defined($image)) {
122
    $image = OSImage->LookupByName($imageid);
123 124 125 126
}
if (!defined($image)) {
    fatal("No such image!");
}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

#
# See if this is a Docker image; use libimageops if so.
#
if ($image->format() eq 'docker') {
    use libimageops;

    libimageops::setDebug(1)
	if ($debug);
    my %args = ('impotent' => $impotent,'quiet' => $quiet,
		'force' => $force,'markready' => $markready);

    my $iops = libimageops::Factory("image" => $image);
    my ($rc,$msg) = $iops->Release($image,\%args);
    if ($rc) {
	print STDERR "Error: $msg\n";
	exit(1);
    }
    else {
	exit(0);
    }
}

150
if ($image->released() && !$force) {
151 152 153
    fatal("Image is already released! ".
	  "Maybe you need to provide imageid:version");
}
154 155 156
if ($UID && !$user->IsAdmin()) {
    fatal("Only admins can release an image.");
}
157
if ($force && $markready) {
158 159 160 161 162 163 164 165
    if ($impotent) {
	print "Would mark image ready/released, but not do anything else\n";
    }
    else {
	print "Marking image ready/released, but not doing anything else\n";
	$image->MarkReady();
	$image->MarkReleased();
    }
166 167
    exit(0);
}
168 169 170 171 172 173 174 175 176 177 178 179 180
if (!$image->ready()) {
    fatal("Image is not ready yet!");
}

#
# Grab version 0 of the descriptor, which tells us if the image is really
# stored on boss. At some point, it would be nice to store unreleased
# versions of system images on boss too, but not enough disk space to
# support that, so we put newer versions in /proj until they are released,
# and then copy them over to avoid the NFS overhead when using the image.
# If we have to copy them back, we might also have to update the path in
# the database.
#
181 182
my $updatepath   = undef;
my %copyfiles    = ();
183 184 185 186 187 188 189 190 191
my $version0;

#
# Before we do anything destructive, we lock the image.
#
if ($image->Lock()) {
    fatal("Image is locked, please try again later!\n");
}
if ($image->version()) {
192
    $version0 = OSImage->Lookup($image->imageid(), 0);
193 194 195 196 197 198 199 200
    if (!defined($version0)) {
	$image->Unlock();
	fatal("Cannot lookup version zero of $image");
    }
}
else {
    $version0 = $image;
}
Leigh Stoller's avatar
Leigh Stoller committed
201
if ($version0->IsSystemImage()) {
202 203 204
    my $vers0src = "$PROJROOT/" . $image->pid() . "/images/";
    if ($version0->IsDirPath()) {
	# Add in the directory.
205
	$vers0src .= basename($version0->path()) . "/";
206 207
    }
    
208 209 210 211 212
    my $dstdir = $SYSIMAGEDIR . "/";
    if ($image->IsDirPath()) {
	$dstdir .= basename($image->path()) . "/";
    }
    
213 214
    #
    # For version 0 of the image, create_image sticks the file out on /proj,
215
    # but leaves the image path set to $TB/images
216
    #
217 218 219
    $copyfiles{($image->version() ?
		$image->FullImageFile() : 
		$vers0src . basename($image->FullImageFile()))} =
220
		$dstdir . basename($image->FullImageFile());
221 222 223
    $copyfiles{($image->version() ?
		$image->DeltaImageFile() :
		$vers0src . basename($image->DeltaImageFile()))} =
224
		$dstdir . basename($image->DeltaImageFile());
225 226 227
    $copyfiles{($image->version() ?
		$image->FullImageSigFile() :
		$vers0src . basename($image->FullImageSigFile()))} =
228
		$dstdir . basename($image->FullImageSigFile());
229 230 231
    $copyfiles{($image->version() ?
		$image->DeltaImageSigFile() :
		$vers0src . basename($image->FullImageSigFile()))} =
232
		$dstdir . basename($image->DeltaImageSigFile());
233
    
234
    if ($image->version()) {
235
	$updatepath = $dstdir;
236
	if ($image->IsDirPath()) {
237
	    $updatepath = $dstdir;
238 239
	}
	else {
240
	    $updatepath = $SYSIMAGEDIR . "/" . basename($image->path());
241
	}
242
    }
243 244
    foreach my $from (keys(%copyfiles)) {
	my $to = $copyfiles{$from};
245

246 247 248
	next
	    if (! -e $from);

249 250 251 252
	if ($impotent) {
	    print "Would copy ${FSNODE}:$from $to\n";
	    next;
	}
253 254 255 256
	if (!$quiet) {
	    print "Copying ${FSNODE}:$from $to\n";
	}
	system("$SUDO $SCP -p ${FSNODE}:$from $to");
257 258
	if ($?) {
	    $image->Unlock();
259
	    fatal("Failed to scp ${FSNODE}:$from $to");
260 261
	}
    }
262
    if (defined($updatepath)) {
263 264 265 266
	if ($impotent) {
	    print "Would update path to $updatepath\n";
	}
	elsif ($image->Update({"path" => $updatepath})) {
267 268 269 270
	    $image->Unlock();
	    fatal("Failed to update path");
	}
    }
271 272
    if ($impotent) {
	print "Would run imagevalidate on " . $image->versname() . "\n";
273
    }
274 275 276 277 278 279 280 281 282 283
    else {
	if (!$quiet) {
	    print "Running imagevalidate on " . $image->versname() . "\n";
	}
	system("$IMAGEVALIDATE -u " . ($quiet ? "-q " : "") .
	       $image->versname());
	if ($?) {
	    $image->Unlock();
	    fatal("Failed to validate the image!");
	}
284
    }
285 286
}
# Now mark as released.
287 288 289 290
if ($impotent) {
    print "Would mark image as released\n";
}
elsif ($image->Release()) {
291 292 293
    $image->Unlock();    
    fatal("Could not mark image as released!");
}
294 295

# If everything worked, remove the copies on ops to avoid unsightly clutter
296 297 298
if (keys(%copyfiles)) {
    my @todelete = ();
    
299
    foreach my $from (keys(%copyfiles)) {
300 301 302
	push(@todelete, $from);
	push(@todelete, $from . ".sha1");
    }
303 304 305 306 307 308 309 310 311 312
    if ($impotent) {
	foreach my $file (@todelete) {
	    print "Would delete $file\n";
	}
    }
    else {
	if (!$quiet) {
	    print "Removing temporary copy on $FSNODE\n";
	}
	system("$SUDO $SSH ${FSNODE} rm -f @todelete");
313 314 315
    }
}

316 317 318 319 320 321 322 323 324 325 326
$image->Unlock();    
exit(0);

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

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