approvelease.in 6.79 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2013-2017 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
# 
# {{{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;
use Date::Parse;

#
# Approve a lease.
# This forces allocation of storage for dataset leases.
#
sub usage()
{
35
    print STDERR "Usage: approvelease [-hd] [-D reason] [-w waittime] [-s state] name\n";
36 37 38
    print STDERR "   -h         This message\n";
    print STDERR "   -d         Print additional debug info\n";
    print STDERR "   -s state   New state for the lease (defaults to 'valid')\n";
39 40
    print STDERR "   -w time    Try for up to time seconds to lock lease (0 means forever)\n";
    print STDERR "   -D reason  Deny the lease and destroy it\n";
41
    print STDERR "   -b         Allocate resources for approved leases in the background\n";
42 43 44
    print STDERR "   name       Name of lease (of form <pid>/<id>)\n";
    exit(-1);
}
45
my $optlist  = "dhs:w:D:b";
46
my $debug = 0;
47
my $background = 0;
48
my $pid;
49
my $gid;
50 51 52
my $state = "valid";
my $lname;
my $lease;
53
my $waittime;
54
my $deny;
55 56 57

# Protos
sub fatal($);
58
sub notifyuser($$$);
59 60 61 62

#
# Configure variables
#
63 64
my $TB	     = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
65
my $MANAGE_DATASET = "$TB/bin/manage_dataset";
66 67 68 69 70 71

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
72
use libtestbed;
73 74
use Lease;
use Project;
75
use Group;
76
use User;
77
use APT_Dataset;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

#
# 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{h})) {
    usage();
}
if (defined($options{d})) {
    $debug++;
}
103 104 105
if (defined($options{b})) {
    $background++;
}
106 107 108
if (defined($options{s})) {
    $state = $options{s};
}
109 110 111 112 113 114
if (defined($options{w})) {
    $waittime = $options{w};
    if ($waittime !~ /^\d+$/) {
	fatal("Wait time must be >= 0.");
    }
}
115 116 117
if (defined($options{D})) {
    $deny = $options{D};
}
118 119 120 121 122 123 124 125
if (@ARGV != 1) {
    print STDERR "Must specify exactly one lease.\n";
    usage();
}

# lease name must include a project
$lname = $ARGV[0];
if ($lname =~ /^([-\w]+)\/([-\w]+)$/) {
126
    $pid   = $gid = $1;
127
    $lname = $2;
128 129 130 131 132 133 134
}
elsif ($lname =~ /^([-\w]+)\/([-\w]+)\/([-\w]+)$/) {
    $pid   = $1;
    $gid   = $2;
    $lname = $3;
}
else {
135 136 137
    fatal("Lease name $lname not in the form <pid>/<lname>.");
}

138
#
139 140
# Normally, leases are approved at creation time after quota checks.
# To approve other leases explicitly, you must be admin.
141
# 
142 143 144 145 146 147 148 149 150 151 152 153
if (!TBAdmin()) {
    fatal("Only admins can approve leases.");
}

my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

#
# Check name: must exist, be modifiable and in the unapproved state.
#
154
$lease = Lease->Lookup($pid, $gid, $lname);
155 156 157
if (!$lease) {
    fatal("$pid/$lname: lease does not exist.");
}
158
my $aptdataset = APT_Dataset->LookupByRemoteUUID($lease->uuid());
159

160
# Acquire the lease lock before we start making state changes.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
if (!defined($waittime)) {
    fatal("$pid/$lname: could not acquire lock, try again with -w")
	if ($lease->Lock());
} else {
    fatal("$pid/$lname: could not acquire lock after $waittime seconds")
	if ($lease->WaitLock($waittime, 1));
}

# Sanity checks on the state.
if ($lease->state() ne LEASE_STATE_UNAPPROVED()) {
    #
    # XXX since we might have waited for the lock, it is possible that
    # someone beat us to the punch. Don't consider a current state of
    # "valid" or "locked" as an error.
    #
    if ($lease->state() eq LEASE_STATE_VALID() ||
	$lease->state() eq LEASE_STATE_LOCKED()) {
	$lease->Unlock();
	print "$pid/$lname: has already been approved.\n";
	exit(0);
    }
182
    fatal("$pid/$lname: lease is in invalid state '". $lease->state(). "'.");
183 184 185 186 187
}
if (!$lease->ValidTransition($state)) {
    fatal("$pid/$lname: cannot approve lease to state '$state'.");
}

188 189 190 191 192 193 194 195 196 197
# If we are denying, send a message to the owner and destroy the lease
if (defined($deny)) {
    print "$pid/$lname: denied, destroying\n";
    notifyuser($lease, 0, $deny);
    if ($lease->Delete()) {
	fatal("$pid/$lname: could not destroy lease.");
    }
    exit(0);
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
# Finally, allocate the resources.
my $logname;
if ($background) {
    print "Resource allocation proceeding the background ...\n";
    
    $logname = TBMakeLogname("approvelease");
    if (my $childpid = TBBackGround($logname)) {
	exit(0);
    }
    # We want the lock in the child.
    $lease->TakeLock();
    # Let parent exit;
    sleep(2);
}
my $rv = $lease->AllocResources($state);
if ($rv != LEASE_ERROR_NONE()) {
    my $msg = ($rv == LEASE_ERROR_ALLOCFAILED() ?
	       "Resource allocation failed" : "Unexpected failure");
    print STDERR "$msg, contact testbed-ops.\n";

    if ($background) {
	SENDMAIL($TBOPS, "Lease resources allocation failed during approval!",
		 "Background $msg for Lease '$pid/$gid/$lname'; ".
		 "lease left in failed state!\n\n",
		 $TBOPS, undef, $logname);
	unlink($logname);
    }

    $lease->UpdateState("failed");
227
    fatal("$pid/$lname: could not approve lease into state '$state'");
228
}
229 230 231
if ($background) {
    unlink($logname);
}
232 233 234 235 236 237 238 239 240
#
# If a Portal created dataset, approved from outside the Portal interface,
# we want to poke things so that the web UI sees the change. Not super
# important, I know. Ignore failure of course.
#
if (defined($aptdataset)) {
    system("$MANAGE_DATASET refresh -p ".
	   $aptdataset->pid() . "/" . $aptdataset->dataset_id());
}
241
$lease->Unlock();
242 243 244 245 246 247 248
print "$pid/$lname: approved, state is now '$state'\n";

#
# Send mail to the lease owner.
#
notifyuser($lease, 1, "");

249 250
exit(0);

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
sub notifyuser($$$)
{
    my ($lease,$approved,$msg) = @_;
    my $action = ($approved ? "approved" : "denied");

    my $user = User->LookupByUid($lease->owner());
    if ($user) {
	my $email = $user->email();
	my $pid = $lease->pid();
	my $lname = $lease->lease_id();
	SENDMAIL($email,
		 "Dataset lease $action",
		 "Your Emulab dataset lease $pid/$lname has been $action.\n".
		 "$msg.\n",
		 $TBOPS);
    }
}

269 270 271 272
sub fatal($)
{
    my ($mesg) = $_[0];

273 274
    $lease->Unlock()
	if (defined($lease) && $lease->GotLock());
275 276 277
    die("*** $0:\n".
	"    $mesg\n");
}