libosload_dell.pm.in 13.2 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2019 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 39 40 41 42
# 
# {{{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/>.
# 
# }}}
#
#
package libosload_dell;

use strict;
use English;
use Data::Dumper;
use Expect;
use File::Temp qw(tempfile);
use File::Basename;

#
# Configure stuff
#
my $TB		= "@prefix@";
my $TESTMODE    = @TESTMODE@;
my $BOSSNODE_IP = "@BOSSNODE_IP@";

use libosload_switch;
use base qw(libosload_switch);
43
use libdb;
44
use libEmulab;
45
use Lan;
46 47 48 49 50 51 52 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
use emutil;
use EmulabConstants;
use libtblog_simple;

my $USER_PROMPT   = 'DellEMC>';
my $ADMIN_PROMPT  = 'DellEMC#';
my $CONFIG_PROMPT = "DellEMC(conf)#";
my $CONN_TIMEOUT  = 10;
my $EXPECT_DEBUG_LOG    = "/tmp/osload_switch.log";

sub New($$$)
{
    my ($class, $parent, $type) = @_;

    my $self = $class->SUPER::New($parent, $type);
    bless($self, $class);

    return $self;
}

#
# Create an Expect object that spawns the ssh process to the switch.
#
sub createExpectObject($$)
{
    my ($self, $nodeobject) = @_;
    my $node_id = $nodeobject->node_id();
    my ($admin_user, $admin_pswd);
    my $error = 0;

    #
    # The Dell starts with admin/elabman accounts set to password from a
    # file on boss:/usr/testbed/etc. So all we have to do is log in using
    # that password. We set up the admin account for the user, elabman is
    # for interactive use by testbed administrators. Partly this is cause
    # it is a pain in the ass to add ssh keys on FTOS.
    #
    $admin_user = PROTOUSER();
    $admin_pswd = $self->DefaultPassword();
    return -1
	if (!defined($admin_pswd));

    # Host keys change every reload, do not want to save them.
    my $spawn_cmd = "ssh -o userknownhostsfile=/dev/null ".
	"-l $admin_user $node_id";
91 92

    $self->dprint(0,"$self createExpectObject($node_id): $spawn_cmd\n");
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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
    
    # Create Expect object and initialize it:
    my $exp = new Expect();
    if (!$exp) {
        # upper layer will check this
        return -1;
    }
    $exp->log_stdout(0);
    
    if ($self->debug() > 3) {
	$exp->log_stdout(1);
	# Copious debugging.
	$exp->exp_internal(1);
    }

    if (!$exp->spawn($spawn_cmd)) {
	warn "$self createExpectObject($node_id): ".
	    "Cannot spawn $spawn_cmd: $!\n";
	return -1;
    }
    $exp->raw_pty(1);

    $exp->expect($CONN_TIMEOUT,
		 ["password:" =>
		  sub { my $e = shift;
			$e->send("$admin_pswd\n");
			exp_continue;}],
		 ["Permission denied" =>
		  sub { $error = "password incorrect!";} ],
		 [timeout =>
		  sub { $error = "Timeout connecting to switch!";} ],
		 ["-ex", "$USER_PROMPT"  =>
		   sub { my $e = shift;
			 $e->send("enable\n");
			 exp_continue;}],
		 ["-ex", "$ADMIN_PROMPT"  =>
		   sub { my $e = shift;
			 $e->send("configure\n");
			 exp_continue;}],
		 "$CONFIG_PROMPT"
		);

    if (!$error && $exp->error()) {
	$error = $exp->error();
    }

    if ($error) {
	warn "$self createExpectObject($node_id): ".
	    "Could not connect to switch: $error\n";
	return -1;
    }
144
    $self->expectobj($nodeobject,$exp);
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
    return 0;
}

sub generateConfig($$)
{
    my ($self,$nodeobject) = @_;
    my $node_id       = $nodeobject->node_id();
    my $experiment    = $nodeobject->Reservation();
    my $protouser     = PROTOUSER();
    my @userlines     = ();
    my $cleartext;

    #
    # Once a switch is dragged into an experiment, we are going to add a
    # user account set to the current node root_password. We leave the
    # elabman account alone, that is our way back in.
    #
    # FTOS does not accept modular crypt strings. So just send it the
    # cleartext password, it will not be visible. I should clarify; I
    # cannot figure out how to give it an encrypted string. :-)
    #
    $cleartext = $nodeobject->root_password();

    #
    # Experiment creator account. Not going to create accounts for all
    # users at this time, does not seem necessary.
    #
    if (defined($experiment)) {
	my $user = $experiment->GetSwapper();
	if (!defined($user)) {
	    print STDERR "generateConfig: No swapper defined for $experiment\n";
	}
	else {
	    my $uid = $user->uid();
	    
	    push(@userlines,
		 ("username $uid password 0 $cleartext ".
		  "privilege 15 role sysadmin"));
	}
    }
185
    my @config = @userlines;
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 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
    if (defined($experiment)) {
	#
	# Find all the ports that are in use on this switch. We want to enable
	# those ports and put them into vlan 1. This will give the user a basic
	# single broadcast domain as a starting point. We will turn on STP to
	# avoid loops created by the user (cause that is gonna happen!).
	#
	my @vlanports = ();
	my @portlines = ();
	my @vlans;
	if (VLan->ExperimentVLans($experiment, \@vlans) != 0) {
	    print STDERR "Could not get list of all vlans for $experiment\n";
	    return -1;
	}
	foreach my $vlan (@vlans) {
	    my @members;
	    
	    next
		if ($vlan->type() ne "wire");

	    if ($vlan->MemberList(\@members)) {
		print STDERR "Could not get member list of members for $vlan\n";
		return -1;
	    }
	    foreach my $member (@members) {
		my ($membnode_id, $membiface);
		if ($member->GetNodeIface(\$membnode_id, \$membiface)) {
		    print STDERR "Could not get node/iface for $member\n";
		    return -1;
		}
		next
		    if ($membnode_id->node_id() ne $node_id);

		my $interface = Interface->LookupByIface($nodeobject,
							 $membiface);
		if (!defined($interface)) {
		    print STDERR "Could not lookup interface for $member\n";
		    return -1;
		}
		my $card  = $interface->card();
		my $port  = $interface->port();

		push(@portlines,
		     "interface te $card/$port",
		     "  switchport",
		     "  spanning-tree rstp loopguard",
		     "  no shutdown",
		     "  exit");
	    }
	}
	if (@portlines) {
	    push(@config,
		 "protocol spanning-tree rstp",
		 "  no disable",
		 "  exit",
		 @portlines);
	}
    }
    push(@config, 
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	 "exit",
	 "write memory",
	);

    if ($self->debug()) {
	foreach my $cl (@config) {
	    $self->dprint(4,"generateConfig($node_id): config line: $cl\n");
	}
    }
    return @config;
}

#
# Write a configuration to the switch using the expect object.
#
sub writeConfig($$@)
{
    my ($self, $nodeobject, @config) = @_;
    my $node_id = $nodeobject->node_id();
265
    my $exp     = $self->expectobj($nodeobject);
266 267 268 269 270 271 272 273 274 275 276 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

    $self->dprint(0,"$self writeConfig($node_id): \n");

    #
    # Write each line, look for some that resembles an error.
    #
    foreach my $line (@config) {
	my $output;
	my $error;
	
	$exp->clear_accum(); # Clean the accumulated output, as a rule.
	$exp->send($line . "\n");
	
	$exp->expect($CONN_TIMEOUT,
		     [qr/% Error: (.*)\n/,
		      sub { my $e = shift;
			    $error = ($e->matchlist)[0];}],
		     ["-ex", "$CONFIG_PROMPT"  =>
		      sub { my $e = shift;
			    $output = $e->before();}],
		     ["-ex", "$ADMIN_PROMPT"  =>
		      sub { my $e = shift;
			    $output = $e->before();}],
		     [qr/\(yes\/no\)/ =>
		      sub { my $e = shift;
			    $e->send("yes\n");
			    exp_continue;}],
		     );

	if (!$error && $exp->error()) {
	    $error = $exp->error();
	}
	if ($error) {
	    $self->dprint(0, "$self writeConfig($node_id): ".
			 "cmd: '$line', error: '$error'\n");
	    return (1, $error);
	}
    }
    return (0);
}

#
# FTOS is very quick to init the flash drive.
#
sub MaxWaitTime($$)
{    
    my ($self, $nodeobject) = @_;

    return 500;
}

#
# Reconfigure.
#
sub DeviceReconfigure($$)
{
    my ($self, $nodeobject) = @_;
    my $node_id    = $nodeobject->node_id();
    my $experiment = $nodeobject->Reservation();

    my @config = $self->generateConfig($nodeobject);	
    if (!@config) {
 	tberror "$self: Reconfigure($node_id): could not generate config!\n";
 	return -1;
    }
    if ($self->createExpectObject($nodeobject)) {
	return -1;
    }
    if ($self->writeConfig($nodeobject, @config)) {
	return -1;
    }
    if (!defined($experiment)) {
338
	$self->expectobj($nodeobject)->soft_close();
339 340
	return 0;
    }
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    #
    # Deal with SSH keys.
    #
    my $nonlocal = 0;
    my @keys = ();
    my $user;

    #
    # Nonlocal (geni experiments) are a little more trouble, since the
    # swapper is "geniuser" and the keys are in nonlocal_user_pubkeys.
    #
    if ($experiment->geniflags()) {
	#
	# Look at the project, if its a nonlocal project then we need
	# the nonlocal keys. But with PROTOGENI_LOCALUSER, we might
	# have a local project (and thus a local user).
	#
	my $project = $experiment->GetProject();
	
	if (!defined($project)) {
	    print STDERR "generateConfig: No project for $experiment\n";
	    return 0;
	}
	$nonlocal = $project->IsNonLocal();
	$user = $experiment->GetCreator();
    }
    else {
	$user = $experiment->GetSwapper();
    }
370 371 372 373 374 375
    if (!defined($user)) {
	print STDERR "generateConfig: No swapper defined for $experiment\n";
	return 0;
    }
    my $uid = $user->uid();

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    # Now the keys.
    if ($nonlocal) {
	#
	# Grab all the nonlocal users and then find the keys for the
	# experiment creator.
	#
	my $tmp;
	$experiment->NonLocalUsers(\$tmp);
	if (defined($tmp)) {
	    foreach my $ref (@{$tmp}) {
		if ($ref->{'urn'} eq $user->nonlocal_id()) {
		    foreach my $key (@{ $ref->{'keys'} }) {
			push(@keys, $key->{'key'})
		    }
		    $uid = $ref->{'login'};
		    last;
		}
	    }
	}
    }
    else {
	$user->GetSSHKeys(\@keys);
    }
    
400 401 402 403 404
    #
    # More fun. FTOS is highly stupid when it comes to SSH keys. You have
    # to copy them over via tftp to a local flash file, and then tell FTOS
    # to use that flash file for a user. Why can't it be simple like
    # MLNX-OS, where you just send a bunch of strings over!
405 406 407 408 409 410 411 412 413 414
    #
    # ssh-rsa keys only, need to have at least one.
    #
    my @tmp = ();
    foreach my $key (@keys) {
	next
	    if ($key !~ /^ssh-rsa/);
	push(@tmp, $key);
    }
    @keys = @tmp;
415 416 417
    if (!@keys) {
	print STDERR "No ssh keys for $uid\n";
	return 0;
418
    }    
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    
    #
    # Create a temporary file to write the keys to.
    #
    my ($fp, $filename) = tempfile(UNLINK => 1,
				   DIR => "/tftpboot/ualloc-downloads");
    if (!defined($fp)) {
	print STDERR "Could not create temp file for ssh keys\n";
	return -1;
    }
    foreach my $key (@keys) {
	print $fp $key . "\n";
    }
    close($fp);
    if (! chmod(0666, $filename)) {
	print STDERR "Could not chmod temp file for ssh keys\n";
	return -1;	
    }
    # Local file name for the switch
    my $basename = basename($filename);

440 441 442 443 444 445 446
    @config = ();

    #
    # For geni experiments, we have to create the user since the uid
    # is different (see above).
    #
    if ($nonlocal) {
447 448 449 450 451 452 453
	#
	# FTOS does not accept modular crypt strings. So just send it the
	# cleartext password, it will not be visible. I should clarify; I
	# cannot figure out how to give it an encrypted string. :-)
	#
	my $cleartext = $nodeobject->root_password();

454
	@config = ("configure",
455 456
		   "username $uid password 0 $cleartext ".
		   "privilege 15 role sysadmin",
457 458 459 460
		   "exit");
    }
    push(@config,
	 "copy tftp://$BOSSNODE_IP/ualloc-downloads/$basename ".
461 462 463
	 " flash://$basename",
	 "ip ssh rsa-authentication username $uid ".
	 " authorized-keys flash://$basename",
464
	 "write memory");
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
    if ($self->writeConfig($nodeobject, @config)) {
	print STDERR "Could not send ssh keys to $node_id\n";
	return -1;
    }
    return 0;
}

#
# Try to reboot a switch, first looking to see if its running the NOS,
# and if it is doing it the NOS way. If not running NOS, call up a level
# for a standard reboot, which will work fine if its runnng ONIE, failing
# back to a power cycle. 
#
sub Reboot($$)
{
    my ($self, $nodeobject) = @_;
    my $node_id  = $nodeobject->node_id();
    my $error;

    #
    # If its dead, use normal reboot which will power cycle.
    #
    if (! $self->Pingable($nodeobject)) {
	goto reboot;
    }
    # Running something else, we use normal reboot.
    if (! $self->RunningNOS($nodeobject)) {
	goto reboot;
    }
    if ($self->createExpectObject($nodeobject)) {
	goto reboot;
    }
498
    my $exp = $self->expectobj($nodeobject);
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

    $exp->clear_accum();  # Clean the accumulated output, as a rule.
    $exp->send("exit\n"); # Back into admin mode.
	
    $exp->expect($CONN_TIMEOUT,
		 [qr/% Error: (.*)\n/,
		  sub { my $e = shift;
			$error = ($e->matchlist)[0];}],
		 ["-ex", "$ADMIN_PROMPT"  =>
		   sub { my $e = shift;
			 $e->send("reload\n");
			 exp_continue;}],
		 [qr/\[yes\/no\]: / =>
		   sub { my $e = shift;
			 $e->send("yes\n");
			 exp_continue;}],
		 [qr/\[confirm yes\/no\]: / =>
		   sub { my $e = shift;
			 $e->send("yes\n");}],
		);

    if (!$error && $exp->error()) {
	$error = $exp->error();
    }
    if ($error) {
Leigh Stoller's avatar
Leigh Stoller committed
524
	$self->dprint(0, "$self Reboot($node_id): error: '$error'\n");
525 526
	goto reboot;
    }
Leigh Stoller's avatar
Leigh Stoller committed
527
    TBSetNodeEventState($node_id, TBDB_NODESTATE_SHUTDOWN);
528 529 530 531 532 533 534 535 536 537
    return 0;

  reboot:
    if (0) {
	return 0;
    }
    return $self->SUPER::Reboot($nodeobject);
}

1;