forked from elastic/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_docs.pl
executable file
·966 lines (834 loc) · 32.1 KB
/
build_docs.pl
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
#!/usr/bin/env perl
# Flush on every print even if we're writing to a pipe (like docker).
$| = 1;
use strict;
use warnings;
use v5.10;
binmode( STDIN, ":utf8" );
binmode( STDOUT, ":utf8" );
binmode( STDERR, ":utf8" );
our ($Old_Pwd);
our @Old_ARGV = @ARGV;
use Cwd;
use FindBin;
use Data::Dumper;
BEGIN {
$Old_Pwd = Cwd::cwd();
chdir "$FindBin::RealBin/";
}
use lib 'lib';
use ES::Util qw(
run $Opts
build_chunked build_single build_pdf
proc_man
timestamp
write_html_redirect
write_nginx_redirects
write_nginx_test_config
write_nginx_preview_config
build_docs_js
);
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case no_getopt_compat);
use YAML qw(LoadFile);
use Path::Class qw(dir file);
use Browser::Open qw(open_browser);
use Sys::Hostname;
use ES::BranchTracker();
use ES::Repo();
use ES::Book();
use ES::TargetRepo();
use ES::Toc();
use ES::LinkCheck();
use ES::Template();
GetOptions($Opts, @{ command_line_opts() }) || exit usage();
check_opts();
our $Conf = LoadFile(pick_conf());
# The script supports running outside of docker, in any docker container *and*
# running in a docker container that we maintain. If we run in a docker
# container that we maintain then the script will change how it functions
# to support all of its command line arguments properly. At some point we
# will drop support for running outside of our docker image and this will
# always be true, but we aren't there yet.
our $running_in_standard_docker = $Opts->{in_standard_docker};
init_env();
$Opts->{template} = ES::Template->new(
%{ $Conf->{template} },
# We'd like to remove abs_urls entirely but we need it to support
# --open without docker and we need it to support sharing docs over
# firebase. Both of those are coming, but not here yet!
abs_urls => $Opts->{doc},
);
$Opts->{doc} ? build_local()
: $Opts->{all} ? build_all()
: $Opts->{preview} ? preview()
: usage();
#===================================
sub build_local {
#===================================
my $doc = $Opts->{doc};
my $index = file($doc)->absolute($Old_Pwd);
die "File <$doc> doesn't exist" unless -f $index;
return build_local_pdf($index) if $Opts->{pdf};
say "Building HTML from $doc";
my $dir = dir( $Opts->{out} || 'html_docs' )->absolute($Old_Pwd);
$Opts->{resource}
= [ map { dir($_)->absolute($Old_Pwd) } @{ $Opts->{resource} || [] } ];
_guess_opts_from_file($index);
if ( $Opts->{asciidoctor} && !$running_in_standard_docker ) {
die "--asciidoctor is only supported by build_docs and not by build_docs.pl";
}
build_docs_js();
my $latest = !$Opts->{suppress_migration_warnings};
if ( $Opts->{single} ) {
$dir->rmtree;
$dir->mkpath;
build_single( $index, $dir, %$Opts,
latest => $latest
);
}
else {
build_chunked( $index, $dir, %$Opts,
latest => $latest
);
}
say "Done";
my $html = $dir->file('index.html');
if ( $Opts->{open} ) {
say "Opening: " . $html;
serve_and_open_browser( $dir, 0 );
}
else {
say "See: $html";
}
}
#===================================
sub _guess_opts_from_file {
#===================================
my $index = shift;
my %edit_urls = ();
my $doc_toplevel = _find_toplevel($index->parent);
unless ( $doc_toplevel ) {
$Opts->{root_dir} = $index->parent;
# If we can't find the edit url for the document then we're never
# going to find it for anyone.
return;
}
$Opts->{root_dir} = $doc_toplevel;
my $edit_url = _guess_edit_url($doc_toplevel);
$edit_urls{ $doc_toplevel } = $edit_url if $edit_url;
for my $resource ( @{ $Opts->{resource} } ) {
my $resource_toplevel = _find_toplevel($resource);
next unless $resource_toplevel;
my $resource_edit_url = _guess_edit_url($resource_toplevel);
$edit_urls{ $resource_toplevel } = $resource_edit_url if $resource_edit_url;
}
$Opts->{edit_urls} = { %edit_urls };
}
#===================================
sub _find_toplevel {
#===================================
my $docpath = shift;
my $original_pwd = Cwd::cwd();
chdir $docpath;
my $toplevel = eval { run qw(git rev-parse --show-toplevel) };
chdir $original_pwd;
say "Couldn't find repo toplevel for $docpath" unless $toplevel;
return $toplevel;
}
#===================================
sub _guess_edit_url {
#===================================
my $toplevel = shift;
local $ENV{GIT_DIR} = dir($toplevel)->subdir('.git');
my $remotes = eval { run qw(git remote -v) } || '';
my $remote;
if ($remotes =~ m|\s+(\S+[/:]elastic(?:search-cn)?/\S+)|) {
$remote = $1;
# We prefer either an elastic or elasticsearch-cn organization. All
# but two books are in elastic but elasticsearch-cn is special.
} else {
say "Couldn't find edit url because there isn't an Elastic remote";
if ($remotes =~ m|\s+(\S+[/:]\S+/\S+)|) {
$remote = $1;
} else {
$remote = 'unknown';
}
}
my $branch = eval {run qw(git rev-parse --abbrev-ref HEAD) } || 'master';
return ES::Repo::edit_url_for_url_and_branch($remote, $branch);
}
#===================================
sub build_local_pdf {
#===================================
my $index = shift;
my $dir = dir( $Opts->{out} || './' )->absolute($Old_Pwd);
build_pdf( $index, $dir, %$Opts );
say "Done";
my $pdf = $index->basename;
$pdf =~ s/\.[^.]+$/.pdf/;
$pdf = $dir->file($pdf);
if ( $Opts->{open} ) {
say "Opening: $pdf";
open_browser($pdf);
}
else {
say "See: $pdf";
}
}
#===================================
sub build_all {
#===================================
die "--target_repo is required with --all" unless ( $Opts->{target_repo} );
my ( $repos_dir, $temp_dir, $reference_dir ) = init_dirs();
say "Updating repositories";
my $target_repo = init_target_repo( $repos_dir, $temp_dir, $reference_dir );
my $tracker = init_repos(
$repos_dir, $temp_dir, $reference_dir, $target_repo );
my $build_dir = $Conf->{paths}{build}
or die "Missing <paths.build> in config";
$build_dir = $target_repo->destination->subdir( $build_dir );
$build_dir->mkpath;
my $contents = $Conf->{contents}
or die "Missing <contents> configuration section";
my $toc = ES::Toc->new( $Conf->{contents_title} || 'Guide' );
my $redirects = $target_repo->destination->file( 'redirects.conf' );
if ( $Opts->{linkcheckonly} ){
say "Skipping documentation builds."
}
else {
build_docs_js();
say "Building docs";
build_entries( $build_dir, $temp_dir, $toc, @$contents );
say "Writing main TOC";
$toc->write( $build_dir, 0 );
say "Writing extra HTML redirects";
for ( @{ $Conf->{redirects} } ) {
write_html_redirect( $build_dir->subdir( $_->{prefix} ),
$_->{redirect} );
}
say "Writing nginx redirects";
write_nginx_redirects( $redirects, $build_dir, $temp_dir );
}
if ( $Opts->{skiplinkcheck} ) {
say "Skipped Checking links";
}
else {
say "Checking links";
check_links($build_dir);
}
$tracker->prune_out_of_date( @$contents );
$tracker->write;
push_changes( $build_dir, $target_repo ) if $Opts->{push};
serve_and_open_browser( $build_dir, $redirects ) if $Opts->{open};
$temp_dir->rmtree;
}
#===================================
sub check_links {
#===================================
my $build_dir = shift;
my $link_checker = ES::LinkCheck->new($build_dir);
$link_checker->check;
check_kibana_links( $build_dir, $link_checker ) if exists $Conf->{repos}{kibana};
if ( $link_checker->has_bad ) {
say $link_checker->report;
}
else {
die $link_checker->report;
}
}
#===================================
sub check_kibana_links {
#===================================
my $build_dir = shift;
my $link_checker = shift;
my $branch;
say "Checking Kibana links";
# ${baseUrl}guide/en/elasticsearch/reference/${urlVersion}/modules-scripting-expression.html
# ${ELASTIC_WEBSITE_URL}guide/en/beats/filebeat/${DOC_LINK_VERSION}
# ${ELASTIC_DOCS}search-aggregations-bucket-datehistogram-aggregation.html
my $extractor = sub {
my $contents = shift;
return sub {
while ( $contents =~ m!`(\$\{(?:baseUrl|ELASTIC_.+)\}[^`]+)`!g ) {
my $path = $1;
$path =~ s/\$\{(?:DOC_LINK_VERSION|urlVersion)\}/$branch/;
$path
=~ s!\$\{ELASTIC_DOCS\}!en/elasticsearch/reference/$branch/!
|| $path =~ s!\$\{(?:baseUrl|ELASTIC_WEBSITE_URL)\}guide/!!;
return ( split /#/, $path );
}
return;
};
};
my $src_path = 'src/ui/public/documentation_links/documentation_links';
my $legacy_path = 'src/legacy/ui/public/documentation_links/documentation_links';
my $repo = ES::Repo->get_repo('kibana');
my @branches = sort map { $_->basename }
grep { $_->is_dir } $build_dir->subdir('en/kibana')->children;
my $link_check_name = 'link-check-kibana';
for (@branches) {
$branch = $_;
next if $branch eq 'current' || $branch =~ /^\d/ && $branch lt 5;
say " Branch $branch";
my $links_file;
my $source = eval {
$links_file = $src_path . ".js";
$repo->show_file( $link_check_name, $branch, $links_file );
} || eval {
$links_file = $src_path . ".ts";
$repo->show_file( $link_check_name, $branch, $links_file );
} || eval {
$links_file = $legacy_path . ".js";
$repo->show_file( $link_check_name, $branch, $links_file );
} || eval {
$links_file = $legacy_path . ".ts";
$repo->show_file( $link_check_name, $branch, $links_file );
};
die "failed to find kibana links file;\n$@" unless $source;
$link_checker->check_source( $source, $extractor,
"Kibana [$branch]: $links_file" );
# Mark the file that we need for the link check done so we can use
# --keep_hash with it during some other build.
$repo->mark_done( $link_check_name, $branch, $links_file, 0 );
}
}
#===================================
sub build_entries {
#===================================
my ( $build, $temp_dir, $toc, @entries ) = @_;
while ( my $entry = shift @entries ) {
my $title = $entry->{title}
or die "Missing title for entry: " . Dumper($entry);
if ( my $sections = $entry->{sections} ) {
my $base_dir = $entry->{base_dir} || '';
my $section_toc = build_entries(
$build->subdir($base_dir), $temp_dir,
ES::Toc->new( $title, $entry->{lang} ), @$sections
);
if ($base_dir) {
$section_toc->write( $build->subdir($base_dir) );
$toc->add_entry(
{ title => $title,
url => $base_dir . '/index.html'
}
);
}
else {
$toc->add_entry($section_toc);
}
next;
}
my $book = ES::Book->new(
dir => $build,
template => $Opts->{template},
temp_dir => $temp_dir,
%$entry
);
$toc->add_entry( $book->build( $Opts->{rebuild} ) );
}
return $toc;
}
#===================================
sub build_sitemap {
#===================================
my ($dir) = @_;
my $sitemap = $dir->file('sitemap.xml');
say "Building sitemap: $sitemap";
open my $fh, '>', $sitemap or die "Couldn't create $sitemap: $!";
say $fh <<SITEMAP_START;
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
SITEMAP_START
my $date = timestamp();
my $add_link = sub {
my $file = shift;
my $url = 'https://www.elastic.co/guide/' . $file->relative($dir);
say $fh <<ENTRY;
<url>
<loc>$url</loc>
<lastmod>$date</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
ENTRY
};
$dir->recurse(
callback => sub {
my $item = shift;
return unless $item->is_dir && $item->basename eq 'current';
if ( -e $item->file('toc.html') ) {
my $content = $item->file('toc.html')
->slurp( iomode => '<:encoding(UTF-8)' );
$add_link->( $item->file($_) )
for ( $content =~ /href="([^"]+)"/g );
}
elsif ( -e $item->file('index.html') ) {
$add_link->( $item->file('index.html') );
}
return $item->PRUNE;
}
);
say $fh "</urlset>";
close $fh or die "Couldn't close $sitemap: $!"
}
#===================================
sub init_dirs {
#===================================
my $repos_dir = $Conf->{paths}{repos}
or die "Missing <paths.repos> in config";
$repos_dir = dir($repos_dir)->absolute;
$repos_dir->mkpath;
my $temp_dir = $running_in_standard_docker ? dir('/tmp/docsbuild') : $repos_dir->subdir('.temp');
$temp_dir = $temp_dir->absolute;
$temp_dir->rmtree;
$temp_dir->mkpath;
my $reference_dir = dir($Opts->{reference});
if ( $reference_dir ) {
$reference_dir = $reference_dir->absolute;
die "Missing reference directory $reference_dir" unless -e $reference_dir;
}
return ( $repos_dir, $temp_dir, $reference_dir );
}
#===================================
sub init_target_repo {
#===================================
my ( $repos_dir, $temp_dir, $reference_dir ) = @_;
my $target_repo = ES::TargetRepo->new(
dir => $repos_dir,
user => $Opts->{user},
url => $Opts->{target_repo},
reference => $reference_dir,
destination => dir( "$temp_dir/target_repo" ),
branch => $Opts->{target_branch} || 'master',
);
$target_repo->update_from_remote;
return $target_repo;
}
#===================================
sub init_repos {
#===================================
my ( $repos_dir, $temp_dir, $reference_dir, $target_repo ) = @_;
printf(" - %20s: Checking out minimal\n", 'target_repo');
$target_repo->checkout_minimal();
my %child_dirs = map { $_ => 1 } $repos_dir->children;
delete $child_dirs{ $temp_dir->absolute };
my $conf = $Conf->{repos}
or die "Missing <repos> in config";
my @repo_names = sort keys %$conf;
delete $child_dirs{ $target_repo->git_dir->absolute };
my $tracker_path = $Conf->{paths}{branch_tracker}
or die "Missing <paths.branch_tracker> in config";
$tracker_path = $target_repo->destination . "/$tracker_path";
# check out all remaining repos in parallel
my $tracker = ES::BranchTracker->new( file($tracker_path), @repo_names );
my $pm = proc_man( $Opts->{procs} * 3 );
unless ( $pm->start('target_repo') ) {
printf(" - %20s: Checking out remaining\n", 'target_repo');
$target_repo->checkout_all();
$pm->finish;
}
for my $name (@repo_names) {
my $url = $conf->{$name};
# We always use ssh-style urls regardless of conf.yaml so we can use
# our ssh key for the cloning.
$url =~ s|https://([^/]+)/|git\@$1:|;
my $repo = ES::Repo->new(
name => $name,
dir => $repos_dir,
tracker => $tracker,
user => $Opts->{user},
url => $url,
reference => $reference_dir,
keep_hash => $Opts->{keep_hash},
);
delete $child_dirs{ $repo->git_dir->absolute };
if ( $Opts->{linkcheckonly} ){
say "Skipping fetching repo $name."
}
else {
$pm->start($name) and next;
$repo->update_from_remote();
$pm->finish;
}
}
$pm->wait_all_children;
# Parse the --sub_dir options and attach the to the repo
my %sub_dirs = ();
foreach (@{ $Opts->{sub_dir} }) {
die "invalid --sub_dir $_"
unless /(?<repo>[^:]+):(?<branch>[^:]+):(?<dir>.+)/;
my $dir = dir($+{dir})->absolute;
die "--sub_dir $dir doesn't exist" unless -e $dir;
ES::Repo->get_repo($+{repo})->add_sub_dir($+{branch}, $dir);
}
for ( keys %child_dirs ) {
my $dir = dir($_);
next unless -d $dir;
say "Removing old repo <" . $dir->basename . ">";
$dir->rmtree;
}
return $tracker;
}
#===================================
sub preview {
#===================================
die "--target_repo is required with --preview" unless $Opts->{target_repo};
die "--preview is only supported by build_docs and not by build_docs.pl"
unless $running_in_standard_docker;
my $nginx_config = file('/tmp/nginx.conf');
write_nginx_preview_config( $nginx_config );
if ( my $node_pid = fork ) {
my ( $repos_dir, $temp_dir, $reference_dir ) = init_dirs();
say "Cloning built docs";
my $target_repo = init_target_repo( $repos_dir, $temp_dir, $reference_dir );
say "Built docs are ready";
if ( my $nginx_pid = fork ) {
$SIG{TERM} = sub {
# We should be a good citizen and shut down the subprocesses.
# This isn't so important in k8s or docker because we shoot
# the entire container when we're done, but it is nice when
# testing.
say 'Terminating preview services...nginx';
kill 'TERM', $nginx_pid;
wait;
say 'Terminating preview services...node';
kill 'TERM', $node_pid;
wait;
say 'Terminated preview services';
exit 0;
};
while (1) {
sleep 1;
my $fetch_result = $target_repo->fetch;
say "$fetch_result" if ( $fetch_result );
}
exit;
} else {
close STDIN;
open( STDIN, "</dev/null" );
exec( qw(node --max-old-space-size=128 /docs_build/preview/preview.js) );
}
} else {
close STDIN;
open( STDIN, "</dev/null" );
exec( qw(nginx -c), $nginx_config );
}
}
#===================================
sub push_changes {
#===================================
my ($build_dir, $target_repo ) = @_;
say 'Building revision.txt';
$build_dir->file('revision.txt')
->spew( iomode => '>:utf8', ES::Repo->all_repo_branches );
if ( $target_repo->outstanding_changes ) {
say 'Preparing commit';
build_sitemap($build_dir);
say "Commiting changes";
$target_repo->commit;
say "Pushing changes";
$target_repo->push_changes;
if ( $Opts->{announce_preview} ) {
say "A preview will soon be available at " .
$Opts->{announce_preview};
}
} else {
say "No changes to push";
}
}
#===================================
sub init_env {
#===================================
$ENV{SGML_CATALOG_FILES} = $ENV{XML_CATALOG_FILES} = join ' ',
file('resources/docbook-xsl-1.78.1/catalog.xml')->absolute,
file('resources/docbook-xml-4.5/catalog.xml')->absolute;
$ENV{PATH}
= dir('resources/asciidoc-8.6.8/')->absolute
. ":$FindBin::RealBin:"
. $ENV{PATH};
if ( $running_in_standard_docker ) {
if (exists $ENV{SSH_AUTH_SOCK}
&& $ENV{SSH_AUTH_SOCK} eq '/tmp/forwarded_ssh_auth') {
print "Waiting for ssh auth to be forwarded to " . hostname . "\n";
while (<>) {
# Read from stdin waiting for the signal that we're ready. We
# use stdin here because it prevents us from leaving the docker
# container running if something goes wrong with the forwarding
# process. The mechanism of action is that when something goes
# wrong build_docs will die, closing stdin. That will cause us
# to drop out of this loop and cause the process to terminate.
last if ($_ eq "ready\n");
}
die '/tmp/forwarded_ssh_auth is missing' unless (-e '/tmp/forwarded_ssh_auth');
print "Found ssh auth\n";
}
if ( $Opts->{preview} ) {
# `--preview` is run in k8s it doesn't *want* a tty
# so it should avoid doing housekeeping below.
return;
}
# If we're in docker we're relying on closing stdin to cause an orderly
# shutdown because it is really the only way for us to know for sure
# that the python build_docs process thats on the host is dead.
# Since perl's threads are "not recommended" we fork early in the run
# process and have the parent synchronously wait read from stdin. A few
# things can happen here and each has a comment below:
if ( my $child_pid = fork ) {
$SIG{CHLD} = sub {
# The child process exits so we should exit with whatever
# exit code it gave us. This can also come about because the
# child process is killed.
use POSIX ":sys_wait_h";
my $child_status = 'missing';
while ((my $child = waitpid(-1, WNOHANG)) > 0) {
my $status = $? >> 8;
if ( $child == $child_pid ) {
$child_status = $status;
} else {
# Some other subprocess died on us. The calling code
# will handle it.
}
}
exit $child_status unless ( $child_status eq 'missing');
};
$SIG{INT} = sub {
# We're interrupted. This'll happen if we somehow end up in
# the foreground. It isn't likely, but if it does happen we
# should interrupt the child just in case it wasn't already
# interrupted and then exit with whatever code the child exits
# with.
kill 'INT', $child_pid;
wait;
exit $? >> 8;
};
$SIG{TERM} = sub {
# We're terminated. We should pass on the love to the
# child process and return its exit code.
kill 'TERM', $child_pid;
wait;
exit $? >> 8;
};
while (<>) {}
# STDIN is closed. This'll happen if the python build_docs process
# on the host dies for some reason. When the host process dies we
# should do our best to die too so the docker container exits and
# is removed. We do that by interrupting the child and exiting with
# whatever exit code it exits with.
kill 'TERM', $child_pid;
wait;
exit $? >> 8;
}
# If we're running in docker then we won't have a useful username
# so we hack one into place with nss wrapper.
open(my $override, '>', '/tmp/passwd')
or dir("Couldn't write override user file");
# We use the `id` command here because it fetches the id. The native
# perl way to do this (getpwuid($<)) doesn't work because it needs a
# complete user. And we *aren't* one.
my $uid = `id -u`;
my $gid = `id -g`;
chomp($uid);
chomp($gid);
print $override "docker:x:$uid:$gid:docker:/tmp:/bin/bash\n";
close $override;
$ENV{LD_PRELOAD} = '/usr/lib/libnss_wrapper.so';
$ENV{NSS_WRAPPER_PASSWD} = '/tmp/passwd';
$ENV{NSS_WRAPPER_GROUP} = '/etc/group';
}
eval { run( 'xsltproc', '--version' ) }
or die "Please install <xsltproc>";
}
#===================================
sub pick_conf {
#===================================
return 'conf.yaml' unless $Opts->{conf};
my $conf = file($Opts->{conf});
$conf = dir($Old_Pwd)->file($Opts->{conf}) if $conf->is_relative;
return $conf if -e $conf;
die "$conf doesn't exist";
}
#===================================
# Serve the documentation that we just built and open a browser to look at it.
#
# docs_dir - directory containing generated docs : Path::Class::dir
# redirects_file - file containing redirects or 0 if there aren't
# - any redirects : Path::Class::file||0
#===================================
sub serve_and_open_browser {
#===================================
my ( $docs_dir, $redirects_file ) = @_;
if ( my $pid = fork ) {
# parent
$SIG{INT} = sub {
kill 'TERM', $pid;
};
$SIG{TERM} = $SIG{INT};
if ( not $running_in_standard_docker ) {
sleep 1;
say "Press Ctrl-C to exit the web server";
open_browser("http://localhost:8000/");
}
wait;
say 'Terminated preview services';
exit;
}
else {
if ( $running_in_standard_docker ) {
# We use nginx to serve files instead of the python built in web server
# when we're running inside docker because the python web server performs
# badly there. nginx is fine.
my $nginx_config = file('/tmp/nginx.conf');
write_nginx_test_config( $nginx_config, $docs_dir, $redirects_file );
close STDIN;
open( STDIN, "</dev/null" );
exec( qw(nginx -c), $nginx_config );
} else {
my $http = dir( 'resources', 'http.py' )->absolute;
close STDIN;
open( STDIN, "</dev/null" );
chdir $docs_dir;
exec( $http '8000' );
}
}
}
#===================================
sub command_line_opts {
#===================================
return [
# Options only compatible with --doc
'doc=s',
'asciidoctor',
'chunk=i',
'lang=s',
'lenient',
'out=s',
'pdf',
'resource=s@',
'respect_edit_url_overrides',
'single',
'suppress_migration_warnings',
'toc',
# Options only compatible with --all
'all',
'announce_preview=s',
'target_branch=s',
'target_repo=s',
'keep_hash',
'linkcheckonly',
'push',
'rebuild',
'reference=s',
'skiplinkcheck',
'sub_dir=s@',
'user=s',
# Options only compatible with --preview
'preview',
# Options that do *something* for either --doc or --all or --preview
'conf=s',
'in_standard_docker',
'open',
'procs=i',
'verbose',
];
}
#===================================
sub usage {
#===================================
my $name = $Opts->{in_standard_docker} ? 'build_docs' : $0;
say <<USAGE;
Build local docs:
$name --doc path/to/index.asciidoc [opts]
Opts:
--asciidoctor Use asciidoctor instead of asciidoc.
--chunk 1 Also chunk sections into separate files
--lang Defaults to 'en'
--lenient Ignore linking errors
--out dest/dir/ Defaults to ./html_docs.
--pdf Generate a PDF file instead of HTML
--resource Path to image dir - may be repeated
--respect_edit_url_overrides
Respects `:edit_url:` overrides in the book.
--single Generate a single HTML page, instead of
a chunking into a file per chapter
--suppress_migration_warnings
Suppress warnings about Asciidoctor migration
issues. Use this when building "old" branches.
--toc Include a TOC at the beginning of the page.
WARNING: Anything in the `out` dir will be deleted!
Build docs from all repos in conf.yaml:
$name --all --target_repo <target> [opts]
Opts:
--keep_hash Build docs from the same commit hash as last time
--linkcheckonly Skips the documentation builds. Checks links only.
--push Commit the updated docs and push to origin
--announce_preview <host>
Causes the build to log a line about where to find
a preview of the build if anything is pushed.
--rebuild Rebuild all branches of every book regardless of
what has changed
--reference Directory of `--mirror` clones to use as a
local cache
--skiplinkcheck Omit the step that checks for broken links
--sub_dir Use a directory as a branch of some repo
(eg --sub_dir elasticsearch:master:~/Code/elasticsearch)
--target_repo Repository to which to commit docs
--target_branch Branch to which to commit docs
--user Specify which GitHub user to use, if not your own
General Opts:
--conf <ymlfile> Use your own configuration file, defaults to the
bundled conf.yaml
--in_standard_docker
Specified by build_docs when running in
its container
--open Open the docs in a browser once built.
--procs Number of processes to run in parallel, defaults
to 3
--verbose Output more logs
USAGE
if ( $Opts->{in_standard_docker} ) {
say <<USAGE;
Self Test:
$name --self-test <args to pass to make>
`--self-test` is a wrapper around `make` which is used exclusively for
testing. Like `make`, the current directory selects the `Makefile` and
you can make specific targets. Some examples:
Execute all tests:
$name --self-test
Execute all of the tests for our extensions to Asciidoctor:
$name --self-test -C resources/asciidoctor
Run rubocop on our extensions to Asciidoctor:
$name --self-test -C resources/asciidoctor rubocop
USAGE
}
}
#===================================
sub check_opts {
#===================================
if ( !$Opts->{doc} ) {
die('--asciidoctor only compatible with --doc') if $Opts->{asciidoctor};
die('--chunk only compatible with --doc') if $Opts->{chunk};
# Lang will be 'en' even if it isn't specified so we don't check it.
die('--lenient only compatible with --doc') if $Opts->{lenient};
die('--out only compatible with --doc') if $Opts->{out};
die('--pdf only compatible with --doc') if $Opts->{pdf};
die('--resource only compatible with --doc') if $Opts->{resource};
die('--respect_edit_url_overrides only compatible with --doc') if $Opts->{respect_edit_url_overrides};
die('--single only compatible with --doc') if $Opts->{single};
die('--toc only compatible with --doc') if $Opts->{toc};
}
if ( !$Opts->{all} ) {
die('--keep_hash only compatible with --all') if $Opts->{keep_hash};
die('--linkcheckonly only compatible with --all') if $Opts->{linkcheckonly};
die('--push only compatible with --all') if $Opts->{push};
die('--announce_preview only compatible with --all') if $Opts->{announce_preview};
die('--rebuild only compatible with --all') if $Opts->{rebuild};
die('--reference only compatible with --all') if $Opts->{reference};
die('--skiplinkcheck only compatible with --all') if $Opts->{skiplinkcheck};
die('--sub_dir only compatible with --all') if $Opts->{sub_dir};
die('--user only compatible with --all') if $Opts->{user};
}
if ( !$Opts->{all} && !$Opts->{preview} ) {
die('--target_branch only compatible with --all or --preview') if $Opts->{target_branch};
die('--target_repo only compatible with --all or --preview') if $Opts->{target_repo};
}
}