mkexpdir.in 3.99 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

#
# This gets invoked from the Web interface. CD into the proper directory
13 14
# 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.
15
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
16 17 18 19 20 21
sub usage()
{
    print STDOUT "Usage: mkexpdir <pid> <gid> <eid>\n";
    exit(-1);
}

22
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23 24 25 26 27 28 29
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
#
# Should be configured.
# 
30 31
my $projroot = PROJROOT();
my $grouproot= GROUPROOT();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32 33 34 35 36 37 38 39

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

40
my $tbdata   = "tbdata";
41
my @dirlist  = ($tbdata, "bin", "tmp", "logs", "tftpboot");
42
my $exitval;
43

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

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

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

87 88 89 90 91
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);
92

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

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

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

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

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

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

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

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

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

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

184 185
exit(0);

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

    $exitval = $ERRNO
	if (!defined($exitval));
192 193 194

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

198 199 200 201
    print STDERR "*** $0:\n".
	         "    $msg\n";

    exit($exitval);
202 203
}