prereserve_check.in 3.62 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#
use strict;
use English;
use Getopt::Std;

#
# Look for pending pre-reserves that need to be activated.
#
sub usage()
{
    print STDERR "Usage: prereserve_check [-d] [-n]\n";
    exit(-1);
}
my $optlist  = "dn";
my $debug    = 0;
my $impotent = 0;

# Protos
sub fatal($);

#
# Configure variables
#
my $TB		 = "@prefix@";
my $PRERESERVE   = "$TB/sbin/prereserve";
my $TBOPS        = "@TBOPSEMAIL@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use libtestbed;
use emutil;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
usage()
    if (@ARGV);

#
# Look for active pre reserves that need to be terminated.
#
my $query_result =
89
    DBQueryFatal("select * from project_reservations ".
90
		 "where end is not null and ".
91
		 "      UNIX_TIMESTAMP(now()) > UNIX_TIMESTAMP(end)");
92 93

while (my $row = $query_result->fetchrow_hashref()) {
94 95
    my $pid     = $row->{'pid'};
    my $resname = $row->{'name'};
96 97

    if ($debug) {
98
	print "Terminating pre-reserve $pid,$resname\n";
99 100
    }
    if (!$impotent) {
101
	my $output = emutil::ExecQuiet("$PRERESERVE -c -r -n $resname $pid");
102
	if ($?) {
103 104
	    print STDERR
		"Error terminating pre reservation $pid,$resname!\n";
105

106 107 108
	    SENDMAIL($TBOPS,
		     "Error Terminating pre reservation $pid,$resname",
		     "The pre reservation request $pid,$resname ".
109 110 111
		     "could not be terminated!\n\n" . $output . "\n", $TBOPS);
	}
	else {
112 113 114
	    SENDMAIL($TBOPS,
		     "Pre Reservation $pid,$resname has been terminated",
		     "The pre reservation $pid,$resname ".
115 116 117 118 119 120 121 122 123
		     "has been terminated\n", $TBOPS);
	}
    }
}

#
# Look for inactive pre reserves that need to be activated.
#
$query_result =
124
    DBQueryFatal("select * from project_reservations ".
125
		 "where active=0 and terminal=0 and start is not null and ".
126
		 "       UNIX_TIMESTAMP(now()) >= UNIX_TIMESTAMP(start) ".
127 128 129
		 "order by priority desc, created asc");

while (my $row = $query_result->fetchrow_hashref()) {
130 131
    my $pid     = $row->{'pid'};
    my $resname = $row->{'name'};
132 133

    if ($debug) {
134
	print "Activating pre-reserve $pid,$resname\n";
135 136
    }
    if (!$impotent) {
137
	my $output = emutil::ExecQuiet("$PRERESERVE -a -n $resname $pid");
138
	if ($?) {
139
	    print STDERR "Error activating pre reservation $pid,$resname!\n";
140
	    
141 142
	    SENDMAIL($TBOPS, "Error activating pre reservation $pid,$resname",
		     "The pre reservation request $pid,$resname ".
143 144 145 146 147 148 149 150 151 152 153 154 155
		     "could not be activated!\n\n" . $output . "\n", $TBOPS);
	}
    }
}

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}