libosload_dell.pm.in 9.81 KB
Newer Older
1 2 3 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 43 44 45 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 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 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 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 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 338 339 340 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 370 371 372 373 374 375 376 377 378 379 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
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
# 
# {{{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);
use libEmulab;
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));

    if (0 && !exists($INC{'libtblog.pm'})) {
	close(SOUT);
	close(SERR);
	print "Closing SOUT\n";
    }
    $self->dprint(0,"$self createExpectObject($node_id):\n");

    # Host keys change every reload, do not want to save them.
    my $spawn_cmd = "ssh -o userknownhostsfile=/dev/null ".
	"-l $admin_user $node_id";
    
    # 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;
    }
    $self->expectobj($exp);
    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"));
	}
    }

    my @config =
	(@userlines,
	 "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();
    my $exp     = $self->expectobj();

    $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)) {
	$self->expectobj()->soft_close();
	return 0;
    }
    my $user = $experiment->GetSwapper();
    if (!defined($user)) {
	print STDERR "generateConfig: No swapper defined for $experiment\n";
	return 0;
    }
    my $uid = $user->uid();

    #
    # 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!
    #
    my @keys;
    $user->GetSSHKeys(\@keys);
    if (!@keys) {
	print STDERR "No ssh keys for $uid\n";
	return 0;
    }
    
    #
    # 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) {
	next
	    if ($key !~ /^ssh-rsa/);
	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);

    @config =
	("copy tftp://$BOSSNODE_IP/ualloc-downloads/$basename ".
	 " flash://$basename",
	 "ip ssh rsa-authentication username $uid ".
	 " authorized-keys flash://$basename",
	 "write memory",
	);

    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;
    }
    my $exp = $self->expectobj();

    $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) {
	$self->dprint(0, "$self RebootNOS($node_id): error: '$error'\n");
	goto reboot;
    }
    return 0;

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

1;