genelists.in 3.42 KB
Newer Older
1
#!/usr/bin/perl -wT
2
use Fcntl ':flock';
3
use English;
4 5 6 7 8 9 10 11 12 13 14

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@";
15
my $USERS       = "@USERNODE@";
16 17
my $TBACTIVE    = "@TBACTIVEARCHIVE@";
my $TBALL       = "@TBUSERSARCHIVE@";
18 19

# Note no -n option. We redirect stdin from the new exports file below.
20
my $SSH		= "$TB/bin/sshtb -l root $USERS";
21
my $PROG	= "/usr/testbed/sbin/genelists.proxy";
22
my $lockfile    = "/var/tmp/testbed_genelists_lockfile";
23
my $tempfile    = "/var/tmp/testbed_genelists_tempfile";
24
my $userlist;
25 26
my $d = 0;

27 28 29 30
#
# Turn off line buffering on output
#
$| = 1; 
31 32

# Load the Testbed support stuff.
33 34 35
use lib "@prefix@/lib";
use libdb;
use libtestbed;
36

37 38 39 40
#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41 42
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
43 44
}
# XXX Hacky!
45
if ($TB ne "/usr/testbed") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
46 47
    die("*** $0:\n".
	"    Wrong version. Maybe its a development version?\n");
48
}
49 50 51 52 53 54 55 56 57 58 59

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

if (@ARGV != 0) {
    usage();
}

60 61 62 63 64 65 66 67 68 69 70 71 72
# 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 ) {
73 74 75 76 77 78 79
    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 ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
80 81
		       "left join group_membership as p ".
		       "     on e.pid=p.pid and p.pid=p.gid ".
82 83 84 85 86 87 88
		       "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";
89
    }
90 91 92 93 94 95 96 97 98 99 100
    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";
101 102
    }

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    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);
125

126 127 128 129 130 131 132
    #
    # 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");
133 134
}

135 136 137 138
#
# Close the lock file. Exiting releases it, but might as well.
#
close(LOCK);
139 140 141 142
exit 0;

sub fatal {
  local($msg) = $_[0];
143
  SENDMAIL($TBOPS, "TESTBED: Failure Generating Email Lists", $msg);
144 145
  die($msg);
}