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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2009, 2011 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
my $INFODIR   = "$TB/expinfo";
78

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

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

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

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

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

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

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

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

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

144 145 146
#
# Create the working directory.
#
147 148 149 150 151 152 153
if (! -e $workdir) {
    if (! mkdir($workdir, 0775)) {
	fatal("Could not create $workdir: $!");
    }
    if (! chown($UID, $unix_gid, "$workdir")) {
	fatal("Could not chown $workdir to $UID/$unix_gid: $!");
    }
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169
#
# Create the expinfo directory.
#
my $expinfo = "$INFODIR/$pid/$eid";

if (! -e $expinfo) {
    if (! mkdir($expinfo, 0777)) {
	fatal("Could not create $expinfo: $!");
    }
    if (! chown($UID, $unix_gid, "$expinfo")) {
	fatal("Could not chown $expinfo to $UID/$unix_gid: $!");
    }
}

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

183 184
exit(0);

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

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

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

197
    tberror($msg);
198
    exit($exitval);
199 200
}