Bisher wurde der Hook-Pfad aus dem letzten Segment von WEBSITE_REPO abgeleitet, die Status-Datei war fix auf ~/deploy/status.json gesetzt. Beides sieht pro Instanz eindeutig aus, ist es aber nicht: Live auf einer Demo-Installation gefunden, bei der zwei Ableger-Instanzen auf demselben Account beide denselben abgeleiteten Hook-Pfad (website.git) erhielten - der Trigger-Button einer Instanz loeste den Deploy der anderen aus, die Status-Seite zeigte dauerhaft 'kein Status vorhanden' statt echter Daten. Neue optionale install.env-Werte DEPLOY_STATUS_FILE/DEPLOY_HOOK_PATH, Default identisch zum bisherigen Standardfall (eine Instanz pro Server) - nur bei mehreren Instanzen auf demselben Account muss man sie anpassen.
78 lines
3.3 KiB
Ruby
78 lines
3.3 KiB
Ruby
#!/usr/bin/env ruby
|
|
# Small, permanently running service for the whole "write" area (/admin/*,
|
|
# gated by the "write" oauth2-proxy instance, see
|
|
# the installation guide on the project website): normally serves the same static files as its
|
|
# former file:// upstream (Decap CMS, admin/status/), AND additionally
|
|
# answers the two dynamic endpoints below. A single upstream instead of two
|
|
# - oauth2-proxy 7.15.3 did not resolve the multiple, path-distinguished
|
|
# upstreams (file:// + http://) via URL fragment in the simple
|
|
# "upstreams = [...]" list form as documented (both ended up on path "/",
|
|
# "upstream ids must be unique"/"upstream paths must be unique" on start) -
|
|
# hence a WEBrick file handler here for the rest instead.
|
|
#
|
|
# This script itself does not check authorization, that is handled entirely
|
|
# by the oauth2-proxy gate in front of it.
|
|
#
|
|
# STATUS_FILE is written by the deploy hook itself (path from install.env,
|
|
# see there - not part of this repo), not by this script.
|
|
require 'webrick'
|
|
require 'json'
|
|
|
|
# Small install.env parser instead of a dotenv gem dependency - install.env
|
|
# deliberately has a simple KEY="value" format (see there), not YAML.
|
|
def load_install_env(pfad)
|
|
werte = {}
|
|
File.readlines(pfad).each do |zeile|
|
|
zeile = zeile.strip
|
|
next if zeile.empty? || zeile.start_with?('#')
|
|
schluessel, wert = zeile.split('=', 2)
|
|
werte[schluessel] = wert.to_s.sub(/\A"/, '').sub(/"\z/, '')
|
|
end
|
|
werte
|
|
end
|
|
|
|
INSTALL_ENV = load_install_env(File.expand_path('../install.env', __dir__))
|
|
|
|
# Explicit paths from install.env, not derived from WEBSITE_REPO - a
|
|
# derived name (e.g. the repo's last path segment) looks unique per
|
|
# instance but isn't guaranteed to be: found live when a second Ableger
|
|
# instance on the same account derived the same "website.git" hook path as
|
|
# an unrelated first one, so its trigger button silently ran the wrong
|
|
# deploy. Only relevant to change on a fork running more than one instance
|
|
# (or another deploy pipeline with a colliding name) on the same server.
|
|
STATUS_FILE = File.expand_path(INSTALL_ENV.fetch('DEPLOY_STATUS_FILE'))
|
|
HOOK = File.expand_path(INSTALL_ENV.fetch('DEPLOY_HOOK_PATH'))
|
|
DOCROOT = INSTALL_ENV.fetch('DEPLOY_DOCROOT')
|
|
PORT = INSTALL_ENV.fetch('STATUS_SERVER_PORT').to_i
|
|
|
|
def status_body
|
|
File.exist?(STATUS_FILE) ? File.read(STATUS_FILE) : { error: 'noch kein Deploy-Status vorhanden' }.to_json
|
|
end
|
|
|
|
server = WEBrick::HTTPServer.new(Port: PORT, BindAddress: '0.0.0.0', DocumentRoot: DOCROOT, Logger: WEBrick::Log.new($stdout), AccessLog: [[$stdout, WEBrick::AccessLog::COMBINED_LOG_FORMAT]])
|
|
|
|
server.mount_proc '/admin/status/api/data.json' do |req, res|
|
|
res['Content-Type'] = 'application/json; charset=utf-8'
|
|
res.body = status_body
|
|
end
|
|
|
|
server.mount_proc '/admin/status/api/trigger' do |req, res|
|
|
res['Content-Type'] = 'application/json; charset=utf-8'
|
|
if req.request_method != 'POST'
|
|
res.status = 405
|
|
res.body = { error: 'nur POST erlaubt' }.to_json
|
|
next
|
|
end
|
|
# Runs synchronously - the hook itself protects against an already
|
|
# running cron/trigger run with a non-blocking flock (see hook script):
|
|
# if one is already running, this call aborts immediately with a
|
|
# corresponding message in the status instead of waiting or duplicating -
|
|
# just click again shortly after.
|
|
system(HOOK)
|
|
res.body = status_body
|
|
end
|
|
|
|
trap('INT') { server.shutdown }
|
|
trap('TERM') { server.shutdown }
|
|
server.start
|