addmanagementiface.in 11.7 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2017 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
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32
#
use strict;
use English;
use Getopt::Std;
use Date::Parse;
use Time::Local;
use Data::Dumper;
use File::Temp qw(tempfile);

#
33
# Add a management interface for a node. ilo, ilo2, ilo3, drac.
34 35 36 37 38
# When adding for a local node, try to insert switch info if -s is given.
# Also restart DHCPD since we now add entries for local management interfaces.
#
sub usage()
{
39
    print STDERR "Usage: addmanagementiface -t <type> -a [key|pswd] ";
40
    print STDERR "[-s <switchinfo>] <node_id> mac IP key_uid key\n";
41
    print STDERR "       management_iface -r <node_id>\n";
42
    print STDERR "       management_iface -i <node_id>\n";
43
    print STDERR "  -h       This message\n";
44
    print STDERR "  -t type  Management type; ilo, ilo2, ilo3, drac, ipmi15, ipmi20\n";
45 46 47 48
    print STDERR "  -s info  Optional switch info; switch,card,port\n";
    print STDERR "  -s -     Search output of switchmac to find switch info\n";
    print STDERR "  -a pswd  Password auth; provide login and password.\n";
    print STDERR "  -a key   SSH key auth; provide login and key path.\n";
49 50
    print STDERR "  -c cable Cable ID\n";
    print STDERR "  -l len   Cable length\n";
51
    print STDERR "  -T info  Optional tipline info; server[,tipname]\n";
52
    print STDERR "  -r       Remove management interface from DB.\n";
53
    print STDERR "  -i       Display management interface from DB.\n";
54
    print STDERR "  -n       Do not restart daemons after changing DB.\n";
55 56
    exit(-1);
}
57
my $optlist  = "ht:a:rs:nic:l:T:";
58 59
my $debug    = 0;
my $remove   = 0;
60
my $info     = 0;
61
my $norestart = 0;
62 63
my $authtype;
my $type;
64 65
my $cable;
my $cablelen;
66
my $switchinfo;
67
my $tipinfo;
68 69 70 71

# Protos
sub fatal($);
sub RemoveManagementInterface($);
72
sub ShowManagementInterface($);
73 74 75 76 77 78 79

#
# Configure variables
#
my $TB		   = "@prefix@";
my $SWITCHMAC      = "$TB/libexec/switchmac";
my $DHCPD_MAKECONF = "$TB/sbin/dhcpd_makeconf";
Leigh Stoller's avatar
Leigh Stoller committed
80
my $NAMED_SETUP    = "$TB/sbin/named_setup";
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use EmulabConstants;
use emutil;
use User;
use Node;
use Interface;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# 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{'h'})) {
    usage();
}
if (defined($options{'d'})) {
    $debug = 1;
}
117 118 119 120 121 122
if (defined($options{'c'})) {
    $cable = $options{'c'};
}
if (defined($options{'l'})) {
    $cablelen = $options{'l'};
}
123 124 125 126 127
if (defined($options{'r'})) {
    $remove = 1;
    usage()
	if (@ARGV != 1);
}
128 129 130 131 132
if (defined($options{'i'})) {
    $info = 1;
    usage()
	if (@ARGV != 1);
}
133 134 135
if (defined($options{'n'})) {
    $norestart = 1;
}
136 137 138
if (defined($options{'s'})) {
    $switchinfo = $options{'s'};
}
139 140 141
if (defined($options{'T'})) {
    $tipinfo = $options{'T'};
}
142 143 144
if (defined($options{'t'})) {
    $type = $options{'t'};
    usage()
145
	if ($type !~ /^(ilo|ilo2|ilo3|drac|ipmi15|ipmi20)$/);
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
}
if (defined($options{'a'})) {
    $authtype = $options{'a'};
    usage()
	if (! ($authtype eq "key" || $authtype eq "pswd"));
}
# Need at least one argument.
usage()
    if (@ARGV < 1);

#
# Verify user, must be admin.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
if (!$this_user->IsAdmin()) {
    fatal("You are not a testbed administrator!");
}

my $node_id = shift();
my $node = Node->Lookup($node_id);
if (!defined($node)) {
    fatal("No such node!");
}

#
# Remove existing interface.
#
if ($remove) {
    exit(RemoveManagementInterface($node));
}
179 180 181
elsif ($info) {
    exit(ShowManagementInterface($node));
}
182 183 184 185 186 187 188 189 190 191 192

#
# Else adding new one.
#
usage()
    if (@ARGV != 4);
my $mac     = shift();
my $IP      = shift();

# Convert mac to DB representation.
if ($mac =~ /(\w\w):(\w\w):(\w\w):(\w\w):(\w\w):(\w\w)/) {
193
    $mac = lc("$1$2$3$4$5$6");
194 195 196 197 198 199
}

# Verify optional switch info
my $switchid;
my $switchcard;
my $switchport;
200
my $switch;
201 202 203 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 235 236 237 238 239 240

if (defined($switchinfo)) {
    if ($switchinfo eq "-") {
	#
	# Search using switchmac
	#
	my $output = emutil::ExecQuiet($SWITCHMAC);
	if ($?) {
	    fatal("Error running $SWITCHMAC:\n$output");
	}
	foreach my $line (split("\n", $output)) {
	    my ($smac,$sinfo,undef,undef,$role) = split(',', $line);
	    if ($smac eq $mac) {
		print "Found mac on switch: $line\n";
	    
		if ($sinfo =~ /([-\w]*)\/(\d*)\.(\d*)/) {
		    $switchid   = $1;
		    $switchcard = $2;
		    $switchport = $3;
		}
		else {
		    fatal("Could not parse switchmac data");
		}
		last;
	    }
	}
	if (!defined($switchid)) {
	    print STDERR "Could not find the switch info with switchmac.\n";
	    print STDERR "Ping the current IP address and then try again\n";
	    exit(1);
	}
    }
    elsif ($switchinfo =~ /^([-\w]+),(\d+),(\d+)$/) {
	$switchid   = $1;
	$switchcard = $2;
	$switchport = $3;
    }
    else {
	fatal("Invalid switch info");
    }
241 242 243 244
    $switch = Node->Lookup($switchid);
    if (!defined($switch)) {
	fatal("Could not lookup switch in the database");
    }
245 246 247 248 249 250 251 252
    my $switchiface = $switchcard . "/" . $switchport;

    # Lets check consistency.
    my $tmp  = Interface->LookupByIface($switch, $switchiface);
    my $wire = Interface::Wire->LookupAnyByIface($switch, $switchiface);
    if (defined($tmp) && defined($wire)) {
	fatal("Switch Interface already exists and is wired up: $wire");
    }
253 254
}

255 256
my $isipmi = ($type =~ /^ipmi/ ? 1 : 0);

257 258 259 260 261
#
# Add the outlet and authinfo.
#
$node->AddOutlet($type, 0,
		 {"key_type"  => $type,
262 263
		  "key_role"  => ($authtype eq "key" ?
				  ($isipmi ? "ipmi-kgkey" : "ssh-key") :
264
				  ($isipmi ? "ipmi-passwd" : "ssh-passwd")),
265 266 267 268
		  "key_uid"   => $ARGV[0],
		  "key"       => $ARGV[1]}) == 0
    or fatal("Could not add outlet records");

269 270 271 272 273 274 275 276 277 278 279
#
# Optional tipline.
#
if (defined($tipinfo)) {
    my ($server,$tipname) = split(",", $tipinfo);
    $tipname = $node_id if (!defined($tipname));

    DBQueryFatal("replace into tiplines (tipname, node_id, server) values ".
		 "('$tipname', '$node_id', '$server')");
}

280
#
281 282 283 284 285 286 287 288 289 290 291 292 293
# Add the interface, if not there.
#
my $interface = Interface->LookupManagement($node);
if (defined($interface)) {
    #
    # Check to make sure consistent.
    #
    fatal("Inconsistent mac in $interface")
	if ($interface->mac() ne $mac);
    
    fatal("Inconsistent ip in $interface")
	if ($interface->IP() ne $IP);
    
294
    if (defined($switchinfo) && $interface->wiredup()) {
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	fatal("Inconsistent switch_id in $interface")
	    if ($interface->switch_id() ne $switchid);
	fatal("Inconsistent switch_port in $interface")
	    if ($interface->switch_port() != $switchport);
	fatal("Inconsistent switch_card in $interface")
	    if ($interface->switch_card() ne $switchcard);
    }
}
else {
    my $ifaceargs = {
	"iface"     => $type,
	"role"      => TBDB_IFACEROLE_MANAGEMENT(),
	"MAC"       => $mac,
	"IP"        => $IP,
	"type"      => $type,
    };
311 312 313 314
    $interface = Interface->Create($node, $ifaceargs);
    if (!defined($interface)) {
	fatal("Could not create interface entry");
    }
315
    if (defined($switchinfo)) {
316
	my $switchiface = $switchcard . "/" . $switchport;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	my $switch_interface;

	# Lets not create it again.
	$switch_interface = Interface->LookupByIface($switch, $switchiface);
	if (!defined($switch_interface)) {
	    $ifaceargs = {
		"iface"       => $switchiface,
		"role"        => TBDB_IFACEROLE_OTHER(),
		"type"        => "generic",
		"trunk"       => 0,
		"mac"         => "000000000000",
	    };
	    $switch_interface = Interface->Create($switch, $ifaceargs);
	    if (!defined($switch_interface)) {
		fatal("Could not create switch interface entry");
	    }
333 334
	}
	my $wireargs = {
335 336 337 338
	    "card1"  => 99,	# XXX Bogus.
	    "port1"  => 1,
	    "card2"  => $switchcard,
	    "port2"  => $switchport,
339 340 341 342 343 344 345
	};
	$wireargs->{"cable"}  = $cable if (defined($cable));
	$wireargs->{"length"} = $cablelen if (defined($cablelen));
	
	Interface::Wire->Create($interface, $switch_interface,
				'Management', $wireargs)
	    or fatal("Could not create wire!");
346
    }
347 348 349 350 351
}

#
# Restart DHCPD. 
#
352
if (!$norestart && !$node->isremotenode()) {
353 354 355 356
    print "Re-generating dhcpd.conf and restarting dhcpd\n";
    if (system("$DHCPD_MAKECONF -i -r")) {
	fatal("Unable to restart dhcpd");
    }
Leigh Stoller's avatar
Leigh Stoller committed
357 358 359 360
    print "Re-generating named config and restarting named\n";
    if (system("$NAMED_SETUP")) {
	fatal("Unable to restart named");
    }
361 362 363 364 365 366 367 368 369 370 371 372 373 374
}
exit(0);

#
# Remove a management interface.
#
sub RemoveManagementInterface($)
{
    my ($node) = @_;

    my $interface = Interface->LookupManagement($node);
    fatal("No management interface")
	if (!defined($interface));

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    #
    # Delete wire and switch side interface.
    #
    my $wire = Interface::Wire->Lookup($interface);
    if ($wire) {
	my $switch_interface = Interface->LookupByIface($wire->node_id2(),
							$wire->iface2());
	if (defined($switch_interface)) {
	    $switch_interface->Delete(1) == 0
		or fatal("Could not lookup switch side of wire in the database: ".
			 $wire->node_id2() . ":" . $wire->iface2());
	}
	$wire->Delete(1) == 0
	    or fatal("Could not delete wire");
    }
390 391 392 393 394 395 396 397 398 399
    $node->DeleteOutlet() == 0
	or fatal("Could not delete outlets");

    # Flag indicates it is okay to delete real interface.
    $interface->Delete(1) == 0
	or fatal("Could not delete $interface");

    #
    # Restart DHCPD. 
    #
400
    if (!$norestart && !$node->isremotenode()) {
401 402 403 404 405 406 407 408
	print "Re-generating dhcpd.conf and restarting dhcpd\n";
	if (system("$DHCPD_MAKECONF -i -r")) {
	    fatal("Unable to restart dhcpd");
	}
	return 0;
    }
}

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
#
# Show management interface.
#
sub ShowManagementInterface($)
{
    my ($node) = @_;
    my $node_id = $node->node_id();

    my $interface = Interface->LookupManagement($node);
    fatal("No management interface")
	if (!defined($interface));
    print "Iface1:\n";
    $interface->Dump();
    print "\n";

    my $wire = Interface::Wire->Lookup($interface);
    if ($wire) {
	my $switch_interface = Interface->LookupByIface($wire->node_id2(),
							$wire->iface2());
	if (defined($switch_interface)) {
	    print "Iface2:\n";
	    $switch_interface->Dump();
	    print "\n";
	}
	print "Wire:\n";
	$wire->Dump();
    }
    my $query_result =
	DBQueryFatal("select * from outlets where node_id='$node_id'");
    if ($query_result->numrows) {
	my $row = $query_result->fetchrow_hashref();
	print "\n";
	print "Outlet:\n";
	print "Power ID:   " . $row->{'power_id'} . "\n";
	print "Outlet:     " . $row->{'outlet'} . "\n";
	print "Last Power: " . $row->{'last_power'} . "\n";

	$query_result =
	    DBQueryFatal("select * from outlets_remoteauth ".
			 "where node_id='$node_id'");
	if ($query_result->numrows) {
	    $row = $query_result->fetchrow_hashref();
	    print "Key Type:   " . $row->{'key_type'} . "\n";
	    print "Key Role:   " . $row->{'key_role'} . "\n";
	    print "Key UID:    " . $row->{'key_uid'} . "\n";
	    if ($row->{'key_privlvl'}) {
		print "Key Level:  " . $row->{'key_privlvl'} . "\n";
	    }
	    if ($row->{'mykey'} ne "") {
		print "Key Key:    " . $row->{'mykey'} . "\n";
	    }
	}
    }

    return 0;
}

466 467 468 469 470 471 472 473 474
sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}