diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-11-25 09:13:11 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-11-25 09:13:11 +0100 |
commit | b7a8611707d393abd6857c8f7986f66a1ea906ab (patch) | |
tree | e6c56cfa503b3c78229bc5206370721dda77a25c | |
parent | 28ba79385cd1b80a99629d35bd535adc5f817497 (diff) | |
download | koszko-org-server-b7a8611707d393abd6857c8f7986f66a1ea906ab.tar.gz koszko-org-server-b7a8611707d393abd6857c8f7986f66a1ea906ab.zip |
add hydrilla.koszko.org virtualhost
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Makefile | 47 | ||||
-rw-r--r-- | container.scm | 77 | ||||
-rwxr-xr-x | guix-container.sh | 3 | ||||
-rw-r--r-- | guix-module-dir/hydrilla-json-schemas.scm | 90 | ||||
-rw-r--r-- | hosts-extra | 2 | ||||
-rw-r--r-- | hydrilla-wsgi.py | 26 | ||||
m--------- | subrepos/hydrilla | 0 |
9 files changed, 232 insertions, 18 deletions
@@ -9,3 +9,5 @@ log pidfile hosts *.touchfile +schemas/ +sample-malcontent/ diff --git a/.gitmodules b/.gitmodules index 06004a1..e343c8e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "hydrilla-website"] path = subrepos/hydrilla-website url = ../hydrilla-website +[submodule "hydrilla"] + path = subrepos/hydrilla + url = ../pydrilla @@ -16,7 +16,8 @@ GUIX_TM = $(GUIX) time-machine --commit=$(GUIX_COMMIT) -- GUIX_LOAD_PATHS = $$( \ find subrepos/ -mindepth 1 -maxdepth 1 \ -exec printf ' -L %s/guix-module-dir/' {} ';' \ - ) + ) \ + -L ./guix-module-dir/ GUIX_SYS_CONTAINER = $(GUIX_TM) system container $(GUIX_LOAD_PATHS) @@ -26,27 +27,31 @@ KOSZKO_ORG_WEBSITE_INFO = \ subrepos/koszko-org-website/src/koszko_org_website.egg-info/PKG-INFO HYDRILLA_WEBSITE_INFO = \ subrepos/hydrilla-website/src/hydrilla_website.egg-info/PKG-INFO +HYDRILLA_INFO = subrepos/hydrilla/src/hydrilla.egg-info/PKG-INFO + +ALL_EGG_INFOS = \ + $(KOSZKO_ORG_WEBSITE_INFO) \ + $(HYDRILLA_WEBSITE_INFO) \ + $(HYDRILLA_INFO) TEST_ROOT_DIR = "/tmp/$$(pwd | sed 's|/|!|g')!!test-root" -all: | container-runner.touchfile log +all: | container-runner.touchfile log sample-malcontent $(KOSZKO_ORG_WEBSITE_INFO): $(MAKE) -C subrepos/koszko-org-website dist $(HYDRILLA_WEBSITE_INFO): $(MAKE) -C subrepos/hydrilla-website dist +$(HYDRILLA_INFO): + $(MAKE) -C subrepos/hydrilla dist -container-runner: | container.scm \ - $(KOSZKO_ORG_WEBSITE_INFO) \ - $(HYDRILLA_WEBSITE_INFO) - $(GUIX_SYS_CONTAINER) container.scm -r container-runner - touch container-runner.touchfile +container-runner: | container.scm $(ALL_EGG_INFOS) hydrilla-wsgi.py +container-runner.touchfile: container.scm $(ALL_EGG_INFOS) hydrilla-wsgi.py -container-runner.touchfile: container.scm \ - $(KOSZKO_ORG_WEBSITE_INFO) \ - $(HYDRILLA_WEBSITE_INFO) +container-runner container-runner.touchfile: $(MAKE) clean-runner - $(MAKE) container-runner + $(GUIX_SYS_CONTAINER) container.scm -r container-runner + touch container-runner.touchfile hosts: hosts-extra /etc/hosts cat $^ > $@ @@ -54,9 +59,16 @@ hosts: hosts-extra /etc/hosts log: ln -sf $(TEST_ROOT_DIR)/var/log/guix-container $@ +sample-malcontent: + mkdir $@ + printf 'hydrilla-builder -s tests/source-package-example/ -d %s' \ + "$$(realpath $@)" | \ + make -C subrepos/hydrilla shell-with-haketilo + HYDRILLA_WEBSITE_ETC_DIR = $(TEST_ROOT_DIR)/etc/guix-container/hydrilla-website +MALCONTENT_DIR = $(TEST_ROOT_DIR)/var/lib/hydrilla/malcontent_dirs -prepare-test-root: +prepare-test-root: sample-malcontent rm -rf $(TEST_ROOT_DIR) @# Prepare replacement `/var/www` mkdir -p $(TEST_ROOT_DIR)/var/www/koszko.org/html @@ -68,6 +80,10 @@ prepare-test-root: printf 'test non-secret\n' > $(HYDRILLA_WEBSITE_ETC_DIR)/secret.txt chgrp -R 133 $(HYDRILLA_WEBSITE_ETC_DIR) chmod 640 $(HYDRILLA_WEBSITE_ETC_DIR)/secret.txt + @# Prepare replacement `/var/lib/hydrilla` + mkdir -p $(MALCONTENT_DIR)/api_v2 + ln -sf api_v2 $(MALCONTENT_DIR)/api_v1 + cp -r $</* $(MALCONTENT_DIR)/api_v2/ GUIX_CONTAINER_FLAGS = -e ./container-runner -p ./pidfile -r $(TEST_ROOT_DIR) @@ -101,12 +117,15 @@ reinstall: rm -rf /usr/local/bin/guix-container $(MAKE) install +build-hydrilla-json-schemas: + $(GUIX_TM) build -L ./guix-module-dir/ hydrilla-json-schemas + clean-runner: rm -rf container-runner container-runner.touchfile clean: clean-runner $(MAKE) -C subrepos/koszko-org-website clean - rm -rf log hosts + rm -rf log hosts schemas sample-malcontent .PHONY: all \ clean-runner clean \ @@ -114,4 +133,4 @@ clean: clean-runner start-container stop-container restart-container \ enter-container fake-client \ install reinstall \ - sdists koszko-org-wesite-sdist + build-hydrilla-json-schemas diff --git a/container.scm b/container.scm index c50a75b..39163d9 100644 --- a/container.scm +++ b/container.scm @@ -4,13 +4,25 @@ ;; ;; Available under the terms of Creative Commons Zero v1.0 Universal. -(use-modules (gnu)) -(use-modules (koszko-org-website)) -(use-modules (hydrilla-website)) +(use-modules (gnu) + (koszko-org-website) + (hydrilla-website) + (hydrilla-json-schemas) + (hydrilla) + ;; The following 4 are needed to construct GUIX_PYTHONPATH for + ;; Hydrilla WSGI scripts. + (guix build-system python) + (guix packages) + (guix search-paths) + (guix modules)) (use-package-modules web) +(use-package-modules python) (use-service-modules web) (use-service-modules shepherd) +(define %here + (getcwd)) + (define* (simple-wsgi-alias-gexp package wsgi-path #:key (aliased-path "/")) #~(let* ((wsgi-file (string-append #$package #$wsgi-path))) (format #f @@ -102,6 +114,62 @@ (simple-wsgi-alias-gexp hydrilla-website "/share/hydrilla-website/wsgi.py")))) +(define %python-path-spec-sexp + (search-path-specification->sexp + (guix-pythonpath-search-path (package-version (default-python))))) + +(define %hydrilla-pythonpath-inputs + (cons hydrilla (map cadr (package-transitive-target-inputs hydrilla)))) + +(define %hydrilla-pythonpath-gexp + (with-imported-modules (source-module-closure '((guix search-paths))) + #~(begin + (use-modules (guix search-paths)) + (let ((evaluated-list (evaluate-search-paths + (list (sexp->search-path-specification + '#$%python-path-spec-sexp)) + '#$%hydrilla-pythonpath-inputs))) + (cdar evaluated-list))))) + +(define %hydrilla-wsgi-regex + "^/api_v[^/]+/((resource|mapping)/[^/]+[.]json|query|list_all)$") + +(define %hydrilla-virtualhost + (httpd-virtualhost + "*:80" + (list "\ + ServerName hydrilla.koszko.org + ServerAlias www.hydrilla.koszko.org + ServerAdmin koszko@koszko.org + + <If \"%{HTTP_HOST} != 'hydrilla.koszko.org'\"> + Redirect permanent / http://hydrilla.koszko.org/ + </If> + " + #~(format #f "Alias /schemas ~a\n" + (string-append #$hydrilla-json-schemas + "/share/hydrilla-json-schemas")) + "\ + + DocumentRoot /var/lib/hydrilla/malcontent_dirs + <Location ~ \"^/api_v[^/]+/(resource|mapping)/\"> + ForceType application/json + </Location> + + WSGIScriptReloading On + " + (format #f "SetEnvIf Request_URI ~s MALCONENT_DIR=~a\n" + "^/(api_v[0-9]+)/" + "/var/lib/hydrilla/malcontent_dirs/$1") + #~(format #f "SetEnvIf Request_URI ~s HYDRILLA_GUIX_PYTHONPATH=~a\n" + "^/api_v[0-9]+/" + #$%hydrilla-pythonpath-gexp) + #~(format #f "WSGIScriptAliasMatch ~s ~a\n" + #$%hydrilla-wsgi-regex + (string-append + #$(local-file (string-append %here "/hydrilla-wsgi.py")) + "/$1"))))) + (define %wsgi-module (httpd-module (name "wsgi_module") @@ -168,7 +236,8 @@ %koszkonutek-tmp-virtualhost %hydrillabugs-virtualhost %hachettebugs-virtualhost - %haketilo-virtualhost)) + %haketilo-virtualhost + %hydrilla-virtualhost)) (service (shepherd-service-type 'dummy-network diff --git a/guix-container.sh b/guix-container.sh index afede73..3167ac7 100755 --- a/guix-container.sh +++ b/guix-container.sh @@ -84,10 +84,12 @@ start() { KOSZKO_SIDELOAD_REAL="$HOST_SYSTEM_ROOT"/var/www/koszko.org/html LOG_REAL="$LOG_DIR"/container ETC_REAL="$HOST_SYSTEM_ROOT"/etc/guix-container + VAR_HYDRILLA_REAL="$HOST_SYSTEM_ROOT"/var/lib/hydrilla HTTP_DIR_SHARE_OPT=--share="$KOSZKO_SIDELOAD_REAL"=/srv/http/koszko.org LOG_DIR_SHARE_OPT=--share="$LOG_REAL"=/var/log ETC_DIR_SHARE_OPT=--share="$ETC_REAL"=/etc + VAR_HYDRILLA_DIR_SHARE_OPT=--share="$VAR_HYDRILLA_REAL"=/var/lib/hydrilla mkdir --mode=700 -p "$LOG_DIR" mkdir --mode=700 -p "$LOG_DIR"/container @@ -95,6 +97,7 @@ start() { "$EXECUTABLE" "$HTTP_DIR_SHARE_OPT" \ "$LOG_DIR_SHARE_OPT" \ "$ETC_DIR_SHARE_OPT" \ + "$VAR_HYDRILLA_DIR_SHARE_OPT" \ >> "$LOG_DIR"/stdout.log 2>> "$LOG_DIR"/stderr.log & GUILE_PID=$! diff --git a/guix-module-dir/hydrilla-json-schemas.scm b/guix-module-dir/hydrilla-json-schemas.scm new file mode 100644 index 0000000..a3a8dbe --- /dev/null +++ b/guix-module-dir/hydrilla-json-schemas.scm @@ -0,0 +1,90 @@ +;; SPDX-License-Identifier: CC0-1.0 + +;; Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org> +;; +;; Available under the terms of Creative Commons Zero v1.0 Universal. + +(define-module (hydrilla-json-schemas) + #:use-module (guix packages) + #:use-module (guix gexp) + #:use-module (guix build-system copy) + #:use-module ((guix licenses) #:prefix license:) + #:use-module (ice-9 match) + #:use-module (ice-9 popen) + #:use-module (ice-9 rdelim) + #:use-module (ice-9 regex) + #:use-module (srfi srfi-26)) + +(define %source-dir + (let* ((this-file (search-path %load-path "hydrilla-json-schemas.scm")) + (proj-dir (dirname (dirname this-file)))) + (if (absolute-file-name? proj-dir) + proj-dir + (string-append (getcwd) "/" proj-dir)))) + +(define %schema-git-locations + '(("d94ef4544faac662f49bed41700c9010804b2450" "-2.schema.json") + ("v1.0.1" "-1.0.1.schema.json") + ("v1.0" "-1.schema.json"))) + +(define %schemas-subrepo + (string-append %source-dir "/subrepos/hydrilla/src/hydrilla/schemas/1.x")) + +(define %schemas-dest + (string-append %source-dir "/schemas")) + +(define* (run-git-in-subrepo #:rest args) + (apply open-pipe* (cons* OPEN_READ "git" "-C" %schemas-subrepo args))) + +(define (filter-locations port file-ending) + (let ((regex (string-append "[^[:space:]]+" (regexp-quote file-ending) "$"))) + (filter (lambda (val) (not (unspecified? val))) + (map (lambda (line) + (let ((match-result (string-match regex line))) + (when match-result (match:substring match-result 0)))) + (string-split (read-string port) #\xA))))) + +(define (get-git-file rev filename) + (let* ((port (run-git-in-subrepo "show" (string-append rev ":" filename))) + (contents (read-string port))) + (close-port port) + contents)) + +(define (write-schema-file filename contents) + (call-with-output-file (string-append %schemas-dest "/" filename) + (cut display contents <>))) + +(define (transfer-file rev filename) + (write-schema-file filename (get-git-file rev filename))) + +(define (prepare-schemas-directory) + (when (not (file-exists? %schemas-dest)) + (mkdir %schemas-dest)) + (for-each (match-lambda + ((rev file-ending) + (let* ((port (run-git-in-subrepo "ls-tree" rev)) + (filtered (filter-locations port file-ending))) + (close-port port) + (for-each (cut transfer-file rev <>) filtered)))) + %schema-git-locations) + %schemas-dest) + +(define-public hydrilla-json-schemas + (package + (name "hydrilla-json-schemas") + (version "current") + (source + (local-file (prepare-schemas-directory) #:recursive? #t)) + (build-system copy-build-system) + (arguments + '(#:install-plan '(("." "share/hydrilla-json-schemas/" + #:include (".schema.json"))))) + (home-page "https://git.koszko.org/hydrilla-json-schemas") + (synopsis + "JSON schemas used by Haketilo and Hydrilla") + (description "Haketilo HTTP proxy facilitates viewing of websites while +having their original JavaScript replaced by user-provided scripts. Haketilo +combines the functionalities of content blocker and user script manager. It can +be used with its script repository, Hydrilla. This package makes available the +JSON schemas describing various interfaces of Haketilo and Hydrilla.") + (license license:cc0))) diff --git a/hosts-extra b/hosts-extra index f9d1a88..0debbef 100644 --- a/hosts-extra +++ b/hosts-extra @@ -14,3 +14,5 @@ 10.207.87.2 www.hydrillabugs.koszko.org 10.207.87.2 haketilo.koszko.org 10.207.87.2 www.haketilo.koszko.org +10.207.87.2 hydrilla.koszko.org +10.207.87.2 www.hydrilla.koszko.org diff --git a/hydrilla-wsgi.py b/hydrilla-wsgi.py new file mode 100644 index 0000000..45ec248 --- /dev/null +++ b/hydrilla-wsgi.py @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: CC0-1.0 + +# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org> +# +# Available under the terms of Creative Commons Zero v1.0 Universal. + +import sys + +_application = None + +def application(environ, *args): + global _application + + if _application is None: + sys.path.extend(environ['HYDRILLA_GUIX_PYTHONPATH'].split(':')) + + from hydrilla.server import start_wsgi, config + + current_config = { + **config.load(), + 'malcontent_dir': environ['MALCONENT_DIR'] + } + + _application = start_wsgi(standalone_mode=False, obj=current_config) + + return _application(environ, *args) diff --git a/subrepos/hydrilla b/subrepos/hydrilla new file mode 160000 +Subproject 0792f29ea1fcc4386246cfc26bf4b2fbe3a5f9d |