#!/usr/bin/perl -wT # # EMULAB-COPYRIGHT # Copyright (c) 2000-2007 University of Utah and the Flux Group. # All rights reserved. # use English; use Getopt::Std; use strict; # # This gets invoked from the Web interface. CD into the proper directory # 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. # sub usage() { print STDOUT "Usage: mkexpdir \n"; exit(-1); } sub fatal($;$); # # Configure variables # my $TB = "@prefix@"; my $TBOPS = "@TBOPSEMAIL@"; # # Turn off line buffering on output # $| = 1; # # Untaint the path # $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; # # 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; # # Check args. # if (@ARGV != 1) { usage(); } my $experiment = Experiment->Lookup($ARGV[0]); if (!defined($experiment)) { tbdie("Could not lookup experiment object for $ARGV[0]!") } my $pid = $experiment->pid(); my $eid = $experiment->eid(); my $gid = $experiment->gid(); 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); # # Unix info for the group # my $group = $experiment->GetGroup(); if (!defined($group)) { tbdie("Could not lookup group object for $experiment!") } my $unix_gid = $group->unix_gid(); my $unix_name = $group->unix_name(); # # We create a directory structure for the experiment in the project directory. # if (! chdir($expdir)) { print "Could not chdir to $expdir: $!\n"; exit(-1); } if (! mkdir($eid, 0770)) { $exitval = $ERRNO; print "Could not mkdir $eid in $expdir: $!\n"; exit($exitval); } if (! chmod(0770, "$eid")) { $exitval = $ERRNO; print "Could not chmod $eid to 0770 in $expdir: $!\n"; rmdir($eid); exit($exitval); } if (! chown($UID, $unix_gid, "$eid")) { $exitval = $ERRNO; print "Could not chown $eid to $UID/$unix_gid in $expdir: $!\n"; rmdir($eid); exit($exitval); } if (! chdir($eid)) { $exitval = $ERRNO; print "Could not chdir to $eid in $expdir: $!\n"; rmdir($eid); exit($exitval); } # # Create the experiment directory list in the new directory. # foreach my $dir (@dirlist) { if (! mkdir($dir, 0770)) { fatal("Could not mkdir $dir in $eiddir: $!"); } if (! chmod(0770, "$dir")) { fatal("Could not chmod $dir to 0770 in $eiddir: $!"); } } # # Update the DB. This leaves the decision about where the directory # is created, in this script. # $experiment->Update({"path" => "$eiddir"}) == 0 or fatal("Could not update path for $experiment"); # # Create the working directory. # if (-e $workdir) { fatal("$workdir already exists!", -1); } if (! mkdir($workdir, 0775)) { fatal("Could not create $workdir: $!"); } 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 # directory to the group experiment directory. This is convenient so that # there is a common path for all experiments. # if ($pid ne $gid) { # XXX Bad. This link should be removed when the experiment is removed. unlink($eidlink) if (-l $eidlink); symlink($eiddir, $eidlink) or fatal("Could not symlink($eiddir, $eidlink): $!"); } exit(0); sub fatal($;$) { my ($msg, $exitval) = @_; $exitval = $ERRNO if (!defined($exitval)); system("/bin/rm -rf $eiddir"); system("/bin/rm -rf $workdir"); unlink($eidlink) if ($pid ne $gid && -l $eidlink); tberror($msg); exit($exitval); }