#!/usr/bin/perl -w # # Copyright (c) 2017-2018 University of Utah and the Flux Group. # # {{{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 . # # }}} # 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; use Reservation; use ResUtil; use User; use Project; # # 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); # 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. my $query_result = DBQueryFatal( "SELECT r.idx, r.pid, r.nodes, r.type, " . "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 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" . "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". "\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" . "feature!\n", $TBOPS); DBQueryFatal( "UPDATE future_reservations SET notified=NOW() WHERE idx=$idx" ); } $query_result = DBQueryFatal( "SELECT r.idx, r.pid, r.nodes, r.type, " . "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 )" ); 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 ); $start = TBDateStringUTC($start); $end = TBDateStringUTC($end); 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" . "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" . "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" . "feature!\n", $TBOPS); DBQueryFatal( "UPDATE future_reservations SET notified=NOW() WHERE idx=$idx" ); } # # 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(); $start = TBDateStringUTC($start); $end = TBDateStringUTC($end); 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); } # # 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", $TBOPS); } 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; } } } exit( 0 );