mkexpdir.in 4.08 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 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", "tftpboot", "swapinfo");
44
my $exitval;
45

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

51 52 53 54 55 56 57 58 59
#
# 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
60 61
if (@ARGV != 3) {
    usage();
62
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63 64 65
my $pid   = $ARGV[0];
my $gid   = $ARGV[1];
my $eid   = $ARGV[2];
66 67 68 69

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

89 90 91 92 93
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);
94

Leigh B. Stoller's avatar
Leigh B. Stoller committed
95 96 97 98 99 100 101 102 103 104
#
# 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!");
}

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

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

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

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

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

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

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
153 154 155 156 157 158 159
#
# 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'");

160 161 162 163
#
# Create the working directory.
#
if (-e $workdir) {
164
    fatal("$workdir already exists!", -1);
165 166
}
if (! mkdir($workdir, 0775)) {
167
    fatal("Could not create $workdir: $!");
168
}
169 170 171 172 173 174
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
175 176
# directory to the group experiment directory. This is convenient so that
# there is a common path for all experiments. 
177
#
178
if ($pid ne $gid) {
179 180
    # XXX Bad. This link should be removed when the experiment is removed.
    unlink($eidlink)
181
	if (-l $eidlink);
182 183 184
    symlink($eiddir, $eidlink) or
	fatal("Could not symlink($eiddir, $eidlink): $!");
}
185

186 187
exit(0);

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

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

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

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

    exit($exitval);
204 205
}