-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcitus_package
executable file
·572 lines (439 loc) · 16.5 KB
/
citus_package
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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
use POSIX qw(setlocale LC_ALL);
use File::Temp qw(tempdir);
use List::Util qw(any none);
use Cwd qw(getcwd);
BEGIN { $Pod::Usage::Formatter = 'Pod::Text::Termcap'; }
use Pod::Usage qw(pod2usage);
# untaint environment
local $ENV{'PATH'} =
'/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin';
delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' };
use constant BAD_USAGE => 64; ## no critic (ProhibitConstantPragma)
use constant BAD_INPUT => 65; ## no critic (ProhibitConstantPragma)
use constant NO_SERVICE => 69; ## no critic (ProhibitConstantPragma)
use constant BAD_CONFIG => 78; ## no critic (ProhibitConstantPragma)
use constant FINGERPRINT => ## no critic (ProhibitConstantPragma)
'47EA 3DE1 08AB EA75 0F81 E34B 4BD9 69CA 3F95 D6C6';
use constant PACKAGE_URL => ## no critic (ProhibitConstantPragma)
'https://github.com/citusdata/packaging/archive';
my %supported_platforms = (
debian => [ "buster", "stretch", "jessie", "wheezy"],
el => [ "8", "7", "6" ],
fedora => [ "28", "27", "26"],
ol => [ "8", "7", "6" ],
ubuntu => [ "focal", "bionic", "xenial", "trusty", "noble" ],
pgxn => [""]
);
my @rh_flavors = qw(el fedora ol);
my %docker_names = (
debian => "debian",
el => "centos",
fedora => "fedora",
ol => "oraclelinux",
ubuntu => "ubuntu",
pgxn => "pgxn"
);
sub verify_platforms {
my (@platforms) = @_;
if ( @platforms == 0 ) {
pod2usage(
-msg => "You must specify at least one platform.",
-exitval => BAD_USAGE,
-verbose => 1
);
}
foreach my $platform (@platforms) {
my ( $os, $release ) = split( '/', $platform, 2 );
$release //= '';
if ( exists $supported_platforms{$os} ) {
my @releases = @{ $supported_platforms{$os} };
if ( none { $_ eq $release } @releases ) {
pod2usage(
-msg => "Unrecognized $os release: $release",
-exitval => BAD_INPUT,
-verbose => 99,
-sections => "SYNOPSIS|OPTIONS|SUPPORTED PLATFORMS"
);
}
}
else {
pod2usage(
-msg => "Unrecognized OS: $os",
-exitval => BAD_INPUT,
-verbose => 99,
-sections => "SYNOPSIS|OPTIONS|SUPPORTED PLATFORMS"
);
}
}
return;
}
sub get_and_verify_token {
unless ( exists $ENV{GITHUB_TOKEN} ) {
pod2usage(
-msg => "You must have a GITHUB_TOKEN set.",
-exitval => BAD_CONFIG,
-verbose => 99,
-sections => "ENVIRONMENT"
);
}
my $github_token = $ENV{GITHUB_TOKEN};
if ( $ENV{GITHUB_TOKEN} =~ /^(\w+)$/x ) {
$github_token = $1;
}
else {
pod2usage(
-msg => "Malformed GITHUB_TOKEN: $github_token",
-exitval => BAD_INPUT,
-verbose => 99,
-sections => "ENVIRONMENT"
);
}
my $cmd = "curl -sf -H 'Authorization: token $github_token' "
. 'https://api.github.com/';
my $result = `$cmd > /dev/null 2>&1`;
my $exit_code = $? >> 8;
if ( $exit_code == 22 ) {
pod2usage(
-msg => "Your token was rejected by GitHub.",
-exitval => BAD_INPUT,
-verbose => 99,
-sections => "ENVIRONMENT"
);
}
return $github_token;
}
sub verify_docker_running {
my $result = `docker info > /dev/null 2>&1`;
my $exit_code = $? >> 8;
unless ( $exit_code == 0 ) {
warn "Cannot connect to the Docker daemon. Is Docker running?\n";
exit NO_SERVICE;
}
return;
}
sub get_signing_creds {
my ( $secret_key, $passphrase );
if ( exists $ENV{PACKAGING_SECRET_KEY} ) {
$secret_key = $ENV{PACKAGING_SECRET_KEY};
}
else {
my $result =
`gpg --batch --fingerprint packaging\@citusdata.com 2> /dev/null`;
my $exit_code = $? >> 8;
my $fingerprint = FINGERPRINT;
unless ( $exit_code == 0 ) {
warn "Could not find signing key. Is gpg installed?\n";
}
if ( $result =~ /fingerprint.*\Q$fingerprint\E/x ) {
# remove spaces
$fingerprint =~ s/\s+//gx;
$secret_key =
`gpg --batch --export-secret-keys -a $fingerprint | base64`;
}
}
if ( exists $ENV{PACKAGING_PASSPHRASE} ) {
$passphrase = $ENV{PACKAGING_PASSPHRASE};
}
return ( $secret_key, $passphrase );
}
sub sign_packages {
my ( $pkgsdir, $secret_key, $passphrase ) = @_;
my @debfiles = glob("$pkgsdir/*/*.deb");
my @rpmfiles = glob("$pkgsdir/*/*.rpm");
my @base_args = (
qw(run --rm -v), "$pkgsdir:/packages",
'-e', "PACKAGING_SECRET_KEY",
'-e', 'PACKAGING_PASSPHRASE',
);
local $ENV{PACKAGING_SECRET_KEY} = $secret_key;
local $ENV{PACKAGING_PASSPHRASE} = $passphrase;
if ( @debfiles > 0 ) {
my @deb_args = @base_args;
push @deb_args, 'citusdata/packaging:debsigner';
system( 'docker', @deb_args );
if ( $? == -1 ) {
die "failed to execute: $!\n";
}
elsif ( $? & 127 ) {
die "child died with signal %d, %s coredump\n",
( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
my $exit_code = $? >> 8;
die "docker run failed. see output for details.\n" if $exit_code;
}
}
if ( @rpmfiles > 0 ) {
my @rpm_args = @base_args;
push @rpm_args, 'citusdata/packaging:rpmsigner';
system( 'docker', @rpm_args );
if ( $? == -1 ) {
die "failed to execute: $!\n";
}
elsif ( $? & 127 ) {
die "child died with signal %d, %s coredump\n",
( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
my $exit_code = $? >> 8;
die "docker run failed. see output for details.\n" if $exit_code;
}
}
return;
}
my ( @platforms, $project, $build_type, $opt_help );
GetOptions( 'p|platform=s' => \@platforms, 'help!' => \$opt_help )
or pod2usage(
-msg => "See '$0 --help' for more information.",
-exitval => BAD_USAGE
);
pod2usage( -verbose => 1 ) if $opt_help;
verify_platforms(@platforms);
if ( @ARGV != 2 ) {
pod2usage(
-msg => "You must specify a project and build type.",
-exitval => BAD_USAGE
);
}
( $project, $build_type ) = @ARGV;
verify_docker_running();
my $github_token = get_and_verify_token();
my $homedir = ( getpwuid($<) )[7];
my $tempdir = tempdir( ".citus_package.XXXXX", DIR => $homedir, CLEANUP => 1 );
my $pkgsdir = $tempdir . '/packages';
my $filedir = $tempdir . '/buildfiles';
mkdir $pkgsdir;
mkdir $filedir;
my $currentdir = getcwd();
local $ENV{GITHUB_TOKEN} = $github_token;
if ( any { $_ eq 'pgxn' } @platforms ) {
pod2usage(
-msg => "PGXN unsupported for project: $project",
-exitval => BAD_INPUT
) unless ( $project eq 'citus' or $project eq 'local' );
pod2usage(
-msg => "PGXN unsupported for build type: $build_type",
-exitval => BAD_INPUT
) unless ( $build_type eq 'release' );
}
foreach my $platform (@platforms) {
my ( $os, $release, $format );
if ( $platform =~ /^(\w+)\/(\w+)$/x ) {
$os = $1;
$release = $2;
$format = ( any { $_ eq $os } @rh_flavors ) ? 'redhat' : 'debian';
}
else {
$os = 'pgxn';
$release = '';
$format = 'pgxn';
}
my $filesubdir =
( $project eq 'local' ) ? $currentdir : $filedir . '/' . $format;
# if this format's build files don't exist yet, fetch them
unless ( -d $filesubdir ) {
mkdir $filesubdir;
my $downloadurl = PACKAGE_URL . "/$format-$project.tar.gz";
my $download = $filedir . '/' . "/$format-$project.tar.gz";
my $httpcode = `curl -sL '$downloadurl' -w '%{http_code}' -o $download`;
unless ( $httpcode == 200 ) {
pod2usage(
-msg =>
"Could not retrieve $format build files for '$project'.\n"
. "Received a $httpcode response from:\n"
. " $downloadurl",
-exitval => NO_SERVICE
);
}
# expand them directly into the target directory
my $untar = "tar xf '$download' -C '$filesubdir' --strip-components 1";
my $result = `$untar > /dev/null 2>&1`;
if ( $? == -1 ) {
die "failed to execute: $!\n";
}
elsif ( $? & 127 ) {
die "child died with signal %d, %s coredump\n",
( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
my $exit_code = $? >> 8;
die "build files tarball expansion failed!\n" if $exit_code;
}
}
# read release and nightly PG versions from pkgvars file
my @pg_versions;
if ( $format eq 'redhat' ) {
my $releasepg = '9.6,10';
my $nightlypg;
my $pkgvars = "$filesubdir/pkgvars";
open( my $fh, '<', $pkgvars ) ## no critic (RequireBriefOpen)
or die "opening pkgvars file failed!\n";
while (<$fh>) {
chomp;
my ( $key, $value ) = split /\s*=\s*/x;
if ( $key eq 'releasepg' ) {
$releasepg = $value;
}
elsif ( $key eq 'nightlypg' ) {
$nightlypg = $value;
}
}
close($fh);
$nightlypg //= $releasepg;
my $finalpg = ( $build_type eq 'release' ) ? $releasepg : $nightlypg;
$finalpg =~ s/(\d)\.?(\d)/pg$1$2/gx;
@pg_versions = split( /,/x, $finalpg );
}
else {
@pg_versions = qw (all);
}
my $docker_platform = $docker_names{$os};
$docker_platform .= "-$release" if $release;
my $outputdir = $pkgsdir . '/' . $docker_platform;
# create output directory to ensure we, and not the docker user, own it
mkdir $outputdir;
foreach my $pg (@pg_versions) {
if (($os eq "el" or $os eq "ol") and $release eq "6" and $pg eq "pg13")
{
# CentOS and OracleLinux 6 doesn't have pg13 packages yet
# so we dont't have package builder docker images yet.
# So skip building distro packages for them.
next;
}
my @docker_args = (
'run',
'--rm',
'-v',
"$outputdir:/packages",
'-v',
"$filesubdir:/buildfiles:ro",
'-e',
"GITHUB_TOKEN",
'-e',
"PACKAGE_ENCRYPTION_KEY",
'-e',
"UNENCRYPTED_PACKAGE",
"citus/packaging:$docker_platform-$pg",
$build_type
);
system( 'docker', @docker_args );
if ( $? == -1 ) {
die "failed to execute: $!\n";
}
elsif ( $? & 127 ) {
die "child died with signal %d, %s coredump\n",
( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
my $exit_code = $? >> 8;
die "docker run failed. see output for details.\n" if $exit_code;
}
}
}
my ( $secret_key, $passphrase ) = get_signing_creds();
if ( $secret_key and $passphrase ) {
sign_packages( $pkgsdir, $secret_key, $passphrase );
}
else {
warn "Could not get signing credentials. Skipping signing...\n";
}
system( 'mv', ( ( glob "$pkgsdir/*" ), $currentdir ) );
__END__
=head1 NAME
citus_package - easily create OS packages for Citus projects
=head1 SYNOPSIS
B<citus_package> [I<options>] I<project> I<build_type>
=head1 DESCRIPTION
Packages a Citus project for one or more platforms and places the results in
platform-specific directories within the working directory. B<citus_package>
uses Docker under the hood to ensure repeatable builds, so a working Docker
installation is the only prerequisite.
Given a Citus I<project> and I<build_type>, B<citus_package> will build one
package for a single platform, specified using the B<--platform> option. This
option can be provided multiple times in order to build a package for many
platforms at once. In addition, if the special value I<local> is provided as
the project name, B<citus_package> will use packaging files from the current
directory rather than fetching any particular project's files from GitHub.
The I<build_type> argument has two special values: I<release> and I<nightly>. A
release build is based on the latest release version (extracted from the build
files contained within the C<citusdata/packaging> GitHub repository), pulling
code from the corresponding git tag, which must be have a GitHub-verified
signature. A nightly build is based on the latest commit to the "active" branch
for a given project, which is usually C<master>, but can differ by project.
All other I<build_type> values are passed directly to GitHub, which is free to
interpret them how it sees fit, e.g. branch names, tags, or commit identifiers.
B<citus_package> uses the GitHub API to gather information about the project
it is building. As such, a valid C<GITHUB_TOKEN> environment variable must be
set. See the L</ENVIRONMENT> section for details.
=head1 OPTIONS
=over 4
=item B<-p> I<os/release>, B<--platform=>I<os/release>
Platform: required. Provide more than once for multi-platform builds
=back
=head1 ENVIRONMENT
For B<citus_package> to do its job, the C<GITHUB_TOKEN> environment variable
must be populated with a valid GitHub personal access token. It is recommended
that you add a line to your shell profile to ensure this variable is always
correctly set.
To generate a new access token, ensure you're logged into GitHub, then navigate
to your account settings. Choose "Personal access tokens" from the sidebar,
press the "Generate new token" button and name your token (like "packaging").
Ensure the top-level C<repo> and C<user> boxes are checked and press the
"Generate token" button.
B<Do not leave the next page until you've copied your new token!> Paste it
into your e.g. C<.bash_profile> or C<.zshrc> to ensure your shells will have
access to your new token.
B<citus_package> will attempt to sign any Linux packages it builds if the
C<PACKAGING_SECRET_KEY> and C<PACKAGING_PASSPHRASE> environment variables are
set. C<PACKAGING_SECRET_KEY> must be a base64-encoded PGP private key. The
passphrase needed to decrypt this key must be specified in the other variable,
C<PACKAGING_PASSPHRASE>.
If L<gpg(1)> is installed, B<citus_package> will search its keychain for the
Citus Data private key so that only the C<PACKAGING_PASSPHRASE> will be needed
to sign packages.
=head1 COMMON PROJECTS
=over 4
=item I<citus> Citus (Open-Source)
=item I<enterprise> Citus Enterprise
=item I<hll> HyperLogLog
=item I<rebalancer> Shard Rebalancer
=back
=head1 SUPPORTED PLATFORMS
=over 4
=item I<debian/buster> Debian 10 "Buster"
=item I<debian/stretch> Debian 9 "Stretch"
=item I<debian/jessie> Debian 8 "Jessie"
=item I<debian/wheezy> Debian 7 "Wheezy"
=item I<el/8> Enterprise Linux 8.0 (CentOS, RedHat, Amazon Linux)
=item I<el/7> Enterprise Linux 7.0 (CentOS, RedHat, Amazon Linux)
=item I<el/6> Enterprise Linux 6.0 (CentOS, RedHat, Amazon Linux)
=item I<fedora/28> Fedora 28
=item I<fedora/27> Fedora 27
=item I<fedora/26> Fedora 26
=item I<ol/8> Oracle Linux 8.0
=item I<ol/7> Oracle Linux 7.0
=item I<ol/6> Oracle Linux 6.0
=item I<ubuntu/focal> Ubuntu 20.04 LTS (Focal Fossa)
=item I<ubuntu/bionic> Ubuntu 18.04 LTS (Bionic Beaver)
=item I<ubuntu/xenial> Ubuntu 16.04 LTS (Xenial Xerus)
=item I<ubuntu/trusty> Ubuntu 14.04 LTS (Trusty Tahr)
=item I<pgxn> PostgreSQL Extension Network (PGXN)
=back
=head1 TODO
Eventually support a different output folder.
=head1 CAVEATS
Projects other than Citus may not use the PGXN platform. When building a PGXN
Citus package, only the I<release> build type may be used.
=head1 SEE ALSO
=over 4
=item L<GitHub Personal Access Tokens|https://github.com/settings/tokens>
=item L<Citus Packaging on GitHub|https://github.com/citusdata/packaging>
=item L<Citus Packaging on Docker Hub|https://hub.docker.com/r/citus/packaging>
=item L<Docker|https://www.docker.com>
=back
=head1 AUTHOR
Jason Petersen L<E<lt>[email protected]<gt>>