dump-descriptors.in 7.42 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2007-2012 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
#
use English;
25
use Getopt::Long;
26 27 28 29 30 31 32 33 34
use strict;

#
# Dump out an SQL file of statements to initialize the images and osids
# on a new testbed. Or maybe even an old testbed? Note that the output
# of thie script is the input to load-descriptors in this directory.
#
sub usage()
{
35
    print STDERR "Usage: dump-descriptors [-v vers] [-m] filename\n";
36
    print STDERR "       dump-descriptors -i imageid [-i imageid] filename\n";
37
    print STDERR "  -m      dump just the MFS descriptors.\n";
38 39
    print STDERR "  -v 1    first generation STD images (FBSD410, RHL90)\n";
    print STDERR "  -v 2    second generation STD images (FBSD62, FC6)\n";
40 41
    print STDERR "  -v 3    third generation STD images (FBSD82, FEDORA15)\n";
    print STDERR "  -v 4    fourth generation STD images (FBSD83, FEDORA15)\n";
42
    print STDERR "Default version is 4.\n";
43 44
    exit(-1);
}
45
my $version = 4;
46
my $mfsonly = 0;
47
my $images;
48

49 50 51
#
# These are the images/osids we dump out. We of course get the embedded osids.
# We should probably add an export flag to the tables so we can avoid this
52 53
# hardwired list. Note that all of these are assumed to live in the emulab-ops
# project.
54
#
55 56
my %imagenames    = (
    "1" => ['FBSD410+RHL90-STD', 'FBSD410-STD', 'RHL90-STD', 'FBSD410-IPFW2'],
57 58
    # XXX note that the FBSD410* images may not work on all HW but are
    # included here to resolve nextosid entries for FW-IPFW and FBSD-JAIL
59
    "2" => ['FBSD62+FC6-STD', 'FBSD62-STD', 'FC6-STD', 'FBSD410-STD',
60
	    'FBSD410-IPFW2', 'UBUNTU10-STD-FW'],
61 62
    # No longer using combined images.
    # The UBUNTU10 image is for FW-IPTABLES link. 
63
    "3" => ['FBSD82-STD', 'FEDORA15-STD', 'UBUNTU10-STD-FW'],
64
    "4" => ['FBSD83-STD', 'FBSD83-64-STD', 'FEDORA15-STD', 'FEDORA15-64-STD', 'UBUNTU10-STD-FW'],
65
);
66

67 68 69 70 71 72 73 74 75
my %osnames	  = (
    "1" => ['FREEBSD-MFS', 'FRISBEE-MFS', 'NEWNODE-MFS',
	    'OPSNODE-BSD', 'FW-IPFW', 'FW-IPFW2', 'FW-IPTABLES',
	    'RHL-STD', 'FBSD-STD', 'FBSD-JAIL', 'POWER-CONTROLLER'],
    "2" => ['FREEBSD-MFS', 'FRISBEE-MFS', 'NEWNODE-MFS',
	    'OPSNODE-BSD', 'FW-IPFW', 'FW-IPFW2', 'FW-IPTABLES',
	    'RHL-STD', 'FBSD-STD', 'FBSD-JAIL', 'POWER-CONTROLLER'],
    "3" => ['FREEBSD-MFS', 'FRISBEE-MFS', 'NEWNODE-MFS', 'FW-IPTABLES',
	    'OPSNODE-BSD', 'RHL-STD', 'FBSD-STD', 'POWER-CONTROLLER'],
76 77 78 79 80
    "4" => ['FREEBSD-MFS', 'FRISBEE-MFS', 'NEWNODE-MFS',
	    'FREEBSD-64-MFS', 'FRISBEE-64-MFS', 'NEWNODE-64-MFS',
	    'ADMIN-LINUX', 'FRISBEE-LINUX','NEWNODE-LINUX',
	    'FW-IPTABLES',
	    'OPSNODE-BSD', 'RHL-STD', 'FBSD-STD', 'POWER-CONTROLLER'],
81
);
82

83 84 85 86 87 88
my @osids	   = ();

#
# Need to map these dynamically since we do not want to hardwire the actual
# nextosid IDs (numbers) into this table.
#
89 90 91 92 93 94 95 96
my %mappings       = (
    "1" => {
	"RHL-STD"  => "RHL90-STD",
	"FBSD-STD" => "FBSD410-STD",
    },
    "2" => {
	"RHL-STD"  => "FC6-STD",
	"FBSD-STD" => "FBSD62-STD",
97 98 99 100
    },
    "3" => {
	"RHL-STD"  => "FEDORA10-STD",
	"FBSD-STD" => "FBSD82-STD",
101 102 103 104
    },
    "4" => {
	"RHL-STD"  => "FEDORA15-STD",
	"FBSD-STD" => "FBSD83-STD",
105 106
    }
);
107 108 109 110 111 112 113 114

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";
my $BOSSNODE	= "@BOSSNODE@";
115
my $DUMPDESC    = "$TB/sbin/dumpdescriptor";
116 117 118 119 120 121 122

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
123 124
use Image;
use OSinfo;
125 126 127 128 129

# Locals
my %tempkeys  = ();
my $protouser = "elabman";
my $protoproj = TBOPSPID();
130
sub fatal($);
131 132 133 134 135 136 137 138 139 140 141 142

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

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

143 144 145 146
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
147 148 149 150 151 152

my $optlist = "v:m";

if (! GetOptions("m"    => \$mfsonly,
		 "v=i"  => \$version,
		 "i=s@" => \$images)) {
153 154
    usage();
}
155 156
if (!exists($imagenames{$version})) {
    usage();
157
}
158
if ($mfsonly) {
159 160 161
    %mappings   = ();
    %imagenames = ();
}
162 163 164 165
elsif (defined($images)) {
    $version    = 0;
    $mfsonly    = 0;
    %mappings   = ();
166
    %osnames    = ("$version" => []);
167 168
    %imagenames = ("$version" => $images);
}
169 170 171 172
usage()
    if (@ARGV != 1);
my $filename = $ARGV[0];

173 174
my %images  = ();
my %osinfos = ();
175 176

#
177
# Gather up the images and any osinfos they reference.
178
#
179 180 181 182 183 184
foreach my $imagename (@{$imagenames{$version}}) {
    my $image = Image->Lookup("$protoproj,$imagename");
    if (!defined($image)) {
	fatal("Cannot find image descriptor for $imagename");
    }
    $images{$image->imageid()} = $image;
185 186

    #
187
    # Get the osids it references.
188 189 190
    #
    foreach my $key (('part1_osid', 'part2_osid',
		      'part3_osid', 'part4_osid', 'default_osid')) {
191 192 193
	my $osid = $image->$key();
	next
	    if (!defined($osid));
194

195 196 197
	my $osinfo = OSinfo->Lookup($osid);
	if (!defined($osinfo)) {
	    fatal("Cannot find OS descriptor for $osid");
198
	}
199
	$osinfos{$osinfo->osid()} = $osinfo;
200 201 202
    }
}
#
203
# And the OSs
204
#
205
foreach my $osname (@{$osnames{$version}}) {
206 207 208 209 210
    my $osinfo = OSinfo->Lookup("$protoproj,$osname");
    if (!defined($osinfo)) {
	fatal("Cannot find os descriptor for $osname");
    }
    $osinfos{$osinfo->osid()} = $osinfo;
211
}
212 213
foreach my $osname (keys(%{$mappings{$version}})) {
    my $nextosname = $mappings{$version}{$osname};
214

215 216 217 218 219 220
    my $osinfo = OSinfo->Lookup("$protoproj,$nextosname");
    if (!defined($osinfo)) {
	fatal("Cannot find os descriptor for $nextosname");
    }
    $osinfos{$osinfo->osid()} = $osinfo;
}
221
#
222
# Open tempfile.
223
#
224 225 226
open(OUTPUT, ">$filename") or
    die("Could not open $filename for writing!\n");
print OUTPUT "<descriptors>\n";
227 228
foreach my $osname (keys(%{$mappings{$version}})) {
    my $nextosname = $mappings{$version}{$osname};
229

230 231 232
    print OUTPUT "<mapping name=\"$osname\">\n";
    print OUTPUT "    <value>$nextosname</value>\n";
    print OUTPUT "</mapping>\n";
233
}
234
close(OUTPUT);
235

236 237 238 239 240 241 242 243 244
#
# Hmm, this is interesting. We reuse OSids that were created via the EZ
# path, typically in the combo images. We need to delete those from the
# osinfos list since they get created when processing the EZ images in
# the load-descriptors script. Ordering is going to be important when
# loading them in.
#
foreach my $osid (keys(%osinfos)) {
    my $osinfo = $osinfos{$osid};
245

246 247
    next
	if ($osinfo->ezid());
248 249
    next
	if ($mfsonly && !$osinfo->mfs());
250

251 252 253 254 255
    system("$DUMPDESC -o $osid >> $filename") == 0
	or fatal("Could not dump $osinfo");
}
foreach my $imageid (keys(%images)) {
    my $image = $images{$imageid};
256
    
257 258
    system("$DUMPDESC -i $imageid >> $filename") == 0
	or fatal("Could not dump $image");
259
}
260 261 262
open(OUTPUT, ">>$filename") or
    die("Could not open $filename for writing!\n");
print OUTPUT "</descriptors>\n";
263 264
close(OUTPUT);
exit(0);
265 266 267 268 269 270 271 272 273 274

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

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