From 45ee6b665e89d99b00f1d7350890954e3f6ccbd2 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Wed, 27 Jul 2022 11:17:38 +0200 Subject: [PATCH] Add a resolver This commit adds a resolver to the toplevel Distillery domain, to allow resolving domains globally. --- README.md | 10 ++++++ distillery/lib/10_config.sh | 2 ++ distillery/lib/30_slug.sh | 8 +++-- .../barrel/wisskiutils/list_uri_prefixes.php | 19 ++++++++++++ .../compose/resolver/docker-compose.yml | 29 +++++++++++++++++ .../resources/compose/self/docker-compose.yml | 1 + .../resources/compose/web/docker-compose.yml | 4 +-- .../resources/templates/docker-env/resolver | 9 ++++++ distillery/system_install.sh | 16 +++++++++- distillery/system_update.sh | 7 +++-- distillery/update_prefix_config.sh | 31 +++++++++++++++++++ 11 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 distillery/resources/compose/barrel/wisskiutils/list_uri_prefixes.php create mode 100644 distillery/resources/compose/resolver/docker-compose.yml create mode 100644 distillery/resources/templates/docker-env/resolver create mode 100644 distillery/update_prefix_config.sh diff --git a/README.md b/README.md index 4f29f14..54d8c7a 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,16 @@ ssh -p 2222 -L localhost:7200:triplestore:7200 -L localhost:8080:phpmyadmin:8080 This will make GraphDB and PhpMyAdmin available at `localhost:7200` and `localhost:8080` for the duration of the connection. +### Resolver + +In order to resolve WissKI URIs globally, we make use of [wdresolve](https://github.com/FAU-CDI/wdresolve). +This can be queried with a single URI, and will be redirected to the page of the corresponding WissKI Entity. +This is deployed under `/go/` path of the top-level domain. + +For example, if the domain name of the distillery instance is `wisski.example.com`, then the resolver would respond to queries like `https://wisski.example.com/go/?uri=https://first.wisski.example.com/content/123`. +The resolver configuration is automatically updated by the `update_prefix_config.sh` script. +It should not be neccessary to reload this configuration manually, as it is automatically called during `system_update.sh`. + ## License This project and associated files in this repository are licensed as follows: diff --git a/distillery/lib/10_config.sh b/distillery/lib/10_config.sh index e40c726..93600d7 100644 --- a/distillery/lib/10_config.sh +++ b/distillery/lib/10_config.sh @@ -247,6 +247,8 @@ GRAPHDB_AUTH_FLAGS="--user $(printf "admin:%s" "$GRAPHDB_ADMIN_PASSWORD")" # paths to composer things DEPLOY_WEB_DIR="$DEPLOY_ROOT/core/web" DEPLOY_SELF_DIR="$DEPLOY_ROOT/core/self" +DEPLOY_RESOLVER_DIR="$DEPLOY_ROOT/core/resolver" +DEPLOY_PREFIX_CONFIG="$DEPLOY_RESOLVER_DIR/prefix.cfg" DEPLOY_TRIPLESTORE_DIR="$DEPLOY_ROOT/core/triplestore" DEPLOY_SQL_DIR="$DEPLOY_ROOT/core/sql" DEPLOY_SSH_DIR="$DEPLOY_ROOT/core/ssh" diff --git a/distillery/lib/30_slug.sh b/distillery/lib/30_slug.sh index 66831da..b312033 100644 --- a/distillery/lib/30_slug.sh +++ b/distillery/lib/30_slug.sh @@ -36,8 +36,12 @@ SLUG="$1" # Compute the domain name for this instance. # Also lowercase the domain name for consistency. -INSTANCE_DOMAIN="$SLUG.$DEFAULT_DOMAIN" -INSTANCE_DOMAIN="$(echo "$INSTANCE_DOMAIN" | tr '[:upper:]' '[:lower:]')" +function compute_instance_domain() { + INSTANCE_DOMAIN="$1.$DEFAULT_DOMAIN" + INSTANCE_DOMAIN="$(echo "$INSTANCE_DOMAIN" | tr '[:upper:]' '[:lower:]')" + echo "$INSTANCE_DOMAIN" +} +INSTANCE_DOMAIN="$(compute_instance_domain "$SLUG")" # Next we need a username base. # This will be used as a username across the system (linux), MySQL and GraphDB. diff --git a/distillery/resources/compose/barrel/wisskiutils/list_uri_prefixes.php b/distillery/resources/compose/barrel/wisskiutils/list_uri_prefixes.php new file mode 100644 index 0000000..f9a52ba --- /dev/null +++ b/distillery/resources/compose/barrel/wisskiutils/list_uri_prefixes.php @@ -0,0 +1,19 @@ +getStorage('wisski_salz_adapter'); +foreach ($storage->loadMultiple() as $adapter) { + // read the configuration, and check if we have a default graph + $conf = $adapter->getEngine()->getConfiguration(); + if(!array_key_exists('default_graph', $conf)) { + continue; + } + + // and echo it out + echo $conf['default_graph'] . "\n"; +} \ No newline at end of file diff --git a/distillery/resources/compose/resolver/docker-compose.yml b/distillery/resources/compose/resolver/docker-compose.yml new file mode 100644 index 0000000..a9c15cc --- /dev/null +++ b/distillery/resources/compose/resolver/docker-compose.yml @@ -0,0 +1,29 @@ +version: "3.7" + +services: + wdresolve: + image: ghcr.io/fau-cdi/wdresolve:latest + restart: always + volumes: + - "${PREFIX_FILE}:/prefixes.cfg:ro" + environment: + # port and hostname for this image to use + VIRTUAL_HOST: ${VIRTUAL_HOST} + VIRTUAL_PORT: 8080 + VIRTUAL_PATH: /go/ + + # optional letsencrypt email + LETSENCRYPT_HOST: ${LETSENCRYPT_HOST} + LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL} + + # default and legacy domain + DEFAULT_DOMAIN: ${DEFAULT_DOMAIN} + LEGACY_DOMAIN: ${LEGACY_DOMAIN} + + # the prefix file + PREFIX_FILE: /prefixes.cfg + +networks: + default: + external: + name: distillery diff --git a/distillery/resources/compose/self/docker-compose.yml b/distillery/resources/compose/self/docker-compose.yml index 585c777..496ca3b 100644 --- a/distillery/resources/compose/self/docker-compose.yml +++ b/distillery/resources/compose/self/docker-compose.yml @@ -10,6 +10,7 @@ services: # port and hostname for this image to use VIRTUAL_HOST: ${VIRTUAL_HOST} VIRTUAL_PORT: 8080 + VIRTUAL_PATH: / # optional letsencrypt email LETSENCRYPT_HOST: ${LETSENCRYPT_HOST} diff --git a/distillery/resources/compose/web/docker-compose.yml b/distillery/resources/compose/web/docker-compose.yml index b9352d5..6b24791 100644 --- a/distillery/resources/compose/web/docker-compose.yml +++ b/distillery/resources/compose/web/docker-compose.yml @@ -2,7 +2,7 @@ version: "3.7" services: nginx-proxy: - image: nginxproxy/nginx-proxy:alpine + image: ghcr.io/nginx-proxy/nginx-proxy:alpine environment: - DEFAULT_HOST=${DEFAULT_HOST} ports: @@ -23,7 +23,7 @@ services: - default letsencrypt-nginx-proxy-companion: - image: jrcs/letsencrypt-nginx-proxy-companion + image: docker.io/nginxproxy/acme-companion:latest volumes: - "/var/run/docker.sock:/var/run/docker.sock:ro" - "htpasswd:/etc/nginx/htpasswd" diff --git a/distillery/resources/templates/docker-env/resolver b/distillery/resources/templates/docker-env/resolver new file mode 100644 index 0000000..1acd9a0 --- /dev/null +++ b/distillery/resources/templates/docker-env/resolver @@ -0,0 +1,9 @@ +VIRTUAL_HOST=${VIRTUAL_HOST} + +PREFIX_FILE=${PREFIX_FILE} + +DEFAULT_DOMAIN=${DEFAULT_DOMAIN} +LEGACY_DOMAIN=${LEGACY_DOMAIN} + +LETSENCRYPT_HOST=${LETSENCRYPT_HOST} +LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL} \ No newline at end of file diff --git a/distillery/system_install.sh b/distillery/system_install.sh index 84cb1e3..334a0fa 100755 --- a/distillery/system_install.sh +++ b/distillery/system_install.sh @@ -45,10 +45,11 @@ apt-get install -y python3-pip libffi-dev log_info "=> Installing docker-compose" pip3 install --upgrade docker-compose -log_info "=> Creating docker-compose directories" +log_info "=> Creating docker-compose directories and files" mkdir -p "$DEPLOY_INSTANCES_DIR" mkdir -p "$DEPLOY_WEB_DIR" mkdir -p "$DEPLOY_SELF_DIR" +mkdir -p "$DEPLOY_RESOLVER_DIR" mkdir -p "$DEPLOY_SSH_DIR" mkdir -p "$DEPLOY_TRIPLESTORE_DIR" mkdir -p "$DEPLOY_SQL_DIR" @@ -69,6 +70,10 @@ load_template "docker-env/web" \ log_info "=> Creating 'docker-compose' files for the 'self'. " install_resource_dir "compose/self" "$DEPLOY_SELF_DIR" +log_info "=> Creating 'docker-compose' files for the 'resolver'. " +install_resource_dir "compose/resolver" "$DEPLOY_RESOLVER_DIR" +touch "$DEPLOY_PREFIX_CONFIG" + log_info "=> Creating 'docker-compose' files for the 'ssh'. " install_resource_dir "compose/ssh" "$DEPLOY_SSH_DIR" @@ -86,6 +91,15 @@ load_template "docker-env/self" \ "OVERRIDES_FILE" "${SELF_OVERRIDES_FILE}" \ > "$DEPLOY_SELF_DIR/.env" +log_info " => Writing 'resolver' configuration file" +load_template "docker-env/resolver" \ + "VIRTUAL_HOST" "${SELF_DOMAIN_SPEC}" \ + "LETSENCRYPT_HOST" "${LETSENCRYPT_HOST}" \ + "LETSENCRYPT_EMAIL" "${LETSENCRYPT_EMAIL}" \ + "PREFIX_FILE" "${DEPLOY_PREFIX_CONFIG}" \ + "DEFAULT_DOMAIN" "${DEFAULT_DOMAIN}" \ + "LEGACY_DOMAIN" "${SELF_EXTRA_DOMAINS}" \ + > "$DEPLOY_RESOLVER_DIR/.env" # copy over the directory log_info "=> Creating 'docker-compose' files for the 'triplestore'. " diff --git a/distillery/system_update.sh b/distillery/system_update.sh index 159a331..8b5727e 100755 --- a/distillery/system_update.sh +++ b/distillery/system_update.sh @@ -12,6 +12,9 @@ update_stack "$DEPLOY_WEB_DIR" log_info "=> Rebuilding and restarting 'self' stack" update_stack "$DEPLOY_SELF_DIR" +log_info "=> Rebuilding and restarting 'resolver' stack" +update_stack "$DEPLOY_RESOLVER_DIR" + # build and start the ssh server log_info "=> Rebuilding and restarting 'ssh' stack" update_stack "$DEPLOY_SSH_DIR" @@ -24,7 +27,7 @@ update_stack "$DEPLOY_TRIPLESTORE_DIR" log_info "=> Rebuilding and restarting 'sql' stack" update_stack "$DEPLOY_SQL_DIR" -# TODO: Iterate over all the instance -# and a pull_and_update +log_info " => Updating Prefix Config" +bash update_prefix_config.sh log_info "=> System up-to-date. " \ No newline at end of file diff --git a/distillery/update_prefix_config.sh b/distillery/update_prefix_config.sh new file mode 100644 index 0000000..f78c0ad --- /dev/null +++ b/distillery/update_prefix_config.sh @@ -0,0 +1,31 @@ +#!/bin/bash +set -e + +# read the lib/shared.sh +DISABLE_LOG=1 +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +cd "$DIR" +source "$DIR/lib/lib.sh" +DISABLE_LOG=0 + +log_info " => Writing prefix configuration" + +echo -n "# Prefix configuration, last updated on" | tee "$DEPLOY_PREFIX_CONFIG" +date | tee -a "$DEPLOY_PREFIX_CONFIG" + +# update all the instances +for slug in $(sql_bookkeep_list); do + INSTANCE_DOMAIN="$(compute_instance_domain "$slug")" + echo "$INSTANCE_DOMAIN:" | tee -a "$DEPLOY_PREFIX_CONFIG" + + read -r INSTANCE_BASE_DIR MYSQL_DATABASE MYSQL_USER GRAPHDB_REPO GRAPHDB_USER GRAPHDB_PASSWORD <<< "$(sql_bookkeep_load "${slug}" "filesystem_base,sql_database,sql_user,graphdb_repository,graphdb_user,graphdb_password" | tail -n +2)" + + pushd "$INSTANCE_BASE_DIR" > /dev/null + docker-compose exec barrel /user_shell.sh -c "drush php:script /wisskiutils/list_uri_prefixes.php" | tee -a "$DEPLOY_PREFIX_CONFIG" + popd > /dev/null +done + +log_info " => Restarting resolver" + +cd "$DEPLOY_RESOLVER_DIR" +docker-compose restart \ No newline at end of file