approvelease.in 5.44 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2013-2014 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 42 43
    print STDERR "   name       Name of lease (of form <pid>/<id>)\n";
    exit(-1);
}
44
my $optlist  = "dhs:w:D:";
45 46 47 48 49
my $debug = 0;
my $pid;
my $state = "valid";
my $lname;
my $lease;
50
my $waittime;
51
my $deny;
52 53 54

# Protos
sub fatal($);
55
sub notifyuser($$$);
56 57 58 59

#
# Configure variables
#
60 61
my $TB	     = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
62 63 64 65 66 67

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

#
# 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++;
}
if (defined($options{s})) {
    $state = $options{s};
}
100 101 102 103 104 105
if (defined($options{w})) {
    $waittime = $options{w};
    if ($waittime !~ /^\d+$/) {
	fatal("Wait time must be >= 0.");
    }
}
106 107 108
if (defined($options{D})) {
    $deny = $options{D};
}
109 110 111 112 113 114 115 116 117 118 119 120 121 122
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]+)$/) {
    $pid = $1;
    $lname = $2;
} else {
    fatal("Lease name $lname not in the form <pid>/<lname>.");
}

123
#
124 125
# Normally, leases are approved at creation time after quota checks.
# To approve other leases explicitly, you must be admin.
126
# 
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
if (!TBAdmin()) {
    fatal("Only admins can approve leases.");
}

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

my $project = Project->Lookup($pid);
if (!defined($project)) {
    fatal("No such project $pid\n");
}

#
# Check name: must exist, be modifiable and in the unapproved state.
#
$lease = Lease->Lookup($pid, $lname);
if (!$lease) {
    fatal("$pid/$lname: lease does not exist.");
}
if (!$lease->AccessCheck($this_user, LEASE_ACCESS_MODIFY())) {
    fatal("$pid/$lname: you are not allowed to modify lease.");
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
# Aquire the lease lock before we start making state changes.
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);
    }
    fatal("$pid/$lname: lease is in invalid state '$state'.");
}
if (!$lease->ValidTransition($state)) {
    fatal("$pid/$lname: cannot approve lease to state '$state'.");
}

180 181 182 183 184 185 186 187 188 189
# 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);
}

190 191
# Allocate the resources.
if ($lease->AllocResources($state)) {
192
    fatal("$pid/$lname: could not approve lease into state '$state'");
193 194
}

195
$lease->Unlock();
196 197 198 199 200 201 202
print "$pid/$lname: approved, state is now '$state'\n";

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

203 204
exit(0);

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
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);
    }
}

223 224 225 226
sub fatal($)
{
    my ($mesg) = $_[0];

227 228
    $lease->Unlock()
	if (defined($lease) && $lease->GotLock());
229 230 231
    die("*** $0:\n".
	"    $mesg\n");
}