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

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# 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
my $projroot = "/proj";
Leigh Stoller's avatar
Leigh 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

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

48 49 50 51 52 53 54 55 56
#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Check args.
# 
Leigh Stoller's avatar
Leigh Stoller committed
57 58
if (@ARGV != 3) {
    usage();
59
}
Leigh Stoller's avatar
Leigh Stoller committed
60 61 62
my $pid   = $ARGV[0];
my $gid   = $ARGV[1];
my $eid   = $ARGV[2];
63 64 65 66

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

Leigh Stoller's avatar
Leigh Stoller committed
86
my $piddir  = "$projroot/$pid";
87 88
my $expdir  = "$piddir/exp";
my $eiddir  = "$expdir/$eid";
89
my $workdir = TBExptWorkDir($pid, $eid);
90

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

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

if (! mkdir($eid, 0770)) {
110
    print STDOUT "Could not mkdir $eid in $expdir: $!\n";
111 112 113 114
    exit(-1);
}

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

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

126
if (! chdir($eid)) {
127
    print STDOUT "Could not chdir to $eid in $expdir: $!\n";
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    rmdir($eid);
    exit(-1);
}

#
# Create the experiment directory list in the new directory.
#
foreach $dir (@dirlist) {
    if (! mkdir($dir, 0770)) {
	print STDOUT "Could not mkdir $dir in $eiddir: $!\n";
	fatal();
    }

    if (! chmod(0770, "$dir")) {
	print STDOUT "Could not chmod $dir to 0770 in $eiddir: $!\n";
	fatal();
    }
}

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 158 159 160 161 162 163 164 165
#
# Create the working directory.
#
if (-e $workdir) {
    fatal("*** $0:\n".
	  "    $workdir already exists!\n");
}
if (! mkdir($workdir, 0775)) {
    fatal("*** $0:\n".
	  "    Could not create $workdir: $!\n");
}

166 167 168 169
exit(0);

sub fatal()
{
170 171
    system("rm -r $eiddir");
    system("rm -r $workdir");
172 173 174
    exit(-1);
}