fixexpinfo.in 5.29 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# Copyright (c) 2009 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
#
use English;
use Getopt::Std;

#
# Fix up the expinfo directory.
#
sub usage()
{
    print(STDOUT "Usage: fixexpinfo\n");
    exit(-1);
}
my $optlist   = "vdna";
my $debug     = 0;
my $verbose   = 0;
my $impotent  = 0;
my $archive   = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $EXPINFO     = "$TB/expinfo";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output. Very important for this script!
#
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use Project;
use Experiment;
use libtestbed;

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"v"})) {
    $verbose = 1;
}
if (defined($options{"a"})) {
    $archive = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
usage()
    if (@ARGV);

#
# Do not run this script unless its the real version.
#
if (!$impotent && $UID != 0) {
    die("*** $0:\n".
	"     Must runs this as root\n");
}

sub fatal($)
{
    my ($msg) = @_;

    die("$msg\n");
}

umask(0000);

#
# List of all projects.
#
my %projects = ();

my $query_result =
    DBQueryFatal("select pid from projects");

while (my ($pid) = $query_result->fetchrow_array()) {
    $projects{$pid} = $pid;
}
my $regexpplist = join("|", keys(%projects));

#
# Read the contents of the expinfo directory and generate new
# directories. 
#
opendir(DIR, $EXPINFO) or
    fatal("Unable to open directory $EXPINFO");
while (my $dirent = readdir(DIR)) {
    next
	if ($dirent eq "." || $dirent eq "..");

    my $pid;
    my $eid;
    my $idx;
	    
    if ($dirent =~ /^($regexpplist)\-(\w[-\w]*)\.(\d*)/) {
	$pid = $1;
	$eid = $2;
	$idx = $3;
    }
    elsif ($dirent =~
	   /^($regexpplist)\-([-\w]*\-\d\d\d\d\d\d\d\d\-\d\d\.\d\d\.\d\d)$/) {
	$pid = $1;
	$eid = $2;
    }
    elsif (exists($projects{$dirent})) {
	# SKip
	next;
    }
    else {
	print STDERR "Bad dir name $dirent. Skipping ...\n";
	next;
    }

    if ($archive) {
	# Archive only; leave current experiments alone.
	my $experiment = Experiment->Lookup($idx);
	next
	    if ($experiment);
    }

    if (!exists($projects{$pid})) {
	fatal("Inconsistency in project $pid: $dirent");
    }
    my $piddir  = "$EXPINFO/$pid";
    my $eiddir  = "$EXPINFO/$pid/$eid";
    my $project = Project->Lookup($pid);
    fatal("Could not find project record")
	if (!defined($project));
    
    my $unix_uid = $project->GetLeader()->unix_uid();
    my $unix_gid = $project->unix_gid();

    if (! -e $piddir) {
	if ($impotent) {
	    print STDERR "Would mkdir $piddir\n"
		if ($verbose);
	}
	else {
	    print STDERR "mkdir $piddir\n"
		if ($debug);
	    if (! mkdir($piddir, 0777)) {
		fatal("Could not create $piddir: $!");
	    }
	    if (! chown($unix_uid, $unix_gid, "$piddir")) {
		fatal("Could not chown $piddir to $UID/$unix_gid: $!");
	    }
	}
    }
    if (! -e $eiddir) {
	if ($impotent) {
	    print STDERR "Would mkdir $eiddir\n"
		if ($verbose);
	}
	else {
	    print STDERR "mkdir $eiddir\n"
		if ($debug);
	    if (! mkdir($eiddir, 0777)) {
		fatal("Could not create $eiddir: $!");
	    }
	    if (! chown($unix_uid, $unix_gid, "$eiddir")) {
		fatal("Could not chown $eiddir to $UID/$unix_gid: $!");
	    }
	}
    }
    if (-e "$eiddir/$idx") {
	fatal("$eiddir/$idx already exists! $dirent\n");
    }
    if ($impotent) {
	print STDERR "Would mv $dirent to $eiddir/$idx\n"
	    if ($verbose);
    }
    else {
	print STDERR "Moving $dirent to $eiddir/$idx\n"
	    if ($debug);
	if (!rename("$EXPINFO/$dirent", "$eiddir/$idx")) {
	    fatal("Could not rename $dirent to $eiddir/$idx\n");
	}
    }
}
closedir(DIR);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

#
# Make sure all projet directories are made.
#
foreach my $pid (keys(%projects)) {
    my $project = Project->Lookup($pid);
    fatal("Could not find project record")
	if (!defined($project));
    
    my $unix_uid = $project->GetLeader()->unix_uid();
    my $unix_gid = $project->unix_gid();
    my $piddir   = "$EXPINFO/$pid";

    if (! -e $piddir) {
	if ($impotent) {
	    print STDERR "Would mkdir $piddir\n"
		if ($verbose);
	}
	else {
	    print STDERR "mkdir $piddir\n"
		if ($debug);
	    if (! mkdir($piddir, 0777)) {
		fatal("Could not create $piddir: $!");
	    }
	    if (! chown($unix_uid, $unix_gid, "$piddir")) {
		fatal("Could not chown $piddir to $UID/$unix_gid: $!");
	    }
	}
    }
}