os_setup.in 15.6 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
4
5
6
7
8

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

9
use English;
10
use Getopt::Std;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
11
require 'ctime.pl';
12

13
#
14
15
16
17
# Reboot the nodes in an experiment. The nodes table will already contain
# all the information. This script deals with possible disk reloading,
# rebooting, and waiting for nodes to come back alive before allowing
# experiment creation to continue.
18
#
19
# TODO: Reload disk images.
20
# 
21
# usage: os_setup <pid> <eid>
22
#
23
24
25
26
27
sub usage()
{
    print STDOUT "Usage: os_setup <pid> <eid>\n";
    exit(-1);
}
28
my  $optlist = "d";
29
30
31
32
33
34

#
# Configure variables
#
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";
35
my $TBOPS       = "@TBOPSEMAIL@";
36
my $TESTMODE    = @TESTMODE@;
37
my $TFTP	= "/tftpboot";
38

39
40
41
42
43
44
45
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

46
my $nodereboot	= "$TB/bin/node_reboot";
47
my $os_load	= "$TB/bin/os_load";
48
my $vnode_setup = "$TB/sbin/vnode_setup";
49
my $dbg		= 0;
50
my $failed      = 0;
51
my @nodes       = ();
52
my @vnodes      = ();
53
my %osids       = ();
54
my %waitfor     = ();
55
my %canfail     = ();
56
my $db_result;
57
my @row;
58

59
#
60
61
62
# Ah, Frisbee works so lets do auto reloading for nodes that do not have
# the proper OS loaded on it. This will be a hash of lists; for each
# imageid, a list of the nodes to pass to os_load for that imageid. 
63
#
64
65
66
my %reloads     = ();
my %reboots	= ();
my $doautoload  = 1;
67
my $dolastload  = 1;
68
    
69
70
71
72
73
74
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

75
76
77
78
79
80
81
82
83
84
85
#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}
86
87
88
89
if (defined($options{"d"})) {
    $dbg = 1;
}

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
my $pid = $ARGV[0];
my $eid = $ARGV[1];

#
# Untaint args.
#
if ($pid =~ /^([-\@\w]+)$/) {
    $pid = $1;
}
else {
    die("Bad data in pid: $pid.");
}
if ($eid =~ /^([-\@\w]+)$/) {
    $eid = $1;
}
else {
    die("Bad data in eid: $eid.");
}

109
#
110
# Figure out who called us. Only root, people with admin status
111
112
# in the DB, or the owner of the experiment can run this script.
#
113
if ($UID && !TBAdmin($UID)) {
114
115
116
    my ($me) = getpwuid($UID)
	or die "$UID not in passwd file";

117
118
119
120
    my $leader = ExpLeader($pid, $eid);

    if ($me ne $leader) {
	die("os_setup: You must be root or a TB administrator\n");
121
122
123
    }
}

124
125
TBDebugTimeStamp("os_setup started");

126
#
127
# Get the set of nodes, as well as the nodes table information for them.
128
#
129
$db_result =
130
131
132
133
    DBQueryFatal("select n.*,l.pid from nodes as n ".
		 "left join reserved as r on n.node_id=r.node_id ".
		 "left join last_reservation as l on n.node_id=l.node_id ".
		 "where r.pid='$pid' and r.eid='$eid'");
134

135
136
if ($db_result->numrows < 1) {	
    die("There are no nodes assigned to experiment '$eid' in project '$pid'.");
137
138
}

139
while (my %row = $db_result->fetchhash()) {
140
141
    my $node     = $row{'node_id'};
    my $osid     = $row{'def_boot_osid'};
142
    my $type     = $row{'type'};
143
    my $bootpath = 0;
144

145
    #
146
    # VIRTNODE HACK: Virtual nodes are special. 
147
148
    #
    if (TBIsNodeVirtual($node)) {
149
	push(@vnodes, $node);
150
151
152
	next;
    }

153
    push(@nodes, $node);
154
    $osids{$node} = $osid;
155
    $reboots{$node} = 1;
156

157
    #
158
159
160
    # Make sure the files specified in the paths exist. We mount the
    # user tftp directory on boss node, so we can ignore the IP address,
    # and just check the path directly. 
161
162
163
164
    #
    if (defined($row{'def_boot_path'})) {
	my $path = $row{'def_boot_path'};

165
166
167
168
169
170
171
172
173
174
175
	if ($path ne "") {
	    my $ip   = 0;

	    # Split out IP address if it exists.
	    if ($path =~ /^([0-9\.]+):(\/.*)$/) {
		$ip   = $1;
		$path = $2;
	    }

	    # Path must begin with $TFTP
	    if (! ($path =~ /^\/$TFTP\//)) {
176
		die("*** File $path for node $node must reside in $TFTP\n");
177
178
	    }

179
	    if (! -f $path) {
180
		die("*** File $path for node $node does not exist!");
181
	    }
182
	    $bootpath = 1;
183
	}
184
185
186
187
    }
    if (defined($row{'next_boot_path'})) {
	my $path = $row{'next_boot_path'};

188
189
190
191
192
193
194
195
196
197
198
	if ($path ne "") {
	    my $ip   = 0;

	    # Split out IP address if it exists.
	    if ($path =~ /^([0-9\.]+):(\/.*)$/) {
		$ip   = $1;
		$path = $2;
	    }

	    # Path must begin with $TFTP
	    if (! ($path =~ /^\/$TFTP\//)) {
199
		die("*** File $path for node $node must reside in $TFTP\n");
200
201
	    }

202
	    if (! -f $path) {
203
		die("*** File $path for node $node does not exist!");
204
205
	    }
	}
206
207
    }

208
209
210
211
212
213
214
    #
    # XXX - Check for existence of the delta files. We do this here
    # cause its easier than looking for a failure later, when the node
    # tries to install the delta. Not a general solution though. Needs
    # more thought.
    #
    foreach my $delta (split(":", $row{'deltas'})) {
215
	if (! -f $delta) {
216
	    die("*** Delta file $delta for node $node does not exist!");
217
218
219
220
221
222
	}
    }
    #
    # XXX - Ditto for RPMs.
    #
    foreach my $rpm (split(":", $row{'rpms'})) {
223
	if (! -f $rpm) {
224
	    die("*** RPM $rpm for node $node does not exist!");
225
226
227
	}
    }
    
228
229
230
231
232
233
    #
    # XXX - Ditto for tarfiles.
    #
    foreach my $tarspec (split(":", $row{'tarballs'})) {
	my ($dir, $tar) = split(" ", $tarspec);
	
234
	if (! -f $tar) {
235
	    die("*** Tarfile $tar for node $node does not exist!");
236
237
	}
    }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

    #
    # If there is a path specified, then we don't worry anymore about it.
    # The user must know what is going on. The OSID might have a path
    # associated with it, which means the same thing; we don't worry about
    # it. 
    #
    if (! $bootpath) {
	#
	# These checks are not necessary if the front end and web page
	# are doing the right thing, but lets be careful anyway.
	# 
	if (! $osid) {
	    die("*** $node has no bootpath and no def_boot_osid set!\n");
	}

	#
	# Grab the info for this OSID. This is part of the image check.
	#
	my $osid_result =
	    DBQueryFatal("select * from os_info where osid='$osid'");
	
	if ($osid_result->numrows == 0) {
	    die("*** No such OSID $osid is defined!\n");
	}
	
	my %osid_row   = $osid_result->fetchhash();

	#
	# If there is an actual path, its an OSKit kernel not an image.
	# 
269
	if (! defined($osid_row{'path'}) || $osid_row{'path'} eq "") {
270
	    #
271
272
	    # Not an OSKit kernel.
	    # Make sure this OSID is actually loaded on the machine. 
273
	    #
274
275
276
	    my $p_result =
		DBQueryFatal("select * from partitions ".
			     "where node_id='$node' and osid='$osid'");
277
278

	    #
279
280
	    # If not loaded, then see if the user was looking for the generic
	    # name of the OS that is loaded. 
281
	    # 
282
283
	    if ($p_result->numrows == 0) {
		#
284
		# Check to see if a non specific version specified.
285
		#
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
		if (! defined($osid_row{'version'}) ||
		    $osid_row{'version'} eq "") {

		    #
		    # A non-specific version. Try to map it.
		    # 
		    my $o_result =
			DBQueryFatal("select o1.* from os_info as o1 ".
				     "left join partitions as p ".
				     "  on o1.osid=p.osid ".
				     "left join os_info as o2 ".
				     "  on o2.OS=o1.OS ".
				     "where p.node_id='$node' ".
				     "  and o2.osid='$osid'");

		    if ($o_result->numrows == 0) {
			die("*** $0:\n".
			    "    No mapping can be made for $osid on $node!\n".
			    "    Perhaps the disk needs reloading?\n");
		    }
		    else {
			my %o_row  = $o_result->fetchhash();
			my $n_osid = $o_row{'osid'};

			print "Mapping $osid on $node to $n_osid.\n";
311
312
313
314
315
316
317
318
319
320

			if ($dolastload &&
			    defined($row{'pid'}) && $row{'pid'} ne $pid) {
			    SetupReload($node, $n_osid, $type);
			}
			else {
			    DBQueryFatal("update nodes ".
					 "set def_boot_osid='$n_osid' ".
					 "where node_id='$node'");
			}
321
322
			$osids{$node} = $n_osid;
		    }
323
324
		}
		else {
325
326
		    #
		    # User wants a specific version of an OS, but its not
327
		    # loaded on the machine. 
328
		    #
329
330
331
332
333
334
335
336
337
338
		    SetupReload($node, $osid, $type);
		}
	    }
	    else {
		#
		# OSID is loaded, but might need to be cleaned. 
		#
		if ($dolastload &&
		    defined($row{'pid'}) && $row{'pid'} ne $pid) {
		    SetupReload($node, $osid, $type);
339
		}
340
341
342
	    }
	}
    }
343
    
344
    #
345
    # If pingable, then the node is "waitable".
346
    #
347
    if (OSFeatureSupported($osids{$node}, "ping")) {
348
	$waitfor{$node} = 1;
349
350
    }
    else {
351
	$waitfor{$node} = 0;
352
    }
353
354
355
356
357

    #
    # Set the canfail bit. Currently, sharks are always canfail=1.
    # Will come from DB at some point.
    #
358
    if ($row{'type'} eq "dnard") {
359
360
361
362
363
364
	$canfail{$node} = 1;
    }
    else {
	$canfail{$node} = 0;
    }
    
365
    print STDERR "$node - $osids{$node} - $waitfor{$node} - $canfail{$node}\n"
366
	if $dbg;
367
}
368

369
#
370
# We need to issue the reboots and the reloads in parallel.
371
#
372
TBDebugTimeStamp("rebooting/reloading started");
373
if (!$TESTMODE) {
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
    my %pids  = ();
    my $count = 0;
    my $cmd;

    if (keys(%reboots)) {
	$cmd = "$nodereboot " . join(" ", keys(%reboots));
	$pids{$cmd} = ForkCmd($cmd);
    }

    foreach my $imageid ( keys(%reloads) ) {
	my @list = @{ $reloads{$imageid} };

	sleep(5);
	$pids{"$os_load -m $imageid @list"} =
	    ForkCmd("$os_load -m $imageid @list");
    }

    foreach $cmd ( keys(%pids) ) {
	my $pid = $pids{$cmd};

	waitpid($pid, 0);
	if ($?) {
	    $failed++;
	    print STDERR "*** Failed: $cmd\n";
	}
    }
400
401
}

402
TBDebugTimeStamp("rebooting/reloading finished");
403
sleep(2);
404

405
#
406
407
408
# Now lets wait for them to come back alive. Set up a retry list though
# so that we can give each node at least 1 second chance. Avoids pointless
# experiment failures.
409
#
410
411
412
413
if (@nodes) {
    print "Waiting for local testbed nodes to finish rebooting ...\n";
}

414
415
my %retries;
my %waitstart;
416
foreach my $node ( @nodes ) {
417
418
419
420
    $retries{$node} = 1;
    $waitstart{$node} = time;
}

421
TBDebugTimeStamp("Local node waiting started");
422
423
424
while ( @nodes ) {
    my $node = shift(@nodes);

425
426
427
428
    #
    # Don't bother to wait for nodes that are running foreign OSs since
    # we are not going to deal with them anyway later in the process.
    #
429
430
    if ($waitfor{$node} == 0) {
	print STDOUT "Not waiting for $node to come alive. Foreign OS.\n";
431
	SetNodeBootStatus($node, NODEBOOTSTATUS_UNKNOWN);
432
433
434
	next;
    }	

435
436
    if (WaitTillAlive($node) == 0) {
	print STDOUT "$node is alive and well\n";
437
	SetNodeBootStatus($node, NODEBOOTSTATUS_OKAY);
438
439
	next;
    }
440

441
442
443
444
445
446
447
448
449
450
451
452
    if ($retries{$node}) {
	$retries{$node} -= 1;

	print "*** Rebooting $node and waiting again ...\n";
	
	if (system("$nodereboot $node") == 0) {
	    push(@nodes, $node);
	    $waitstart{$node} = time;
	    next;
	}
	# Fall through on failure.
    }
453

454
455
456
457
458
    SetNodeBootStatus($node, NODEBOOTSTATUS_FAILED);
	
    print "*** WARNING: $node may be down.\n".
	  "    This has been reported to testbed-ops.\n";
	
459
460
461
462
    if ($canfail{$node}) {
	# Send mail to testbed-ops and to the user about it.
	my ($user) = getpwuid($UID);
	
463
	SENDMAIL($user, "Node $node is down",
464
465
466
467
468
469
470
471
472
473
474
475
476
		 "Node $node in pid/eid $pid/$eid appears to be dead.\n\n".
		 "Your experiment will continue to run since this failure\n".
		 "is nonfatal, although you might encounter other problems\n".
		 "if your experiment depends explicitly on this node.\n".
		 "You should terminate this experiment if it cannot ".
		 "tolerate this failure.\n\n".
		 "Testbed Operations has also been notified so they can ".
		 "investigate.\n\n".
		 "Thanks\n".
		 "Testbed Operations\n",
		 0,
		 "Cc: $TBOPS");

477
478
	print "*** Continuing with experiment setup anyway ...\n";
	next;
479
480
    }

481
482
    # Reserve it to down experiment.
    MarkNodeDown($node);
483

484
485
486
487
488
489
490
    # Send mail to testbed-ops about it
    SENDMAIL($TBOPS, "Node $node is down",
	     "Node $node in pid/eid $pid/$eid appears to be dead.\n\n".
	     "$node has been taken out of the pool until this matter ".
	     "is resolved.\n");

    print "*** Experiment will be terminated automatically.\n";
491
    $failed++;
492
}
493
TBDebugTimeStamp("Local node waiting finished");
494

495
496
497
498
499
500
501
#
# Now deal with virtual nodes.
#
# We do this in a sub script since nodes are not owned by the user
# and so must be setuid root so that ssh will work.
#
if ( @vnodes ) {
502
    system("$vnode_setup -f $pid $eid");
503
504
    
    if ($?) {
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
	die("*** $0:\n".
	    "    Vnode setup failed!\n");
    }

    foreach my $node ( @vnodes ) {
	$waitstart{$node} = time;
    }

    print "Waiting for widearea testbed nodes to finish rebooting ...\n";

    TBDebugTimeStamp("Widearea node waiting started");
    while ( @vnodes ) {
	my $node = shift(@vnodes);

	if (WaitTillAlive($node) == 0) {
	    print STDOUT "$node is alive and well\n";
	    SetNodeBootStatus($node, NODEBOOTSTATUS_OKAY);
	    next;
	}

	SetNodeBootStatus($node, NODEBOOTSTATUS_FAILED);
	
	print "*** WARNING: $node may be down.\n".
	      "    This has been reported to testbed-ops.\n";
	
	if ($canfail{$node}) {
	    # Send mail to testbed-ops and to the user about it.
	    my ($user) = getpwuid($UID);
	
	    SENDMAIL($user, "Widearea Node $node is down",
		 "Node $node in pid/eid $pid/$eid appears to be dead.\n\n".
		 "Your experiment will continue to run since this failure\n".
		 "is nonfatal, although you might encounter other problems\n".
		 "if your experiment depends explicitly on this node.\n".
		 "You should terminate this experiment if it cannot ".
		 "tolerate this failure.\n\n".
		 "Testbed Operations has also been notified so they can ".
		 "investigate.\n\n".
		 "Thanks\n".
		 "Testbed Operations\n",
		 0,
		 "Cc: $TBOPS");

	    print "*** Continuing with experiment setup anyway ...\n";
	    next;
	}

	# Reserve it to down experiment. 
	# MarkNodeDown($node);

	# Send mail to testbed-ops about it
	SENDMAIL($TBOPS, "Widearea Node $node is down",
		 "Widearea node $node in pid/eid $pid/$eid appears to be ".
		 "unresponsive.\n\n");

	print "*** Experiment will be terminated automatically.\n";
561
562
	$failed++;
    }
563
    TBDebugTimeStamp("Widearea node waiting finished");
564
565
}

566
print STDOUT "OS Setup Done!\n";
567
TBDebugTimeStamp("os_setup finished");
568
exit $failed;
569

570
571
572
#
# Wait for a node to come back alive.
# 
573
sub WaitTillAlive ($) {
574
    my ($pc) = @_;
575

576
577
578
    print STDERR "Waiting for $pc to come alive\n" if $dbg;
    
    #
579
    # Seems like a long time to wait, but it ain't!
580
    # 
581
    my $maxwait = (60 * 7);
582

583
584
585
586
587
588
589
    #
    # Start a counter going, relative to the time we rebooted the first
    # node. 
    # 
    my $waittime  = 0;
    my $minutes   = 0;

590
    #
591
    # Wait for the node to finish booting, as recorded in database
592
    #
593
    while (1) {
594
595
596
597
598
	my $state;
	if (!TBGetNodeEventState($pc,\$state)) {
	    print "*** Error getting event state for $pc.\n";
	    return 1;
	}
599

600
	if ($state eq TBDB_NODESTATE_ISUP) {
601
	    print "$pc is alive and well\n" if $dbg;
602
603
	    return 0;
	}
604
	
605
	$waittime = time - $waitstart{$pc};
606
	if ($waittime > $maxwait) {
607
	    $minutes = int($waittime / 60);
Robert Ricci's avatar
Robert Ricci committed
608
	    print "*** $pc appears dead - it's been $minutes minute(s).\n";
609
	    return 1;
610
	}
611
612
	if (int($waittime / 60) > $minutes) {
	    $minutes = int($waittime / 60);
Robert Ricci's avatar
Robert Ricci committed
613
	    print "Still waiting for $pc - it's been $minutes minute(s).\n";
614
	}
615
	sleep(1);
616
617
    }
}
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637

#
# Map an OSID to an imageid for a node type.
#
sub TBMapOSIDtoImageID($$)
{
    my ($osid, $type) = @_;

    my $query_result =
	DBQueryFatal("select imageid from osidtoimageid ".
		     "where type='$type' and osid='$osid'");

    if ($query_result->numrows == 0) {
	return 0;
    }
    my ($imageid) = $query_result->fetchrow_array();

    return $imageid;
}

638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#
# Setup a reload of a node if we can find an image.
# This goo constructs a hashed array of lists.
#
sub SetupReload($$$)
{
    my ($node, $osid, $type) = @_;

    if ((my $imageid = TBMapOSIDtoImageID($osid, $type))) {
	if (! defined($reloads{$imageid})) {
	    $reloads{$imageid} = [ $node ];
	}
	else {
	    push(@{ $reloads{$imageid} }, $node);
	}
	delete $reboots{$node};
    }
    else {
	die("*** $0:\n".
	    "    No image can be found for $osid on $node!\n");
    }
}

661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#
# Fork a process to exec a command. Return the pid to wait on.
# 
sub ForkCmd($) {
    my ($cmd) = @_;
    my($mypid);

    $mypid = fork();
    if ($mypid) {
	return $mypid;
    }

    if ($dbg) {
	print STDERR "Forking command: $cmd\n";
    }

    system($cmd);
    exit($? >> 8);
}