-
Notifications
You must be signed in to change notification settings - Fork 335
/
build_docs.pl
executable file
·1164 lines (1022 loc) · 41.2 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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/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;
use XML::LibXML;
BEGIN {
$Old_Pwd = Cwd::cwd();
chdir "$FindBin::RealBin/";
}
use lib 'lib';
use ES::Util qw(
run $Opts
build_chunked build_single
proc_man
timestamp
write_html_redirect
write_nginx_redirects
write_nginx_test_config
write_nginx_preview_config
start_web_resources_watcher
start_preview
build_web_resources
);
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 Sys::Hostname;
use ES::BranchTracker();
use ES::DocsRepo();
use ES::Repo();
use ES::Book();
use ES::TargetRepo();
use ES::Toc();
use ES::LinkCheck();
GetOptions($Opts, @{ command_line_opts() }) || exit usage();
check_opts();
our $ConfPath = pick_conf();
our $Conf = LoadFile($ConfPath);
# We no longer support running outside of our "standard" docker container.
# `build_docs` signals to us that it is in the standard docker container by
# passing this argument.
die 'build_docs.pl is unsupported. Use build_docs instead' unless $Opts->{in_standard_docker};
if ( $Opts->{asciidoctor} ) {
say <<MSG
The Asciidoctor migration is complete! --asciidoctor will emit this message
forever in honor of our success but otherwise doesn't do anything.
MSG
}
if ( $Opts->{direct_html} ) {
say <<MSG
The direct_html migration is complete! --direct_html will emit this message
forever in honor of our success but otherwise doesn't do anything.
MSG
}
init_env();
$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;
say "Building HTML from $doc";
my $dir = dir( $Opts->{out} || 'html_docs' )->absolute($Old_Pwd);
my $raw_dir = $dir->subdir( 'raw' );
$Opts->{resource}
= [ map { dir($_)->absolute($Old_Pwd) } @{ $Opts->{resource} || [] } ];
_guess_opts( $index );
$Opts->{roots}{docs} = '/docs_build' unless $Opts->{roots}{docs};
my @alternatives;
if ( $Opts->{alternatives} ) {
for ( @{ $Opts->{alternatives} } ) {
my @parts = split /:/;
unless (scalar @parts == 3) {
die "alternatives must contain exactly two :s but was [$_]";
}
push @alternatives, {
source_lang => $parts[0],
alternative_lang => $parts[1],
dir => $parts[2],
};
}
}
# Get a head start on web resources if we're going to need them.
my $web_resources_pid = start_web_resources_watcher if $Opts->{open};
my $latest = !$Opts->{suppress_migration_warnings};
if ( $Opts->{single} ) {
build_single( $index, $raw_dir, $dir, %$Opts,
latest => $latest,
alternatives => \@alternatives,
relativize => 0,
);
}
else {
build_chunked( $index, $raw_dir, $dir, %$Opts,
latest => $latest,
alternatives => \@alternatives,
relativize => 0,
);
}
say "Done";
if ( $Opts->{open} ) {
my $preview_pid = start_preview( 'fs', $raw_dir, 'template.html', 0 );
serve_local_preview( $dir, 0, $web_resources_pid, $preview_pid );
}
}
#===================================
sub _guess_opts {
#===================================
my $index = shift;
$Opts->{edit_urls} = {};
$Opts->{roots} = {};
my $toplevel = _find_toplevel( $index->parent );
my $remote = _pick_best_remote( $toplevel );
my $branch = _guess_branch( $toplevel );
my $repo_name = _guess_repo_name( $remote );
# We couldn't find the top level so lets make a wild guess.
$toplevel = $index->parent unless $toplevel;
printf "Guessed toplevel=[%s] remote=[%s] branch=[%s] repo=[%s]\n", $toplevel, $remote, $branch, $repo_name;
$Opts->{branch} = $branch;
$Opts->{roots}{ $repo_name } = $toplevel;
$Opts->{edit_urls}{ $toplevel } = ES::Repo::edit_url_for_url_and_branch(
$remote || 'unknown', $branch
);
for my $resource ( @{ $Opts->{resource} } ) {
$toplevel = _find_toplevel( $resource );
$remote = _pick_best_remote( $toplevel );
$branch = _guess_branch( $toplevel );
$repo_name = _guess_repo_name( $remote );
# We couldn't find the top level so lets make a wild guess.
$toplevel = $resource unless $toplevel;
$Opts->{roots}{ $repo_name } = $toplevel;
$Opts->{edit_urls}{ $toplevel } = ES::Repo::edit_url_for_url_and_branch(
$remote || 'unknown', $branch
);
}
}
#===================================
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 || 0;
}
#===================================
sub _pick_best_remote {
#===================================
my $toplevel = shift;
return 0 unless $toplevel;
local $ENV{GIT_DIR} = dir($toplevel)->subdir('.git');
my $projectName = dir( $toplevel )->basename;
my $remotes = eval { run qw(git remote -v) } || '';
# We prefer either an elastic or elasticsearch-cn organization. All
# but two books are in elastic but elasticsearch-cn is special.
if ($remotes =~ m|\s+(\S+[/:]elastic(?:search-cn)?/$projectName)\.git|) {
# Prefer a remote with the same name as the working directory, if it exists
return $1;
}
if ($remotes =~ m|\s+(\S+[/:]elastic(?:search-cn)?/\S+)|) {
# Otherwise, take any remote from one of the preferred organizations
return $1;
}
say "Couldn't find an Elastic remote for $toplevel. Generating edit links targeting the first remote instead.";
if ($remotes =~ m|\s+(\S+[/:]\S+/\S+)|) {
return $1;
}
return 0;
}
#===================================
sub _guess_branch {
#===================================
my $toplevel = shift;
return 'master' unless $toplevel;
local $ENV{GIT_DIR} = dir($toplevel)->subdir('.git');
my $real_branch = eval { run qw(git rev-parse --abbrev-ref HEAD) } || 'master';
# Detects common branch patterns like:
# 7.x
# 7.1
# 18.5
# Also normalizes brackport style patters like:
# blah_blah_7.x
# bort_foo_7_x
# zip_zop_12.8
# qux_12_8
return $1 if $real_branch =~ /(\d+[\._][\dx]+)$/;
# Otherwise we just assume we're trageting master. This'll be right when
# the branch is actually 'master' and when this is a feature branch. It
# obviously won't always be right, but for the most part that *should* be
# ok because we have pull request builds which will double check the links.
return 'master';
}
#===================================
sub _guess_repo_name {
#===================================
my ( $remote ) = @_;
return 'repo' unless $remote;
$remote = dir( $remote )->basename;
$remote =~ s/\.git$//;
return $remote;
}
#===================================
sub build_all {
#===================================
$Opts->{target_repo} = 'git@github.com:elastic/built-docs.git' 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 = $target_repo->destination->subdir( 'html' );
$build_dir->mkpath;
my $raw_build_dir = $target_repo->destination->subdir( 'raw' );
my $contents = $Conf->{contents}
or die "Missing <contents> configuration section";
my $toc_extra = $Conf->{toc_extra} ? $ConfPath->parent->file( $Conf->{toc_extra} ) : 0;
my $toc = ES::Toc->new( $Conf->{contents_title} || 'Guide', $toc_extra );
my $redirects = $target_repo->destination->file( 'redirects.conf' );
if ( $Opts->{linkcheckonly} ){
say "Skipping documentation builds."
}
else {
say "Building docs";
build_entries(
$raw_build_dir, $build_dir, $temp_dir, $toc, $tracker, @$contents
);
say "Writing main TOC";
$toc->write( $raw_build_dir, $build_dir, $temp_dir, 0 );
build_web_resources( $target_repo->destination );
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;
push_changes( $build_dir, $target_repo, $tracker ) if $Opts->{push};
serve_local_preview( $build_dir, $redirects, 0, 0 ) 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};
# Comment out due to build errors
# check_elasticsearch_links( $build_dir, $link_checker ) if exists $Conf->{repos}{elasticsearch};
if ( $link_checker->has_bad || $Opts->{warnlinkcheck}) {
say $link_checker->report;
}
else {
die $link_checker->report;
}
}
#===================================
sub check_kibana_links {
#===================================
my $build_dir = shift;
my $link_checker = shift;
my $branch;
my $version;
say "Checking Kibana links";
my $extractor = sub {
my $contents = shift;
return sub {
while ( $contents =~ m!`(\$\{(?:baseUrl|ELASTIC.+|KIBANA_DOCS|PLUGIN_DOCS|FLEET_DOCS|APM_DOCS|STACK_DOCS|SECURITY_SOLUTION_DOCS|STACK_GETTING_STARTED|APP_SEARCH_DOCS|ENTERPRISE_SEARCH_DOCS|INTEGRATIONS_DEV_DOCS|WORKPLACE_SEARCH_DOCS|SERVERLESS_DOCS)\}[^`]+)`!g ) {
my $path = $1;
$path =~ s/\$\{(?:DOC_LINK_VERSION|urlVersion)\}/$version/;
$path =~ s/\$\{(?:ECS_VERSION)\}/current/;
# In older versions, the variable `${ELASTIC_DOCS}` referred to
# the Elasticsearch Guide. In newer branches, the
# variable is called `${ELASTICSEARCH_DOCS}`
$path =~ s!\$\{ELASTIC_DOCS\}!en/elasticsearch/reference/$version/!;
$path =~ s!\$\{ELASTICSEARCH_DOCS\}!en/elasticsearch/reference/$version/!;
$path =~ s!\$\{KIBANA_DOCS\}!en/kibana/$version/!;
$path =~ s!\$\{PLUGIN_DOCS\}!en/elasticsearch/plugins/$version/!;
$path =~ s!\$\{OBSERVABILITY_DOCS\}!en/observability/$version/!;
$path =~ s!\$\{FLEET_DOCS\}!en/fleet/$version/!;
$path =~ s!\$\{APM_DOCS\}!en/apm/!;
$path =~ s!\$\{STACK_DOCS\}!en/elastic-stack/$version/!;
$path =~ s!\$\{SECURITY_SOLUTION_DOCS\}!en/security/$version/!;
$path =~ s!\$\{STACK_GETTING_STARTED\}!en/elastic-stack-get-started/$version/!;
$path =~ s!\$\{APP_SEARCH_DOCS\}!en/app-search/$version/!;
$path =~ s!\$\{ENTERPRISE_SEARCH_DOCS\}!en/enterprise-search/$version/!;
$path =~ s!\$\{WORKPLACE_SEARCH_DOCS\}!en/workplace-search/$version/!;
$path =~ s!\$\{MACHINE_LEARNING_DOCS\}!en/machine-learning/$version/!;
$path =~ s!\$\{INTEGRATIONS_DEV_DOCS}!en/integrations-developer/current/!;
$path =~ s!\$\{SERVERLESS_DOCS}!/en/serverless/current/!;
# Replace the "https://www.elastic.co/guide/" URL prefix so that
# it becomes a file path in the built docs.
$path =~ s!\$\{(?:baseUrl|ELASTIC_WEBSITE_URL)\}guide/!!;
# We don't want to check any links to www.elastic.co that aren't
# part of the docs.
return "" if $path =~ m/\$\{(?:baseUrl|ELASTIC_WEBSITE_URL|ELASTIC_GITHUB)\}.*/;
# Otherwise, return the link to check
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 @versions = sort map { $_->basename }
grep { $_->is_dir } $build_dir->subdir('en/kibana')->children;
my $link_check_name = 'link-check-kibana';
for (@versions) {
$version = $_;
next if $version eq 'current' || $version =~ /^\d/ && $version lt 5;
# @versions is looping through the directories in the output (which
# still contains `master`), but we need to look in the `main` branch of
# the Kibana repo for this file.
#
# TODO: remove as part of
# https://github.com/elastic/docs/issues/2264
$branch = $version eq "master" ? "main" : $version;
say " Branch: $branch, Version: $version";
my $links_file;
my $source = eval {
$links_file = "src/platform/packages/shared/kbn-doc-links/src/get_doc_links.ts";
$repo->show_file( $link_check_name, $branch, $links_file );
} || eval {
$links_file = "packages/kbn-doc-links/src/get_doc_links.ts";
$repo->show_file( $link_check_name, $branch, $links_file );
} || 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 );
} || eval {
$links_file = "src/core/packages/doc-links/core-doc-links-browser-internal/src/doc_links_service.ts";
$repo->show_file( $link_check_name, $branch, $links_file );
} || eval {
$links_file = "packages/core/doc-links/core-doc-links-browser-internal/src/doc_links_service.ts";
$repo->show_file( $link_check_name, $branch, $links_file );
} || eval {
$links_file = "src/core/public/doc_links/doc_links_service.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 [$version]: $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 check_elasticsearch_links {
#===================================
my $build_dir = shift;
my $link_checker = shift;
my $branch;
my $version;
say "Checking Elasticsearch links";
# Grab URLs from the JSON file. This is lame, but we sort of need to parse
# using regexes because that's what the rest of the infrastructure expects.
# So we grab all quoted strings that contain `html`. This *should* be fine
# for a while because the keys in the file are all in SHOUTING_SNAKE_CASE
# so even if one contains "html" it'll contain "HTML" which doesn't match.
my $json_extractor = sub {
my $contents = shift;
return sub {
while ( $contents =~ m!"([^"\#]+)(?:\#([^"]+))?"!g ) {
my $path = $1;
next unless $path =~ m!html!;
return "en/elasticsearch/reference/$version/$path";
}
return;
};
};
my $tabdelim_extractor = sub {
my $contents = shift;
return sub {
while ( $contents =~ m!"[^\t]+\t(.*)"!g ) {
return "en/elasticsearch/reference/$version/$1";
}
return;
};
};
my $src_path = 'server/src/main/resources/org/elasticsearch/common/reference-docs-links.json';
my $repo = ES::Repo->get_repo('elasticsearch');
my @versions = sort map { $_->basename }
grep { $_->is_dir } $build_dir->subdir('en/elasticsearch/reference')->children;
my $link_check_name = 'link-check-elasticsearch';
for (@versions) {
$version = $_;
# check versions after 8.6
next if $version eq 'current' || $version =~ /^(\d+)\.(\d+)/ && ($1 lt 8 || ($1 eq 8 && $2 lt 7));
# @versions is looping through the directories in the output (which
# still contains `master`), but we need to look in the `main` branch of
# the ES repo for this file.
#
# TODO: remove as part of
# https://github.com/elastic/docs/issues/2264
$branch = $version eq "master" ? "main" : $version;
say " Branch: $branch, Version: $version";
my $links_file;
my $extractor;
my $source = eval {
$links_file = 'server/src/main/resources/org/elasticsearch/common/reference-docs-links.json';
$extractor = $json_extractor;
$repo->show_file( $link_check_name, $branch, $links_file );
} || eval {
$links_file = 'libs/core/src/main/resources/org/elasticsearch/core/reference-docs-links.txt';
$extractor = $tabdelim_extractor;
$repo->show_file( $link_check_name, $branch, $links_file );
};
die "failed to find elasticsearch links file;\n$@" unless $source;
$link_checker->check_source( $source, $extractor,
"Elasticsearch [$version]: $src_path" );
# 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, $src_path, 0 );
}
}
#===================================
sub build_entries {
#===================================
my ( $raw_build, $build, $temp_dir, $toc, $tracker, @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 $raw_sub_build = $raw_build->subdir($base_dir);
my $sub_build = $build->subdir($base_dir);
my $toc_extra = $entry->{toc_extra} ? $ConfPath->parent->file( $entry->{toc_extra} ) : 0;
my $section_toc = build_entries(
$raw_sub_build, $sub_build, $temp_dir,
ES::Toc->new( $title, $toc_extra, $entry->{lang} ),
$tracker, @$sections
);
if ($base_dir) {
$section_toc->write( $raw_sub_build, $sub_build, $temp_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,
raw_dir => $raw_build,
temp_dir => $temp_dir,
%$entry
);
$toc->add_entry( $book->build( $Opts->{rebuild}, $ConfPath ) );
$tracker->allowed_book( $book );
}
return $toc;
}
#===================================
sub build_sitemap {
#===================================
my ( $dir, $changed ) = @_;
# Build the sitemap by iterating over all of the toc and index files. Uses
# the old sitemap to populate the dates for files that haven't changed.
# Use "now" for files that have.
my $sitemap = $dir->file('sitemap.xml');
my $now = timestamp();
my %dates;
if ( -e $sitemap ) {
my $doc = XML::LibXML->load_xml( location => $sitemap );
for ($doc->firstChild->childNodes) {
next unless $_->nodeName eq 'url';
my $loc;
my $lastmod;
for ($_->childNodes) {
$loc = $_->to_literal if $_->nodeName eq 'loc';
$lastmod = $_->to_literal if $_->nodeName eq 'lastmod';
}
die "Dind't find <loc> in $_" unless $loc;
die "Dind't find <lastmod> in $_" unless $lastmod;
$loc =~ s|https://www.elastic.co/guide/||;
$dates{$loc} = $lastmod;
}
}
for ( split /\0/, $changed ) {
next unless s|^html/||;
$dates{$_} = $now;
}
# Build a list of the files we're going to index and sort it so entries in
# the sitemap don't "jump around".
my @files;
$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)' );
push @files, $item->file($_)
for ( $content =~ /href="([^"]+)"/g );
}
elsif ( -e $item->file('index.html') ) {
push @files, $item->file('index.html');
}
return $item->PRUNE;
}
);
@files = sort @files;
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
for ( @files ) {
my $loc = $_->relative($dir);
my $url = "https://www.elastic.co/guide/$loc";
my $date = $dates{$loc};
die "Couldn't find a modified time for $loc" unless $date;
say $fh <<ENTRY;
<url>
<loc>$url</loc>
<lastmod>$date</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
ENTRY
}
say $fh "</urlset>";
close $fh or die "Couldn't close $sitemap: $!"
}
#===================================
sub init_dirs {
#===================================
my $repos_dir = $Opts->{reposcache} || '.repos';
$repos_dir = dir($repos_dir)->absolute;
$repos_dir->mkpath;
my $temp_dir = dir('/tmp/docsbuild');
$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(
git_dir => $repos_dir->subdir('target_repo.git'),
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 = $target_repo->destination . '/html/branches.yaml';
# 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) {
next if $name eq 'docs';
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,
git_dir => $repos_dir->subdir("$name.git"),
tracker => $tracker,
url => $url,
reference => $reference_dir,
keep_hash => $Opts->{keep_hash} || 0,
);
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;
}
# Setup the docs repo
# We support configuring the remote for the docs repo for testing
ES::DocsRepo->new(
tracker => $tracker,
dir => $conf->{docs} || '/docs_build',
keep_hash => $Opts->{keep_hash} || 0
);
return $tracker;
}
#===================================
sub preview {
#===================================
$Opts->{target_repo} = 'git@github.com:elastic/built-docs.git' unless ( $Opts->{target_repo} );
my $nginx_config = file('/tmp/nginx.conf');
write_nginx_preview_config( $nginx_config );
if ( my $nginx_pid = fork ) {
my ( $repos_dir, $temp_dir, $reference_dir ) = init_dirs();
my $target_repo;
unless ( $Opts->{gapped} ) {
say "Cloning built docs";
$target_repo = init_target_repo( $repos_dir, $temp_dir, $reference_dir );
}
say "Built docs are ready";
my $default_template = $Opts->{gapped} ? "air_gapped_template.html" : "template.html";
my $preview_pid = start_preview(
'git', '/docs_build/.repos/target_repo.git', $default_template, $Opts->{gapped}
);
$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...preview';
kill 'TERM', $preview_pid;
wait;
say 'Terminated preview services';
exit 0;
};
if ( $Opts->{gapped} ) {
wait;
} else {
while (1) {
sleep 1;
my $fetch_result = eval { $target_repo->fetch };
say $fetch_result if $fetch_result;
say $@ if $@;
}
}
exit;
} else {
close STDIN;
open( STDIN, "</dev/null" );
exec( qw(nginx -c), $nginx_config );
}
}
#===================================
sub push_changes {
#===================================
my ($build_dir, $target_repo, $tracker ) = @_;
my $outstanding = $target_repo->outstanding_changes;
if ( $tracker->has_non_local_changes || $outstanding ) {
say "Saving branch tracker";
$tracker->write;
say "Building sitemap";
build_sitemap( $build_dir, $outstanding );
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 {
#===================================
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';
}
#===================================
sub pick_conf {
#===================================
return file( '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.
#
# 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
# web_resources_pid - pid of a subprocess that rebuilds the web resources on
# the fly if we're running one or 0
# preview_pid - pid of the preview application or 0 if we're not running it
#===================================
sub serve_local_preview {
#===================================
my ( $docs_dir, $redirects_file, $web_resources_pid, $preview_pid ) = @_;
if ( my $nginx_pid = fork ) {
# parent
$SIG{INT} = sub {
say 'Terminating preview services...nginx';
kill 'TERM', $nginx_pid;
wait;
if ( $preview_pid ) {
say 'Terminating preview services...preview';
kill 'TERM', $preview_pid;
wait;
}
if ( $web_resources_pid ) {
say 'Terminating preview services...parcel';
kill 'TERM', $web_resources_pid;
wait;
}
};
$SIG{TERM} = $SIG{INT};
wait;
say 'Terminated preview services';