createlease.in 5.07 KB
Newer Older
1 2 3 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
#!/usr/bin/perl -w
#
# Copyright (c) 2013 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 <http://www.gnu.org/licenses/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Date::Parse;

#
30
# Create a new lease.
31 32 33 34 35 36
#
sub usage()
{
    print STDERR "Usage: createlease [-hd] [-o uid] [-a attrs] -t type -e expiration name\n";
    print STDERR "   -h       This message\n";
    print STDERR "   -d       Print additional debug info\n";
37 38 39
    print STDERR "   -t type  Type (must be specified)\n";
    print STDERR "   -o uid   Owner (defaults to caller)\n";
    print STDERR "   -e date  Expiration date (or 'never')\n";
40
    print STDERR "   -a attrs comma-seperated string of key=value attributes\n";
41
    print STDERR "   name     Name (in the form <pid>/<id>)\n";
42 43 44 45 46 47 48 49 50
    exit(-1);
}
my $optlist  = "dho:t:e:a:";
my $debug = 0;
my $pid;
my $uid;
my $expire;
my $ltype;
my $lname;
51
my $attrstr = "";
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
my %attrs = ();

# Valid lease types
my %ltypes = (
    "stdataset" => "Short-term dataset",
    "ltdataset" => "Long-term dataset"
);

# Protos
sub fatal($);

#
# Configure variables
#
my $TB		 = "@prefix@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use Lease;
use Project;
use User;

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

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

87 88 89 90 91 92 93 94
#
# XXX this is out of date right now, "deprecated" in favor of
# createdataset since those are the only recognized lease types right
# now. When we have other lease types, this should be updated and
# generalized to handle quotas, etc.
#
fatal("Use 'createdataset' to create dataset leases.");

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#
# 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{o})) {
    $uid = $options{o};
}
if (defined($options{t})) {
    $ltype = $options{t};
}
if (defined($options{e})) {
116 117 118 119 120 121 122
    if ($options{e} eq "never") {
	$expire = 0;
    } else {
	$expire = str2time($options{e});
	if (!defined($expire)) {
	    fatal("Could not parse expiration date.");
	}
123 124 125 126 127 128
    }
}
if (defined($options{a})) {
    $attrstr = $options{a};
}

129 130
if (!$ltype || @ARGV != 1) {
    print STDERR "Must specify type and lease name\n";
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    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>.");
}

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

#
# Check owner: caller must be admin or be the specified user.
#
my $user;
if ($uid) {
    $user = User->Lookup($uid);
    if (TBAdmin()) {
	if (!defined($user)) {
156
	    fatal("No such user $uid");
157 158 159 160 161 162 163 164 165 166 167 168
	}
    } else {
	if (!$user || !$user->SameUser($this_user)) {
	    fatal("Must be admin to act on behalf of uid $uid");
	}
    }
} else {
    $user = $this_user;
    $uid = $this_user->uid();
}

#
169
# Check project: caller must be admin or have local_root access in the project.
170 171 172
#
my $project = Project->Lookup($pid);
if (!defined($project)) {
173
    fatal("No such project $pid");
174 175
}
if (!TBAdmin() &&
176
    !$project->AccessCheck($this_user, TB_PROJECT_CREATELEASE())) {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    fatal("Must have local_root privileges in pid $pid");
}

#
# Check type: currently only two defined.
#
if (!exists($ltypes{$ltype})) {
    print STDERR "Invalid lease type $ltype, should be one of:\n";
    foreach my $l (keys %ltypes) {
	print STDERR "'$l': ", $ltypes{$l}, "\n";
    }
    exit(1);
}

#
# Check expiration: must be in the future.
#
if ($expire < time()) {
    fatal("Expiration date is in the past!");
}

#
# Check attributes: right now, must just be a well-formed string.
#
foreach my $kv (split(',', $attrstr)) {
    if ($kv =~ /^([-\w]+)=([-\w\.\+\/:]+)$/) {
	$attrs{$1} = $2;
    } else {
	fatal("Malformed attribute string '$attrstr'.");
    }
}

#
# Check name: must not already exist.
#
if (Lease->Lookup($pid, $lname)) {
    fatal("Lease $pid/$lname already exists.");
}

my $args = {
    "lease_id" => $lname,
    "pid" => $project,
    "uid" => $user,
    "type" => $ltype,
    "lease_end" => $expire,
222
    "state" => LEASE_STATE_UNAPPROVED()
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
};

if (!Lease->Create($args, \%attrs)) {
    fatal("Could not create lease $lname in $pid.");
}

exit(0);

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

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