All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

genelists.in 3.39 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 15

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@";

16 17 18 19 20 21 22
# Should be configure variable!
my $TBACTIVE	= "testbed-active-users-archive\@flux.cs.utah.edu";
my $TBALL	= "testbed-users-archive\@flux.cs.utah.edu";

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

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

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

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

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

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

59 60 61 62 63 64 65 66 67 68 69 70 71
# 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 ) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    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 proj_memb as p on e.pid=p.pid ".
		       "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";
87
    }
88 89 90 91 92 93 94 95 96 97 98
    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";
99 100
    }

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

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

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

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