dump-descriptors.in 5.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2007 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;
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()
{
    print STDOUT "Usage: dump-descriptors filename\n";
    exit(-1);
}

#
# 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
25
26
# hardwired list. Note that all of these are assumed to live in the emulab-ops
# project.
27
#
28
29
my @imagenames	  = ('FBSD410+RHL90-STD',
		     'FBSD410-STD',
30
31
		     'RHL90-STD',
		     'FBSD410-IPFW2');
32

33
my @osnames	  = ('FREEBSD-MFS', 'FRISBEE-MFS', 'NEWNODE-MFS',
34
		     'OPSNODE-BSD', 'FW-IPFW', 'FW-IPFW2',
35
		     'RHL-STD', 'FBSD-STD', 'FBSD-JAIL', 'POWER-CONTROLLER');
36

37
38
39
40
41
42
43
44
my @osids	   = ();

#
# Need to map these dynamically since we do not want to hardwire the actual
# nextosid IDs (numbers) into this table.
#
my %mappings       = ("RHL-STD"  => "RHL90-STD",
		      "FBSD-STD" => "FBSD410-STD");
45
46
47
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";
my $BOSSNODE	= "@BOSSNODE@";
my $TFTPDIR     = "/tftpboot";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

# Locals
my %tempkeys  = ();
my $protouser = "elabman";
my $protoproj = TBOPSPID();

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

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

usage()
    if (@ARGV != 1);
my $filename = $ARGV[0];

#
# Open tempfile.
#
open(OUTPUT, ">$filename") or
    die("Could not open $filename for writing!\n");

#
# Create some temporary tables to mirror the existing tables. We
# populate these tables then spit them out. On the other side, when we
# load this table, we will move the rows out and into the local table,
# after adjustment.
#

#
# The images table.
#
DBQueryFatal("create temporary table temp_images like images");
DBQueryFatal("insert into temp_images ".
100
101
	     "select * from images where pid='$protoproj' and (".
	     join(" or ", map("imagename='$_'", @imagenames)) . ")");
102
103
104
105
106
107
108
109
110

my $query_result  = DBQueryFatal("show create table temp_images");
my $create_string = ($query_result->fetchrow_array())[1];

#
# Kill the idx slots; they may not be valid on the remote testbeds, and
# it simplifies backwards compatability.
#
DBQueryFatal("alter table temp_images ".
111
	     "drop pid_idx, drop gid_idx, drop creator_idx, drop uuid");
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

#
# Before we dump the table contents, reset a few things.
#
DBQueryFatal("update temp_images set ".
	     "creator='$protouser', ".
	     "pid='$protoproj', gid='$protoproj', ".
	     "load_address='', frisbee_pid=0, load_busy=0, ".
	     "created=now(), updated=NULL");

$query_result =
    DBQueryFatal("select * from temp_images");

while (my $rowref = $query_result->fetchrow_hashref()) {
    my $query_string = "insert into temp_images set ";
    my @query_sets   = ();

    #
    # Pick up the osids that are used in this image, and add them to the
    # list of osids to dump out.
    #
    foreach my $key (('part1_osid', 'part2_osid',
		      'part3_osid', 'part4_osid', 'default_osid')) {

	$tempkeys{$rowref->{$key}} = 1
	    if (defined($rowref->{$key}));
    }
    
    foreach my $key (keys(%{$rowref})) {
	if (defined($rowref->{$key})) {
	    push(@query_sets, "${key}='" . $rowref->{$key} . "'");
	}
	else {
	    push(@query_sets, "${key}=NULL");
	}
    }
    $query_string .= join(",", @query_sets);
    
    print OUTPUT "$query_string;\n";
}

#
# Add the osids we found above to the list.
#
foreach my $key (keys(%tempkeys)) {
    push(@osids, $key);
}

#
# Now do the osinfo table
#
DBQueryFatal("create temporary table temp_os_info like os_info");
DBQueryFatal("insert into temp_os_info ".
165
166
167
	     "select * from os_info where pid='$protoproj' and ((".
	     join(" or ", map("osname='$_'", @osnames)) . ") or (".
	     join(" or ", map("osid='$_'", @osids)) . "))");
168
169
170
171
172
173
174
175
176

$query_result  = DBQueryFatal("show create table temp_os_info");
$create_string = ($query_result->fetchrow_array())[1];

#
# Kill the idx slots; they may not be valid on the remote testbeds, and
# it simplifies backwards compatability.
#
DBQueryFatal("alter table temp_os_info ".
177
	     "drop pid_idx, drop creator_idx, drop uuid");
178
179
180
181
182
183
184
185
186
187
188
189

#
# Before we dump the table contents, reset a few things.
#
DBQueryFatal("update temp_os_info set ".
	     "creator='$protouser', ".
	     "pid='$protoproj', created=now()");

#
# We reset the nextosid slots in the -STD osids, rather then worrying
# about the osid_map table. 
#
190
191
192
193
194
195
196
197
198
199
200
foreach my $osname (keys(%mappings)) {
    my $nextosname = $mappings{$osname};

    $query_result =
	DBQueryFatal("select osid from temp_os_info ".
		     "where pid='$protoproj' and osname='$nextosname'");
    if (! $query_result->numrows) {
	print "Could not map $protoproj,$osname to its osid!\n";
	exit(-1);
    }
    my ($nextosid) = $query_result->fetchrow_array();
201
202

    DBQueryFatal("update temp_os_info set nextosid='$nextosid' ".
203
		 "where pid='$protoproj' and osname='$osname'");
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
}

$query_result =
    DBQueryFatal("select * from temp_os_info");

while (my $rowref = $query_result->fetchrow_hashref()) {
    my $query_string = "insert into temp_os_info set ";
    my @query_sets   = ();

    #
    # Replace any host:path spec with just the path; pxeboot knows what to
    # do and does not need the hostname in it. 
    #
    if (defined($rowref->{'path'}) && $rowref->{'path'} ne "" &
	$rowref->{'path'} =~ /^(.*):(.*)$/) {
	$rowref->{'path'} = $2;
    }
    foreach my $key (keys(%{$rowref})) {
	if (defined($rowref->{$key})) {
	    push(@query_sets, "${key}='" . $rowref->{$key} . "'");
	}
	else {
	    push(@query_sets, "${key}=NULL");
	}
    }
    $query_string .= join(",", @query_sets);
    
    print OUTPUT "$query_string;\n";
}
close(OUTPUT);
exit(0);