From aa65d2630ce5394be033a9689f9f6d639af4a838 Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Thu, 25 Jan 2024 14:57:59 +0100 Subject: [PATCH] Useful version output --- .github/workflows/bouncer.yml | 4 +++- bouncer/Dockerfile | 24 +++++++++++++++++------- bouncer/Makefile | 5 +++-- bouncer/Readme.md | 1 + bouncer/src/Bouncer.php | 15 ++++++++++----- bouncer/src/Settings/Settings.php | 2 +- bouncer/src/Target.php | 26 ++++++++++++++++++++++++-- 7 files changed, 59 insertions(+), 18 deletions(-) diff --git a/.github/workflows/bouncer.yml b/.github/workflows/bouncer.yml index 080af6b..a9f076a 100644 --- a/.github/workflows/bouncer.yml +++ b/.github/workflows/bouncer.yml @@ -47,7 +47,9 @@ jobs: push: true target: bouncer build-args: | - GIT_SHA=${{ github.sha }} + GIT_SHA="${{ github.sha }}" + BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" + GIT_COMMIT_MESSAGE="$(git log -1 --pretty=%B)" tags: | benzine/bouncer ghcr.io/benzine-framework/bouncer:latest diff --git a/bouncer/Dockerfile b/bouncer/Dockerfile index 10335de..3ff59df 100644 --- a/bouncer/Dockerfile +++ b/bouncer/Dockerfile @@ -1,9 +1,4 @@ FROM benzine/php:cli-8.1 as bouncer -ARG BUILD_DATE -ARG GIT_SHA -ENV BUILD_DATE=${BUILD_DATE} \ - GIT_SHA=${GIT_SHA} - LABEL maintainer="Matthew Baggett " \ org.label-schema.vcs-url="https://github.com/benzine-framework/docker" \ org.opencontainers.image.source="https://github.com/benzine-framework/docker" @@ -37,7 +32,10 @@ RUN apt-get -qq update && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/lib/dpkg/status.old /var/cache/debconf/templates.dat /var/log/dpkg.log /var/log/lastlog /var/log/apt/*.log +# copy some default self-signed certs COPY self-signed-certificates /certs + +# Install runits for services COPY nginx.runit /etc/service/nginx/run COPY logs.runit /etc/service/nginx-logs/run COPY bouncer.runit /etc/service/bouncer/run @@ -45,28 +43,40 @@ COPY bouncer.finish /etc/service/bouncer/finish COPY logs-nginx-access.runit /etc/service/logs-nginx-access/run COPY logs-nginx-error.runit /etc/service/logs-nginx-error/run RUN chmod +x /etc/service/*/run /etc/service/*/finish + +# Copy default nginx bits COPY NginxDefault /etc/nginx/sites-enabled/default.conf COPY Nginx-tweak.conf /etc/nginx/conf.d/tweak.conf + # Disable daemonising in nginx RUN sed -i '1s;^;daemon off\;\n;' /etc/nginx/nginx.conf && \ sed -i 's|include /etc/nginx/sites-enabled/*|include /etc/nginx/sites-enabled/*.conf|g' /etc/nginx/nginx.conf && \ rm /etc/nginx/sites-enabled/default && \ rm -R /etc/nginx/sites-available +# Copy over vendored code plus install just in case COPY vendor /app/vendor COPY composer.* /app/ RUN composer install +# Copy over application code COPY public /app/public COPY bin /app/bin COPY src /app/src COPY templates /app/templates +RUN chmod +x /app/bin/bouncer +# Create some volumes for logs and certs VOLUME /etc/letsencrypt VOLUME /var/log/bouncer -RUN chmod +x /app/bin/bouncer && \ - mkdir -p /var/log/bouncer +# stuff some envs from build +ARG BUILD_DATE +ARG GIT_SHA +ARG GIT_COMMIT_MESSAGE +ENV BUILD_DATE=${BUILD_DATE} \ + GIT_SHA=${GIT_SHA} \ + GIT_COMMIT_MESSAGE=${GIT_COMMIT_MESSAGE} FROM benzine/php:nginx-8.1 as test-app-a COPY ./test/public-web-a /app/public diff --git a/bouncer/Makefile b/bouncer/Makefile index 792103c..643a863 100644 --- a/bouncer/Makefile +++ b/bouncer/Makefile @@ -6,8 +6,9 @@ fix: php-cs-fixer build-n-push: fix docker build \ - --build-arg BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \ - --build-arg GIT_SHA=$(shell git rev-parse HEAD) \ + --build-arg BUILD_DATE="$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")" \ + --build-arg GIT_SHA="$(shell git rev-parse HEAD)" \ + --build-arg GIT_COMMIT_MESSAGE="$(shell git log -1 --pretty=%B | head -n1)" \ --tag benzine/bouncer \ --tag ghcr.io/benzine-framework/bouncer \ --target bouncer \ diff --git a/bouncer/Readme.md b/bouncer/Readme.md index 72c500c..5d0217d 100644 --- a/bouncer/Readme.md +++ b/bouncer/Readme.md @@ -16,6 +16,7 @@ These should not be confused. | LOG_NAME | bouncer | | The name of the log file to write to | | LOG_FILE | /var/log/bouncer/bouncer.log | | The path to the log file to write to | | LOG_LEVEL | debug | info, debug, critical etc | The level of logging to write to the log file. See Monolog docs. | +| LOG_LEVEL_NAME_LENGTH | 4 | positive numbers | The length of the level name to be written to the log file. See Monolog docs. | | LOG_LINE_FORMAT | [%datetime%] %level_name%: %channel%: %message% | | The format of the log line. See Monolog docs. | | LOG_COLOUR | true | true, false | Whether to colourise the log output sent to stdout. | | diff --git a/bouncer/src/Bouncer.php b/bouncer/src/Bouncer.php index d087fd3..e3c6e23 100644 --- a/bouncer/src/Bouncer.php +++ b/bouncer/src/Bouncer.php @@ -303,11 +303,11 @@ class Bouncer public function run(): void { - $gitHash = substr($this->environment['GIT_SHA'], 0, 7); - $this->logger->info('{emoji} Starting Bouncer (Build {git_sha})...', ['emoji' => Emoji::timerClock(), 'git_sha' => '#' . $gitHash]); - - $buildDate = Carbon::parse($this->environment['BUILD_DATE']); - $this->logger->info('{emoji} Built on {build_date}, {build_ago}', ['emoji' => Emoji::redHeart(), 'build_date' => $buildDate->toDateTimeString(), 'build_ago' => $buildDate->ago()]); + $gitHash = substr($this->environment['GIT_SHA'], 0, 7); + $buildDate = Carbon::parse($this->environment['BUILD_DATE']); + $gitMessage = trim($this->environment['GIT_COMMIT_MESSAGE']); + $this->logger->info('{emoji} Starting Bouncer. Built on {build_date}, {build_ago}', ['emoji' => Emoji::redHeart(), 'build_date' => $buildDate->toDateTimeString(), 'build_ago' => $buildDate->ago()]); + $this->logger->info('{emoji} Build #{git_sha}: "{git_message}"', ['emoji' => Emoji::memo(), 'git_sha' => $gitHash, 'git_message' => $gitMessage]); try { $this->stateHasChanged(); @@ -325,6 +325,11 @@ class Bouncer { foreach ($envs as $eKey => $eVal) { switch ($eKey) { + case 'BOUNCER_NAME': + $bouncerTarget->setName($eVal); + + break; + case 'BOUNCER_DOMAIN': $domains = explode(',', $eVal); array_walk($domains, function (&$domain, $key): void { diff --git a/bouncer/src/Settings/Settings.php b/bouncer/src/Settings/Settings.php index c87c71d..25f3406 100644 --- a/bouncer/src/Settings/Settings.php +++ b/bouncer/src/Settings/Settings.php @@ -47,7 +47,7 @@ class Settings implements SettingsInterface 'path' => Settings::getEnvironment('LOG_FILE', '/var/log/bouncer/bouncer.log'), 'level' => Level::fromName(Settings::getEnvironment('LOG_LEVEL', 'DEBUG')), 'line_format' => Settings::getEnvironment('LOG_LINE_FORMAT', '[%datetime%] %level_name%: %channel%: %message%') . "\n", - 'max_level_name_length' => 9, + 'max_level_name_length' => Settings::getEnvironment('LOG_LEVEL_NAME_LENGTH', 4), 'coloured_output' => Settings::isEnabled('LOG_COLOUR', true), ], ]; diff --git a/bouncer/src/Target.php b/bouncer/src/Target.php index fd58650..1ce5b29 100644 --- a/bouncer/src/Target.php +++ b/bouncer/src/Target.php @@ -4,12 +4,13 @@ declare(strict_types=1); namespace Bouncer; -use Monolog\Logger; +use Bouncer\Logger\Logger; use Spatie\Emoji\Emoji; class Target { private string $id; + private ?string $name = null; private array $domains; private string $endpointHostnameOrIp; private ?int $port = null; @@ -214,6 +215,7 @@ class Target public function setDomains(array $domains): self { $this->domains = $domains; + $this->updateLogger(); return $this; } @@ -273,7 +275,15 @@ class Target public function getName() { - return reset($this->domains); + return $this->name ?? reset($this->domains); + } + + public function setName(string $name): self + { + $this->name = $name; + $this->updateLogger(); + + return $this; } public function isAllowNonSSL(): bool @@ -288,6 +298,18 @@ class Target return $this; } + public function getLogger(): Logger + { + return $this->logger; + } + + public function updateLogger(): self + { + $this->logger = $this->logger->withName($this->getName()); + + return $this; + } + public function isEndpointValid(): bool { // Is it just an IP?