notify-reservations.in 9.98 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2017-2018 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 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
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;

#
# Send e-mail about upcoming reservations.
#
sub usage()
{
    print STDERR "Usage: notify-reservations [-d] [-n]\n";
    exit( 1 );
}

my $optlist  = "dn";
my $debug    = 0;
my $impotent = 0;

#
# Configure variables
#
my $SITE  = "@THISHOMEBASE@";
my $TBOPS = "@TBOPSEMAIL@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use libtestbed;
use emutil;
54
use Reservation;
55 56 57
use ResUtil;
use User;
use Project;
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

#
# 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);

86 87 88 89
# Check for current reservations BEFORE the 24 hour warning... that way,
# if the 24 hour one hasn't been sent for some reason (e.g., boss was
# down for a long time or they scheduled a reservation starting almost
# immediately), then we'll nag them only once.
90
my $query_result = DBQueryFatal( "SELECT r.idx, r.pid, r.nodes, r.type, " .
91 92 93
				 "r.end, u.usr_email FROM " .
				 "future_reservations AS r, users AS u " .
				 "WHERE r.uid_idx=u.uid_idx AND " .
94
				 "r.approved IS NOT NULL AND " .
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
				 "(r.notified IS NULL OR " .
				 "r.notified < r.start) AND " .
				 "r.start <= NOW()" );

while( my ($idx, $pid, $count, $type, $end, $email) =
       $query_result->fetchrow_array() ) {
    print STDERR "Reservation $idx for project $pid, $count x $type " .
	"now-$end <$email> (now active!)\n" if( $debug );
    
    next if( $impotent );
    SENDMAIL( $email, "Reservation at $SITE is active",
	      "You have a current reservation at $SITE, with project\n" .
	      "$pid.  $count $type nodes are now set aside for your\n" .
	      "project, until $end.\n" .
	      "\n" .
	      "If you do not intend to use these resources, please\n" .
	      "cancel this reservation as soon as possible, since\n" .
	      "the nodes are now unavailable to other users for\n" .
113 114 115 116 117
	      "the duration of your reservation.\n".
	      "\n".
	      "Failure to use your reservation within six hours will result\n".
	      "in cancellation of the reservation so that the nodes are\n".
	      "available to other users.\n".
118 119 120 121 122 123 124 125 126 127 128
	      "\n" .
	      "Please note that we make no guarantees about the\n" .
	      "availability or suitability of these nodes for your\n" .
	      "experiment(s).\n" .
	      "\n" .
	      "PLEASE NOTE: Reservations are an experimental\n" .
	      "testbed feature under active development.  Until\n" .
	      "further notice, you should expect reservation\n" .
	      "system failures.  Please send reports about the\n" .
	      "reservation system to $TBOPS.\n" .
	      "Thank you for your assistance in debugging this\n" .
129 130
	      "feature!\n",
	      $TBOPS);
131 132 133 134
    DBQueryFatal( "UPDATE future_reservations SET notified=NOW() WHERE idx=$idx" );
}

$query_result = DBQueryFatal( "SELECT r.idx, r.pid, r.nodes, r.type, " .
135 136 137 138 139 140
			      "r.start, r.end, u.usr_email FROM " .
			      "future_reservations AS r, users AS u " .
			      "WHERE r.uid_idx=u.uid_idx AND " .
			      "r.approved IS NOT NULL AND " .
			      "r.notified IS NULL AND " .
			      "r.start <= ADDDATE( NOW(), 1 )" );
141 142 143 144 145

while( my ($idx, $pid, $count, $type, $start, $end, $email) =
       $query_result->fetchrow_array() ) {
    print STDERR "Reservation $idx for project $pid, $count x $type from " .
	"$start-$end <$email>\n" if( $debug );
146 147 148

    $start = TBDateStringUTC($start);
    $end   = TBDateStringUTC($end);
149 150 151 152 153 154 155 156 157 158 159 160 161
    
    next if( $impotent );
    SENDMAIL( $email, "Upcoming $SITE reservation",
	      "You have a reservation scheduled at $SITE, with project\n" .
	      "$pid.  $count $type nodes will be set aside for your\n" .
	      "project, starting at $start and ending at\n" .
	      "$end.\n" .
	      "\n" .
	      "If you do not intend to use these resources, please\n" .
	      "cancel this reservation as soon as possible, since\n" .
	      "the nodes will be unavailable to other users for\n" .
	      "the duration of your reservation.\n" .
	      "\n" .
162 163 164 165
	      "Failure to use your reservation within six hours will result\n".
	      "in cancellation of the reservation so that the nodes are\n".
	      "available to other users.\n".
	      "\n" .
166 167 168 169 170 171 172 173 174 175
	      "Please note that we make no guarantees about the\n" .
	      "availability or suitability of these nodes for your\n" .
	      "experiment(s).\n" .
	      "\n" .
	      "PLEASE NOTE: Reservations are an experimental\n" .
	      "testbed feature under active development.  Until\n" .
	      "further notice, you should expect reservation\n" .
	      "system failures.  Please send reports about the\n" .
	      "reservation system to $TBOPS.\n" .
	      "Thank you for your assistance in debugging this\n" .
176 177
	      "feature!\n",
	      $TBOPS);
178 179 180
    DBQueryFatal( "UPDATE future_reservations SET notified=NOW() WHERE idx=$idx" );
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#
# Handle cancellations. 
#

$query_result = DBQueryFatal( "SELECT r.idx, r.pid, r.nodes, r.type, " .
			      "r.start, r.end, u.usr_email, r.cancel FROM " .
			      "future_reservations AS r, users AS u " .
			      "WHERE r.uid_idx=u.uid_idx AND " .
			      "r.cancel IS NOT NULL AND " .
			      "r.cancel < NOW()" );

while( my ($idx, $pid, $count, $type, $start, $end, $email, $cancel) =
       $query_result->fetchrow_array() ) {
    print STDERR "Reservation $idx for project $pid, $count x $type from " .
	"$start-$end <$email> canceled\n" if( $debug );

    my $res = Reservation->Lookup($idx);
    next if (!defined($res)); 

    next if( $impotent );

    $res->Cancel();
    
204 205 206
    $start = TBDateStringUTC($start);
    $end   = TBDateStringUTC($end);

207 208 209 210 211 212 213
    SENDMAIL( $email, "$SITE reservation",
	      "Your reservation for $count $type nodes in project $pid,\n" .
	      "starting at $start and ending at $end,\n".
	      "was canceled at $cancel, and has now been removed.\n",
	      $TBOPS);
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301


#
# Handle reservations that are unused within the first six hours. The
# idea is that we start check after four hours. If idle we mark it for
# cancel in two hours, and then if they start using it, we cancel the
# cancelation. We stop checking after a while, in case we missed it
# cause the testbed was down; do not want to keep checking cause its
# expensive.
#
$query_result = DBQueryFatal("SELECT r.idx,".
			     "       TIMESTAMPDIFF(MINUTE, r.start, now())/60 ".
			     "    from future_reservations as r ".
			     "where r.approved IS NOT NULL AND " .
			     "      r.start <= NOW() AND ".
			     "      r.override_unused=0 AND ".
			     "      (TIMESTAMPDIFF(MINUTE, r.start, now()) > ".
			     "       (6 * 60)) AND ".
			     "      (TIMESTAMPDIFF(MINUTE, r.start, now()) < ".
			     "       (10 * 60))");

while (my ($idx, $hours) = $query_result->fetchrow_array()) {
    my $res = Reservation->Lookup($idx);
    next
	if (!defined($res));

    my $project = Project->Lookup($res->pid());
    next
	if (!defined($project));

    eval {
	my @records = CreateTimeline($project, $res);

	ReservationUtilization($res, @records);
    };
    if ($@) {
	print STDERR $@;
	next;
    }
    my $utilization = $res->data("utilization");
    print STDERR "$res - utilization:$utilization\n"
	if ($debug);

    if ($utilization == 0.0) {
	# Skip if cancel already set; do not interfere with a command
	# line operation that set the cancel.
	next
	    if ($res->cancel());
	
	# Mark for cancelation.
	print "Marking unused reservation for cancellation.\n"
	    if ($debug);
	next
	    if ($impotent);
	my $cancel = time() + (3 * 3600);

	while (1) {
	    if (!defined(Reservation->
			 BeginTransaction(Reservation->GetVersion()))) {
		sleep(1);
		next;
	    }
	    $res->MarkUnused($cancel);
	    Reservation->EndTransaction();
	    last;
	}
	my $user = User->Lookup($res->uid());
	if (!defined($user)) {
	    print STDERR "Could not lookup user " . $res->uid() ."\n";
	    next;
	}
	my $email = $user->email();
	my $count = $res->nodes();
	my $pid   = $res->pid();
	my $type  = $res->type();
	my $start = TBDateStringUTC($res->start());
	my $end   = TBDateStringUTC($res->end());
	$cancel   = TBDateStringUTC($cancel);
	
	SENDMAIL($email, "$SITE reservation",
	      "Your reservation for $count $type nodes in project $pid,\n" .
	      "starting at $start and ending at $end,\n".
	      "has not been used since it started.\n".
	      "\n".
	      "We have marked your reservation for cancellation at $cancel.\n".
	      "\n".
	      "If you use your reservation before then, we will rescind\n".
	      "the cancellation.\n",
302
	      $TBOPS, "Bcc: $TBOPS");
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    }
    elsif ($res->notified_unused()) {
	#
	# We only cancel our own cancellation. If the command line tool
	# aborts the cancel (or even sets the cancel), we view that as an
	# override on our cancel (the notified_unused flag is cleared on
	# that path, and override_unused is set).
	#
	print "Clearing unused reservation cancellation.\n"
	    if ($debug);
	next
	    if ($impotent);
	
	while (1) {
	    if (!defined(Reservation->
			 BeginTransaction(Reservation->GetVersion()))) {
		sleep(1);
		next;
	    }
	    $res->ClearUnused();
	    Reservation->EndTransaction();
	    last;
	}
    }
}
			     
329
exit( 0 );