Skip to content

Commit 011cf93

Browse files
authored
Allow running --open with --all (#660)
This allows you to `--open` the docs built with `--all`. This should be useful when testing changes that effect all docs, like the redirect work that I plan on doing soon. There is a wrinkle though: to make serving the docs locally work we actually built *different* docs. We'd switch from finding jQuery at `/static/js/jquery.min.js` to getting it from `https://www.elastic.co/static/js/jquery.min.js`. This worked, and it was simple enough to do for the built in python web server that we use when you run `--open` outside of docker, but we don't want to do that when we serve all of the docs because most of the docs will have been built with the standard template. But now that we have docker we don't have to! In docker we're using nginx to serve the docs and we can write a few rewrite rules to fix the missing assets. For good measure we assets rewrite when we're running in docker even when we're building a single doc. So now building a single doc is just that much more real!
1 parent 8b77c28 commit 011cf93

File tree

1 file changed

+81
-68
lines changed

1 file changed

+81
-68
lines changed

build_docs.pl

Lines changed: 81 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ BEGIN
8080
%$template_urls,
8181
lenient => $Opts->{lenient},
8282
force => $Opts->{reload_template},
83-
abs_urls => $Opts->{doc}
83+
abs_urls => ! $running_in_standard_docker && $Opts->{doc},
8484
);
8585

8686
$Opts->{doc} ? build_local( $Opts->{doc} )
@@ -124,72 +124,8 @@ sub build_local {
124124
my $html = $dir->file('index.html');
125125

126126
if ( $Opts->{open} ) {
127-
if ( my $pid = fork ) {
128-
# parent
129-
$SIG{INT} = sub {
130-
kill -9, $pid;
131-
};
132-
if ( $Opts->{open} && not $running_in_standard_docker ) {
133-
sleep 1;
134-
say "Opening: " . $html;
135-
say "Press Ctrl-C to exit the web server";
136-
open_browser('http://localhost:8000/index.html');
137-
}
138-
139-
wait;
140-
say "\nExiting";
141-
exit;
142-
}
143-
else {
144-
if ( $running_in_standard_docker ) {
145-
# We use nginx to serve files instead of the python built in web server
146-
# when we're running inside docker because the python web server performs
147-
# badly there. nginx is fine.
148-
open(my $nginx_conf, '>', '/tmp/docs.conf') or dir("Couldn't write nginx conf to /tmp/docs/.conf");
149-
print $nginx_conf <<"CONF";
150-
daemon off;
151-
error_log /dev/stdout info;
152-
pid /run/nginx/nginx.pid;
153-
154-
events {
155-
worker_connections 64;
156-
}
157-
158-
http {
159-
error_log /dev/stdout crit;
160-
log_format short '[\$time_local] "\$request" \$status';
161-
access_log /dev/stdout short;
162-
server {
163-
listen 8000;
164-
location / {
165-
root $dir;
166-
add_header 'Access-Control-Allow-Origin' '*';
167-
if (\$request_method = 'OPTIONS') {
168-
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
169-
add_header 'Access-Control-Allow-Headers' 'kbn-xsrf-token';
170-
}
171-
}
172-
types {
173-
text/html html;
174-
application/javascript js;
175-
text/css css;
176-
}
177-
}
178-
}
179-
CONF
180-
close $nginx_conf;
181-
close STDIN;
182-
open( STDIN, "</dev/null" );
183-
chdir $dir;
184-
exec( 'nginx', '-c', '/tmp/docs.conf' );
185-
} else {
186-
my $http = dir( 'resources', 'http.py' )->absolute;
187-
close STDIN;
188-
open( STDIN, "</dev/null" );
189-
chdir $dir;
190-
exec( $http '8000' );
191-
}
192-
}
127+
say "Opening: " . $html;
128+
serve_and_open_browser( $dir, 'index.html' );
193129
}
194130
else {
195131
say "See: $html";
@@ -282,6 +218,7 @@ sub build_all {
282218
check_links($build_dir);
283219
}
284220
push_changes($build_dir, $target_repo, $target_repo_checkout) if $Opts->{push};
221+
serve_and_open_browser( $build_dir, '/' ) if $Opts->{open};
285222

286223
$temp_dir->rmtree;
287224
}
@@ -733,6 +670,82 @@ sub pick_conf {
733670
die $Opts->{conf} . " doesn't exist";
734671
}
735672

673+
#===================================
674+
sub serve_and_open_browser {
675+
#===================================
676+
my ( $dir, $open_path ) = @_;
677+
678+
if ( my $pid = fork ) {
679+
# parent
680+
$SIG{INT} = sub {
681+
kill -9, $pid;
682+
};
683+
if ( not $running_in_standard_docker ) {
684+
sleep 1;
685+
say "Press Ctrl-C to exit the web server";
686+
open_browser("http://localhost:8000/$open_path");
687+
}
688+
689+
wait;
690+
say "\nExiting";
691+
exit;
692+
}
693+
else {
694+
if ( $running_in_standard_docker ) {
695+
# We use nginx to serve files instead of the python built in web server
696+
# when we're running inside docker because the python web server performs
697+
# badly there. nginx is fine.
698+
open(my $nginx_conf, '>', '/tmp/docs.conf') or dir("Couldn't write nginx conf to /tmp/docs/.conf");
699+
print $nginx_conf <<"CONF";
700+
daemon off;
701+
error_log /dev/stdout info;
702+
pid /run/nginx/nginx.pid;
703+
704+
events {
705+
worker_connections 64;
706+
}
707+
708+
http {
709+
error_log /dev/stdout crit;
710+
log_format short '[\$time_local] "\$request" \$status';
711+
access_log /dev/stdout short;
712+
server {
713+
listen 8000;
714+
location / {
715+
root $dir;
716+
add_header 'Access-Control-Allow-Origin' '*';
717+
if (\$request_method = 'OPTIONS') {
718+
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
719+
add_header 'Access-Control-Allow-Headers' 'kbn-xsrf-token';
720+
}
721+
}
722+
types {
723+
text/html html;
724+
application/javascript js;
725+
text/css css;
726+
}
727+
rewrite ^/android-chrome-(.+)\$ https://www.elastic.co/android-chrome-\$1 permanent;
728+
rewrite ^/assets/(.+)\$ https://www.elastic.co/assets/\$1 permanent;
729+
rewrite ^/favicon(.+)\$ https://www.elastic.co/favicon\$1 permanent;
730+
rewrite ^/gdpr-data\$ https://www.elastic.co/gdpr-data permanent;
731+
rewrite ^/static/(.+)\$ https://www.elastic.co/static/\$1 permanent;
732+
}
733+
}
734+
CONF
735+
close $nginx_conf;
736+
close STDIN;
737+
open( STDIN, "</dev/null" );
738+
exec( 'nginx', '-c', '/tmp/docs.conf' );
739+
} else {
740+
my $http = dir( 'resources', 'http.py' )->absolute;
741+
close STDIN;
742+
open( STDIN, "</dev/null" );
743+
chdir $dir;
744+
exec( $http '8000' );
745+
}
746+
}
747+
}
748+
736749
#===================================
737750
sub usage {
738751
#===================================
@@ -750,7 +763,6 @@ sub usage {
750763
--toc Include a TOC at the beginning of the page.
751764
--out dest/dir/ Defaults to ./html_docs.
752765
--chunk 1 Also chunk sections into separate files
753-
--open Open the docs in a browser once built.
754766
--lenient Ignore linking errors
755767
--lang Defaults to 'en'
756768
--resource Path to image dir - may be repeated
@@ -780,6 +792,7 @@ sub usage {
780792
--in_standard_docker
781793
Specified by build_docs when running in its container
782794
--conf <ymlfile> Use your own configuration file, defaults to the bundled conf.yaml
795+
--open Open the docs in a browser once built.
783796
784797
USAGE
785798
if ( $Opts->{in_standard_docker} ) {

0 commit comments

Comments
 (0)