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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
6 7
# All rights reserved.
#
8
use English;
Leigh Stoller's avatar
Leigh Stoller committed
9
use Getopt::Std;
10
use strict;
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
sub usage()
{
19
    print STDOUT "Usage: mkexpdir <eid>\n";
Leigh Stoller's avatar
Leigh Stoller committed
20 21
    exit(-1);
}
22

23
sub fatal($;$);
Leigh Stoller's avatar
Leigh Stoller committed
24

25
#
Leigh Stoller's avatar
Leigh Stoller committed
26 27 28 29
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
30

Leigh Stoller's avatar
Leigh Stoller committed
31 32 33 34 35
#
# Turn off line buffering on output
#
$| = 1;

36 37 38 39 40 41
#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
use Experiment;

# Locals
my $projroot = PROJROOT();
my $grouproot= GROUPROOT();
my $tbdata   = "tbdata";
my @dirlist  = ($tbdata, "bin", "tmp", "logs", "archive", "datastore",
		"tftpboot", "swapinfo");
my $exitval;

59 60 61
#
# Check args.
# 
62
if (@ARGV != 1) {
Leigh Stoller's avatar
Leigh Stoller committed
63
    usage();
64
}
65 66 67
my $experiment = Experiment->Lookup($ARGV[0]);
if (!defined($experiment)) {
    tbdie("Could not lookup experiment object for $ARGV[0]!")
Leigh Stoller's avatar
Leigh Stoller committed
68
}
69 70 71
my $pid       = $experiment->pid();
my $eid       = $experiment->eid();
my $gid       = $experiment->gid();
72 73 74 75 76
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);
77

Leigh Stoller's avatar
Leigh Stoller committed
78 79 80
#
# Unix info for the group
#
81 82 83
my $group = $experiment->GetGroup();
if (!defined($group)) {
    tbdie("Could not lookup group object for $experiment!")
Leigh Stoller's avatar
Leigh Stoller committed
84
}
85 86
my $unix_gid  = $group->unix_gid();
my $unix_name = $group->unix_name();
Leigh Stoller's avatar
Leigh Stoller committed
87

88 89 90 91
#
# We create a directory structure for the experiment in the project directory.
#
if (! chdir($expdir)) {
92
    print "Could not chdir to $expdir: $!\n";
93 94 95 96
    exit(-1);
}

if (! mkdir($eid, 0770)) {
97
    $exitval = $ERRNO;
98
    print "Could not mkdir $eid in $expdir: $!\n";
99
    exit($exitval);
100 101 102
}

if (! chmod(0770, "$eid")) {
103
    $exitval = $ERRNO;
104
    print "Could not chmod $eid to 0770 in $expdir: $!\n";
105
    rmdir($eid);
106
    exit($exitval);
107 108
}

Leigh Stoller's avatar
Leigh Stoller committed
109
if (! chown($UID, $unix_gid, "$eid")) {
110
    $exitval = $ERRNO;
111
    print "Could not chown $eid to $UID/$unix_gid in $expdir: $!\n";
Leigh Stoller's avatar
Leigh Stoller committed
112
    rmdir($eid);
113
    exit($exitval);
Leigh Stoller's avatar
Leigh Stoller committed
114 115
}

116
if (! chdir($eid)) {
117
    $exitval = $ERRNO;
118
    print "Could not chdir to $eid in $expdir: $!\n";
119
    rmdir($eid);
120
    exit($exitval);
121 122 123 124 125
}

#
# Create the experiment directory list in the new directory.
#
126
foreach my $dir (@dirlist) {
127
    if (! mkdir($dir, 0770)) {
128
	fatal("Could not mkdir $dir in $eiddir: $!");
129 130 131
    }

    if (! chmod(0770, "$dir")) {
132
	fatal("Could not chmod $dir to 0770 in $eiddir: $!");
133 134 135
    }
}

Leigh Stoller's avatar
Leigh Stoller committed
136 137 138 139
#
# Update the DB. This leaves the decision about where the directory
# is created, in this script. 
#
140 141
$experiment->Update({"path" => "$eiddir"}) == 0 or
    fatal("Could not update path for $experiment");
Leigh Stoller's avatar
Leigh Stoller committed
142

143 144 145 146
#
# Create the working directory.
#
if (-e $workdir) {
147
    fatal("$workdir already exists!", -1);
148 149
}
if (! mkdir($workdir, 0775)) {
150
    fatal("Could not create $workdir: $!");
151
}
152 153 154 155 156 157
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
158 159
# directory to the group experiment directory. This is convenient so that
# there is a common path for all experiments. 
160
#
161
if ($pid ne $gid) {
162 163
    # XXX Bad. This link should be removed when the experiment is removed.
    unlink($eidlink)
164
	if (-l $eidlink);
165 166 167
    symlink($eiddir, $eidlink) or
	fatal("Could not symlink($eiddir, $eidlink): $!");
}
168

169 170
exit(0);

171
sub fatal($;$)
172
{
173 174 175 176
    my ($msg, $exitval) = @_;

    $exitval = $ERRNO
	if (!defined($exitval));
177 178 179

    system("/bin/rm -rf $eiddir");
    system("/bin/rm -rf $workdir");
180 181
    unlink($eidlink)
	if ($pid ne $gid && -l $eidlink);
182

183
    tberror($msg);
184
    exit($exitval);
185 186
}