newclient 13.1 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2003, 2004 University of Utah and the Flux Group.
5 6 7 8 9 10 11
# All rights reserved.
#

#
# Client-side script to report a new node into a testbed
#
#
12 13 14 15 16 17 18 19

use strict;

#
# Constants and the like
#
my $PREFIX;
my $TMCC;
20 21

if (-e "/usr/local/etc/emulab") {
22 23
    $PREFIX = "/usr/local/etc/emulab";
    $TMCC = "$PREFIX/tmcc.bin";
24 25
}
else {
26 27
    $PREFIX = "/etc/emulab";
    $TMCC = "$PREFIX/tmcc";
28
}
29

30 31
my $FETCH = "/usr/bin/fetch";

32
my $FORMURL = "newnodecheckin.php";
33

34 35 36
my $IFCONFIG = "/sbin/ifconfig";
my $DMESG = "/var/run/dmesg.boot";
my $TEACHSWITCH = "$PREFIX/teachswitch";
37

38 39 40 41 42 43

my $FLOPPYDEV  = "/dev/fd0";
my $FORMAT     = "/sbin/newfs_msdos";
my $MOUNTPOINT = "/mnt";
my $MOUNT      = "/sbin/mount";
my $UNMOUNT    = "/sbin/umount";
44

45
#
46
# Error codes, and how many times we beep to indicate each one.
47
#
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
my $ERROR_USAGE       = 0;
my $ERROR_NOBOSS      = 1;
my $ERROR_HWPROB      = 2;
my $ERROR_NODHCP      = 3;
my $ERROR_FLOPPYWRITE = 4;
my $ERROR_CHECKIN     = 5;

my %BEEP_CODES = (
    # 1 beep means okay, 2 means stick in a floppy
    $ERROR_NODHCP      => 3,
    $ERROR_NOBOSS      => 3,
    $ERROR_CHECKIN     => 3,
    $ERROR_FLOPPYWRITE => 4,
    $ERROR_HWPROB      => 4,
    $ERROR_USAGE       => 4
);
my $UNKNOWN_ERROR_BEEPS = 6;

66 67 68
my $MAXIMUM_BEEP_TIME   = 60 * 60; # In seconds - one hour
my $BEEP_SLEEP          = 3; # In seconds

69 70 71 72 73 74
#
# Constants for use in determining if the floppy is in or not
#
my $FLOPPY_REMOVED  = 0;
my $FLOPPY_INSERTED = 1;

75 76 77 78 79 80 81
#
# What type of client this node is. Possible values:
# testnode - The default, a regular testbed node
# ops      - The node checking in is actually ops
#
my $client_type = "testnode";

82 83 84 85 86 87 88 89 90 91
#
# Handle command-line arguments - we do them ourselves unstead of using getopt,
# because we want to be able to run this on minimalist environments where that
# may not exist.
#
my $headless	= 0;
my $writefloppy	= 0;
my $testing	= 0;
my $bossnode	= "";

92

93 94 95 96 97 98 99 100
my $uniquifier;
foreach my $arg (@ARGV) {
    if ($arg =~ s/^-//) {
	foreach my $letter (split('',$arg)) {
	    SWITCH: for ($letter) {
		/h/ && do { $headless = 1; last; };
		/w/ && do { $writefloppy = 1; last; };
		/t/ && do { $testing = 1; last; };
101
		/o/ && do { $client_type = "ops"; last; };
102 103
	    };
	}
104
    } else {
105 106 107 108 109
	if ($uniquifier) {
	    exit usage();
	} else {
	    $uniquifier = $arg;
	}
110 111 112
    }
}

113
sub usage {
114
    print "Usage: $0 [-h] [-w] [-t] [-o] [identifer]\n";
115 116 117
    print " -h         - Run headless (beep codes for errors)\n";
    print " -w         - Write an the node's unique identifier to a floppy\n";
    print " -t         - Test mode\n";
118
    print " -o         - 'ops' mode\n";
119 120 121 122
    print " identifier - supply the given identifier to boss\n";
    1;
}

123
#
124 125 126
# Find out what our boss node was, so we don't have to hardcode it here - allow
# the user to provide one on the command line, though, so that we can run tests
# on nodes that aren't in the testbed
127 128
#
if (!$bossnode) {
129 130
    my $bossinfo = `$TMCC bossinfo`;
    $bossinfo =~ /^([\w\-.]+)/;
131
    $bossnode = $1;
132 133 134
    if (!$bossnode) {
	error_fatal($ERROR_NOBOSS,"Unable to parse boss name from '$bossinfo'\n");
    }
135 136 137
}

#
138
# Get physical information about this node
139
#
140 141 142
my @ifaces = find_interfaces();
my ($diskdev, $disksize) = get_disksize();
my $speed = get_cpuspeed();
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#
# For some node types, we may already know the node_id we're supposed to use
#
my $node_id;
if ($client_type eq "ops") {
    $node_id = "ops";
}

#
# For some node types, we know we're supposed to use our 'temporary' IP as the
# permanent one - take care of those now
#
my $use_temp_IP = 0;
if ($client_type eq "ops") {
    $use_temp_IP = 1;
}

#
# Also, keep track of the role this node will have in the testbed
#
my $role = "testnode";
if ($client_type eq "ops") {
    $role = "ctrlnode";
}

#
# In some cases, we may know the node's type in advance
#
my $type;
if ($client_type eq "ops") {
    $type = "ops";
}

177 178 179 180
#
# Start the program that will annouce us to the switch, so that it learns
# MAC addresses, etc.
#
181
if (!$testing && ($client_type ne "ops")) {
182 183
    teachswitch();
}
184

185 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 217
#
# Report this stuff back to the web script on boss - build up a URL to do so
#
my $URL = buildURL();

print "URL is $URL\n";
my $identifier;
if (!$testing) {
    $identifier = checkin($URL);
} else {
    # Just make one up for testing purposes
    $identifier = 314;
}

print "This node's identifier is: $identifier\n";

if ($writefloppy) {
    writefloppy($identifier);
}

#
# Little subroutine to URL encode data we're sending through the web interface
#
sub urlencode {
    my ($string) = @_;
    my @chars = split //, $string;
    my $encoded = "";
    foreach my $char (@chars) {
	if ($char =~ /[0-9a-zA-Z]/) {
	    $encoded .= $char;
	} else {
	    $encoded .= sprintf "%%%02X", ord($char);
	}
218 219
    }

220
    return $encoded;
221 222 223
}

#
224 225
# Return a URL containing the data we've collected, to be fetched from boss to
# check in
226
#
227 228 229 230 231
sub buildURL {
    my $URL = "http://$bossnode/$FORMURL?";
    $URL .= "cpuspeed=" . urlencode($speed);
    $URL .= "&diskdev=" . urlencode($diskdev);
    $URL .= "&disksize=" . urlencode($disksize);
232 233 234 235
    $URL .= "&role=" . urlencode($role);
    if ($node_id) {
	$URL .= "&node_id=" . urlencode($node_id);
    }
236 237 238
    if ($uniquifier) {
	$URL .= "&identifier=". urlencode($uniquifier);
    }
239 240 241 242 243 244
    if ($use_temp_IP) {
	$URL .= "&use_temp_IP=1";
    }
    if ($type) {
	$URL .= "&type=" . urlencode($type);
    }
245 246 247 248 249 250 251 252 253 254 255 256

    my $ifaceindex = 0;
    foreach my $aref (@ifaces) {
	my ($iface, $mac, $status) = @$aref;
	$URL .= "&ifacename$ifaceindex=" . urlencode($iface);
	$URL .= "&ifacemac$ifaceindex=" . urlencode($mac);
	$ifaceindex++;
    }

    $URL .= "&messages=" . urlencode(join "", @::messages);

    return $URL;
257 258 259
}

#
260
# Actually check in - return this node's ID if we get one
261
#
262 263
sub checkin {
    my ($URL) = @_;
264
    open(CHECKIN,"$FETCH -o '-' '$URL'|")
265 266 267 268 269 270 271
	or error_fatal($ERROR_CHECKIN,"Unable to check in with boss!\n");
    my $id = undef;
    while (<CHECKIN>) {
	print;
	if (/Node ID is (\d+)/) {
	    $id = $1;
	}
272
    }
273
    return $id;
274 275
}

276 277 278 279
######################################################################
# Node hardware functions
######################################################################

280
#
281 282
# Grab a list of all interfaces on this node - returns a list of [$iface, $mac,
# $status] for each one
283
#
284 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
sub find_interfaces {
    #
    # Grab this node's MAC addresses - we'll just parse them from the output of
    # ifconfig for now.
    #
    my @ifconfig_lines = `$IFCONFIG`;
    my @ifaces;
    my $iface = "";
    my $mac;
    my $status;
    foreach my $line (@ifconfig_lines) {
	chomp $line;
	SWITCH: for ($line) {
	    #
	    # A line beginning a new interface
	    #
	    (/^(\w+):/) && do {
		if ($iface) {
		    #
		    # We have an old interface to save away, but only if it's
		    # Ethernet
		    #
		    if ($mac) {
			push @ifaces, [$iface, $mac, $status];
		    }
		}

		$iface = $1;
		$mac = $status = "";
		last SWITCH;
	    };

	    #
	    # A line containing a MAC address
	    #
	    (/\s+ether ([0-9a-f:]+)/) && do {
		$mac = $1;
		$mac =~ s/://g;
		if (length($mac) != 12) {
		    error($ERROR_HWPROB,"Malformed MAC $mac\n");
		    $mac = undef;
		}
		last SWITCH;
	    };
	
	    #
	    # A line containing the interface status
	    #
	    (/^\s+status: (.*)/) && do {
		$status = $1;
		last SWITCH;
	    };
	}

    }

    #
    # Get the last one
342
    #
343 344 345 346 347 348
    if ($iface && $mac) {
	push @ifaces, [$iface, $mac, $status];
    }

    #
    # Warn about any that don't have carrier
349
    #
350 351 352 353 354
    foreach my $aref (@ifaces) {
	my ($iface, $mac, $status) = @$aref;
	if ($status ne "active") {
	    message("WARNING: $iface has no carrier!\n");
	}
355
    }
356 357

    return @ifaces;
358 359
}

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
#
# Figure out the disk drive size - returns a pair containing the disk device
# (ie. ad0 or da1) and the size in megabytes
#
sub get_disksize {
    if (!open(DMESG,"<$DMESG")) {
	error($ERROR_HWPROB,"Unable to open $DMESG\n");
	return ("",0);
    }
    my ($diskdev, $disksize);
    while (<DMESG>) {
	chomp;
	#
	# Take the first of ad or da, whichever we find first
	#
	if (/^((ad|da|ar|aacd)\d): (\d+)MB/) {
	    $diskdev = $1;
	    $disksize = $3;
	    last;
	}
    }

    if (!$diskdev) {
	message("WARNING: Unable to find disk drive\n");
	$diskdev = "unknown";
	$disksize = 0;
    }

    return ($diskdev, $disksize);
389 390 391
}

#
392
# Find this node's CPU speed - returns the speed in MHz
393
#
394 395 396 397 398 399 400 401 402 403 404 405 406 407
sub get_cpuspeed {
    if (!open(DMESG,"<$DMESG")) {
	error($ERROR_HWPROB,"Unable to open $DMESG\n");
	return (0);
    }
    my $speed = 0;
    while (<DMESG>) {
	if (/^CPU:.*\((\d+(\.\d+)?)+\-MHz/) {
	    $speed = $1;
	    last;
	}
    }
    return $speed;
}
408 409

#
410
# Teach the switch where we are
411
#
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
sub teachswitch {
    system "$TEACHSWITCH &" and message("Unable to start teachswitch: $!\n");

    #
    # Sleep for a few seconds to give teachswitch the chance to do its thing
    # before anyone comes looking
    #
    sleep(10);
}

######################################################################
# Error functions
######################################################################

# 
# Print a message, and send it on to the script we're checking in with
# 
sub message {
    my ($message) = @_;
    print $message;
    push @::messages, $message;
}

435

436
#
437
# Report an error, which is non-fatal
438
#
439 440 441
sub error {
    my ($errno, $string) = @_;

442 443 444 445
    my $errstr = "*** Error $errno: $string\n"; 

    print STDERR $errstr;
    push @::messages, $errstr;
446 447 448 449 450

    #
    # Maybe we ought to beep when headless - right now, we do this only for
    # fatal errors, but maybe we should do it for all of them.
    #
451

452
}
453

454
#
455
# Same as above, but a fatal error
456
#
457
sub error_fatal {
458
    my ($errno, $string) = @_;
459 460 461
    my $errstr =  "*** Fatal Error $errno: $string\n";
    print STDERR $errstr;
    push @::messages, $errstr;
462 463 464 465 466

    #
    # If headless, beep to let the user know what went wrong
    #
    if ($headless) {
467 468 469 470 471
	# Try to write the messages to the floppy
	if ($writefloppy) {
	    writefloppy_messages();
	}

472 473 474 475 476
	my $beeps = $BEEP_CODES{$errno} || $UNKNOWN_ERROR_BEEPS;
	beep($beeps);
    } else {
	exit 1;
    }
477
}
478

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
######################################################################
# Functions for dealing with the floppy drive
######################################################################

#
# Beep to let the user know that they are expected to insert or remove the
# floppy.
#
sub wait_for_floppy {
    my ($state) = @_;
    if ($state == $FLOPPY_INSERTED) {
	print "Waiting for user to insert a floppy\n";
	beep(2,$state);
    } else {
	print "Waiting for user to remove the floppy\n";
	beep(1,$state);
    }
496 497
}

498 499 500 501 502
#
# Write out the node's identifier to a floppy
#
sub writefloppy {
    my ($id) = @_;
503

504 505 506 507 508 509
    #
    # Make sure the floppy is readable
    #
    if (!checkfloppy()) {
	wait_for_floppy($FLOPPY_INSERTED);
    }
510

511 512 513 514 515 516
    #
    # Format that sucker
    #
    if (system "$FORMAT $FLOPPYDEV") {
	error_fatal($ERROR_FLOPPYWRITE,"Floppy format failed: $!\n");
    }
517
    $::floppy_formatted = 1;
518

519 520 521 522 523 524
    #
    # Drop a file down with the unique identifier we were provided
    #
    if (system "$MOUNT -t msdos $FLOPPYDEV $MOUNTPOINT") {
	error_fatal($ERROR_FLOPPYWRITE,"Failed to mount floppy: $!\n");
    }
525
    $::floppy_mounted = 1;
526 527 528 529 530 531 532

    if (!open(IDFILE,">$MOUNTPOINT/node.id")) {
	error_fatal($ERROR_FLOPPYWRITE,"Failed to write floppy: $!\n");
    }

    print IDFILE "$id\n";
    close(IDFILE);
533 534 535 536 537 538

    #
    # Write out any messages we had, too
    #
    writefloppy_messages();

539
    system "$UNMOUNT $MOUNTPOINT";
540
    $::floppy_mounted = 0;
541 542 543 544 545 546 547

    #
    # Let the user know we're done
    #
    wait_for_floppy($FLOPPY_REMOVED);

    print "Floppy written!\n";
548 549
}

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
#
# Write out a log of messages to the floppy - check to see if any of the basic
# tasks have already been done by writefloppy, and avoid re-doing them if they
# have. If we fail, just silently return, we have no way to tell anyone why
# we failed.
#
sub writefloppy_messages {

    #
    # Format that sucker
    #
    if (!$::floppy_mounted && !$::floppy_formatted) {
	if (system "$FORMAT $FLOPPYDEV") {
	    return;
	}
    }

    #
    # Drop a file down with the unique identifier we were provided
    #
    my $mounted_floppy = 0;
    if (!$::floppy_mounted) {
	$mounted_floppy = 1;
	if (system "$MOUNT -t msdos $FLOPPYDEV $MOUNTPOINT") {
	    return;
	}
    }

    if (!open(MFILE,">$MOUNTPOINT/messages")) {
	return;
    }

    print MFILE join("",@::messages), "\n";
    close(MFILE);

    if ($mounted_floppy) {
	system "$UNMOUNT $MOUNTPOINT";
    }

    return;

}

593
#
594 595 596
# Beep for a while - first argument is the number of beeps in each group.  If
# the second argument is non-zero, will stop beeping and return when the floppy
# is inserted or removed
597
#
598 599 600 601 602 603
sub beep {
    my ($count, $exitOnChange) = @_;

    $count = 1 unless $count;

    my $floppystate;
604 605
    my $iters = 0;
    my $maxiters = $MAXIMUM_BEEP_TIME / $BEEP_SLEEP;
606 607 608 609 610 611 612 613
    while (1) {
	foreach my $i (1 .. $count) {
	    syswrite STDOUT, "\a";
	    select(undef, undef, undef,.2);
	}

	if (defined($exitOnChange) && (checkfloppy() == $exitOnChange)) {
	    return;
614
	}
615

616 617 618 619
	sleep($BEEP_SLEEP);
	if ($iters++ > $maxiters) {
	    die("Timed out\n");
	}
620
    }
621
}
622

623 624 625 626 627 628 629
#
# Check to see if the floppy is readable - 1 if it is, 0 if not
#
sub checkfloppy {
    open(FLOPPY,"<$FLOPPYDEV") or return $FLOPPY_REMOVED;
    close(FLOPPY);
    return $FLOPPY_INSERTED;
630
}