mkexpdir.in 2.82 KB
Newer Older
1 2
#!/usr/bin/perl -wT
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
use Getopt::Std;
4 5 6

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

16
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
17 18 19 20 21 22 23
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
#
# Should be configured.
# 
24
my $projroot = "/proj";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25 26 27 28 29 30 31 32

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

33
my $tbdata   = "tbdata";
34
my @dirlist  = ($tbdata, "bin", "tmp", "logs", "tftpboot");
35

Leigh B. Stoller's avatar
Leigh B. Stoller committed
36 37 38 39 40
#
# Turn off line buffering on output
#
$| = 1;

41 42 43 44 45 46 47 48 49
#
# 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
50 51
if (@ARGV != 3) {
    usage();
52
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53 54 55
my $pid   = $ARGV[0];
my $gid   = $ARGV[1];
my $eid   = $ARGV[2];
56 57 58 59

#
# Untaint the arguments.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60 61 62 63 64
if ($pid =~ /^([-\@\w.]+)$/) {
    $pid = $1;
}
else {
    die("Tainted project name: $pid!\n");
65 66 67 68
}
if ($eid =~ /^([-\@\w.]+)$/) {
    $eid = $1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69 70 71 72 73 74 75 76 77
else {
    die("Tainted experiment name: $eid!\n");
}
if ($gid =~ /^([-\@\w.]+)$/) {
    $gid = $1;
}
else {
    die("Tainted group name: $gid!\n");
}
78

Leigh B. Stoller's avatar
Leigh B. Stoller committed
79
my $piddir  = "$projroot/$pid";
80 81 82
my $expdir  = "$piddir/exp";
my $eiddir  = "$expdir/$eid";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
83 84 85 86 87 88 89 90 91 92
#
# 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!");
}

93 94 95 96 97 98 99 100 101
#
# 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)) {
102
    print STDOUT "Could not mkdir $eid in $expdir: $!\n";
103 104 105 106
    exit(-1);
}

if (! chmod(0770, "$eid")) {
107
    print STDOUT "Could not chmod $eid to 0770 in $expdir: $!\n";
108 109 110 111
    rmdir($eid);
    exit(-1);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
112 113 114 115 116 117
if (! chown($UID, $unix_gid, "$eid")) {
    print STDOUT "Could not chown $eid to $UID/$unix_gid in $expdir: $!\n";
    rmdir($eid);
    exit(-1);
}

118
if (! chdir($eid)) {
119
    print STDOUT "Could not chdir to $eid in $expdir: $!\n";
120 121 122 123 124 125 126 127 128 129 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)) {
	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 B. Stoller's avatar
Leigh B. Stoller committed
139 140 141 142 143 144 145
#
# 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'");

146 147 148 149 150 151 152 153 154 155 156 157 158
exit(0);

sub fatal()
{
    if (! chdir($expdir)) {
	print STDOUT "In Fatal: Could not chdir to $expdir!\n";
	exit(-1);
    }
    system("rm -r $eid");

    exit(-1);
}