newwanode.in 7.18 KB
Newer Older
1
2
3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
5
6
7
8
9
10
11
12
13
14
# All rights reserved.
#
use English;
use Getopt::Std;

#
# Add a new node to the DB. Strictly for widearea nodes right now.
# 
sub usage()
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
15
16
    print "Usage: ".
	"newwanode [-w] [-n nickname] -t <nodetype> -i <ip address>\n";
17
18
    exit(1);
}
19
my  $optlist = "wt:i:a:n:";
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";
my $named_setup = "$TB/sbin/named_setup";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use libtestbed;

#
# This is run from the web page, but tbadmins can run it from the command
# line. 
#
if ($UID != getpwnam("nobody") && !TBAdmin($UID)) {
    die("*** $0:\n".
	"    You do not have permission to run this script!\n");
}

my $nodetype;
my $nodeip;
my $fromweb = 0;
55
my $reuse   = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56
my $nickname;
57
58
my $nodename;
my $nodevtype;
59
my $nodeauxtype;
60
my $nodevname;
61
62
63
64
my $pid;
my $eid;
my $nextid;
my $nextpri;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"w"})) {
    $fromweb = 1;
}
if (defined($options{"i"})) {
    $nodeip = $options{"i"};
}
if (defined($options{"t"})) {
    $nodetype = $options{"t"};
}
83
84
if (defined($options{"a"})) {
    $nodeauxtype = $options{"a"};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
85
86
87
88
}
if (defined($options{"n"})) {
    $nickname = $options{"n"};
}
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
if (!defined($nodeip) || !defined($nodetype)) {
    usage();
}

#
# Taint check.
#
if ($nodetype =~ /^([\w]+)$/) {
    $nodetype = $1;
}
else {
    die("*** Bad data in $nodetype\n");
}
if ($nodeip =~ /^([\d\.]+)$/) {
    $nodeip = $1;
}
else {
    die("*** Bad data in $nodeip\n");
}
108
109
110
if (defined($nodeauxtype)) {
    if ($nodeauxtype =~ /^([\w]+)$/) {
	$nodeauxtype = $1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
111
112
    }
    else {
113
	die("*** Bad data in $nodeauxtype\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
114
115
116
117
118
119
120
121
122
123
    }
}
if (defined($nickname)) {
    if ($nickname =~ /^([-\w]+)$/) {
	$nickname = $1;
    }
    else {
	die("*** Bad data in $nickname\n");
    }
}
124
125
126
127
128

#
# Must be a valid type.
#
my $query_result =
129
    DBQueryFatal("select osid,control_iface from node_types ".
130
131
132
133
134
		 "where type='$nodetype'");
if (! $query_result->numrows) {
    fatal("*** $0:\n".
	  "    No such nodetype $nodetype is defined in the DB!\n");
}
135
my ($defosid,$control_iface) = $query_result->fetchrow_array();
136
137
138
139
140
141
142
143
144

#
# For now, just pcwa/pcron is allowed until we have more DB state that says
# how to build the virtual nodes, although that is temporary as well.
#
if ($nodetype ne "pcwa" && $nodetype ne "pcron") {
    fatal("Can only handle pcwa/pcron nodetype right now!");
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
145
#
146
# Create the virtual type name. Again, this is bogus.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
147
#
148
149
150
151
152
if ($nodetype eq "pcwa") {
    $nodevtype = "pcvwa";
    $nodeauxtype = "pcvwainet"
	if (!defined($nodeauxtype));
}    
153
else {
154
155
156
    $nodevtype = "pcvron";
    $nodeauxtype = "pcvroninet"
	if (!defined($nodeauxtype));
157
}
158

159
#
160
161
162
163
164
165
166
167
168
# Again, this is bogus.
#
if ($nodetype eq "pcwa") {
    $pid = "emulab-ops";
    $eid = "widearea-nodes";
}    
else {
    $pid = "ron";
    $eid = "all";
169
170
171
}

#
172
# Lets see if a known IP. If so we want to reuse the existing record.
173
#
174
175
176
177
178
179
180
181
182
183
if (TBIPtoNodeID($nodeip, \$nodename)) {
    #
    # We need the existing priority for creating the vnodes below.
    #
    $query_result =
	DBQueryFatal("select priority from nodes where node_id='$nodename'");

    my %row  = $query_result->fetchhash();
    $nextpri = $row{'priority'};
    $reuse   = 1;
184
185
}
else {
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    #
    # We need the next id and priority.
    # 
    DBQueryFatal("lock tables nextfreenode write");

    $query_result =
	DBQueryFatal("select nextid,nextpri from nextfreenode ".
		     "where nodetype='$nodetype'");

    if (!$query_result->numrows) {
	fatal("nodetype $nodetype is not in the nextfreenode table!");
    }
    my %row  = $query_result->fetchhash();
    $nextid  = $row{'nextid'};
    $nextpri = $row{'nextpri'};

    DBQueryFatal("update nextfreenode ".
		 "set nextid=nextid+1,nextpri=nextpri+1 ".
		 "where nodetype='$nodetype'");

    DBQueryFatal("unlock tables");

    #
    # Form a new node name! Bogus. need more DB state.
    #
    if ($nodetype =~ /^pc(\w+)$/) {
	$nodename = "${1}${nextid}";
    }
    else {
	fatal("Could not determine a name from type for $nodetype!");
    }
217
}
218

Leigh B. Stoller's avatar
Leigh B. Stoller committed
219
220
221
222
223
if (!defined($nickname)) {
    $nickname = $nodename;
}

#
224
# Enter the records
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225
#
226
227
if ($reuse) {
    print "Reusing exiting record for $nodename. IP=$nodeip\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
228
}
229
230
231
232
else {
    print "Creating widearea node $nodename ...\n";
    DBQueryFatal("insert into nodes ".
		 "(node_id, type, phys_nodeid, role, priority, ".
233
		 " op_mode, def_boot_osid) ".
234
		 "values ('$nodename', '$nodetype', '$nodename', ".
235
		 "        'testnode', $nextpri, 'NORMAL', ".
236
		 "        '$defosid') ");
237

238
239
240
241
    DBQueryFatal("insert into node_auxtypes ".
		 "(node_id, type, count) ".
		 "values ('$nodename', '$nodeauxtype', 1)");

242
243
244
245
    DBQueryFatal("replace into node_status ".
		 "(node_id, status, status_timestamp) ".
		 "values ('$nodename', 'down', now()) ");

246
247
    # Note that card/port are arbitrary for remote nodes since they are
    # not connected to local switches! 
248
    my $ifacerole = TBDB_IFACEROLE_CONTROL();
249
        
250
    DBQueryFatal("insert into interfaces ".
251
		 "(node_id, card, port, IP, interface_type, iface, role) ".
252
253
		 "values ('$nodename', 0, 1, '$nodeip', ".
		 "        'fxp', '$control_iface', '$ifacerole')");
254
255
256
257
258
259
260
261
262

    DBQueryFatal("insert into reserved ".
		 "(node_id, pid, eid, rsrv_time, vname) ".
		 "values ('$nodename', '$pid', '$eid', now(), '$nickname')");
}

#
# Always create these. I'll kill off the old ones as needed for now.
# 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
263
for ($i = 1; $i < 9; $i++) {
264
    my $priority  = ($nextpri * 100) + $i;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
265
    $nodevname = "v${nodename}-${i}";
266

267
268
269
    if (TBValidNodeName($nodevname)) {
	next;
    }
270
    print "Creating widearea virtual node $nodevname ...\n";
271
272
    DBQueryFatal("insert into nodes ".
		 "(node_id, type, phys_nodeid, role, priority, ".
273
		 " op_mode, def_boot_osid, update_accounts) ".
274
		 "values ('$nodevname', '$nodevtype', '$nodename', ".
275
		 "        'virtnode', $priority, 'NORMAL', ".
276
		 "        '$defosid', 1) ");
277
278
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
279
#
280
# Lets log it.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
281
#
282
283
284
285
286
287
my $subject;
my $message;
if ($reuse) {
    $subject = "Existing WA node has reinstalled";
    $message = "Existing WA node has been reinstalled";
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
288
else {
289
290
    $subject = "New WA node created";
    $message = "New Wide Area node has been created";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
291
}
292

293
294
SENDMAIL($TBLOGS, $subject,
	 "$message\n".
295
296
297
298
299
	 "\n".
	 "Type:      $nodetype\n".
	 "Name:      $nodename\n".
	 "IP:        $nodeip\n".
	 "Vtype:     $nodevtype\n".
300
	 "Auxtype:   $nodeauxtype\n".
301
302
303
	 "Vname:     $nodevname (actually the last vnode)\n".
	 "Nickname:  $nickname\n",
	 $TBOPS);
304
305
306
307
308

#
# update the named maps. Do it in the background since we do not
# want to hold up the caller.
#
309
print "Forcing a named map update ...\n";
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
my $logname = TBMakeLogname("newwanode");
if (TBBackGround($logname)) {
    #
    # Parent exits normally
    #
    exit(0);
}
system("$named_setup");
unlink($logname);
exit(0);

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

    print STDERR "$msg\n";
    SENDMAIL($TBOPS, "Failure creating new WA node", $msg, $TBOPS);
    exit(1);
}