power_ilo.pm.in 7.87 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -wT

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2008-2010, 2012 University of Utah and the Flux Group.
6 7 8 9
# All rights reserved.
#

#
10
# Handle iLO[23] remote power control.
11
# Also handle DRAC since its so similar.
12
# Node must have an interface such that role='mngmnt' and
13
# interface_type='ilo[23]'.
14 15 16 17 18 19 20 21 22 23 24 25
#
# Supports either pubkey or passwd auth, depending on what's in db.
#

package power_ilo;

use Exporter;
@ISA = ("Exporter");
@EXPORT = qw( iloctrl );

use lib "@prefix@/lib";
use libdb;
26
use emutil;
27 28 29

use IO::Pty;
use POSIX qw(setsid);
30
use POSIX ":sys_wait_h";
31

Leigh B Stoller's avatar
Leigh B Stoller committed
32
my $debug = 0;
33 34 35
# Always parallelize for now cause we are vulnerable to timeouts with 
# unreachable nodes or weird iLO crap.
my $parallelize = 1;
36 37 38 39 40 41 42 43

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

my %portinfo = ();

#
# usage: iloctrl(type, cmd, nodes)
44
# type = { "ilo" | "ilo2" | "ilo3" }
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
# cmd = { "cycle" | "on" | "off" }
# nodes = list of one or more physical node names
#
# Returns 0 on success. Non-zero on failure.
# 
sub iloctrl($$@) {
    my ($type,$cmd,@nodes) = @_;
    my $exitval = 0;

    if ($debug) {
	print "iloctrl called with $type,$cmd,(" . join(',',@nodes) . ")\n";
    }

    if ($cmd ne "cycle" && $cmd ne "on" && $cmd ne "off") {
	warn "invalid power command '$cmd'; \n" . 
	    "  valid commands are 'cycle, 'off', and 'on'.\n";
	return scalar(@nodes);
    }

    my %ilo_nodeinfo = ();

    # grab ilo IP and auth info
    foreach my $n (@nodes) {
	my $res = DBQueryFatal("select IP from interfaces" . 
69 70
			       " where node_id='$n' and ".
			       " role='" . TBDB_IFACEROLE_MANAGEMENT() . "'" . 
71 72
			       "   and interface_type='$type'");
	if (!defined($res) || !$res || $res->num_rows() == 0) {
73
	    warn "No $type interface for $n; cannot find $type IP!\n";
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	    ++$exitval;
	    next;
	}
	my ($IP) = $res->fetchrow();

	$res = DBQueryFatal("select key_role,key_uid,mykey" . 
			    " from outlets_remoteauth" . 
			    " where node_id='$n' and key_type='$type'");
	if (!defined($res) || !$res || $res->num_rows() == 0) {
	    warn "No $type remote auth info for $n!\n";
	    ++$exitval;
	    next;
	}
	my ($krole,$kuid,$kkey) = $res->fetchrow();

89
	$ilo_nodeinfo{$n} = [ $n,$IP,$krole,$kuid,$kkey ];
90 91
    }

92
    my $timeout = 30;
93 94 95 96 97 98 99 100 101 102 103
    if ($parallelize) {
	my $coderef = sub {
	    my ($n,$IP,$krole,$kuid,$kkey) = @{ $_[0] };
	    
	    my $tret;
	    eval {
		$tret = iloexec($n,$type,$cmd,$IP,$krole,$kuid,$kkey,$timeout);
	    };
	    if ($@) {
		print "$@";
		return -1;
104
	    }
105 106 107 108 109 110 111 112
	    return 0;
	};
	my @results = ();
	my @ilos    = values(%ilo_nodeinfo);
	
	if (ParRun(undef, \@results, $coderef, @ilos)) {
	    print STDERR "*** power_ilo: Internal error in ParRun()!\n";
	    return -1;
113
	}
114 115 116 117 118 119
	#
	# Check the exit codes. 
	#
	foreach my $result (@results) {
	    ++$exitval
		if ($result != 0);
120
	}
121
    }
122 123 124
    else {
	for my $key (keys(%ilo_nodeinfo)) {
	    my ($n,$IP,$krole,$kuid,$kkey) = @{$ilo_nodeinfo{$key}};
125

126
	    if (iloexec($n,$type,$cmd,$IP,$krole,$kuid,$kkey,$timeout)) {
127
		++$exitval;
128 129 130 131 132 133 134
	    }
	}
    }
    return $exitval;
}

#
135
# Arguments: $node_id,$type,$cmd,$IP,$key_role,$key_uid,$key[,$timeout]
136
#
137 138
sub iloexec($$$$$$$;$) {
    my ($node_id,$type,$cmd,$IP,$key_role,$key_uid,$key,$timeout) = @_;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

    if ($debug) {
	print "iloexec called with (" . join(',',@_) . ")\n";
    }

    if (!defined($type) || !defined($cmd) || !defined($IP) 
	|| !defined($key_role) || !defined($key_uid) || !defined($key)) {
	warn "Incomplete argument list, skipping node" . 
	    (defined($node_id)?" $node_id":"");

	return -1;
    }

    my $power_cmd;
    if ($cmd eq 'cycle') {
154 155
	$power_cmd = ($type eq "drac" ?
		      'racadm serveraction powercycle' : 'power reset');
156 157
    }
    elsif ($cmd eq 'reset') {
158 159
	$power_cmd = ($type eq "drac" ?
		      'racadm serveraction hardreset' : 'power warm');
160 161
    }
    elsif ($cmd eq 'on') {
162 163
	$power_cmd = ($type eq "drac" ?
		      'racadm serveraction powerup' : 'power on');
164 165
    }
    elsif ($cmd eq 'off') {
166 167
	$power_cmd = ($type eq "drac" ?
		      'racadm serveraction powerdown' : 'power off');
168 169 170 171 172 173
    }
    else {
	warn "Bad iLO power command $cmd";
	return -11;
    }

174 175
    if ($type ne 'ilo' && $type ne 'ilo2' && $type ne 'ilo3' &&
	$type ne "drac") {
176
	warn "Unsupported iLO/DRAC type $type!";
177 178 179 180
	return -7;
    }

    my @expect_seq;
181
    my $ssh_cmd = "ssh -o StrictHostKeyChecking=no -l '$key_uid'";
182 183 184 185 186

    if ($key_role eq 'ssh-key') {
	if ($key ne '') {
	    $ssh_cmd .= " -i '$key'";
	}
187 188 189 190 191 192 193
	if ($type eq "drac") {
	    @expect_seq = (['\$ ',  $power_cmd],
			   ['\$ ',  'exit']);
	}
	else {
	    @expect_seq = (['hpiLO-> ',$power_cmd],['hpiLO-> ','exit']);
	}
194 195 196 197 198 199 200 201
    }
    elsif ($key_role eq 'ssh-passwd') {
	$ssh_cmd .= " -o PubkeyAuthentication=no";
	$ssh_cmd .= " -o PasswordAuthentication=yes";
	if ($key eq '') {
	    warn "iLO key_role ssh-passwd specified, but no passwd!";
	    return -13;
	}
202 203 204 205 206 207 208 209 210
	if ($type eq "drac") {
	    @expect_seq = (['password: ', $key],
			   ['\$ ',        $power_cmd],
			   ['\$ ',        'exit']);
	}
	else {
	    @expect_seq = (['password: ',$key],['hpiLO-> ',$power_cmd],
			   ['hpiLO-> ','exit']);
	}
211 212 213 214 215 216 217 218
    }
    else {
	warn "Unsupported key_role $key_role!";
	return -14;
    }

    $ssh_cmd .= " $IP";

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    my $pid;
    my $sentall = 0;
    # Setup some signal handlers so we can avoid leaving ssh zombies.
    $SIG{'CHLD'} = sub { die "iloexec($node_id) child ssh died unexpectedly!"; };
    $SIG{'PIPE'} = sub { die "iloexec($node_id) ssh died unexpectedly!"; };
    if (defined($timeout)) {
	$SIG{'ALRM'} = sub {
	    $SIG{'PIPE'} = 'IGNORE';
	    $SIG{'CHLD'} = 'IGNORE';
	    kill(INT,$pid);
	    select(undef,undef,undef,0.1);
	    kill(TERM,$pid);
	    select(undef,undef,undef,0.1);
	    kill(KILL,$pid);
	    die "iloexec($node_id) timed out in ssh!";
	};

	alarm($timeout);
    }

239
    my $pty = IO::Pty->new() || die "can't make pty: $!";
240
    defined ($pid = fork()) || die "fork: $!";
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    if (!$pid) {
	# Flip to UID 0 to ensure we can read whatever private key we need
	$EUID = 0;
	
	if ($debug) {
	    print "Flipped to root: $EUID\n";
	}

	# Connect our kid to the tty so the parent can chat through the pty
        POSIX::setsid();

	$pty->make_slave_controlling_terminal();

	my $tty = $pty->slave();
	my $tty_fd = $tty->fileno();
	close($pty);

	open(STDIN,"<&$tty_fd");
	open(STDOUT,">&$tty_fd");
	open(STDERR,">&STDOUT");
	close($tty);

	# Don't want ssh to prompt us via ssh-askpass!
	delete $ENV{DISPLAY};
265 266 267 268

	if ($debug) {
	    print "ssh_cmd($node_id): $ssh_cmd\n";
	}
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
	
	exec("$ssh_cmd") || die "exec: $!";
    }

    #
    # Talk to ssh over the pty: wait for expected output and send responses
    #
    my @lines = ();
    foreach $es (@expect_seq) {
	my ($rval,$sval) = @$es;

	my $found = 0;
	my $line = '';
	while (1) {
	    my $char;
	    if (read($pty,$char,1) != 1) {
		warn "Error in read in iLO pseudo expect loop!\n";
		print "Had read the following lines:\n";
		foreach my $ln (@lines) {
		    print "  $ln\n";
		}
		last;
	    }
	    if ($char eq "\r" || $char eq "\n") {
		push @lines,$line;
		if ($debug) {
		    print "read '$line' while looking for '$rval'\n";
		}
		$line = '';
	    }
	    else {
		$line .= $char;
	    }

	    if ($line =~ /$rval$/) {
304 305
		print $pty $sval;
		print $pty ($type eq "ilo3" ? "\r" : "\r\n");
306 307 308 309 310 311 312 313 314 315 316 317 318 319
		if ($debug) {
		    print "sent '$sval'\n";
		}
		$found = 1;
		last;
	    }
	}

	if (!$found) {
	    # some sort of error; try to kill off ssh
	    kill(15,$pid);
	    return -16;
	}
    }
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    # this is a race, but there's nothing better, because we want the remote 
    # side to see an appropriate exit so it frees its resources, so there is
    # a very miniscule chance that the connection could break and ssh could 
    # exit before we get here... but it seems unlikely.
    $SIG{'CHLD'} = 'IGNORE';

    # make sure the local ssh dies:
    my $i = 5;
    my $dead = 0;
    while (--$i) {
	my $ret = waitpid($pid,WNOHANG);
	if ($ret == -1 || $ret == $pid) {
	    $dead = 1;
	    last;
	}
	sleep(1);
    }
    kill(KILL,$pid) if (!$dead);
338 339 340 341 342 343

    # if we get here, things probably went ok...
    return 0;
}

1;