console_setup.in 3.2 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003, 2005 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
6 7 8
# All rights reserved.
#

9 10 11 12 13
use English;

#
# usage: console_setup node [node node ...]
#
14 15 16 17 18
sub usage()
{
    print STDOUT "Usage: console_setup node [node ...]\n";
    exit(-1);
}
19 20 21 22 23

#
# Configure variables
#
my $TB		= "@prefix@";
24
my $TESTMODE	= @TESTMODE@;
25
my $TBPID	= "@TBADMINGROUP@";
26 27

#
28
# Testbed Support libraries
29
#
30 31 32
use lib "@prefix@/lib";
use libdb;
use libtestbed;
33 34 35

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

37
my $SSH		= "$TB/bin/sshtb -n -l root ";
Leigh Stoller's avatar
Leigh Stoller committed
38
my $PROG	= "/usr/testbed/sbin/console_setup.proxy";
39
my %cmdargs     = ();
40 41 42 43 44 45 46
my @row;

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

#
47 48 49 50 51
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
if (@ARGV == 0) {
    usage();
52
}
53
my @nodes = @ARGV;
54 55 56 57 58 59

#
# Script must be setuid root. We don't worry about who called us or what
# nodes are specified since this script always does the right thing.
#
if ($EUID != 0) {
Leigh Stoller's avatar
Leigh Stoller committed
60 61
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
62 63 64 65 66 67 68
}

#
# Build of a list of nodes/pid pairs and then send the command over to
# plastic.
# 
foreach my $node (@nodes) {
69 70
    my($db_result);
    
71 72 73 74 75 76
    #
    # Untaint the argument. 
    #
    if ($node =~ /^([-\@\w.]+)$/) {
	$node = $1;
    }
77 78 79
    else {
	die("Tainted node name: $node");
    }
80 81

    #
82 83 84 85
    # We need to know all of the tiplines associated with this node,
    # and where they live. There might not be any at all, in which
    # case we are done. This query could probably be rolled into the
    # next query, but that would be confusing.
86
    #
87 88
    $tiplines_result =
	DBQueryFatal("select tipname,server from tiplines ".
89 90
		     "where node_id='$node' and ".
		     "server!='' and keydata is not null");
91 92 93

    if (! $tiplines_result->numrows) {
	next;
94
    }
95 96 97 98 99

    #
    # Determine the unix group for the node. 
    #
    $db_result =
100 101 102 103 104
	DBQueryFatal("select g.unix_name from reserved as r ".
		     "left join experiments as e on ".
		     " r.pid=e.pid and r.eid=e.eid ".
		     "left join groups as g on ".
		     " g.pid=e.pid and g.gid=e.gid ".
105 106
		     "where r.node_id='$node'");

107
    if ($db_result->numrows > 0) {
108 109 110 111 112 113
	@row = $db_result->fetchrow_array();
	$pid = $row[0];
    }
    else {
	$pid = $TBPID;
    }
114 115

    #
116 117 118 119
    # For each tipline associated with the node (might be more than one),
    # we want to issue the proxy command. However, we want to group all
    # commands for each server together to avoid a zillion ssh calls. So,
    # use an array of command arguments, indexed by the tip server.
120
    #
121 122 123 124 125 126 127 128 129 130
    while (@row = $tiplines_result->fetchrow_array()) {
	my $tipname = $row[0];
	my $server  = $row[1];

	if (defined($cmdargs{$server})) {
	    $cmdargs{$server} = $cmdargs{$server} . " $tipname $pid";
	}
	else {
	    $cmdargs{$server} = "$tipname $pid ";
	}
131 132 133 134 135
    }
}

if ($TESTMODE) {
    exit 0;
136 137
}

138 139 140
#
# Run the console setup program on the tip server nodes.
# 
141
$UID = 0;
142 143 144
foreach my $server (keys(%cmdargs)) {
    my $args = $cmdargs{$server};

145
    if (system("$SSH -host $server $PROG $args")) {
146 147 148
	print STDERR "*** Failed: $SSH $server $PROG $args: $?\n";
	exit 1;
    }
149
}
150 151

exit 0;