tbsgmotepower.in 2.67 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
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004 University of Utah and the Flux Group.
# All rights reserved.
#

#
# tbsgmotepower - An emulab frontend to turning on and off the power to a mote
# hosted on a stargate
#

use lib '@prefix@/lib';
my $TB = '@prefix@';
 
use libdb;
use English;

#
# We have to be setuid root so that we can ssh into stargates as root
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

# un-taint path
$ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin:$TB/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
 
use strict;

#
# Constants
#
my $MOTEPOWER = "/usr/local/bin/motepower";
my $SSHTB     = "$TB/bin/sshtb";

#
# Handle command-line arguments
#
sub usage() {
    warn "Usage: $0 <on | off | cycle> <motes ...>\n";
    return 1;
}

my $operation = shift @ARGV;
if (!$operation) {
    exit usage();
}

# Check the operation type
if ($operation !~ /^(on|off|cycle)$/) {
    exit &usage;
} else {
    # Untaint operation
    $operation = $1;
}    

# They have to give us at least one mote
my @motes = @ARGV;
if (!@motes) {
    exit usage();
}

#
# Tait check the node names
#
@motes = map {
    if (/^([-\w]+)$/) {
	$1;
    } else {
	die("*** Tainted node name: $_\n");
    }
} @motes;

#
# Permission check
#
if ($UID && !TBNodeAccessCheck($UID,TB_NODEACCESS_LOADIMAGE,@motes)) {
    die "You do not have permission to modify one or more nodes\n";
}

#
# Log into each stargate to reboot the mote
#
my $errors = 0;
MOTE: foreach my $mote (@motes) {
    #
    # Make sure they gave us an actual mote
    #
    my ($motetype, $moteclass) = TBNodeType($mote);
    if ($moteclass ne "mote") {
	warn "$mote is not a mote - skipping\n";
	$errors++;
	next MOTE;
    }

    #
    # Find out the type of the mote's host
    #
    my $host;
    if (!TBPhysNodeID($mote,\$host)) {
	warn "Error getting host for $mote - skipping\n";
	$errors++;
	next MOTE;
    }
    if ($host eq $mote) {
	warn "Error - no host found for $mote - skipping\n";
	$errors++;
	next MOTE;
    }
    my ($hosttype, $hostclass) = TBNodeType($host);

    # Make sure the host is a stargate
    # XXX - garcia is temporary - hopefully, at some point, we will
    # distinguish the garcia from the stargate that rides on it
    if ($hosttype !~ /^(sg|garcia)/) {
	die "Mote host $host for $mote has unsupported type $hosttype";
    }

    #
    # Actually do the power control
    #
    my $commandstr = "$SSHTB -host $host $MOTEPOWER $operation";

    #
    # SSH gets ticked if UID != EUID, so set that now
    #
    $UID = $EUID;
    if (system($commandstr)) {
	$errors++;
	warn "Failed to control power for $mote";
    }
}

if ($errors) {
    exit 1;
} else {
    exit 0;
}