management_iface.in 7.41 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2014 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 39 40 41 42
# 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()
{
    print STDERR "Usage: management_iface -t <type> -a [key|pswd] ";
    print STDERR "  [-s <switchinfo>] <node_id> mac IP arg1 arg2\n";
    print STDERR "       management_iface -r <node_id>\n";
    print STDERR "  -h       This message\n";
43
    print STDERR "  -t type  Management type; ilo, ilo2, ilo3, drac, ipmi15, ipmi20\n";
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    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";
    print STDERR "  -r       Remove management interface from DB.\n";
    exit(-1);
}
my $optlist  = "ht:a:rs:";
my $debug    = 0;
my $remove   = 0;
my $authtype;
my $type;
my $switchinfo;

# Protos
sub fatal($);
sub RemoveManagementInterface($);

#
# Configure variables
#
my $TB		   = "@prefix@";
my $SWITCHMAC      = "$TB/libexec/switchmac";
my $DHCPD_MAKECONF = "$TB/sbin/dhcpd_makeconf";
Leigh B Stoller's avatar
Leigh B Stoller committed
68
my $NAMED_SETUP    = "$TB/sbin/named_setup";
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 103 104 105 106 107 108 109 110 111 112 113 114 115

#
# 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;
}
if (defined($options{'r'})) {
    $remove = 1;
    usage()
	if (@ARGV != 1);
}
if (defined($options{'s'})) {
    $switchinfo = $options{'s'};
}
if (defined($options{'t'})) {
    $type = $options{'t'};
    usage()
116
	if ($type !~ /^(ilo|ilo2|ilo3|drac|ipmi15|ipmi20)$/);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
}
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));
}

#
# 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)/) {
161
    $mac = lc("$1$2$3$4$5$6");
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

# Verify optional switch info
my $switchid;
my $switchcard;
my $switchport;

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");
    }
}

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

212 213 214 215 216
#
# Add the outlet and authinfo.
#
$node->AddOutlet($type, 0,
		 {"key_type"  => $type,
217 218
		  "key_role"  => ($authtype eq "key" ?
				  ($isipmi ? "ipmi-kgkey" : "ssh-key") :
219
				  ($isipmi ? "ipmi-passwd" : "ssh-passwd")),
220 221 222 223 224
		  "key_uid"   => $ARGV[0],
		  "key"       => $ARGV[1]}) == 0
    or fatal("Could not add outlet records");

#
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
# 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);
    
    if (defined($switchinfo)) {
	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 = {
	"card"      => 99,	# XXX Bogus.
	"iface"     => $type,
	"role"      => TBDB_IFACEROLE_MANAGEMENT(),
	"MAC"       => $mac,
	"IP"        => $IP,
	"type"      => $type,
    };
    if (defined($switchinfo)) {
	$ifaceargs->{'switch_id'}   = $switchid;
Leigh B Stoller's avatar
Leigh B Stoller committed
258 259
	$ifaceargs->{'switch_port'} = $switchport;
	$ifaceargs->{'switch_card'} = $switchcard;
260 261 262
    }
    Interface->Create($node, $ifaceargs)
	or fatal("Could not create interface entry");
263 264 265 266 267 268 269 270 271 272
}

#
# Restart DHCPD. 
#
if (!$node->isremotenode()) {
    print "Re-generating dhcpd.conf and restarting dhcpd\n";
    if (system("$DHCPD_MAKECONF -i -r")) {
	fatal("Unable to restart dhcpd");
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
273 274 275 276
    print "Re-generating named config and restarting named\n";
    if (system("$NAMED_SETUP")) {
	fatal("Unable to restart named");
    }
277 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
}
exit(0);

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

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

    $node->DeleteOutlet() == 0
	or fatal("Could not delete outlets");

    $interface->DeleteWire() == 0
	or fatal("Could not delete wire for $interface");

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

    #
    # Restart DHCPD. 
    #
    if (!$node->isremotenode()) {
	print "Re-generating dhcpd.conf and restarting dhcpd\n";
	if (system("$DHCPD_MAKECONF -i -r")) {
	    fatal("Unable to restart dhcpd");
	}
	return 0;
    }
}

sub fatal($)
{
    my ($mesg) = $_[0];

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