console_setup.in 2.9 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/perl -wT
use English;

#
# usage: console_setup node [node node ...]
#
7 8 9 10 11
sub usage()
{
    print STDOUT "Usage: console_setup node [node ...]\n";
    exit(-1);
}
12 13 14 15 16

#
# Configure variables
#
my $TB		= "@prefix@";
17
my $TESTMODE	= @TESTMODE@;
18 19

#
20
# Testbed Support libraries
21
#
22 23 24
use lib "@prefix@/lib";
use libdb;
use libtestbed;
25 26 27

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

29
my $SSH		= "$TB/bin/sshtb -n -l root ";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
30
my $PROG	= "/usr/testbed/sbin/console_setup.proxy";
31
my $TBPID	= "flux";
32
my %cmdargs     = ();
33 34 35 36 37 38 39
my @row;

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

#
40 41 42 43 44
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
if (@ARGV == 0) {
    usage();
45
}
46
my @nodes = @ARGV;
47 48 49 50 51 52

#
# 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 B. Stoller's avatar
Leigh B. Stoller committed
53 54
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
55 56 57 58 59 60 61
}

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

    #
    # Need the project for the node since that is the group.
76 77
    #
    # HACK! If its a shark shelf, then need a wildcard query so we can
78
    # find the nodes.
79 80 81
    #
    if ($node =~ /sh\d+/) {
	$db_result =
82 83
	    DBQueryFatal("select pid from reserved ".
			 "where node_id like '$node%'");
84 85 86
    }
    else {
	$db_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
87 88 89 90 91 92
	    DBQueryFatal("select g.unix_name from groups as g ".
			 "left join experiments as e ".
			 " on g.pid=e.pid and g.gid=e.gid ".
			 "left join reserved as r ".
			 " on e.pid=r.pid and e.eid=r.eid ".
			 "where r.node_id='$node'");
93 94
    }
    if ($db_result->numrows > 0) {
95 96 97 98 99 100
	@row = $db_result->fetchrow_array();
	$pid = $row[0];
    }
    else {
	$pid = $TBPID;
    }
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

    #
    # Now we need to know *where* the tip line lives, since there are
    # multiple tip servers. We want to group these still, so use an
    # array of command arguments, indexed by the tip server.
    #
    $db_result =
	DBQueryFatal("select server from tiplines where node_id='$node'");
    
    if ($db_result->numrows == 0) {
	print STDERR "*** No tip server defined for $node\n";
	exit 1;
    }
    @row    = $db_result->fetchrow_array();
    $server = $row[0];

    if (defined($cmdargs{$server})) {
	$cmdargs{$server} = $cmdargs{$server} . " $node $pid";
    }
    else {
	$cmdargs{$server} = "$node $pid ";
    }
}

if ($TESTMODE) {
    exit 0;
127 128
}

129 130 131
#
# Run the console setup program on the tip server nodes.
# 
132
$UID = 0;
133 134 135 136 137 138 139
foreach my $server (keys(%cmdargs)) {
    my $args = $cmdargs{$server};

    if (system("$SSH $server $PROG $args")) {
	print STDERR "*** Failed: $SSH $server $PROG $args: $?\n";
	exit 1;
    }
140
}
141 142

exit 0;