image_setup.in 6.03 KB
Newer Older
Leigh B Stoller's avatar
Leigh B Stoller committed
1
2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2015 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh B Stoller's avatar
Leigh B Stoller committed
23
24
25
26
#
use English;
use Getopt::Std;
use Socket;
27
use Data::Dumper;
Leigh B Stoller's avatar
Leigh B Stoller committed
28
29
30
31
32
33
34
    
#
# Fetch external image definitions and create local descriptors.
#

sub usage()
{
35
    print "Usage: $0 [-d] [-v] [-g] [-p pid] eid\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
36
37
38
39
    print("Options:\n");
    print(" -d      - Turn on debugging\n");
    print(" -v      - Verify XML descriptions only\n");
    print(" -g      - Download image after creating descriptors\n");
40
    print(" -p pid  - Import image into specified project instead\n");
Leigh B Stoller's avatar
Leigh B Stoller committed
41
42
    exit(-1);
}
43
my $optlist  = "dvgp:";
Leigh B Stoller's avatar
Leigh B Stoller committed
44
45
46
my $debug    = 0;
my $verify   = 0;
my $getimages= 0;
47
my $importpid;
Leigh B Stoller's avatar
Leigh B Stoller committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#
# Functions
#
sub verifyURL($);

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $IMPORTER = "$TB/sbin/image_import";

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

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libtestbed;
use libdb;
use libtblog;
use Experiment;
use Image;
use User;
82
use Project;
Leigh B Stoller's avatar
Leigh B Stoller committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96

my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"v"})) {
    $verify = 1;
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"g"})) {
    $getimages = 1;
}
97
98
99
if (defined($options{"p"})) {
    $importpid = $options{"p"};
}
Leigh B Stoller's avatar
Leigh B Stoller committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
if (@ARGV != 1) {
    usage();
}

#
# Verify user and get his DB uid and other info for later.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
}
my $user_uid   = $this_user->uid();

#
# First, make sure the experiment exists
#
my $experiment = Experiment->Lookup($ARGV[0]);
if (! $experiment) {
    tbdie("There is no such experiment");
}
my $pid = $experiment->pid();
my $eid = $experiment->eid();

123
124
125
126
127
128
129
130
131
132
133
134
# We allow override of what project the image is imported into.
# Caller must be a member of this project too. 
if (defined($importpid)) {
    my $project = Project->Lookup($importpid);
    tbdie("No such project for import")
	if (!defined($project));
    $importpid = $project->pid();
}
else {
    $importpid = $pid;
}

135
136
my @urllist = ();

Leigh B Stoller's avatar
Leigh B Stoller committed
137
138
139
140
141
142
143
144
#
# Look for any nodes that specify a url for the osname. 
#
my $result = $experiment->TableLookUp("virt_nodes", "vname,osname");

while (my ($vname, $osname) = $result->fetchrow()) {
    my $url;
    
145
146
147
148
149
150
151
152
153
    if ($osname =~ /^(ftp|http|https):/) {
	# Verify entire URL and taint check.
	if ($osname =~ /^((http|https|ftp)\:\/\/[-\w\.\/\@\:\~\?\=\&]*)$/) {
	    $url = $1;
	}
	else {
	    tbdie("Invalid URL $osname\n");
	}
	push(@urllist, $url);
Leigh B Stoller's avatar
Leigh B Stoller committed
154
    }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

    #
    # Look for image backed datasets that refer to an external URL.
    #
    my @blockstores = $experiment->LookupBlockstoresForNode($vname);
    foreach my $blockstore (@blockstores) {
	next
	    if ($blockstore->type() ne "imdataset");

	if (!exists($blockstore->{'attributes'}->{'dataset'})) {
	    tbdie("No dataset if for blockstore on $vname\n");
	}
	my $dataset = $blockstore->{'attributes'}->{'dataset'};
	my $image   = Image->Lookup($dataset);
	if (!defined($image)) {
	    tbdie("Could not lookup image for $dataset");
	}
	next
	    if ($image->IsLocal());

	push(@urllist, $image->metadata_url());
Leigh B Stoller's avatar
Leigh B Stoller committed
176
    }
177
178
179
}

foreach my $url (@urllist) {
Leigh B Stoller's avatar
Leigh B Stoller committed
180
    my $safe_url = User::escapeshellarg($url);
181
    my $imagepid = $importpid;
182
    my $imageuid = $user_uid;
Leigh B Stoller's avatar
Leigh B Stoller committed
183
184

    #
185
186
    # See if we have already created this descriptor. We want to
    # verify the information and create the descriptor. Later we will
Leigh B Stoller's avatar
Leigh B Stoller committed
187
188
189
    # fetch the image file, or refetch if it is stale.
    #
    my $image = Image->LookupByURL($url);
190
191
192
193
194
195
196
197
    if (defined($image)) {
	#
	# LookupByURL might return a local image, which of course
	# we want to skip; later phases know its a local image.
	#
	next
	    if ($image->IsLocal());
    }
198
199
200
201
202
203
204
205
206
207
208
209
    #
    # Because of versioning, we still want to create a new version
    # of the image descriptor now (if it is stale) so that the mapper
    # sees the new version. We do not have to fetch it till later.
    # The importer will not do anything if the image is not stale. 
    #
    my $opts = "";
    $opts .= " -d"
	if ($debug);
    $opts .= " -v"
	if ($verify);
    if (defined($image)) {
210
211
212
	# Update image in the project it already exists in. Technically,
	# I should (probably) not have to specify this. 
	$imagepid = $image->pid();
213
214
215
216
217
218
219
220
221
222

	#
	# Backwards compat problem for IG slices. We used to import
	# images as the user into their project, but now we are importing
	# into GeniSlices as geniuser. But if an image was already here,
	# we will fail the permissions checks, so tell the importer to
	# to the update as the user that owns the image. Might need to
	# to actually move those images at some point.
	#
	$imageuid = $image->creator();
223
	
224
225
	system("$IMPORTER $opts -u $imageuid ".
	       "   -p $imagepid -r " . $image->imageid());
226
    }
227
    else {
228
	system("$IMPORTER $opts -u $imageuid -p $imagepid $safe_url");
Leigh B Stoller's avatar
Leigh B Stoller committed
229
    }
230
231
232
    exit(-1)
	if ($?);

Leigh B Stoller's avatar
Leigh B Stoller committed
233
234
235
236
    next
	if ($verify);

    if (!defined($image)) {
237
238
239
240
	$image = Image->LookupByURL($url);
	if (!defined($image)) {
	    tbdie("Could not look up image object for $url\n");
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
241
242
243
244
    }
    next
	if (! $getimages);

245
    $opts = "";
Leigh B Stoller's avatar
Leigh B Stoller committed
246
247
    $opts .= " -d"
	if ($debug);
248
249
    system("$IMPORTER $opts -g -u $imageuid ".
	   "  -p $imagepid -r " . $image->imageid());
Leigh B Stoller's avatar
Leigh B Stoller committed
250
251
252
253
254
    exit(-1)
	if ($?);
}
exit(0);