mkexpdir.in 4.11 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003, 2005, 2006 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7
# All rights reserved.
#
8
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
9
use Getopt::Std;
10

11 12 13 14 15 16 17
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

18 19
#
# This gets invoked from the Web interface. CD into the proper directory
20 21
# and do the tb stuff. This script cannot do any damage since it can
# only create directories where the caller has the permission to do so.
22
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23 24 25 26 27
sub usage()
{
    print STDOUT "Usage: mkexpdir <pid> <gid> <eid>\n";
    exit(-1);
}
28
sub fatal($;$);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29

30
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31 32 33 34 35 36 37
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
#
# Should be configured.
# 
38
# FIXME: Where are PROJROOT() and GROUPROOT() defined -- kevina
39 40
my $projroot = PROJROOT();
my $grouproot= GROUPROOT();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41

42
my $tbdata   = "tbdata";
43
my @dirlist  = ($tbdata, "bin", "tmp", "logs", "archive", "datastore",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
		"tftpboot", "swapinfo");
45
my $exitval;
46

Leigh B. Stoller's avatar
Leigh B. Stoller committed
47 48 49 50 51
#
# Turn off line buffering on output
#
$| = 1;

52 53 54 55 56 57 58 59 60
#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Check args.
# 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61 62
if (@ARGV != 3) {
    usage();
63
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
64 65 66
my $pid   = $ARGV[0];
my $gid   = $ARGV[1];
my $eid   = $ARGV[2];
67 68 69 70

#
# Untaint the arguments.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71 72 73 74 75
if ($pid =~ /^([-\@\w.]+)$/) {
    $pid = $1;
}
else {
    die("Tainted project name: $pid!\n");
76 77 78 79
}
if ($eid =~ /^([-\@\w.]+)$/) {
    $eid = $1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
80 81 82 83 84 85 86 87 88
else {
    die("Tainted experiment name: $eid!\n");
}
if ($gid =~ /^([-\@\w.]+)$/) {
    $gid = $1;
}
else {
    die("Tainted group name: $gid!\n");
}
89

90 91 92 93 94
my $piddir    = (($pid eq $gid) ? "$projroot/$pid" : "$grouproot/$pid/$gid");
my $expdir    = "$piddir/exp";
my $eiddir    = "$expdir/$eid";
my $eidlink   = "$projroot/$pid/exp/$eid";
my $workdir   = TBExptWorkDir($pid, $eid);
95

Leigh B. Stoller's avatar
Leigh B. Stoller committed
96 97 98 99 100 101 102 103 104 105
#
# Unix info for the group
#
my $unix_gid;
my $unix_name;

if (! TBGroupUnixInfo($pid, $gid, \$unix_gid, \$unix_name)) {
    die("*** No info for project/group $pid/$gid!");
}

106 107 108 109
#
# We create a directory structure for the experiment in the project directory.
#
if (! chdir($expdir)) {
110
    print "Could not chdir to $expdir: $!\n";
111 112 113 114
    exit(-1);
}

if (! mkdir($eid, 0770)) {
115
    $exitval = $ERRNO;
116
    print "Could not mkdir $eid in $expdir: $!\n";
117
    exit($exitval);
118 119 120
}

if (! chmod(0770, "$eid")) {
121
    $exitval = $ERRNO;
122
    print "Could not chmod $eid to 0770 in $expdir: $!\n";
123
    rmdir($eid);
124
    exit($exitval);
125 126
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
127
if (! chown($UID, $unix_gid, "$eid")) {
128
    $exitval = $ERRNO;
129
    print "Could not chown $eid to $UID/$unix_gid in $expdir: $!\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
130
    rmdir($eid);
131
    exit($exitval);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
132 133
}

134
if (! chdir($eid)) {
135
    $exitval = $ERRNO;
136
    print "Could not chdir to $eid in $expdir: $!\n";
137
    rmdir($eid);
138
    exit($exitval);
139 140 141 142 143 144 145
}

#
# Create the experiment directory list in the new directory.
#
foreach $dir (@dirlist) {
    if (! mkdir($dir, 0770)) {
146
	fatal("Could not mkdir $dir in $eiddir: $!");
147 148 149
    }

    if (! chmod(0770, "$dir")) {
150
	fatal("Could not chmod $dir to 0770 in $eiddir: $!");
151 152 153
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
154 155 156 157 158 159 160
#
# Update the DB. This leaves the decision about where the directory
# is created, in this script. 
#
DBQueryFatal("update experiments set path='$eiddir' ".
	     "where pid='$pid' and eid='$eid'");

161 162 163 164
#
# Create the working directory.
#
if (-e $workdir) {
165
    fatal("$workdir already exists!", -1);
166 167
}
if (! mkdir($workdir, 0775)) {
168
    fatal("Could not create $workdir: $!");
169
}
170 171 172 173 174 175
if (! chown($UID, $unix_gid, "$workdir")) {
    fatal("Could not chown $workdir to $UID/$unix_gid: $!");
}

#
# If a group experiment, leave behind a symlink from the project experiment
176 177
# directory to the group experiment directory. This is convenient so that
# there is a common path for all experiments. 
178
#
179
if ($pid ne $gid) {
180 181
    # XXX Bad. This link should be removed when the experiment is removed.
    unlink($eidlink)
182
	if (-l $eidlink);
183 184 185
    symlink($eiddir, $eidlink) or
	fatal("Could not symlink($eiddir, $eidlink): $!");
}
186

187 188
exit(0);

189
sub fatal($;$)
190
{
191 192 193 194
    my ($msg, $exitval) = @_;

    $exitval = $ERRNO
	if (!defined($exitval));
195 196 197

    system("/bin/rm -rf $eiddir");
    system("/bin/rm -rf $workdir");
198 199
    unlink($eidlink)
	if ($pid ne $gid && -l $eidlink);
200

201 202 203 204
    print STDERR "*** $0:\n".
	         "    $msg\n";

    exit($exitval);
205 206
}