Add service discovery script to jobs, extend nginx and host directory support. Update environment variables, configs, and examples.
This commit is contained in:
1 parent
5305687f98
commit
3aecb3a631
6 files changed
+151
-15
No files matched your search
@@ -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` |
|
||||
|
||||
@@ -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,
|
||||
|
||||
+3
-2
@@ -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"]
|
||||
|
||||
+4
-3
@@ -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
|
||||
|
||||
@@ -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]]
|
||||
|
||||
Executable
+127
@@ -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}"
|
||||
Reference in new issue
Block a user