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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
6 7 8
# All rights reserved.
#

9
use English;
Leigh Stoller's avatar
Leigh Stoller committed
10
use Getopt::Std;
11 12 13

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

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

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

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

Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
58 59
if (@ARGV != 3) {
    usage();
60
}
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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
    print "Could not mkdir $eid in $expdir: $!\n";
113 114 115 116
    exit(-1);
}

if (! chmod(0770, "$eid")) {
117
    print "Could not chmod $eid to 0770 in $expdir: $!\n";
118 119 120 121
    rmdir($eid);
    exit(-1);
}

Leigh Stoller's avatar
Leigh Stoller committed
122
if (! chown($UID, $unix_gid, "$eid")) {
123
    print "Could not chown $eid to $UID/$unix_gid in $expdir: $!\n";
Leigh Stoller's avatar
Leigh Stoller committed
124 125 126 127
    rmdir($eid);
    exit(-1);
}

128
if (! chdir($eid)) {
129
    print "Could not chdir to $eid in $expdir: $!\n";
130 131 132 133 134 135 136 137 138
    rmdir($eid);
    exit(-1);
}

#
# Create the experiment directory list in the new directory.
#
foreach $dir (@dirlist) {
    if (! mkdir($dir, 0770)) {
139
	fatal("Could not mkdir $dir in $eiddir: $!");
140 141 142
    }

    if (! chmod(0770, "$dir")) {
143
	fatal("Could not chmod $dir to 0770 in $eiddir: $!");
144 145 146
    }
}

Leigh Stoller's avatar
Leigh Stoller committed
147 148 149 150 151 152 153
#
# 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'");

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

180 181
exit(0);

182
sub fatal($)
183
{
184 185 186 187
    my ($msg) = @_;

    system("/bin/rm -rf $eiddir");
    system("/bin/rm -rf $workdir");
188 189
    unlink($eidlink)
	if ($pid ne $gid && -l $eidlink);
190 191 192

    die("*** $0:\n".
	"    $msg\n");
193 194
}