image_setup.in 5.54 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
43
 
    exit(-1);
}
44
my $optlist  = "dvgp:";
Leigh B Stoller's avatar
Leigh B Stoller committed
45
46
47
my $debug    = 0;
my $verify   = 0;
my $getimages= 0;
48
my $importpid;
Leigh B Stoller's avatar
Leigh B Stoller committed
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
82

#
# 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;
83
use Project;
Leigh B Stoller's avatar
Leigh B Stoller committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97

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;
}
98
99
100
if (defined($options{"p"})) {
    $importpid = $options{"p"};
}
Leigh B Stoller's avatar
Leigh B Stoller committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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();

124
125
126
127
128
129
130
131
132
133
134
135
# 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;
}

136
137
my @urllist = ();

Leigh B Stoller's avatar
Leigh B Stoller committed
138
139
140
141
142
143
144
145
#
# 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;
    
146
147
148
149
150
151
152
153
154
    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
155
    }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

    #
    # 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
177
    }
178
179
180
}

foreach my $url (@urllist) {
Leigh B Stoller's avatar
Leigh B Stoller committed
181
    my $safe_url = User::escapeshellarg($url);
182
    my $imagepid = $importpid;
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
213
214
	# Update image in the project it already exists in. Technically,
	# I should (probably) not have to specify this. 
	$imagepid = $image->pid();
	
	system("$IMPORTER $opts -p $imagepid -r " . $image->imageid());
215
    }
216
    else {
217
	system("$IMPORTER $opts -p $imagepid $safe_url");
Leigh B Stoller's avatar
Leigh B Stoller committed
218
    }
219
220
221
    exit(-1)
	if ($?);

Leigh B Stoller's avatar
Leigh B Stoller committed
222
223
224
225
    next
	if ($verify);

    if (!defined($image)) {
226
227
228
229
	$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
230
231
232
233
    }
    next
	if (! $getimages);

234
    $opts = "";
Leigh B Stoller's avatar
Leigh B Stoller committed
235
236
    $opts .= " -d"
	if ($debug);
237
    system("$IMPORTER $opts -g -p $imagepid -r " . $image->imageid());
Leigh B Stoller's avatar
Leigh B Stoller committed
238
239
240
241
242
    exit(-1)
	if ($?);
}
exit(0);