diff --git a/README.md b/README.md index 53be052..f56bccf 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,8 @@ fails if they do not become healthy. Every update is another | Setting | Default | |-------------------|----------------------------------------------------| | `CONTENT_DIR` | asked on the first deploy | -| `JOBS_HOST_DIR` | `/srv`, mounted read only into the jobs service | +| `JOBS_SRV_DIR` | `/srv`, read only for jobs at `/host/srv` | +| `JOBS_NGINX_DIR` | `/etc/nginx`, read only for jobs at `/host/nginx` | | `SITE_URL` | `https://security-command.org` | | `LEGAL_PAGES` | `impressum/imprint:impressum.md,datenschutz/privacy:datenschutz.md` | | `REGISTRY` | `git.security-command.org/securitycommand` | diff --git a/apps/jobs/src/config.ts b/apps/jobs/src/config.ts index 01094b2..817eb79 100644 --- a/apps/jobs/src/config.ts +++ b/apps/jobs/src/config.ts @@ -3,7 +3,7 @@ export type JobsConfig = { /** Directory of the job scripts; relative script paths start here. */ scriptsDir: string; contentDir: string; - /** Read only view of /srv on the host. Watch paths are relative to it and must stay inside. */ + /** Read only views of the host, /srv as srv and the nginx config as nginx. Watch paths are relative to it and must stay inside. */ hostDir: string; /** Builtin jobs of the backend are triggered through files in this directory. */ triggerDir: string; @@ -21,7 +21,7 @@ export function loadConfig(env: NodeJS.ProcessEnv): JobsConfig jobsFile: env.JOBS_FILE ?? '/config/jobs.toml', scriptsDir: env.SCRIPTS_DIR ?? '/jobs', contentDir: env.CONTENT_DIR ?? '/content', - hostDir: env.HOST_DIR ?? '/host/srv', + hostDir: env.HOST_DIR ?? '/host', triggerDir: env.TRIGGER_DIR ?? '/run/jobs/triggers', controlSocket: env.CONTROL_SOCKET ?? '/run/jobs/control.sock', killGraceMs: killGraceSeconds * 1000, diff --git a/deploy/compose.yml b/deploy/compose.yml index e073aaf..271217c 100644 --- a/deploy/compose.yml +++ b/deploy/compose.yml @@ -91,14 +91,15 @@ services: JOBS_FILE: /config/jobs.toml SCRIPTS_DIR: /jobs CONTENT_DIR: /content - HOST_DIR: /host/srv + HOST_DIR: /host TRIGGER_DIR: /run/jobs/triggers CONTROL_SOCKET: /run/jobs/control.sock volumes: - ${CONTENT_DIR}:/content - ./config:/config - ./jobs:/jobs:ro - - ${JOBS_HOST_DIR:-/srv}:/host/srv:ro + - ${JOBS_SRV_DIR:-/srv}:/host/srv:ro + - ${JOBS_NGINX_DIR:-/etc/nginx}:/host/nginx:ro - ./runtime/jobs:/run/jobs healthcheck: test: ["CMD", "jobctl", "list"] diff --git a/deploy/env.example b/deploy/env.example index 4f223c1..faf54a7 100644 --- a/deploy/env.example +++ b/deploy/env.example @@ -2,9 +2,10 @@ # read it; jobs may write into it. CONTENT_DIR= -# Host directory job scripts see read only at /host/srv, for example to -# describe the services running next to this one. -JOBS_HOST_DIR=/srv +# Host directories job scripts see read only at /host/srv and /host/nginx, +# for example to describe the services running next to this one. +JOBS_SRV_DIR=/srv +JOBS_NGINX_DIR=/etc/nginx # Public address of the site, used by the sitemap. SITE_URL=https://security-command.org diff --git a/deploy/jobs.example.toml b/deploy/jobs.example.toml index 794d2f4..694a1f7 100644 --- a/deploy/jobs.example.toml +++ b/deploy/jobs.example.toml @@ -13,9 +13,10 @@ # every run at a fixed interval: "30s", "15m", "1h", "1d"; at least 1s # cron run on a cron schedule in UTC: "0 4 * * *" # on events: "start", "content-change" (builtin jobs only) and "watch" -# watch paths relative to /srv on the host; needs on = ["watch"]. A -# directory is watched with everything inside it. The job runs -# once things have been quiet for 2s. +# watch paths relative to HOST_DIR, like "srv/gitea/compose.yml" or +# "nginx/sites-enabled"; needs on = ["watch"]. A directory is +# watched with everything inside it. The job runs once things +# have been quiet for 2s. # then jobs to trigger after a successful run, builtin ones included # timeout run time after which the job is cancelled; default "5m" # env table of extra environment variables @@ -27,7 +28,8 @@ # git and jq available, and get these variables: # JOB_ID the id of the job # CONTENT_DIR the content directory, writable -# HOST_DIR /srv of the host, read only +# HOST_DIR /host, with /srv of the host in srv/ and its nginx config +# in nginx/, both read only # # Cancelling sends SIGTERM to the script and everything it started, then # SIGKILL after 5s. Processes a script leaves behind are killed when it exits. @@ -37,14 +39,18 @@ # docker exec security-command-jobs-1 jobctl run services # docker exec security-command-jobs-1 jobctl cancel services -# Describes the services next to this one in services/, whenever one of their -# compose files changes, and rebuilds the static site right away. +# Writes links/ and services/ from the nginx config and the directories in +# /srv. Copy deploy/jobs/services.sh to jobs/ for it. It runs when the nginx +# config changes and every hour for new services, and rebuilds the static +# site right away. EXCLUDE lists /srv directories to leave out. [[job]] id = "services" path = "services.sh" +every = "1h" on = ["start", "watch"] -watch = ["gitea/compose.yml", "nextcloud/compose.yml"] +watch = ["nginx"] then = ["index"] +env = { EXCLUDE = "lost+found" } # Mirrors a public repository into the content once a night. [[job]] diff --git a/deploy/jobs/services.sh b/deploy/jobs/services.sh new file mode 100755 index 0000000..b081e73 --- /dev/null +++ b/deploy/jobs/services.sh @@ -0,0 +1,127 @@ +#!/usr/bin/env bash +# Describes the services of this host in the content. links/ gets one file per +# domain nginx serves, services/ one Markdown file per directory in /srv with +# its images and domains. A domain belongs to a service when nginx proxies it +# to a port the service's compose file publishes, or when the directory has +# the domain's name. Both directories belong to this script and are only +# replaced when their contents change. +# +# Uses CONTENT_DIR and HOST_DIR from the jobs service. EXCLUDE may list /srv +# directory names to leave out, separated by spaces. +set -euo pipefail + +srv=$HOST_DIR/srv +nginx=$HOST_DIR/nginx +exclude=" ${EXCLUDE:-} " + +# Prints "domain port" for every server_name of nginx, with the local port its +# server block proxies to, directly or through an upstream, or "-" without one. +nginx_sites() { + local files=() dir file + for dir in sites-enabled conf.d; do + for file in "$nginx/$dir"/*; do + [[ -f $file ]] && files+=("$file") + done + done + ((${#files[@]})) || return 0 + + awk ' + { + sub(/#.*/, "") + if (match($0, /upstream[ \t]+[A-Za-z0-9_.-]+/)) { + split(substr($0, RSTART, RLENGTH), words, /[ \t]+/) + upstream = words[2]; upstream_depth = depth + } + if (upstream != "" && match($0, /server[ \t]+[^;]*:[0-9]+/)) { + port = substr($0, RSTART, RLENGTH); sub(/.*:/, "", port); ports[upstream] = port + } else if (upstream == "" && $0 ~ /(^|[ \t;{])server[ \t]*\{/) { + blocks++; block_depth[blocks] = depth; current = blocks + } + if (current && match($0, /server_name[ \t]+[^;]+/)) { + value = substr($0, RSTART, RLENGTH); sub(/server_name[ \t]+/, "", value); names[current] = names[current] " " value + } + if (current && match($0, /proxy_pass[ \t]+[a-z]+:\/\/[^;\/]+/)) { + value = substr($0, RSTART, RLENGTH); sub(/.*:\/\//, "", value); targets[current] = value + } + depth += gsub(/\{/, "{") - gsub(/\}/, "}") + if (upstream != "" && depth <= upstream_depth) upstream = "" + if (current && depth <= block_depth[current]) current = 0 + } + END { + for (block = 1; block <= blocks; block++) { + target = targets[block]; port = "-" + if (target ~ /:[0-9]+$/) { port = target; sub(/.*:/, "", port) } + else if (target in ports) port = ports[target] + count = split(names[block], list, /[ \t]+/) + for (i = 1; i <= count; i++) if (list[i] ~ /\./ && list[i] !~ /[*~]/) print list[i], port + } + } + ' "${files[@]}" +} + +compose_file() { + local name + for name in compose.yml compose.yaml docker-compose.yml docker-compose.yaml; do + [[ -f $1/$name ]] && { echo "$1/$name"; return; } + done + return 0 +} + +out=$(mktemp -d "$CONTENT_DIR/.services.XXXXXX") +trap 'rm -rf "$out"' EXIT +mkdir "$out/links" "$out/services" + +# A domain served by a redirect block and a proxy block keeps the proxied port. +declare -A domain_port=() +while read -r domain port; do + [[ ${domain_port[$domain]:--} == - ]] && domain_port[$domain]=$port +done < <(nginx_sites | sort -u) + +domains=() +((${#domain_port[@]})) && mapfile -t domains < <(printf '%s\n' "${!domain_port[@]}" | sort) +for domain in "${domains[@]}"; do + echo "https://$domain" > "$out/links/$domain" +done + +services=0 +for dir in "$srv"/*/; do + [[ -d $dir ]] || continue + name=$(basename "$dir") + [[ $name == .* || $exclude == *" $name "* ]] && continue + + compose=$(compose_file "$dir") + images=() host_ports=() + if [[ -n $compose ]]; then + mapfile -t images < <(sed -nE 's/^[[:space:]]*image:[[:space:]]*["'\'']?([^"'\''[:space:]]+).*/\1/p' "$compose" | grep -v '\$' | sort -u) + mapfile -t host_ports < <(grep -oE '[0-9]{2,5}\}?:[0-9]{1,5}(/(tcp|udp))?["'\'']?[[:space:]]*$' "$compose" | sed -E 's/\}?:.*//' | sort -u) + fi + + { + echo "# $name" + echo + found=0 + for domain in "${domains[@]}"; do + match=0 + [[ $domain == "$name" ]] && match=1 + for port in "${host_ports[@]}"; do + [[ ${domain_port[$domain]} == "$port" ]] && match=1 + done + ((match)) && { echo "- url: https://$domain"; found=1; } + done + ((found)) || echo "- url: none, internal only" + for image in "${images[@]}"; do echo "- image: $image"; done + } > "$out/services/$name.md" + services=$((services + 1)) +done + +changed=() +for part in links services; do + diff -rq "$out/$part" "$CONTENT_DIR/$part" >/dev/null 2>&1 && continue + rm -rf "$CONTENT_DIR/.$part.old" + [[ -e $CONTENT_DIR/$part ]] && mv "$CONTENT_DIR/$part" "$CONTENT_DIR/.$part.old" + mv "$out/$part" "$CONTENT_DIR/$part" + rm -rf "$CONTENT_DIR/.$part.old" + changed+=("$part") +done + +echo "${#domain_port[@]} domain(s), $services service(s), updated: ${changed[*]:-nothing}"