From 356bfe73b9b25c9cdf86dd9e125f411824db74fc Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Wed, 19 Apr 2017 09:50:38 -0400 Subject: Bug 1357809 - Add endpoints for future cloud-services integration --- .htaccess | 13 ++++++++++++ Bugzilla/Install/Filesystem.pm | 10 +++++----- extensions/BMO/Extension.pm | 36 +++++++++++++++++++++++++++++++++ heartbeat.cgi | 45 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 heartbeat.cgi diff --git a/.htaccess b/.htaccess index f76d352e2..3cc7c69ea 100644 --- a/.htaccess +++ b/.htaccess @@ -44,6 +44,19 @@ Redirect permanent /etiquette.html https://bugzilla.mozilla.org/page.cgi?id=etiq Redirect permanent /duplicates.html https://bugzilla.mozilla.org/duplicates.cgi RewriteEngine On +# This rewrite rule skips over the rest, which is good because the load balancers +# might hit this file once a second and we want apache to not take much time. +# Note that this file is generated by checksetup.pl +RewriteRule ^__lbheartbeat__$ - [L] + +# allow cloud-services to identify the version we're running. +# version.json is also generated by checksetup.pl +RewriteRule ^__version__$ version.json [L] + +# Unlike lbheartbeat, this endpoint is called less frequently (every five minutes or so) +# heartbeat.cgi returns 200 if the DB and memcached are both working, and 500 otherwise. +RewriteRule ^__heartbeat__$ heartbeat.cgi [L] + RewriteRule ^template_cache/ - [F,L,NC] RewriteRule ^template_cache.deleteme/ - [F,L,NC] RewriteRule ^review(.*) page.cgi?id=splinter.html$1 [QSA] diff --git a/Bugzilla/Install/Filesystem.pm b/Bugzilla/Install/Filesystem.pm index c0bc26262..c67823d7a 100644 --- a/Bugzilla/Install/Filesystem.pm +++ b/Bugzilla/Install/Filesystem.pm @@ -623,12 +623,12 @@ sub _create_files { # It's not necessary to sort these, but it does make the # output of checksetup.pl look a bit nicer. foreach my $file (sort keys %files) { - unless (-e $file) { + my $info = $files{$file}; + if ($info->{overwrite} or not -f $file) { print "Creating $file...\n"; - my $info = $files{$file}; - my $fh = new IO::File($file, O_WRONLY | O_CREAT, $info->{perms}) - || die $!; - print $fh $info->{contents} if $info->{contents}; + my $fh = IO::File->new( $file, O_WRONLY | O_CREAT, $info->{perms} ) + or die "unable to write $file: $!"; + print $fh $info->{contents} if exists $info->{contents}; $fh->close; } } diff --git a/extensions/BMO/Extension.pm b/extensions/BMO/Extension.pm index 40ab8424e..1bf6ebf8c 100644 --- a/extensions/BMO/Extension.pm +++ b/extensions/BMO/Extension.pm @@ -57,6 +57,8 @@ use List::Util qw(first); use Scalar::Util qw(blessed); use Sys::Syslog qw(:DEFAULT); use Text::Balanced qw( extract_bracketed extract_multiple ); +use File::Slurp qw(read_file); +use JSON::XS; use Bugzilla::Extension::BMO::Constants; use Bugzilla::Extension::BMO::FakeBug; @@ -2522,7 +2524,41 @@ sub _check_default_product_security_group { sub install_filesystem { my ($self, $args) = @_; my $files = $args->{files}; + my $create_files = $args->{create_files}; my $extensions_dir = bz_locations()->{extensionsdir}; + $create_files->{__lbheartbeat__} = { + perms => Bugzilla::Install::Filesystem::WS_SERVE, + contents => 'This mission is too important for me to allow you to jeopardize it.', + }; + + + # version.json needs to have a source attribute pointing to + # our repository. We already have this information in the (static) + # contribute.json file, so parse that in + my $json = JSON::XS->new->pretty->utf8->canonical(); + my $contribute = eval { + $json->decode(scalar read_file(bz_locations()->{cgi_path} . "/contribute.json")); + }; + my $commit = `git rev-parse HEAD`; + chomp $commit; + + if (!$contribute) { + die "Missing or invalid contribute.json file"; + } + + my $version_obj = { + source => $contribute->{repository}{url}, + version => BUGZILLA_VERSION, + commit => $commit // "unknown", + build => $ENV{BUGZILLA_CI_BUILD} // "unknown", + }; + + $create_files->{'version.json'} = { + overwrite => 1, + perms => Bugzilla::Install::Filesystem::WS_SERVE, + contents => $json->encode($version_obj), + }; + $files->{"$extensions_dir/BMO/bin/migrate-github-pull-requests.pl"} = { perms => Bugzilla::Install::Filesystem::OWNER_EXECUTE }; diff --git a/heartbeat.cgi b/heartbeat.cgi new file mode 100644 index 000000000..47f3d59a2 --- /dev/null +++ b/heartbeat.cgi @@ -0,0 +1,45 @@ +#!/usr/bin/perl -T +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use 5.10.1; +use strict; +use warnings; + +use lib qw(. lib local/lib/perl5); + +use Bugzilla; +use Bugzilla::Constants; +use Bugzilla::Error; +use Bugzilla::Update; + +my $ok = eval { + # Ensure that any Throw*Error calls just use die, rather than trying to return html... + Bugzilla->error_mode(ERROR_MODE_DIE); + my $memcached = Bugzilla->memcached; + my $dbh = Bugzilla->dbh; + my $database_ok = $dbh->ping; + my $versions = $memcached->{memcached}->server_versions; + my $memcached_ok = keys %$versions; + + die "database not available" unless $database_ok; + die "memcached server(s) not available" unless $memcached_ok; + die "mod_perl not configured?" unless $ENV{MOD_PERL}; + 1; +}; +warn "heartbeat error: $@" if !$ok && $@; + +my $cgi = Bugzilla->cgi; +print $cgi->header(-type => 'text/plain', -status => $ok ? '200 OK' : '500 Internal Server Error'); +print $ok ? "Bugzilla OK\n" : "Bugzilla NOT OK\n"; + +if ($ENV{MOD_PERL}) { + my $r = $cgi->r; + # doing this supresses the error document, but does not change the http response code. + $r->rflush; + $r->status(200); +} -- cgit v1.2.3-24-g4f1b