Commit b12c4034 authored by Leigh B. Stoller's avatar Leigh B. Stoller

A slew of little changes; use the DB library. Makes for less

repetition and clutter. Mostly a visual and convenience thing. I plan
to work through other scripts as time permits.
parent e5e03b1a
...@@ -4,17 +4,30 @@ use English; ...@@ -4,17 +4,30 @@ use English;
# #
# usage: console_setup node [node node ...] # usage: console_setup node [node node ...]
# #
sub usage()
{
print STDOUT "Usage: console_setup node [node ...]\n";
exit(-1);
}
my $optlist = "";
# #
# Configure variables # Configure variables
# #
my $TB = "@prefix@"; my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
#
# Load the Testbed support stuff.
#
push(@INC, "$TB/lib");
require libdb;
# Turn off line buffering on output
$| = 1;
my $SSH = "sshtb -n -q -l root plastic.emulab.net"; my $SSH = "sshtb -n -q -l root plastic.emulab.net";
my $PROG = "$TB/sbin/console_setup.proxy"; my $PROG = "$TB/sbin/console_setup.proxy";
my $TBPID = "flux"; my $TBPID = "flux";
my $dbg = 1;
my $cmdargs = ""; my $cmdargs = "";
my @row; my @row;
...@@ -23,23 +36,24 @@ $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin'; ...@@ -23,23 +36,24 @@ $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
# #
# Set up for querying the database. # Parse command arguments. Once we return from getopts, all that should
# # left are the required arguments.
use Mysql; #
my $DB = Mysql->connect("localhost", $DBNAME, "script", "none"); %options = ();
if (! getopts($optlist, \%options)) {
if ( $#ARGV < 0 ) { usage();
die("Usage: console_setup node [nodes ... ]\n". }
"Enables project members to access consoles logs of nodes.\n"); if (@ARGV == 0) {
usage();
} }
my @nodes = @ARGV; my @nodes = @ARGV;
# #
# Script must be setuid root. We don't worry about who called us or what # 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. # nodes are specified since this script always does the right thing.
# #
if ($EUID != 0) { if ($EUID != 0) {
die("Must be run as root"); die("Must be root! Maybe its a development version?");
} }
# #
...@@ -47,6 +61,8 @@ if ($EUID != 0) { ...@@ -47,6 +61,8 @@ if ($EUID != 0) {
# plastic. # plastic.
# #
foreach my $node (@nodes) { foreach my $node (@nodes) {
my($db_result);
# #
# Untaint the argument. # Untaint the argument.
# #
...@@ -59,18 +75,18 @@ foreach my $node (@nodes) { ...@@ -59,18 +75,18 @@ foreach my $node (@nodes) {
# #
# Need the project for the node since that is the group. # Need the project for the node since that is the group.
#
# #
# HACK! If its a shark shelf, then need a wildcard query so we can # HACK! If its a shark shelf, then need a wildcard query so we can
# the nodes. # the nodes.
# #
if ($node =~ /sh\d+/) { if ($node =~ /sh\d+/) {
$db_result = $db_result =
$DB->query("select pid from reserved where node_id like '$node%'"); DBQueryFatal("select pid from reserved ".
"where node_id like '$node%'");
} }
else { else {
$db_result = $db_result =
$DB->query("select pid from reserved where node_id='$node'"); DBQueryFatal("select pid from reserved where node_id='$node'");
} }
if ($db_result->numrows > 0) { if ($db_result->numrows > 0) {
@row = $db_result->fetchrow_array(); @row = $db_result->fetchrow_array();
......
...@@ -7,6 +7,8 @@ use Fcntl ':flock'; ...@@ -7,6 +7,8 @@ use Fcntl ':flock';
# members. Fire that tail over to the fileserver where it is concatenated with # members. Fire that tail over to the fileserver where it is concatenated with
# the head file to become the new /etc/exports # the head file to become the new /etc/exports
# #
# This script always does the right thing, so it does not matter who calls it.
#
# usage: exports_setup # usage: exports_setup
# #
...@@ -14,7 +16,6 @@ use Fcntl ':flock'; ...@@ -14,7 +16,6 @@ use Fcntl ':flock';
# Configure variables # Configure variables
# #
my $TB = "@prefix@"; my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
my $TBOPS = "@TBOPSEMAIL@"; my $TBOPS = "@TBOPSEMAIL@";
# Note no -n option. We redirect stdin from the new exports file below. # Note no -n option. We redirect stdin from the new exports file below.
...@@ -34,32 +35,25 @@ if ($EUID != 0) { ...@@ -34,32 +35,25 @@ if ($EUID != 0) {
die("Must be root! Maybe its a development version?"); die("Must be root! Maybe its a development version?");
} }
# XXX Hacky! # XXX Hacky!
if ($TB ne "/usr/testbed") { #if ($TB ne "/usr/testbed") {
die("Wrong version. Maybe its a development version?"); # die("Wrong version. Maybe its a development version?");
} #}
# un-taint path # un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin'; $ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$| = 1; #Turn off line buffering on output #
# Turn off line buffering on output
#
$| = 1;
# #
# Testbed Support library # Testbed Support libraries
# #
push(@INC, "$TB/lib"); push(@INC, "$TB/lib");
require libtestbed; require libtestbed;
require libdb;
#
# Set up for querying the database.
#
use Mysql;
my $DB = Mysql->connect("localhost", $DBNAME, "script", "none");
#
# This script always does the right thing, so it does not matter who
# calls it.
#
# #
# We need to serialize this script to avoid a trashed map file. Use # We need to serialize this script to avoid a trashed map file. Use
...@@ -93,13 +87,14 @@ print MAP "\n"; ...@@ -93,13 +87,14 @@ print MAP "\n";
# card. # card.
# #
$db_result = $db_result =
DBquery("select reserved.node_id,reserved.pid,interfaces.IP ". DBQueryFatal("select reserved.node_id,reserved.pid,interfaces.IP ".
"from reserved ". "from reserved ".
"left join nodes on reserved.node_id=nodes.node_id ". "left join nodes on reserved.node_id=nodes.node_id ".
"left join node_types on node_types.type=nodes.type ". "left join node_types on node_types.type=nodes.type ".
"left join interfaces on reserved.node_id=interfaces.node_id and ". "left join interfaces on reserved.node_id=interfaces.node_id".
"interfaces.card=node_types.control_net ". " and interfaces.card=node_types.control_net ".
"where interfaces.IP!='NULL' order by reserved.pid,interfaces.IP"); "where interfaces.IP!='NULL' ".
"order by reserved.pid,interfaces.IP");
# #
# Generate a per project directory line, listing all of the nodes for # Generate a per project directory line, listing all of the nodes for
...@@ -148,15 +143,15 @@ if ($db_result->numrows > 0) { ...@@ -148,15 +143,15 @@ if ($db_result->numrows > 0) {
# /users/mike 101.101.101.103 # /users/mike 101.101.101.103
# #
$db_result = $db_result =
DBquery("select reserved.node_id,reserved.pid,proj_memb.uid,interfaces.IP ". DBQueryFatal("select reserved.node_id,reserved.pid,".
"from reserved ". "proj_memb.uid,interfaces.IP from reserved ".
"left join proj_memb on proj_memb.pid=reserved.pid ". "left join proj_memb on proj_memb.pid=reserved.pid ".
"left join nodes on reserved.node_id=nodes.node_id ". "left join nodes on reserved.node_id=nodes.node_id ".
"left join node_types on node_types.type=nodes.type ". "left join node_types on node_types.type=nodes.type ".
"left join interfaces on reserved.node_id=interfaces.node_id ". "left join interfaces on reserved.node_id=interfaces.node_id ".
"and interfaces.card=node_types.control_net ". "and interfaces.card=node_types.control_net ".
"where interfaces.IP!='NULL' and proj_memb.trust!='none' ". "where interfaces.IP!='NULL' and proj_memb.trust!='none' ".
"order by reserved.pid,reserved.node_id"); "order by reserved.pid,reserved.node_id");
# #
# Generate a per user directory line, listing all of the nodes for # Generate a per user directory line, listing all of the nodes for
...@@ -241,18 +236,3 @@ sub fatal { ...@@ -241,18 +236,3 @@ sub fatal {
SENDMAIL($TBOPS, "TESTBED: Exports Setup Failed", $msg); SENDMAIL($TBOPS, "TESTBED: Exports Setup Failed", $msg);
die($msg); die($msg);
} }
sub DBquery($)
{
my($query) = $_[0];
my($result);
$result = $DB->query($query);
if (! $result) {
print "DB Query failed: $query\n";
}
return $result;
}
...@@ -18,29 +18,28 @@ my $optlist = ""; ...@@ -18,29 +18,28 @@ my $optlist = "";
# Configure variables # Configure variables
# #
my $TB = "@prefix@"; my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
my $TBOPS = "@TBOPSEMAIL@";
my $tbbindir = "$TB/bin/"; my $tbbindir = "$TB/bin/";
my $batchdir = "$TB/batch"; my $batchdir = "$TB/batch";
my $projroot = "/proj"; my $projroot = "/proj";
#
# Untaint the path
#
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
# #
# Turn off line buffering on output # Turn off line buffering on output
# #
$| = 1; $| = 1;
# #
# Testbed Support library # Testbed Support libraries
# #
push(@INC, "$TB/lib"); push(@INC, "$TB/lib");
require libtestbed; require libtestbed;
require libdb;
#
# Untaint the path
#
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
# #
# Parse command arguments. Once we return from getopts, all that should # Parse command arguments. Once we return from getopts, all that should
...@@ -66,16 +65,10 @@ if ($eid =~ /^([-\@\w.]+)$/) { ...@@ -66,16 +65,10 @@ if ($eid =~ /^([-\@\w.]+)$/) {
$eid = $1; $eid = $1;
} }
#
# Set up for querying the database.
#
use Mysql;
my $DB = Mysql->connect("localhost", $DBNAME, "script", "none");
# #
# This is where we currently hold the batch goo. # This is where we currently hold the batch goo.
# #
$dirname = "$batchdir/$pid-$eid"; my $dirname = "$batchdir/$pid-$eid";
# #
# Need to lock the table for this. We could avoid the locking if we just # Need to lock the table for this. We could avoid the locking if we just
...@@ -83,14 +76,14 @@ $dirname = "$batchdir/$pid-$eid"; ...@@ -83,14 +76,14 @@ $dirname = "$batchdir/$pid-$eid";
# introduces needless (and annoying) delay when killing a batch experiment # introduces needless (and annoying) delay when killing a batch experiment
# that is not even running. See corresponding lock in the batch_daemon. # that is not even running. See corresponding lock in the batch_daemon.
# #
DBquery("lock tables batch_experiments write"); DBQueryFatal("lock tables batch_experiments write");
# #
# Set the canceled state right away. This will prevent the batch_daemon # Set the canceled state right away. This will prevent the batch_daemon
# from trying to run it. It might already be running, but thats okay. # from trying to run it. It might already be running, but thats okay.
# #
DBquery("UPDATE batch_experiments set canceled=1 ". DBQueryFatal("UPDATE batch_experiments set canceled=1 ".
"WHERE eid='$eid' and pid='$pid'"); "WHERE eid='$eid' and pid='$pid'");
# #
# Now its safe to look at the state. If its in the "new" state, then # Now its safe to look at the state. If its in the "new" state, then
...@@ -98,10 +91,10 @@ DBquery("UPDATE batch_experiments set canceled=1 ". ...@@ -98,10 +91,10 @@ DBquery("UPDATE batch_experiments set canceled=1 ".
# batch daemon will not bother with it once the canceled bit is set. # batch daemon will not bother with it once the canceled bit is set.
# #
$query_result = $query_result =
DBquery("SELECT status from batch_experiments ". DBQueryFatal("SELECT status from batch_experiments ".
"WHERE eid='$eid' and pid='$pid'"); "WHERE eid='$eid' and pid='$pid'");
DBquery("unlock tables"); DBQueryFatal("unlock tables");
@row = $query_result->fetchrow_array(); @row = $query_result->fetchrow_array();
$state = $row[0]; $state = $row[0];
...@@ -125,7 +118,7 @@ if ($state ne "new") { ...@@ -125,7 +118,7 @@ if ($state ne "new") {
# #
# Delete the DB record. LOCKING! # Delete the DB record. LOCKING!
# #
DBquery("DELETE from batch_experiments WHERE eid='$eid' and pid='$pid'"); DBQueryFatal("DELETE from batch_experiments WHERE eid='$eid' and pid='$pid'");
# #
# And kill the directory. # And kill the directory.
...@@ -140,31 +133,3 @@ print STDOUT ...@@ -140,31 +133,3 @@ print STDOUT
"Batch Experiment $eid in project $pid has been canceled!\n"; "Batch Experiment $eid in project $pid has been canceled!\n";
exit(0); exit(0);
sub fatal($)
{
my($mesg) = $_[0];
print STDOUT "$mesg\n";
#
# Send a message to the testbed list
#
SENDMAIL($TBOPS,
"TESTBED: Batch Mode Cancelation Failure $pid/$eid", $mesg);
exit(-1);
}
sub DBquery()
{
my($query) = $_[0];
my($result);
$result = $DB->query($query);
if (! $result) {
fatal("DB Error: $query");
}
return $result;
}
...@@ -14,17 +14,28 @@ use English; ...@@ -14,17 +14,28 @@ use English;
# #
# Configure variables # Configure variables
# #
my $TB = "@prefix@"; my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
my $PROJROOT = "/proj"; my $PROJROOT = "/proj";
my $TFTPROOT = "/tftpboot"; my $TFTPROOT = "/tftpboot";
my $dbh = Mysql->connect("localhost",$DBNAME,"script","none"); my @DIRLIST = ("exp", "images", "logs", "deltas", "tarfiles", "rpms");
my $db_result= "";
#
# Untaint the path
#
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
#
# Turn off line buffering on output
#
$| = 1;
#
# Load the Testbed support stuff.
#
push(@INC, "$TB/lib");
require libdb;
# #
# Check args. # Check args.
...@@ -45,18 +56,11 @@ else { ...@@ -45,18 +56,11 @@ else {
} }
# #
# Figure out who called us. Only root, tbroot, or people with admin status # Figure out who called us. Only root or people with admin status
# in the DB can run this script. # in the DB can run this script.
# #
if ($UID != 0) { if ($UID && !TBAdmin($UID)) {
my ($me) = getpwuid($UID) die("mkprojdir: You must be root or a TB administrator\n");
or die "$UID not in passwd file";
$db_result = $dbh->query("select admin from users where uid='$me'");
my @row = $db_result->fetchrow_array();
if ($row[0] != 1) {
die("mkprojdir: You must be root or a TB administrator\n");
}
} }
# #
...@@ -65,8 +69,9 @@ if ($UID != 0) { ...@@ -65,8 +69,9 @@ if ($UID != 0) {
# * The project gid. # * The project gid.
# * The project leader uid. # * The project leader uid.
# #
$db_result = $dbh->query("select unix_gid,head_uid ". $db_result =
"from projects where pid='$pid'"); DBQueryFatal("select unix_gid,head_uid from projects where pid='$pid'");
if ($db_result->numrows < 1) { if ($db_result->numrows < 1) {
die("There is no project '$pid'.\n"); die("There is no project '$pid'.\n");
} }
...@@ -96,36 +101,19 @@ if (! chown($uid, $gid, "$PROJROOT/$pid")) { ...@@ -96,36 +101,19 @@ if (! chown($uid, $gid, "$PROJROOT/$pid")) {
} }
# #
# Make a few subdirs. If this gets long, make it a loop. # Make project subdirs.
# #
if (! mkdir("$PROJROOT/$pid/exp", 0770)) { foreach my $dir (@DIRLIST) {
die("Could not make directory $PROJROOT/$pid/exp: $!\n");
}
if (! chmod(0770, "$PROJROOT/$pid/exp")) {
die("Could not chmod directory $PROJROOT/$pid/exp: $!\n");
}
if (! chown($uid, $gid, "$PROJROOT/$pid/exp")) {
die("Could not chown $PROJROOT/$pid/exp to $uid/$gid: $!\n");
}
if (! mkdir("$PROJROOT/$pid/images", 0770)) {
die("Could not make directory $PROJROOT/$pid/images: $!\n");
}
if (! chmod(0770, "$PROJROOT/$pid/images")) {
die("Could not chmod directory $PROJROOT/$pid/images: $!\n");
}
if (! chown($uid, $gid, "$PROJROOT/$pid/images")) {
die("Could not chown $PROJROOT/$pid/images to $uid/$gid: $!\n");
}
if (! mkdir("$PROJROOT/$pid/logs", 0770)) { if (! mkdir("$PROJROOT/$pid/$dir", 0770)) {
die("Could not make directory $PROJROOT/$pid/logs: $!\n"); die("Could not make directory $PROJROOT/$pid/$dir: $!\n");
} }
if (! chmod(0770, "$PROJROOT/$pid/logs")) { if (! chmod(0770, "$PROJROOT/$pid/$dir")) {
die("Could not chmod directory $PROJROOT/$pid/logs: $!\n"); die("Could not chmod directory $PROJROOT/$pid/$dir: $!\n");
} }
if (! chown($uid, $gid, "$PROJROOT/$pid/logs")) { if (! chown($uid, $gid, "$PROJROOT/$pid/$dir")) {
die("Could not chown $PROJROOT/$pid/logs to $uid/$gid: $!\n"); die("Could not chown $PROJROOT/$pid/$dir to $uid/$gid: $!\n");
}
} }
# #
......
...@@ -5,6 +5,8 @@ use Fcntl ':flock'; ...@@ -5,6 +5,8 @@ use Fcntl ':flock';
# #
# Suck out virtual names and create CNAME map entries. # Suck out virtual names and create CNAME map entries.
# #
# This script always does the right thing, so it does not matter who calls it.
#
# usage: named_setup # usage: named_setup
# #
...@@ -12,7 +14,6 @@ use Fcntl ':flock'; ...@@ -12,7 +14,6 @@ use Fcntl ':flock';
# Configure variables # Configure variables
# #
my $TB = "@prefix@"; my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
my $TBOPS = "@TBOPSEMAIL@"; my $TBOPS = "@TBOPSEMAIL@";
my $mapdir = "/etc/namedb"; my $mapdir = "/etc/namedb";
...@@ -39,24 +40,17 @@ if ($TB ne "/usr/testbed") { ...@@ -39,24 +40,17 @@ if ($TB ne "/usr/testbed") {
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin'; $ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$| = 1; #Turn off line buffering on output #
# Turn off line buffering on output
#
$| = 1;