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

#
# Copyright (c) 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
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
#

#
# Handle RMCP-ASF power control.  Can be extended for IPMI later.
# Sends cycle,off,on to node list in parallel.
#

package power_rmcp;

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

use lib "@prefix@/lib";
use libdb;
use rmcp;


my $debug = 0;
my $parallelize = 0;

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

my %portinfo = ();

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#
# Quick conversion from hex to char.  Note that if there's an odd number of
# chars, we just process the last char c as "0x0c".
#
sub hextostr($) {
    my $hstr = shift;
    if (!defined($hstr)) {
	return '';
    }
    my $retval = "";
    my $len = length($hstr);
    for (my $i = 0; $i < $len; $i += 2) {
	my $ch = substr($hstr,$i,(($i + 2) == $len)?1:2);
	$retval .= chr(hex($ch));
    }

    return $retval;
}

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
# usage: rmcpctrl(type, cmd, nodes)
# type = { "asf" } # can probably be extended to ipmi
# cmd = { "cycle" | "on" | "off" }
# nodes = list of one or more physcial node names
#
# Returns 0 on success. Non-zero on failure.
# 
sub rmcpctrl($$@) {
    my ($type,$cmd,@nodes) = @_;
    my $exitval = 0;

    if ($debug) {
	print "rmcpctrl 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 %rmcp_nodeinfo = ();

    # grab rmcp keys and info
    for my $n (@nodes) {
	my $res = DBQueryFatal("select key_type,key_role,key_uid,mykey" . 
			       " from outlets_remoteauth" . 
			       " where node_id='$n' and key_type='$type'");
	if (!$res || $res->num_rows() == 0) {
	    warn "No keys of type $type in database for $n, skipping\n";
	    ++$exitval;
	}

	while (my ($ttype,$role,$uid,$key) = $res->fetchrow()) {
	    my $atref = [ $ttype,$role,$uid,$key ];

	    if (!defined($rmcp_nodeinfo{$n})) {
		$rmcp_nodeinfo{$n} = [];
	    }

	    if ($debug) {
		print "DEBUG: considered key ($ttype,$role,$uid,$key) for $n\n";
	    }

	    push @{$rmcp_nodeinfo{$n}}, $atref;
	}
    }

    if ($debug) {
        rmcp::rmcp_set_debug_level(5);
	rmcp::rmcp_set_enable_warn_err(1);
    }

    for my $n (keys(%rmcp_nodeinfo)) {
	if ($debug) {
	    print "DEBUG: selecting asf keys for $n\n";
	}
	if ($type eq 'asf' || $type eq 'ASF') {
	    my ($rkey,$rolestr,$gkey);
	    for my $keylist (@{$rmcp_nodeinfo{$n}}) {

		if ($debug) {
		    print "DEBUG: considered keylist (" . join(',',@{$keylist}) . ")\n";
		}

		if ($keylist->[0] eq "$type") {
		    if ($keylist->[1] eq 'generation'
			|| $keylist->[1] eq 'Generation') {
			$gkey = $keylist->[3];
		    }
		    else {
			#
			# XXX: yes, this will use only the LAST
			# non-"generation" key as the role key for asf.
			#
			$rkey = $keylist->[3];
			$rolestr = $keylist->[1];
		    }
		}
	    }

150 151 152 153 154 155 156 157
	    # if keys are in hex, convert them to char strings:
	    if ($rkey =~ /^0x/) {
		$rkey = hextostr(substr($rkey,2));
	    }
	    if ($gkey =~ /^0x/) {
		$gkey = hextostr(substr($gkey,2));
	    }
	    
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
	    if ($parallelize) {
		if (my $pid = fork()) {
		    push @kids, $pid;
		}
		else {
		    my $tret = rmcpexec($type,$cmd,$n,$rolestr,$rkey,$gkey);
		    exit $tret;
		}
	    }
	    else {
		if (rmcpexec($type,$cmd,$n,$rolestr,$rkey,$gkey)) {
		    ++$exitval;
		}
	    }

	    # grab child exit vals
	    if ($parallelize) {
		while (wait() > 0) {
		    if ($?) {
			++$exitval;
		    }
		}
	    }
	}
	else {
	    warn "Unsupported RMCP type $type for $node_id, skipping";
	    ++$exitval;
	}
    }

    return $exitval;
}

#
# Arguments: $type,$cmd,$node_id,$rolestr,$role_key,$gen_key
#
# XXX: add support for key usernames (no rush cause we don't have machines
# that support that functionality.
sub rmcpexec($$$$$$) {
    my ($type,$cmd,$node_id,$rolestr,$role_key,$gen_key) = @_;

    if (!defined($type) || !defined($cmd) || !defined($node_id) 
	|| !defined($rolestr) || !defined($role_key) || !defined($gen_key)) {
	
	warn "Incomplete argument list, skipping node" . 
	    (defined($node_id)?" $node_id":"");
	
	return -1;
    }

    if ($type eq 'asf' || $type eq 'ASF') {
	my $rrole;

	if ($rolestr eq 'administrator' || $rolestr eq 'Administrator') {
	    $rrole = $rmcp::RMCP_ROLE_ADM;
	}
	elsif ($rolestr eq 'operator' || $rolestr eq 'Operator') {
	    $rrole = $rmcp::RMCP_ROLE_OP;
	}
	else {
	    warn "Unknown RMCP role $rolestr, skipping";
	    return -2;
	}

	if ($debug) {
	    print "DEBUG: starting rmcp for $node_id\n";
	}

	my $rkey = rmcp::rmcp_setstrbuf($role_key,length($role_key));
	my $gkey = rmcp::rmcp_setstrbuf($gen_key,length($gen_key));

	my $ctx = rmcp::rmcp_ctx_init(5,3);

	if (rmcp::rmcp_ctx_setsecure($ctx,$rrole,
				     $rkey,length($role_key),
				     $gkey,length($gen_key)) 
	    != $rmcp::RMCP_SUCCESS) {
	    warn "Could not create a secure RMCP context for " . 
		"$node_id, skipping";
   	    rmcp::rmcp_finalize($ctx);
	    return -3;
	}

	if (rmcp::rmcp_open($ctx,$node_id) != $rmcp::RMCP_SUCCESS) {
	    warn "Could not open an RMCP connection for $node_id, skipping";
	    rmcp::rmcp_finalize($ctx);
	    return -4;
	}

	if (rmcp::rmcp_start_secure_session($ctx) != $rmcp::RMCP_SUCCESS) {
	    warn "Could not start a secure RMCP session for $node_id, skipping";
	    rmcp::rmcp_finalize($ctx);
	    return -5;
	}

	my $retval;
	if ($cmd eq 'cycle') {
	    $retval = rmcp::rmcp_asf_power_cycle($ctx);
	}
	elsif ($cmd eq 'on') {
	    $retval = rmcp::rmcp_asf_power_up($ctx);
	}
	elsif ($cmd eq 'off') {
	    $retval = rmcp::rmcp_asf_power_down($ctx);
	}
	else {
	    warn "Unknown RMCP command $cmd for $node_id, skipping";
	    return -6;
	}

	if ($retval != $rmcp::RMCP_SUCCESS) {
	    warn "While cycling $node_id, RMCP error (" . 
  	        rmcp::rmcp_error_tostr($retval) . ")";
	    return $retval;
	}
    }
    else {
	warn "Unsupported RMCP type $type for $node_id, skipping";
	return -7;
    }

    return 0;
}

1;