gitrepo.proxy.in 7.55 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use BSD::Resource;
use POSIX qw(:signal_h);
    
#
# Simply a wrapper for the geni-lib python environment
#
sub usage()
{
    print STDOUT
	"Usage: gitrepo.proxy -n reponame clone url\n".
	"Usage: gitrepo.proxy -n reponame update\n".
	"Usage: gitrepo.proxy -n reponame delete\n";
 
    exit(-1);
}
my $optlist   = "dn:";
my $debug    = 0;
my $reponame;

#
# Configure variables
#
my $TB         = "@prefix@";
my $TBOPS      = "@TBOPSEMAIL@";
52
my $REPODIR    = "/repos";
53 54 55 56 57 58 59 60 61 62
my $GIT        = "/usr/local/bin/git";

# Locals

# Protos
sub fatal($);
sub RunCommand($);
sub Clone();
sub Delete();
sub Update();
63 64
sub GetDefaultBranch($);
sub GetRemoteDefaultBranch($);
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

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

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

my $action   = shift(@ARGV);

if (! -e $REPODIR) {
    fatal("$REPODIR directory does not exist");
}

# Do not want to be asked for password, we want straight out failure.
$ENV{"GIT_ASKPASS"} = "/bin/true";

#
# Dispatch.
#
if ($action eq "clone") {
    exit(Clone());
}
elsif ($action eq "update") {
    exit(Update());
}
elsif ($action eq "delete") {
    exit(Delete());
}
else {
    fatal("Unknown command");
}
exit(-1);

#
# Clone a repo. 
#
sub Clone()
{
    usage()
	if (!@ARGV);
    my $repourl = shift(@ARGV);
    
    if (-e "$REPODIR/$reponame") {
	fatal("Repository has already been cloned");
    }
    chdir($REPODIR) or
	fatal("Could not chdir to $REPODIR");

    my $status = RunCommand("$GIT clone --bare ".
			    "   $repourl $reponame");
    if ($status) {
	fatal("Not able to clone repo from $repourl");
    }
    if (! -d "$REPODIR/$reponame") {
	fatal("Cannot find the git repo after cloning");
    }
    #
    # Make sure a source file exists, delete if not. The caller will
    # pick up the file via NFS if it needs it.
    #
    chdir("$REPODIR/$reponame") or
	fatal("Could not chdir to $REPODIR/$reponame");
154 155

    my $refspec = GetDefaultBranch($reponame);
156
    
157 158
    if (system("$GIT cat-file -e ${refspec}:profile.py") &&
	system("$GIT cat-file -e ${refspec}:profile.rspec")) {
159
	print STDERR "No geni-lib script or rspec in this repository\n";
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
    }
    return 0;
}

#
# Delete a repository.
#
sub Delete()
{
    if (! -e "$REPODIR/$reponame") {
	return 0;
    }
    system("/bin/rm -rf $REPODIR/$reponame");
    if ($?) {
	fatal("Not able to delete repository");
    }
    return 0;
}

#
# Update a repo. 
#
sub Update()
{
    if (! -e "$REPODIR/$reponame") {
	fatal("Repository does not exist.");
    }
    chdir("$REPODIR/$reponame") or
	fatal("Could not chdir to $REPODIR/$reponame");
189 190 191

    # -p prunes deleted branches. But tags are not pruned. 
    my $status = RunCommand("$GIT fetch -p -t origin '+refs/*:refs/*'");
192 193 194
    if ($status) {
	fatal("Not able to update repo");
    }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    my $current_refspec = GetDefaultBranch($reponame);
    my $remote_refspec  = GetRemoteDefaultBranch($reponame);

    #
    # Update local default branch if the remote has switched it.
    #
    if ($current_refspec ne $remote_refspec) {
	system("$GIT symbolic-ref --short HEAD '$remote_refspec'");
	if ($?) {
	    fatal("Could not update default branch tp $remote_refspec");
	}
	$current_refspec = $remote_refspec;
    }
    if (system("$GIT cat-file -e ${current_refspec}:profile.py") &&
	system("$GIT cat-file -e ${current_refspec}:profile.rspec")) {
211
	print STDERR "No geni-lib script or rspec in this repository\n";
212 213 214 215
    }
    return 0;
}

216 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
#
# Get default branch (refspec)
#
sub GetDefaultBranch($)
{
    my ($reponame) = @_;
    
    chdir("$REPODIR/$reponame") or
	fatal("Could not chdir to $REPODIR/$reponame");

    my $branch = `$GIT symbolic-ref --short HEAD`;
    if ($?) {
	fatal("Could not get default branch for $reponame");
    }
    chomp($branch);

    if ($branch =~ /^([-\w]+)$/) {
	$branch = $1;
    }
    else {
	fatal("Illegal default branch name: $branch");
    }
    return "refs/heads/$branch";
}

#
# Ask the remote for the default branch; user can change the default
# branch at the origin, but we will not know that after the initial
# clone unless we ask. 
#
sub GetRemoteDefaultBranch($)
{
    my ($reponame) = @_;
    my $branch;
    
    chdir("$REPODIR/$reponame") or
	fatal("Could not chdir to $REPODIR/$reponame");

    my $command = "$GIT remote show origin";

    #
    # This open implicitly forks a child, which goes on to execute the
    # command. The parent is going to sit in this loop and capture the
    # output of the child. We do this so that we have better control
    # over the descriptors.
    #
    my $pid = open(PIPE, "-|");
    if (!defined($pid)) {
	fatal("popen failed!");
    }
    if ($pid) {
	while (<PIPE>) {
	    if ($_ =~ /^\s*head branch:\s+([-\w]+)$/i) {
		$branch = $1;
	    }
	}
	if (!close(PIPE)) {
	    fatal("$command failed");
	}
    }
    else {
	open(STDERR, ">&STDOUT");
	exec($command);
    }
    if (!defined($branch)) {
	fatal("Could not get default branch from remote repo");
    }
    return "refs/heads/$branch";
}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
#
# Run git protected by CPU limit.
#
sub RunCommand($)
{
    my ($cmd) = @_;
    
    #
    # Fork a child process to run git in.
    #
    my $pid = fork();
    if (!defined($pid)) {
	fatal("Could not fork a new process!");
    }

    #
    # Child runs git, niced down, and then exits.
    #
    if (! $pid) {
	# Set the CPU limit for us.
	setrlimit(RLIMIT_CPU, 600, 600);
	
	# Give parent a chance to react.
	sleep(1);

	exec("nice -5 $cmd");
	die("Could not exec: '$cmd'!\n");
    }

    # Parent waits.
    waitpid($pid, 0);
    my $exit_status = $?;

    #
    # If the child was KILLed, then it overran its time limit.
    # Otherwise, exit with result of child.
    #
    if (($exit_status & 0xff) == SIGKILL) {
	print STDERR "git exceeded CPU limit\n";
	$exit_status = 15;
    }
    elsif ($exit_status & 0xff) {
	# Get the mapping from signal num. to name
	use Config;
	my (%sig_num, @sig_name);
	my @names = split ' ', $Config{sig_name};
	@sig_num{@names} = split ' ', $Config{sig_num};
	foreach (@names) {$sig_name[$sig_num{$_}] ||= $_}
    
	my $signal = $exit_status & 0x7f;
	my $signame = $sig_name[$signal];
	
	print STDERR "git died with SIG$signame.\n";
	$exit_status = 128 + $signal;
    }
    elsif ($exit_status) {
	$exit_status = $exit_status >> 8;
    }
    return $exit_status;
}

sub fatal($) {
    my ($mesg) = $_[0];

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}