update_sitevars.in 13.1 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2012, 2018 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# 
# {{{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/>.
# 
# }}}
#
use English;
use Socket;
use strict;
use Getopt::Std;

#
# Update site-specific site variables.
# The intent is that this can be run at any time to update "things that
# might change." We'll see how that works out...
#
sub usage()
{
    print("Usage: update_sitevars [-d level] [-n] [host]\n");
    exit(-1);
}
39
my $optlist = "d:nvqi";
40 41
my $debug   = 0;
my $doit    = 1;
42 43 44
my $verify  = 0;
my $quiet   = 0;
my $verify_count = 0;
45 46 47 48 49 50 51
my $hostip;

#
# Configure variables
#
my $TB		= "@prefix@";
my $SBINDIR	= "$TB/sbin";
52
my $OPSVM_ENABLE= @OPSVM_ENABLE@;
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 100 101 102

my $NC_NET	= "@CONTROL_NETWORK@";
my $NC_MASK	= "@CONTROL_NETMASK@";
my $PR_NET	= "@PRIVATE_NETWORK@";
my $PR_MASK	= "@PRIVATE_NETMASK@";
my $PU_NET	= "@PUBLIC_NETWORK@";
my $PU_MASK	= "@PUBLIC_NETMASK@";

my $GW_IP	= "@CONTROL_ROUTER_IP@";
my $BOSS_IP	= "@BOSSNODE_IP@";
my $OPS_IP	= "@USERNODE_IP@";
my $FS_IP	= "@FSNODE_IP@";

my $sshopt      = "-o ConnectTimeout=5 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null";

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

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

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use emdb;
use libEmulab;
#use EmulabConstants;

# Protos
sub update_arplockdown();
sub fatal($);
sub ExecQuiet($$);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = $options{"d"};
}
103
if (defined($options{"i"}) || defined($options{"n"})) {
104 105
    $doit = 0;
}
106 107 108 109 110 111 112
if (defined($options{"v"})) {
    $verify = 1;
    $doit = 0;
}
if (defined($options{"q"})) {
    $quiet = 1;
}
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
if (@ARGV > 0) {
    my $addr = inet_aton($ARGV[0]);
    if ($addr) {
	$hostip = inet_ntoa($addr);
    }
    if (!$hostip) {
	fatal("Could not resolve " . $ARGV[0]);
    }
    my $hostnet = inet_ntoa(inet_aton($hostip) & inet_aton($NC_MASK));
    my $nodenet = inet_ntoa(inet_aton($NC_NET) & inet_aton($NC_MASK));
    if ($hostnet ne $nodenet) {
	fatal("Node " . $ARGV[0] . " is not on the not control net ($NC_NET)");
    }
    shift @ARGV;
}
usage() 
    if (@ARGV != 0);

#
# Right now, all we know how to do is update the ARP-related sitevars
#
update_arplockdown();

136 137 138 139 140 141 142
if ($verify) {
    if (!$quiet && $verify_count) {
	print "*** Your site variables are out of date.\n";
	print "    Run 'update_sitevars' to update them as indicated.\n";
    }
    exit($verify_count);
}
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
exit(0);

sub update_arplockdown()
{
    my %vars = (
	"node/gw_ip" => {
	    'desc'     => "IP address of the control net router (NULL if none)",
	    'defval'   => "",
	},
	"node/gw_mac" => {
	    'desc'     => "MAC address of the control net router (NULL if none)",
	    'defval'   => "",
	},
	"node/boss_ip" => {
	    'desc'     => "IP address of the boss node",
	    'defval'   => "",
	},
	"node/boss_mac" => {
	    'desc'     => "MAC address of the boss node (NULL if behind GW)",
	    'defval'   => "",
	},
	"node/ops_ip" => {
	    'desc'     => "IP address of the ops node",
	    'defval'   => "",
	},
	"node/ops_mac" => {
	    'desc'     => "MAC address of the ops node (NULL if behind GW)",
	    'defval'   => "",
	},
	"node/fs_ip" => {
	    'desc'     => "IP address of the fs node (same as ops if same node)",
	    'defval'   => "",
	},
	"node/fs_mac" => {
	    'desc'     => "MAC address of the fs node (NULL if behind GW, same as ops if same node)",
	    'defval'   => "",
	}
    );

    #
    # Make sure all the sitevars exist
    #
    foreach my $name (keys %vars) {
	if (!SiteVarExists($name)) {
	    my $desc = $vars{$name}{'desc'};
	    my $defval = $vars{$name}{'defval'};
	    if ($doit) {
		if (!CreateSiteVar($name, $desc, $defval)) {
		    fatal("Could not create sitevar '$name'");
		}
	    } else {
194 195 196 197
		$verify_count++;
		if (!$quiet) {
		    print STDERR "Would create sitevar '$name' [$desc]\n";
		}
198 199 200 201 202 203
	    }
	}

	#
	# XXX make sure the description is up to date
	#
204
	else {
205 206 207 208
	    my $desc = $vars{$name}{'desc'};
	    my $odesc = GetSiteVarDesc($name);

	    if ($desc ne $odesc) {
209 210 211 212 213 214 215 216 217
		if ($doit) {
		    print "Updating description of $name from '$odesc' to '$desc'\n";
		    SetSiteVarDesc($name, $desc);
		} else {
		    $verify_count++;
		    if (!$quiet) {
			print "Would update description of $name\n";
		    }
		}
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	    }
	}
    }

    my ($gw_mac, $boss_mac, $ops_mac, $fs_mac) = GetMACs();

    #
    # Update site variables
    #
    foreach my $name (sort keys %vars) {
	my $curval = "";
	if ($doit || SiteVarExists($name)) {
	    $curval = GetSiteVar($name);
	}
	if ($name !~ /^node\/(.*)_(.*)$/) {
	    next;
	}
	my $host = $1;
	my $field = $2;
	my $newval = undef;
	if ($host eq "gw") {
	    $newval = ($field eq "ip") ? $GW_IP : $gw_mac;
	} elsif ($host eq "boss") {
	    $newval = ($field eq "ip") ? $BOSS_IP : $boss_mac;
	} elsif ($host eq "ops") {
	    $newval = ($field eq "ip") ? $OPS_IP : $ops_mac;
	} elsif ($host eq "fs") {
	    $newval = ($field eq "ip") ? $FS_IP : $fs_mac;
	}

	# XXX use empty values rather than NULL unless old val is also NULL
	if (defined($curval) && !defined($newval)) {
	    $newval = "";
	}

	if (!defined($newval) && !defined($curval)) {
	    next;
	}
	if (!defined($newval) || !defined($curval) || $newval ne $curval) {
	    if ($doit) {
		print "Changing $name from ";
	    } else {
260 261 262 263
		$verify_count++;
		if (!$quiet) {
		    print "Would change $name from ";
		}
264
	    }
265 266 267 268 269 270 271 272 273 274 275 276 277
	    if ($doit || !$quiet) {
		if (defined($curval)) {
		    print "'$curval'";
		} else {
		    print "undefined";
		}
		print " to ";
		if (defined($newval)) {
		    print "'$newval'";
		} else {
		    print "undefined";
		}
		print "\n";
278 279 280 281 282 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 342 343 344 345 346 347 348
	    }
	    if ($doit && !SetSiteVar($name, $newval)) {
		fatal("Could not set sitevar '$name'");
	    }
	}
    }
}

sub GetMACs()
{
    #
    # Determine if there is a gateway and which (if any) servers are
    # behind it.
    #
    my $nodenet = inet_ntoa(inet_aton($NC_NET) & inet_aton($NC_MASK));
    my $privnet = inet_ntoa(inet_aton($PR_NET) & inet_aton($PR_MASK));
    my $pubnet = inet_ntoa(inet_aton($PU_NET) & inet_aton($PU_MASK));
    my $gwnet = inet_ntoa(inet_aton($GW_IP) & inet_aton($NC_MASK));
    my $bossnet = inet_ntoa(inet_aton($BOSS_IP) & inet_aton($PR_MASK));
    my $opsnet = inet_ntoa(inet_aton($OPS_IP) & inet_aton($PU_MASK));
    my $fsnet = inet_ntoa(inet_aton($FS_IP) & inet_aton($PU_MASK));
    my $bossoncnet = ($bossnet eq $nodenet) ? 1 : 0;
    my $opsoncnet = ($opsnet eq $nodenet) ? 1 : 0;
    my $fsoncnet = ($fsnet eq $nodenet) ? 1 : 0;
    my $fsisops = ($OPS_IP eq $FS_IP) ? 1 : 0;

    #
    # Gateway: even if everybody is on the node control net, there may
    # still be a gateway to the outside world. This might be a seperate 
    # box, or it may be one of boss/ops.
    #
    my $gotgw = ($nodenet eq $privnet && $nodenet eq $pubnet) ? 0 : 1;
    if ($gotgw && !$GW_IP) {
	fatal("multiple network segments but GW_IP not defined;".
	      " fix your defs file and re-run!\n");
    }

    #
    # If at least one of our servers is on the control net or they
    # explicitly specified a host, our job is easy.
    #
    my ($gw_mac, $boss_mac, $ops_mac, $fs_mac);
    if ($hostip || $bossoncnet || $opsoncnet || (!$fsisops && $fsoncnet)) {
	my $host;
	if ($bossoncnet) {
	    $host = $BOSS_IP;
	} elsif ($opsoncnet) {
	    $host = $OPS_IP;
	} elsif ($fsoncnet) {
	    $host = $FS_IP;
	} else {
	    $host = $hostip;
	}
	if ($hostip && $host ne $hostip) {
	    print STDERR "WARNING: using $host instead of $hostip...\n";
	}

	# Make sure the host is alive and running FreeBSD
	if (GetHostOS($host) ne "FreeBSD") {
	    fatal("Host $host is not up or not running FreeBSD");
	}

	if ($gotgw || $GW_IP) {
	    $gw_mac = GetMACFromARP($host, $GW_IP);
	}
	if ($bossoncnet) {
	    $boss_mac = GetMACFromARP($host, $BOSS_IP);
	}
	if ($opsoncnet) {
	    $ops_mac = GetMACFromARP($host, $OPS_IP);
	}
349 350 351
	elsif ($OPSVM_ENABLE) {
	    $ops_mac = $boss_mac;
	}
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	if ($fsoncnet) {
	    if ($fsisops) {
		$fs_mac = $ops_mac;
	    } else {
		$fs_mac = GetMACFromARP($host, $FS_IP);
	    }
	}
    }

    #
    # Otherwise, there is nothing we can do!
    # The good news is that the only variable that will be set for which
    # we cannot determine a value, is the gw_mac. For that we just return
    # the current value.
    #
    else {
	$gw_mac = GetSiteVar("node/gw_mac");
369 370 371 372 373 374 375
	if (!$quiet) {
	    print STDERR "update_sitevars: WARNING: cannot determine GW mac, ";
	    if ($gw_mac) {
		print STDERR "keeping existing value '$gw_mac'\n";
	    } else {
		print STDERR "you need to manually fix this value\n";
	    }
376 377 378
	}
    }

379
    if ((!$doit && !($verify && $quiet)) || $debug) {
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	if ($gotgw) {
	    print "Multiple-segment configuration:\n";
	    print "  Private net: $PR_NET/$PR_MASK\n";
	    print "  Public net: $PU_NET/$PU_MASK\n";
	} else {
	    print "Single-segment configuration:\n";
	}
	print "  Node control net: $NC_NET/$NC_MASK\n";
	if ($gotgw || $GW_IP) {
	    print "  Gateway: $GW_IP ($gw_mac)\n";
	} else {
	    print "  No gateway\n";
	}
	if ($bossoncnet) {
	    print "  Boss on node control net: $BOSS_IP ($boss_mac)\n";
	} else {
	    print "  Boss on private net: $BOSS_IP\n";
	}
	if ($fsisops) {
	    if ($opsoncnet) {
		print "  Ops/fs on node control net: $OPS_IP ($ops_mac)\n";
	    } else {
		print "  Ops/fs on public net: $OPS_IP\n";
	    }
	} else {
	    if ($opsoncnet) {
		print "  Ops on node control net: $OPS_IP ($ops_mac)\n";
	    } else {
		print "  Ops on public net: $OPS_IP\n";
	    }
	    if ($fsoncnet) {
		print "  Fs on node control net: $FS_IP ($fs_mac)\n";
	    } else {
		print "  Fs on public net: $FS_IP\n";
	    }
	}
416 417 418
	if ($doit) {
	    print "\n";
	}
419 420 421 422 423 424 425 426 427 428
    }

    return ($gw_mac, $boss_mac, $ops_mac, $fs_mac);
}

sub GetHostOS($)
{
    my ($ip) = @_;
    my $output = "";

429 430 431 432 433
    my $sshcmd = "";
    if ($ip ne $BOSS_IP) {
	$sshcmd = "ssh $sshopt $ip ";
    }

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    if (ExecQuiet("$sshcmd uname", \$output)) {
	fatal("Could not get uname info from $ip");
    }
    if ($output =~ /FreeBSD/) {
	return "FreeBSD";
    }

    return "unsupported";
}

#
# Find a MAC address from ifconfig info.
# We ssh to $ip, harvest the ifconfig info, and parse that to find $ip
# and its MAC address. Returns the MAC string or undef on error.
#
sub GetMACFromIfconfig($)
{
    my ($ip) = @_;
    my $output = "";

454 455 456 457 458
    my $sshcmd = "";
    if ($ip ne $BOSS_IP) {
	$sshcmd = "ssh $sshopt $ip ";
    }

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    if (ExecQuiet("$sshcmd ifconfig", \$output)) {
	fatal("Could not harvest ifconfig info from $ip");
    }

    # XXX this is probably BSD-specific
    my $iface = "";
    my $mac = "";
    my $nextmac = 0;
    foreach (split('\n', $output)) {
	if (/^([^\s:]+):/) {
	    $iface = $1;
	    $mac = "";
	    $nextmac = 0;
	    next;
	}
	if (/ether (..:..:..:..:..:..)/) {
	    $mac = $1;
	    if ($nextmac) {
		return $mac;
	    }
	    next;
	}
	if (/inet ([\d\.]+)/) {
	    if ($1 eq $ip) {
		if ($mac) {
		    return $mac;
		}
		$nextmac = 1;
	    }
	    next;
	}
    }

    return undef;
}

#
# Find a MAC address from ARP info.
# We ssh to $host, run "ping $ip", harvest the ARP info, and parse that to
# find $ip and get the MAC address. Returns the MAC string or undef on error.
#
sub GetMACFromARP($$)
{
    my ($host,$ip) = @_;
    my $output = "";

505 506 507 508 509
    my $sshcmd = "sh -c";
    if ($host ne $BOSS_IP) {
	$sshcmd = "ssh $sshopt $host";
    }

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 561 562 563 564 565 566 567 568 569 570 571
    if (ExecQuiet("$sshcmd 'ping -t 5 -c 1 $ip; arp -an'", \$output)) {
	fatal("Could not harvest ifconfig info from $ip");
    }

    # XXX very much FreeBSD specific
    foreach (split('\n', $output)) {
	if (/^\S+ \(([\d\.]+)\) at (..:..:..:..:..:..) on /) {
	    if ($ip eq $1) {
		return $2;
	    }
	}
    }

    return undef;
}

#
# Run a command, being sure to capture all output. 
#
sub ExecQuiet($$)
{
    #
    # Use a pipe read, so that we save away the output
    #
    my ($command,$outp) = @_;
    my $output    = "";

    if ($debug > 1) {
	print STDERR "exec('$command')\n";
    }

    if (!open(PIPE,"$command 2>&1 |")) {
	print STDERR "Failed pipe('$command')\n";
	return -1;
    }
    while (<PIPE>) {
	$output .= $_;
    }
    close(PIPE);
    my $exit_value  = $? >> 8;

    if ($exit_value) {
	print STDERR "Failed: exec('$command'):\n";
    }
    if ($debug > 2 || $exit_value) {
	print STDERR "$output\n";
    }
    
    $$outp = $output
	if ($outp);

    return $exit_value;
}

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

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