createlease.in 4.77 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 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 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 222 223 224 225 226 227
#!/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;

#
# Create a lease.
#
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";
    print STDERR "   -t type  Type of lease (stdataset or ltdataset)\n";
    print STDERR "   -o uid   Owner of lease (defaults to caller)\n";
    print STDERR "   -e date  Expiration date for lease\n";
    print STDERR "   -a attrs comma-seperated string of key=value attributes\n";
    print STDERR "   name     Name of lease (of form <pid>/<id>)\n";
    exit(-1);
}
my $optlist  = "dho:t:e:a:";
my $debug = 0;
my $pid;
my $uid;
my $expire;
my $ltype;
my $lname;
my $attrstr;
my %attrs = ();

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

# Protos
sub fatal($);

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

#
# 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:";

#
# 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})) {
    $expire = str2time($options{e});
    if (!$expire) {
	fatal("Could not parse expiration date.");
    }
}
if (defined($options{a})) {
    $attrstr = $options{a};
}

if (!$ltype || !$expire || @ARGV != 1) {
    print STDERR "Must specify type, expiration date, and lease name\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>.");
}

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)) {
	    fatal("No such user $uid\n");
	}
    } 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();
}

#
# Check project: user must be admin or have access to the project.
#
my $project = Project->Lookup($pid);
if (!defined($project)) {
    fatal("No such project $pid\n");
}
if (!TBAdmin() &&
    !$project->AccessCheck($this_user, PROJMEMBERTRUST_ROOT())) {
    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,
    "state" => "unapproved"
};

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");
}