forked from ESGF/esgf-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esg-gitstrap
executable file
·707 lines (638 loc) · 19.9 KB
/
esg-gitstrap
1
2
3
4
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
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
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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/usr/bin/env perl
# -*-mode:cperl-*-
#
# esg-gitstrap
#
# This script clones all of the ESG git repositories and symlinks the
# relevant portions into standard locations. It is intended to
# replace esg-bootstrap for developers.
#
# Since it will create files in /usr/local, it must be run with root
# privileges.
#
# Since this script will be run on a bare-bones system with nothing
# but (and maybe not even) all of the requisite system packages
# installed, this script should never use any Perl module outside of
# the Perl core.
#
# If you are reading this because you want to update this script, you
# probably want to start at the "Global Variables" section below.
#
#
### Perl Critic Notes ###
#
## no critic qw(ErrorHandling::RequireCarping)
#
# This is a standalone script, not a module. Nobody should be calling
# our subroutines from outside this script, and again we need to avoid
# using external modules like Carp.
#
## no critic qw(InputOutput::ProhibitBacktickOperators)
#
# We want error dumps on STDERR when things fail. Debugging data
# should be as simple to capture as possible.
#
## no critic qw(Subroutines::RequireArgUnpacking)
#
# Argument arrays are being passed and used verbatim, unmodified in
# debugging subroutines. Perl Critic doesn't allow this to be
# overridden on a per-subroutine basis, so we have to just disable it
# globally.
#
## no critic qw(ValuesAndExpressions::ProhibitConstantPragma)
#
# We are using constants for exit values. These will never be
# interpolated, and a bootstrap script needs to not use any modules
# outside of core, so ReadOnly is unavailable.
#
## no critic qw(ValuesAndExpressions::ProhibitLeadingZeros)
#
# We are using octal in a few places for filesystem permissions that
# Perl Critic doesn't automatically recognize.
#
use strict; use warnings;
use English '-no_match_vars';
use Cwd qw(chdir cwd);
use File::Path qw(make_path);
use Getopt::Long qw(:config bundling);
###################
### Exit Values ###
###################
use constant EXIT_SUCCESS => 0;
use constant EXIT_INTERNALFAIL => 1;
use constant EXIT_EXTERNALFAIL => 2;
#######################
### Option Handling ###
#######################
my %opt = (
'debug' => 0,
'prefix' => '/usr/local',
'bindir' => 'bin',
'configdir' => 'esgf/config_defaults',
'installerdir' => 'esgf/installer',
'srcdir' => 'src/esgf',
'branch' => 'devel',
'force-branch' => 0,
'os' => detect_os(),
);
GetOptions(
\%opt,
'debug|v',
'prefix=s',
'bindir=s',
'configdir=s',
'installerdir=s',
'srcdir=s',
'branch=s',
'force-branch',
'os=s',
);
########################
### Global Variables ###
########################
local $ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";
my $prefix = $opt{'prefix'};
my $binpath = "$opt{'prefix'}/$opt{'bindir'}";
my $configpath = "$opt{'prefix'}/$opt{'configdir'}";
my $installerpath = "$opt{'prefix'}/$opt{'installerdir'}";
my $srcpath = "$opt{'prefix'}/$opt{'srcdir'}";
# System package prerequisites for a full build
my %packages = (
'RedHat' => [ "autoconf", "automake",
"bison",
"curl",
"file",
"flex",
"gcc", "gcc-c++",
"gettext-devel",
"git",
"libtool",
"libuuid-devel",
"libxml2", "libxml2-devel",
"libxslt", "libxslt-devel",
"lsof",
"make",
"openssl-devel",
"pam-devel",
"pax",
"postgresql",
"readline-devel",
"tk-devel",
"wget",
"zlib-devel",
],
'SuSE' => [ ],
'Debian' => [ ],
);
my %repoinfo = (
'esgf-installer' => {
'url' => 'git://github.com/ESGF/esgf-installer.git',
# Alternate installer path if you want to help with experimentation
#'url' => 'git://github.com/AZed/esgf-installer.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'experiment',
},
'binlinks' => [ "esg-functions",
"esg-gitstrap",
"esg-node",
"filters/esg-access-logging-filter",
"filters/esg-security-las-ip-filter",
"filters/esg-security-token-filters",
"filters/esg-security-tokenless-filters",
"globus/esg-globus",
"product-server/esg-product-server",
],
'configlinks' => [],
'installerlinks' => [ "esg-autoinstall-testnode", "esg-purge.sh"],
},
'esgf-dashboard' => {
'url' => 'git://github.com/ESGF/esgf-dashboard.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'devel',
},
'binlinks' => ["bin/esg-dashboard"],
'configlinks' => [],
'installerlinks' => [],
},
'esgf-drslib' => {
'url' => 'git://github.com/ESGF/esgf-drslib.git',
'branch' => {
'stable' => 'master',
'devel' => 'master',
'experiment' => 'master',
},
'binlinks' => [],
'configlinks' => [],
'installerlinks' => [],
},
'esgf-idp' => {
'url' => 'git://github.com/ESGF/esgf-idp.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'devel',
},
'binlinks' => ["bin/esg-idp"],
'configlinks' => [],
'installerlinks' => [],
},
'esgf-node-manager' => {
'url' => 'git://github.com/ESGF/esgf-node-manager.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'devel',
},
'binlinks' => ["bin/esg-node-manager","bin/esgf-sh","bin/esgf-spotcheck"],
'configlinks' => [],
'installerlinks' => [],
},
'esg-orp' => {
'url' => 'git://github.com/ESGF/esg-orp.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'devel',
},
'binlinks' => ["bin/esg-orp"],
'configlinks' => [],
'installerlinks' => [],
},
'esg-search' => {
'url' => 'git://github.com/ESGF/esg-search.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'devel',
},
'binlinks' => ["bin/esg-search","bin/esgf-crawl"],
'configlinks' => [],
'installerlinks' => [],
},
'esgf-security' => {
'url' => 'git://github.com/ESGF/esgf-security.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'devel',
},
'binlinks' => ["bin/esg-security","bin/esgf-user-migrate"],
'configlinks' => [],
'installerlinks' => [],
},
'esgf-web-fe' => {
'url' => 'git://github.com/ESGF/esgf-web-fe.git',
'branch' => {
'stable' => 'master',
'devel' => 'devel',
'experiment' => 'devel',
},
'binlinks' => ["bin/esg-web-fe"],
'configlinks' => [],
'installerlinks' => [],
},
);
####################
### Main Routine ###
####################
debug_print("Main routine begins");
umask 022;
install_packages();
foreach my $repo (keys %repoinfo) {
my $repobranch = $repoinfo{$repo}->{'branch'}->{$opt{'branch'}};
debug_print("now handling ${repo}");
git_clone($repo);
if($opt{'force-branch'}) {
unless($repobranch) {
print {*STDERR} "ERROR: no branch defined for repository ${repo} and branch type $opt{'branch'}!\n";
exit EXIT_INTERNALFAIL;
}
git_checkout($repo,$repobranch);
}
git_pull($repo);
setup_links($repo);
}
exit EXIT_SUCCESS;
########################
### Main Subroutines ###
########################
#
# sub install_packages
#
# Installs all of the prerequisite system packages if they are not
# already installed.
#
# Arguments: none
# Returns: 1 (true) on success, dies on failure
#
sub install_packages {
if($opt{'os'} eq 'RedHat') {
foreach my $package (@{$packages{'RedHat'}}) {
my $version = rpm_installed_version($package);
if(!$version) {
print "Package '${package}' not found, installing...";
yum_install($package);
print " done.\n";
}
}
}
else
{
print {*STDERR} "WARNING: package prerequisites are not handled for OS '$opt{'os'}'!\n";
print {*STDERR} "Skipping package validation -- you must verify this yourself.\n";
}
return 1;
}
#
# sub git_checkout($repo,$revision)
#
# Checks out a git repository to a specified revision (or branch or tag)
#
# Arguments:
# $repo: the repository name (this must be a key of the %repoinfo global hash)
# $revision: the branch name or specific revision to check out
#
# Returns:
# 1 (true) on success, dies on failure
#
sub git_checkout {
my ($repo,$revision) = @_;
my $repopath = "${srcpath}/${repo}";
my @systemcmd;
debug_print("checking out ${repo} at ${revision}");
if(!$repo) {
print {*STDERR} "git_checkout called with no repository specified!\n";
exit EXIT_INTERNALFAIL;
}
if(!$revision) {
print {*STDERR} "git_checkout called with no revision specified!\n";
exit EXIT_INTERNALFAIL;
}
if(! -d "${repopath}") {
print {*STDERR} "Cannot check out ${repo} at ${revision}!\n";
print {*STDERR} "The expected path '${repopath}' does not exist!\n";
exit EXIT_INTERNALFAIL;
}
if(! -d "${repopath}/.git") {
print {*STDERR} "Cannot check out ${repo} at ${revision}!\n";
print {*STDERR} "${repopath} is not a git repository!\n";
exit EXIT_INTERNALFAIL;
}
chdir($repopath)
or die("ERROR: unable to change to directory ${repopath}!");
@systemcmd = ('git','checkout',$revision);
system(@systemcmd);
system_result($CHILD_ERROR,$OS_ERROR,@systemcmd);
return 1;
}
#
# sub git_clone($repo)
#
# Clones a git repository into the source path, if a repository does not already exist there.
#
# Arguments:
# $repo: the repository name (this must be a key of the %repoinfo global hash)
#
# Returns:
# 1 (true) on success, dies on failure
#
sub git_clone {
my $repo = shift;
my $repopath = "${srcpath}/${repo}";
my @systemcmd;
debug_print("cloning ${repo}");
umask 022;
if (-e ${repopath}) {
if (-d ${repopath}) {
if (-d "${repopath}/.git") {
# Target directory is already a git repository. We
# assume it is set up in a desired fashion and return
# silently.
debug_print("${repo} already present, skipping");
return;
}
# Target directory exists, but is not a git repository.
print {*STDERR} "The destination directory for ${repo}",
" (${repopath}) exists, but is not a repository!\n";
print {*STDERR} "Please check your filesystem.\n";
exit EXIT_INTERNALFAIL;
}
# The target name exists, but isn't even a directory
print {*STDERR} "The destination name for ${repo}",
" (${repopath}) exists, but is not a directory!\n";
print {*STDERR} "Please check your filesystem.\n";
exit EXIT_INTERNALFAIL;
}
make_path($srcpath, { mode => 2755, });
chdir($srcpath)
or die("ERROR: unable to change to directory ${srcpath}!");
@systemcmd = ('git','clone',$repoinfo{$repo}->{'url'});
system(@systemcmd);
system_result($CHILD_ERROR,$OS_ERROR,@systemcmd);
# On a fresh clone, we should also check out to the expected branch
unless($repoinfo{$repo}->{'branch'}->{$opt{'branch'}}) {
print {*STDERR} "FATAL (git_clone): No branch is defined for repository ${repo}, branch type $opt{'branch'}!\n";
exit EXIT_INTERNALFAIL;
}
git_checkout($repo,$repoinfo{$repo}->{'branch'}->{$opt{'branch'}});
return 1;
}
#
# sub git_pull($repo)
#
# Pulls a repository up to the most recent revision for the branch it
# is in
#
# If you want to change branches, or switch to a specific revision,
# use git_checkout($repo) instead.
#
# Arguments:
# $repo: the repository name to handle
# (must be a key of the %repoinfo global hash)
#
# Returns:
# 1 (true) on success, dies on failure
#
sub git_pull {
my ($repo) = @_;
my $repopath = "${srcpath}/${repo}";
my @systemcmd;
debug_print("pulling latest revision in repository ${repo}");
umask 022;
if(! -d "${repopath}") {
print {*STDERR} "Cannot pull in ${repo}!\n";
print {*STDERR} "The expected path '${repopath}' does not exist!\n";
exit EXIT_INTERNALFAIL;
}
if(! -d "${repopath}/.git") {
print {*STDERR} "Cannot pull in ${repo}!\n";
print {*STDERR} "${repopath} is not a git repository!\n";
exit EXIT_INTERNALFAIL;
}
chdir($repopath)
or die("ERROR: unable to change to directory ${srcpath}!");
@systemcmd = ('git','pull','-q');
system(@systemcmd);
system_result($CHILD_ERROR,$OS_ERROR,@systemcmd);
return 1;
}
#
# sub setup_links($repo)
#
# Sets up the symlinks for a repository
#
# Arguments:
# $repo: the repository name to handle
# (must be a key of the %repoinfo global hash)
# Returns:
# 1 (true) on success, dies on failure
#
sub setup_links {
my $repo = shift;
debug_print("setting up links for ${repo}");
umask 022;
# Links that go into the bin path
create_repo_symlinks($repo,'binlinks',$binpath);
# Links that go into the default configuration file directory
create_repo_symlinks($repo,'configlinks',$configpath);
# Links that go into the installer source directory
create_repo_symlinks($repo,'installerlinks',$installerpath);
return 1;
}
##########################
### Helper Subroutines ###
##########################
#
# sub create_repo_symlinks($repo,$linktype,$linkpath)
#
# Helper function for setup_links() that actually creates the symlinks.
#
# Arguments:
# $repo: the repository to set up
# This must be a top-level entry in the %repoinfo hash.
# $linktype: the type of link being handled
# This must be a second-level array entry in the %repoinfo hash.
# $linkpath: the path into which the links will be installed
# This path will be created with mode 0755 if it does not exist.
#
# Returns:
# 1 (true) on success, dies on failure
#
sub create_repo_symlinks {
my ($repo,$linktype,$linkpath) = @_;
my $repopath = "${srcpath}/${repo}";
# Argument sanity checks
unless(ref($repoinfo{$repo}) eq 'HASH') {
print {*STDERR} "Unable to find repository info for ${repo}!\n";
exit EXIT_INTERNALFAIL;
}
unless(ref($repoinfo{$repo}->{$linktype}) eq 'ARRAY') {
print {*STDERR} "'${linktype}' is not a valid link type for repository ${repo}!\n";
exit EXIT_INTERNALFAIL;
}
unless($linkpath) {
print {*STDERR} "create_repo_symlinks called without a link path for ${repo}->${linktype}!\n";
exit EXIT_INTERNALFAIL;
}
umask 022;
make_path($linkpath, { mode => 0755, });
chdir($linkpath)
or die("ERROR: unable to change to directory ${linkpath}!");
foreach my $srcfile (@{$repoinfo{$repo}->{$linktype}}) {
my $destfile;
(undef,$destfile) = $srcfile =~ m|(.*/)?([-.\w]+)|x;
debug_print("handling ${linktype} symlink for ${srcfile} into ${linkpath}/${destfile}");
# We don't care if the unlink fails (file might not exist, and
# a failed unlink will cause the next symlink to fail, and
# that is tested)
unlink("${linkpath}/${destfile}");
symlink("${repopath}/${srcfile}","${linkpath}/${destfile}")
or die("Unable to create symlink from ${repopath}/${srcfile} into ${linkpath}/${destfile}!");
# Anything placed in the binlinks area should be executable
if($linktype eq 'binlinks') {
chmod(0755,"${repopath}/${srcfile}")
or die("Unable to set executable bit on binlink ${repopath}/${srcfile}!");
}
}
return 1;
}
#
# sub debug_print(@text)
#
# Prints a string to STDERR if $opt{'debug'} is true
# The text will be prefixed with "DEBUG: " and ended with a newline.
#
# Arguments:
# @text is passed to the print statement unmodified
#
# Returns 1 (true) on a successful print and undefined if
# $opt{'debug'} is not set.
# I'm not sure how it could fail.
#
sub debug_print {
return unless $opt{'debug'};
print {*STDERR} "DEBUG: ",@_,"\n";
return 1;
}
#
# sub detect_os()
#
# Attempts to detect the existing OS version
#
# This is called by the option parser, and thus sets the default, but
# the result can be overridden on the command line.
#
# This is a very simplistic detection, and easy to spoof. RedHat and
# CentOS are both treated as RedHat.
#
# Arguments: none
#
# Returns:
# One of the following strings, depending on what was detected:
# Debian
# RedHat
# SuSE
# Unknown
#
sub detect_os {
if(-e "/etc/redhat-release" or -e "/etc/centos-release") {
return "RedHat";
}
elsif(-e "/etc/debian_version") {
return "Debian";
}
elsif(-e "/etc/SuSE-release") {
return "SuSE";
}
return "Unknown";
}
#
# sub rpm_installed_version($package)
#
# Finds the version of an installed RPM package
#
# Arguments:
# $package: package name to check
#
# Returns:
# A string containing the RPM version if found, or undef otherwise
#
# Dies if the 'rpm' command fails
#
sub rpm_installed_version {
my ($package) = @_;
my $systemcmd;
my $version;
$systemcmd = "rpm -q --queryformat %{VERSION} ${package}";
$version = `${systemcmd}`;
# This command will return with error 1 if the package is not
# found, which is an expected case, so we're only going to handle
# a failure to execute, and not use system_result()
if($CHILD_ERROR == -1) {
print {*STDERR} "ERROR: failed to execute: '${systemcmd}'\n";
print {*STDERR} " Error message: ${OS_ERROR}\n";
exit EXIT_EXTERNALFAIL;
}
if($version =~ m/not installed/) {
$version = undef;
}
return $version;
}
#
# sub system_result($CHILD_ERROR,$OS_ERROR,@systemcmd)
#
# Result handler for system calls
#
# Arguments:
# $CHILD_ERROR: return value from previous system command (aka $?)
# $OS_ERROR: error value from previous system command (aka $!)
# @systemcmd: array passed to the system call
#
# Returns:
# 1 (true) if the return value was 0 (success)
# exits EXIT_EXTERNALFAIL after an error message on any error
#
sub system_result {
my $retval = shift;
my $error = shift;
my $cmdtext = join(' ',@_);
return 1 if ($retval == 0);
if ($retval == -1) {
print {*STDERR} "ERROR: failed to execute: '${cmdtext}'\n";
print {*STDERR} " Error message: ${error}\n";
exit EXIT_EXTERNALFAIL;
}
elsif ($retval & 127) {
printf {*STDERR} "ERROR: '${cmdtext}' died with signal %d, %s coredump\n",
($retval & 127), ($retval & 128) ? 'with' : 'without';
exit EXIT_EXTERNALFAIL;
}
else {
printf "ERROR: '${cmdtext}' exited with value %d\n", $retval >> 8;
print " CWD = ",cwd(),"\n";
exit EXIT_EXTERNALFAIL;
}
}
#
# sub yum_install($package)
#
# Installs a package via 'yum'
# This is only expected to work on RedHat/CentOS
#
# Arguments:
# $package: the package name to install
#
# Returns:
# 1 (true) on success, dies on failure.
#
sub yum_install {
my ($package) = @_;
my @systemcmd = ('yum','install','-q','-y',$package);
system(@systemcmd);
system_result($CHILD_ERROR,$OS_ERROR,@systemcmd);
return 1;
}