#!/usr/bin/perl -wT use Fcntl ':flock'; use English; sub usage() { print STDOUT "Usage: genelists\n". "Generate the email list files after things change\n"; exit(-1); } my $optlist = ""; # Configure variables my $TB = "@prefix@"; my $TBOPS = "@TBOPSEMAIL@"; my $USERS = "@USERNODE@"; my $TBACTIVE = "@TBACTIVEARCHIVE@"; my $TBALL = "@TBUSERSARCHIVE@"; # Note no -n option. We redirect stdin from the new exports file below. my $SSH = "$TB/bin/sshtb -l root $USERS"; my $PROG = "/usr/testbed/sbin/genelists.proxy"; my $lockfile = "/var/tmp/testbed_genelists_lockfile"; my $tempfile = "/var/tmp/testbed_genelists_tempfile"; my $userlist; my $d = 0; # # Turn off line buffering on output # $| = 1; # Load the Testbed support stuff. use lib "@prefix@/lib"; use libdb; use libtestbed; # # We don't want to run this script unless its the real version. # if ($EUID != 0) { die("*** $0:\n". " Must be root! Maybe its a development version?\n"); } # XXX Hacky! if ($TB ne "/usr/testbed") { die("*** $0:\n". " Wrong version. Maybe its a development version?\n"); } # # un-taint path # $ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; if (@ARGV != 0) { usage(); } # Set up a mutex so this doesn't get run twice at the same time open(LOCK, ">>$lockfile") || fatal("Couldn't open $lockfile\n"); $count = 0; while (flock(LOCK, LOCK_EX|LOCK_NB) == 0) { print "Another genelists in progress. Waiting a moment ...\n"; if ($count++ > 20) { fatal("Could not get the lock after a long time!\n"); } sleep(1); } foreach my $active ( 0, 1 ) { my $progarg; if ($active) { print "Getting Active Users\n" if $d; # All active users on the testbed if (! ($query_result = DBQuery("SELECT DISTINCT u.usr_email from experiments as e ". "left join group_membership as p ". " on e.pid=p.pid and p.pid=p.gid ". "left join users as u on u.uid=p.uid ". "where u.status='active' order by u.usr_email"))) { DBFatal("Getting Active Users!"); } $userlist = "$TBOPS\n". "$TBACTIVE\n"; $progarg = "-active"; } else { print "Getting All Users\n" if $d; # All approved users on the testbed if (! ($query_result = DBQuery("SELECT DISTINCT usr_email FROM users ". "where status='active' order by usr_email"))) { DBFatal("Getting Users!"); } $userlist = "$TBOPS\n". "$TBALL\n"; $progarg = "-all"; } open(LIST,"> $tempfile") || fatal("Couldn't open $tempfile: $!\n"); print LIST "#\n"; print LIST "# WARNING! THIS FILE IS AUTOGENERATED. DO NOT EDIT!\n"; print LIST "#\n"; for ($i = 0; $i < $query_result->numrows; $i++) { $user_email = ($query_result->fetchrow_array())[0]; if (! defined($user_email)) { next; } if ($userlist) { $userlist .= "$user_email\n"; } else { $userlist = "$user_email\n"; } } print LIST $userlist; print $userlist if $d; close(LIST); # # Fire the new file over to the fileserver to finish up. # $UID = 0; system("$SSH $PROG $progarg < $tempfile") == 0 or fatal("Failed: $SSH $PROG $progarg < $tempfile: $?"); unlink("$tempfile"); } # # Close the lock file. Exiting releases it, but might as well. # close(LOCK); exit 0; sub fatal { local($msg) = $_[0]; SENDMAIL($TBOPS, "TESTBED: Failure Generating Email Lists", $msg); die($msg); }