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;
#
# usage: console_setup node [node node ...]
#
sub usage()
{
print STDOUT "Usage: console_setup node [node ...]\n";
exit(-1);
}
my $optlist = "";
#
# Configure variables
#
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 $PROG = "$TB/sbin/console_setup.proxy";
my $TBPID = "flux";
my $dbg = 1;
my $cmdargs = "";
my @row;
......@@ -23,23 +36,24 @@ $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
#
# Set up for querying the database.
#
use Mysql;
my $DB = Mysql->connect("localhost", $DBNAME, "script", "none");
if ( $#ARGV < 0 ) {
die("Usage: console_setup node [nodes ... ]\n".
"Enables project members to access consoles logs of nodes.\n");
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
usage();
}
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
# nodes are specified since this script always does the right thing.
#
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) {
# plastic.
#
foreach my $node (@nodes) {
my($db_result);
#
# Untaint the argument.
#
......@@ -59,18 +75,18 @@ foreach my $node (@nodes) {
#
# 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
# the nodes.
#
if ($node =~ /sh\d+/) {
$db_result =
$DB->query("select pid from reserved where node_id like '$node%'");
DBQueryFatal("select pid from reserved ".
"where node_id like '$node%'");
}
else {
$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) {
@row = $db_result->fetchrow_array();
......
......@@ -7,6 +7,8 @@ use Fcntl ':flock';
# members. Fire that tail over to the fileserver where it is concatenated with
# 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
#
......@@ -14,7 +16,6 @@ use Fcntl ':flock';
# Configure variables
#
my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
my $TBOPS = "@TBOPSEMAIL@";
# Note no -n option. We redirect stdin from the new exports file below.
......@@ -34,32 +35,25 @@ if ($EUID != 0) {
die("Must be root! Maybe its a development version?");
}
# XXX Hacky!
if ($TB ne "/usr/testbed") {
die("Wrong version. Maybe its a development version?");
}
#if ($TB ne "/usr/testbed") {
# die("Wrong version. Maybe its a development version?");
#}
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
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");
require libtestbed;
#
# 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.
#
require libdb;
#
# We need to serialize this script to avoid a trashed map file. Use
......@@ -93,13 +87,14 @@ print MAP "\n";
# card.
#
$db_result =
DBquery("select reserved.node_id,reserved.pid,interfaces.IP ".
"from reserved ".
"left join nodes on reserved.node_id=nodes.node_id ".
"left join node_types on node_types.type=nodes.type ".
"left join interfaces on reserved.node_id=interfaces.node_id and ".
"interfaces.card=node_types.control_net ".
"where interfaces.IP!='NULL' order by reserved.pid,interfaces.IP");
DBQueryFatal("select reserved.node_id,reserved.pid,interfaces.IP ".
"from reserved ".
"left join nodes on reserved.node_id=nodes.node_id ".
"left join node_types on node_types.type=nodes.type ".
"left join interfaces on reserved.node_id=interfaces.node_id".
" and interfaces.card=node_types.control_net ".
"where interfaces.IP!='NULL' ".
"order by reserved.pid,interfaces.IP");
#
# Generate a per project directory line, listing all of the nodes for
......@@ -148,15 +143,15 @@ if ($db_result->numrows > 0) {
# /users/mike 101.101.101.103
#
$db_result =
DBquery("select reserved.node_id,reserved.pid,proj_memb.uid,interfaces.IP ".
"from reserved ".
"left join proj_memb on proj_memb.pid=reserved.pid ".
"left join nodes on reserved.node_id=nodes.node_id ".
"left join node_types on node_types.type=nodes.type ".
"left join interfaces on reserved.node_id=interfaces.node_id ".
"and interfaces.card=node_types.control_net ".
"where interfaces.IP!='NULL' and proj_memb.trust!='none' ".
"order by reserved.pid,reserved.node_id");
DBQueryFatal("select reserved.node_id,reserved.pid,".
"proj_memb.uid,interfaces.IP from reserved ".
"left join proj_memb on proj_memb.pid=reserved.pid ".
"left join nodes on reserved.node_id=nodes.node_id ".
"left join node_types on node_types.type=nodes.type ".
"left join interfaces on reserved.node_id=interfaces.node_id ".
"and interfaces.card=node_types.control_net ".
"where interfaces.IP!='NULL' and proj_memb.trust!='none' ".
"order by reserved.pid,reserved.node_id");
#
# Generate a per user directory line, listing all of the nodes for
......@@ -241,18 +236,3 @@ sub fatal {
SENDMAIL($TBOPS, "TESTBED: Exports Setup Failed", $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 = "";
# Configure variables
#
my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
my $TBOPS = "@TBOPSEMAIL@";
my $tbbindir = "$TB/bin/";
my $batchdir = "$TB/batch";
my $projroot = "/proj";
#
# Untaint the path
#
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
#
# Turn off line buffering on output
#
$| = 1;
#
# Testbed Support library
# Testbed Support libraries
#
push(@INC, "$TB/lib");
require libtestbed;
#
# Untaint the path
#
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
require libdb;
#
# Parse command arguments. Once we return from getopts, all that should
......@@ -66,16 +65,10 @@ if ($eid =~ /^([-\@\w.]+)$/) {
$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.
#
$dirname = "$batchdir/$pid-$eid";
my $dirname = "$batchdir/$pid-$eid";
#
# Need to lock the table for this. We could avoid the locking if we just
......@@ -83,14 +76,14 @@ $dirname = "$batchdir/$pid-$eid";
# introduces needless (and annoying) delay when killing a batch experiment
# 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
# from trying to run it. It might already be running, but thats okay.
#
DBquery("UPDATE batch_experiments set canceled=1 ".
"WHERE eid='$eid' and pid='$pid'");
DBQueryFatal("UPDATE batch_experiments set canceled=1 ".
"WHERE eid='$eid' and pid='$pid'");
#
# 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 ".
# batch daemon will not bother with it once the canceled bit is set.
#
$query_result =
DBquery("SELECT status from batch_experiments ".
"WHERE eid='$eid' and pid='$pid'");
DBQueryFatal("SELECT status from batch_experiments ".
"WHERE eid='$eid' and pid='$pid'");
DBquery("unlock tables");
DBQueryFatal("unlock tables");
@row = $query_result->fetchrow_array();
$state = $row[0];
......@@ -125,7 +118,7 @@ if ($state ne "new") {
#
# 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.
......@@ -140,31 +133,3 @@ print STDOUT
"Batch Experiment $eid in project $pid has been canceled!\n";
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;
#
# Configure variables
#
my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
my $TB = "@prefix@";
my $PROJROOT = "/proj";
my $TFTPROOT = "/tftpboot";
my $dbh = Mysql->connect("localhost",$DBNAME,"script","none");
my $db_result= "";
my @DIRLIST = ("exp", "images", "logs", "deltas", "tarfiles", "rpms");
#
# 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.
......@@ -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.
#
if ($UID != 0) {
my ($me) = getpwuid($UID)
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");
}
if ($UID && !TBAdmin($UID)) {
die("mkprojdir: You must be root or a TB administrator\n");
}
#
......@@ -65,8 +69,9 @@ if ($UID != 0) {
# * The project gid.
# * The project leader uid.
#
$db_result = $dbh->query("select unix_gid,head_uid ".
"from projects where pid='$pid'");
$db_result =
DBQueryFatal("select unix_gid,head_uid from projects where pid='$pid'");
if ($db_result->numrows < 1) {
die("There is no project '$pid'.\n");
}
......@@ -96,36 +101,19 @@ if (! chown($uid, $gid, "$PROJROOT/$pid")) {
}
#
# Make a few subdirs. If this gets long, make it a loop.
#
if (! mkdir("$PROJROOT/$pid/exp", 0770)) {
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");
}
# Make project subdirs.
#
foreach my $dir (@DIRLIST) {
if (! mkdir("$PROJROOT/$pid/logs", 0770)) {
die("Could not make directory $PROJROOT/$pid/logs: $!\n");
}
if (! chmod(0770, "$PROJROOT/$pid/logs")) {
die("Could not chmod directory $PROJROOT/$pid/logs: $!\n");
}
if (! chown($uid, $gid, "$PROJROOT/$pid/logs")) {
die("Could not chown $PROJROOT/$pid/logs to $uid/$gid: $!\n");
if (! mkdir("$PROJROOT/$pid/$dir", 0770)) {
die("Could not make directory $PROJROOT/$pid/$dir: $!\n");
}
if (! chmod(0770, "$PROJROOT/$pid/$dir")) {
die("Could not chmod directory $PROJROOT/$pid/$dir: $!\n");
}
if (! chown($uid, $gid, "$PROJROOT/$pid/$dir")) {
die("Could not chown $PROJROOT/$pid/$dir to $uid/$gid: $!\n");
}
}
#
......
......@@ -5,6 +5,8 @@ use Fcntl ':flock';
#
# 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
#
......@@ -12,7 +14,6 @@ use Fcntl ':flock';
# Configure variables
#
my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
my $TBOPS = "@TBOPSEMAIL@";
my $mapdir = "/etc/namedb";
......@@ -39,24 +40,17 @@ if ($TB ne "/usr/testbed") {
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
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");
require libtestbed;
#
# 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.
#
require libdb;
#
# We need to serialize this script to avoid a trashed map file. Use
......@@ -87,8 +81,8 @@ print MAP "\$TTL\t1\n";
# Figure out the set of nodes we are working on. These are the nodes
# that have a vname in the reserved table.
#
$db_result = $DB->query("select node_id,pid,eid,vname from reserved ".
"where vname!=''");
$db_result =
DBQueryFatal("select node_id,pid,eid,vname from reserved where vname!=''");
if ($db_result->numrows > 0) {
#
......
......@@ -22,7 +22,12 @@ my $optlist = "df";
# Configure variables
#
my $TB = "@prefix@";
my $DBNAME = "@TBDBNAME@";
#
# Load the Testbed support stuff.
#
push(@INC, "$TB/lib");
require libdb;
my $ssh = "ssh -n -q";
my $power = "$TB/bin/power";
......@@ -42,12 +47,6 @@ delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
# Turn off line buffering on output
$| = 1;
#
# Set up for querying the database.
#
use Mysql;
my $DB = Mysql->connect("localhost", $DBNAME, "script", "none");
#
# We don't want to run this script unless its the real version.
#
......@@ -89,30 +88,10 @@ foreach my $node ( @ARGV ) {
# Figure out who called us. Root and admin types can do whatever they
# want. Normal users can reboot nodes in their experiment.
#
if ($UID != 0) {
my $query_result;
my ($name) = getpwuid($UID) or
die("$UID not in passwd file");
$query_result = DBquery("select admin from users where uid='$name'");
if (! $query_result) {
die("DB Error getting user status");
}
@row = $query_result->fetchrow_array();
if ($row[0] != 1) {
#
# Check to make sure that mere user is allowed to muck with nodes
#
foreach my $node (@nodes) {
$query_result = DBquery("select reserved.node_id from reserved ".
"left join proj_memb on ".
"reserved.pid=proj_memb.pid and ".
"reserved.node_id='$node' ".
"where proj_memb.uid='$name'");
if ($query_result->numrows < 1) {
die("You do not have permission to reboot $node\n");
}
if ($UID && !TBAdmin($UID)) {
foreach my $node (@nodes) {
if (! NodeAccessCheck(\$node)) {
die("You do not have permission to reboot $node\n");
}
}
}
......@@ -306,17 +285,3 @@ sub DoesPing {
}
return 1;
}
sub DBquery($)
{
my($query) = $_[0];
my($result);
$result = $DB->query($query);
if (! $result) {
print "DB Query failed: $query\n";
}
return $result;
}
......@@ -104,7 +104,7 @@ if ($UID && !TBAdmin($UID)) {
# or any image that has a null pid field, since those are defined to be
# open to anyone.
#
$db_result = DBQuery("select * from images where imageid='$imageid'");
$db_result = DBQueryFatal("select * from images where imageid='$imageid'");
if ($db_result->numrows < 1) {
die("No such imageid $imageid!");
......@@ -169,12 +169,9 @@ foreach my $node (@nodes) {
my $pc = $node;
print STDOUT "Changing default OS for $pc to $defosid\n";
$sth = DBQuery("update nodes set ".
"def_boot_osid='$defosid',def_boot_path='' ".
"where node_id='$pc'");
if ($sth == 0) {
die("Database update failed (nodes def_boot_osid).");
}
DBQueryFatal("update nodes set ".
"def_boot_osid='$defosid',def_boot_path='' ".
"where node_id='$pc'");
#
# Assign partition table entries for each partition in the image.
......@@ -193,29 +190,21 @@ foreach my $node (@nodes) {
if (defined($imageid_row{$partname})) {
my $osid = $imageid_row{$partname};
$sth = DBQuery("replace into partitions ".
"(partition, osid, node_id) ".
"values('$i', '$osid', '$pc')");
DBQueryFatal("replace into partitions ".
"(partition, osid, node_id) ".
"values('$i', '$osid', '$pc')");
}
else {
$sth = DBQuery("delete from partitions ".
"where node_id='$pc' and partition='$i'");
}
if ($sth == 0) {
die("Database update failed (partitions table). Aborted ...");
DBQueryFatal("delete from partitions ".
"where node_id='$pc' and partition='$i'");
}
}
print STDOUT "Setting up reload for $pc\n";
$sth = DBQuery("update nodes set ".
"next_boot_path='$NETDISK',".
"next_boot_cmd_line='$cmdline' ".
"where node_id='$pc'");
if ($sth == 0) {
die("Database update failed (nodes next_boot). Aborted ...");
}
DBQueryFatal("update nodes set ".
"next_boot_path='$NETDISK',".
"next_boot_cmd_line='$cmdline' ".
"where node_id='$pc'");
}
#
......
......@@ -95,23 +95,16 @@ if ($UID && !TBAdmin($UID)) {
my @load_list=();
foreach my $node (@nodes) {
my $pc = $node;
my $allocated = 0;
$sth = DBQuery("select * from nodes where node_id='$pc'");
if ($sth == 0) {
die("Database lookup failed (exists). Aborted...\n");
}
$sth = DBQueryFatal("select * from nodes where node_id='$pc'");
if ($sth->num_rows() != 1) {
print STDERR "Node $pc doesn't exist. Skipping $pc.\n";
next;
}
print STDERR "Checking if $pc is reserved...";
$sth = DBQuery("select * from reserved where node_id='$pc'");
if ($sth == 0) {
die("Database lookup failed (reserved). Aborted...\n");
}
my $allocated = 0;
$sth = DBQueryFatal("select * from reserved where node_id='$pc'");
if ( ($sth->num_rows()) < 1) {
print STDERR "Available.\nReserving and adding to list.\n";
......@@ -127,7 +120,7 @@ foreach my $node (@nodes) {
# Kill the last_reservation so that whoever gets the node next
# won't be fooled into thinking a reload is required.
#
$sth = DBQuery("delete from last_reservation where node_id='$pc'");
DBQueryFatal("delete from last_reservation where node_id='$pc'");
push (@load_list,$pc);
$allocated = 1;
}
......@@ -145,11 +138,8 @@ foreach my $node (@nodes) {
# Put it in the reloads table so TMCD knows to free it.
print STDERR "Scheduling reload of $imageid for $pc:\n";
$sth = DBQuery("replace into reloads ".
"(node_id, image_id) values ('$pc', '$imageid')");
if ($sth == 0) {
die("Database update failed (reloads). Aborted...\n");
}
DBQueryFatal("replace into reloads ".
"(node_id, image_id) values ('$pc', '$imageid')");
}
if (@load_list > 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment