power_icebox.pm 5.63 KB
Newer Older
1 2 3
#! /usr/bin/perl -w

#
4
# Copyright (c) 2011-2013 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
#

#
# module for controlling power and reset via Linux NetworX's ICEBox
#
# supports new(ip), power(on|off|cyc[le]), status
#

package power_icebox;

use POSIX;
use IO::Pty;

use strict;
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin'; # Required when using system() or backticks `` in combination with the perl -T taint checks

40 41 42
# timeout for telnet operations, library default is 10 seconds
my $TIMEOUT = 10;

43 44 45 46
sub spawn_subprocess {
    my(@cmd) = @_;
    my($pid, $pty, $tty, $tty_fd);

47 48 49 50 51
    $pty = new IO::Pty();
    if (not defined $pty) {
	print STDERR "pty failed: $!\n";
	return undef;
    }
52 53 54

    $pid = fork();
    if (not defined $pid) {
55 56
        print STDERR "fork() failed: $!\n";
	return undef;
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
    } elsif ($pid == 0) {
        # detach from controlling terminal
        POSIX::setsid or die "setsid failed: $!";

        $tty = $pty->slave;
        $pty->make_slave_controlling_terminal();
        $tty_fd = $tty->fileno;
	close $pty;
  
	open STDIN, "<&$tty_fd" or die $!;
	open STDOUT, ">&$tty_fd" or die $!;
	open STDERR, ">&STDOUT" or die $!;
	close $tty;

        exec @cmd or die "exec($cmd[0]) failed: $!\n";
    }

    return $pty;
}

sub _icebox_exec ($$) {
    my ($self, $host, $cmd) = @_;
    my $user = "admin";
    my $password = "icebox";
    my $prompt = '/# /';

    # OK, start ssh child process
    my $pty = spawn_subprocess("ssh",
                  "-o", "RSAAuthentication=no",
                  "-o", "PubkeyAuthentication=no",
                  "-o", "PasswordAuthentication=yes",
                  "-l", $user, $host);
89 90 91 92
    if (not defined $pty) {
	print STDERR "$host: could not start ssh\n";
	return undef;
    }
93 94 95 96 97

    my $ssh = new Net::Telnet (-fhopen => $pty,
                               -prompt => $prompt,
                               -telnetmode => 0,
                               -cmd_remove_mode => 1,
98
                               -output_record_separator => "\r",
99
			       -timeout => $TIMEOUT,
100 101 102 103 104
			       -errmode => "return");
    if (not defined $ssh) {
	print STDERR "$host: could not create telnet object\n";
	return undef;
    }
105 106

    # Log in to the icebox
107 108 109 110 111
    if (!$ssh->waitfor(-match => '/password: ?$/i',
		       -errmode => "return")) {
        print STDERR "$host: failed to connect: ", $ssh->lastline;
	return undef;
    }
112
    $ssh->print($password);
113 114 115 116 117
    if (!$ssh->waitfor(-match => $ssh->prompt,
		       -errmode => "return")) {
        print STDERR "$host: login failed: ", $ssh->lastline;
	return undef;
    }
118 119 120 121 122 123 124 125

    # Send the command to the icebox and get any output
    my @output = $ssh->cmd($cmd);
    return \@output;
}

sub new($$;$) {

126 127
    require Net::Telnet; # Saves us from parsing the ssh output by hand

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
    # The next two lines are some voodoo taken from perltoot(1)
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $devicename = shift;
    my $debug = shift;

    if (!defined($debug)) {
        $debug = 0;
    }

    if ($debug) {
        print "power_icebox module initializing... debug level $debug\n";
    }

    my $self = {};

    $self->{DEBUG} = $debug;

    $self->{DEVICENAME} = $devicename;

    bless($self,$class);
    return $self;
}

153
sub _icebox_power {
154 155 156 157 158 159 160 161 162 163 164 165 166
    my ($self, $op, @ports) = @_;

    my $errors = 0;
    my $output;

    if    ($op eq "on")  { $op = "power on";    }
    elsif ($op eq "off") { $op = "power off";   }
    
    $output = $self->_icebox_exec($self->{DEVICENAME}, $op . ' ' . join(',', @ports));

    if (not defined $output) {
	    print STDERR $self->{DEVICENAME}, ": could not execute power command \"$op\" for ports @ports\n";
	    $errors++;
167
    } elsif ($$output[-1] !~ /^\s*OK$/) {
168 169 170 171 172 173 174
	    print STDERR $self->{DEVICENAME}, ": power command \"$op\" failed with error @$output\n";
	    $errors++;
    }

    return $errors;
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
sub power {
	my ($self, $op, @ports) = @_;
	my $rc;
	
	if ($op =~ /cyc/) {
		$rc = $self->_icebox_power("off", @ports);
		if (!$rc) {
			sleep(3); # XXX Is three seconds enough?
			$rc = $self->_icebox_power("on", @ports);
		}
	} else {
		$rc = $self->_icebox_power($op, @ports);
	}

	return $rc;
}

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
sub status {
    my $self = shift;
    my $statusp = shift; # pointer to an associative (hashed) array (i.o.w. passed by reference)
    my %status;          # local associative array which we'll pass back through $statusp

    my $errors = 0;
    my $output;

    # Get power status (i.e. whether system is on/off)
    $output = $self->_icebox_exec($self->{DEVICENAME}, "power status all");
    if (not defined $output) {
        $errors++;
        print STDERR $self->{DEVICENAME}, ": could not get power status from device\n";
    }
    else {
        for my $line (@$output) {
            next unless $line =~ /^port\s+(\d+):\s+(.*)$/;
	    $status{"outlet$1"} = ucfirst $2;
	    print("Power status for outlet $1 is: $2\n") if $self->{DEBUG};
	}
    }

    if ($statusp) { %$statusp = %status; } # update passed-by-reference array
    return $errors;
}

1;