#!/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 (~/repos/ableger.git/ # hooks/post-receive on the server, not part of this repo - see # the installation guide on the project website), 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__)) STATUS_FILE = File.expand_path('~/deploy/status.json') # Bare repo name (see the installation guide on the project website, Abschnitt 4) is derived from # the website repo name, not maintained separately. HOOK = File.expand_path("~/repos/#{INSTALL_ENV.fetch('WEBSITE_REPO').split('/').last}.git/hooks/post-receive") 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