Run the broker, the relay and the worker in the stand

Embed the checked lab 5 preparation: RabbitMQ 4.1.8 pinned by digest
with a fixed node hostname, a volume, a read-only root and 512 MiB,
whose topology (vhost messenger, exchange attachments, quorum queue
attachments.process with a dead letter exchange, delivery limit 10 and
reject-publish overflow) and users come from definitions imported on
every boot. The relay may only write to the exchange, the worker may
only read the queue, configure belongs to the operator, there is no
guest, and the management UI is published on 127.0.0.1 only. The relay
and the worker run from the application image with a heartbeat health
check, init, a read-only root and their own limits; the worker gets its
own S3 user that reads staging and writes originals and variants, and
s3-init creates it idempotently. The API has no broker credentials.

make secrets also creates the broker passwords, URLs, hashed users and
the worker S3 keys; the restart hook now stops the balancer, the API,
the relay and the worker and restarts PostgreSQL, Valkey, S3 and the
broker with their volumes; worker.sh and broker.sh stop, start and
inspect the processes for the failure demonstrations; the limits
policy covers the new services and the loopback UI port.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 01:19:04 +03:00
co-authored by Claude Opus 5.5
parent 34741dc2d2
commit c55fb1b998
15 changed files with 374 additions and 13 deletions
+60
View File
@@ -0,0 +1,60 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
umask 077
mkdir -p secrets
for role in api worker ops; do
file="secrets/rabbitmq_${role}_password"
[ -s "$file" ] || od -An -tx1 -N24 /dev/urandom | tr -d ' \n' >"$file"
done
[ -s secrets/s3_worker_access_key_id ] || printf 'worker%s' "$(od -An -tx1 -N8 /dev/urandom | tr -d ' \n')" >secrets/s3_worker_access_key_id
[ -s secrets/s3_worker_secret_access_key ] || od -An -tx1 -N32 /dev/urandom | tr -d ' \n' >secrets/s3_worker_secret_access_key
for role in api worker; do
file="secrets/broker_url_${role}"
[ -s "$file" ] || printf 'amqp://messenger-%s:%s@%s:5672/messenger' \
"$role" "$(cat "secrets/rabbitmq_${role}_password")" "${BROKER_HOST:-broker}" >"$file"
done
python3 - <<'PY'
import base64
import hashlib
import json
import os
def user(role, tags):
salt = os.urandom(4)
with open("secrets/rabbitmq_%s_password" % role, "rb") as source:
secret = source.read().strip()
digest = hashlib.sha256(salt + secret).digest()
return {
"name": "messenger-" + role,
"password_hash": base64.b64encode(salt + digest).decode(),
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": tags,
}
def grant(role, configure, write, read):
return {"user": "messenger-" + role, "vhost": "messenger", "configure": configure, "write": write, "read": read}
definitions = {
"users": [user("api", []), user("worker", []), user("ops", ["administrator"])],
"permissions": [
grant("api", "^$", "^attachments$", "^$"),
grant("worker", "^$", "^$", "^attachments\\.process$"),
grant("ops", ".*", ".*", ".*"),
],
}
with open("secrets/rabbitmq_users.json", "w") as target:
json.dump(definitions, target, indent=2)
target.write("\n")
PY
chmod 700 secrets
chmod 644 secrets/rabbitmq_users.json secrets/rabbitmq_ops_password secrets/broker_url_api secrets/broker_url_worker \
secrets/s3_worker_access_key_id secrets/s3_worker_secret_access_key
echo "broker and S3 worker secrets ready in secrets/ (values not printed)"
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
COMPOSE=${COMPOSE:-docker compose}
service=${BROKER_SERVICE:-broker}
queues() {
$COMPOSE exec -T "$service" rabbitmqctl -q list_queues -p messenger \
name type messages_ready messages_unacknowledged consumers
}
case "${1:-}" in
restart) $COMPOSE restart -t 20 "$service" && $COMPOSE up -d --no-deps --no-recreate --wait --wait-timeout 90 "$service" ;;
queues) queues ;;
*) echo "usage: $0 restart|queues" >&2; exit 2 ;;
esac
+31 -2
View File
@@ -7,7 +7,14 @@ import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
S3_CLIENT_SECRETS = ["s3_access_key_id", "s3_root_password", "s3_root_user", "s3_secret_access_key"]
S3_CLIENT_SECRETS = [
"s3_access_key_id",
"s3_root_password",
"s3_root_user",
"s3_secret_access_key",
"s3_worker_access_key_id",
"s3_worker_secret_access_key",
]
POLICY = {
"lb": {"secrets": [], "volumes": [], "networks": ["backend", "edge"], "ports": True},
"api": {
@@ -32,7 +39,29 @@ POLICY = {
},
"s3-init": {"secrets": S3_CLIENT_SECRETS, "volumes": [], "networks": ["backend"]},
"s3-cli": {"secrets": S3_CLIENT_SECRETS, "volumes": [], "networks": ["backend"]},
"broker": {
"secrets": ["rabbitmq_ops_password", "rabbitmq_users"],
"volumes": ["broker-data"],
"networks": ["backend", "edge"],
"ports": True,
},
"relay": {
"secrets": ["broker_url_api", "database_url", "s3_access_key_id", "s3_secret_access_key"],
"volumes": [],
"networks": ["backend"],
},
"worker": {
"secrets": [
"broker_url_worker",
"database_url",
"s3_worker_access_key_id",
"s3_worker_secret_access_key",
],
"volumes": [],
"networks": ["backend"],
},
}
PUBLISHING = frozenset(name for name, rule in POLICY.items() if rule.get("ports"))
def run(*command):
@@ -94,7 +123,7 @@ def running(problems):
]
if int(cpus) <= 0 or int(memory) <= 0:
problems.append(f"container {service}/{host}: no CPU or memory limit applied")
if published and service != "lb":
if published and service not in PUBLISHING:
problems.append(f"container {service}/{host}: publishes {published}")
rows.append(
[service, host, f"{int(cpus) / 1e9:g}", f"{int(memory) >> 20}MiB", ",".join(published)]
+8 -5
View File
@@ -2,11 +2,14 @@
set -eu
cd "$(dirname "$0")/.."
defined=$(docker compose config --services)
stores=""
for service in ${STATE_SERVICES:-postgres valkey s3}; do
if printf '%s\n' "$defined" | grep -qx "$service"; then stores="$stores $service"; fi
done
pick() {
for service in $1; do
if printf '%s\n' "$defined" | grep -qx "$service"; then printf ' %s' "$service"; fi
done
}
apps=$(pick "${APP_SERVICES:-lb api relay worker}")
stores=$(pick "${STATE_SERVICES:-postgres valkey s3 broker}")
[ -n "$stores" ] || { echo "no state services found" >&2; exit 3; }
docker compose stop lb "${API_SERVICE:-api}"
docker compose stop $apps
docker compose restart $stores
docker compose up -d --no-recreate --wait --wait-timeout "${WAIT_TIMEOUT:-75}"
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
COMPOSE=${COMPOSE:-docker compose}
service=${WORKER_SERVICE:-worker}
case "${1:-}" in
stop) $COMPOSE stop -t 10 "$service" ;;
kill) $COMPOSE kill -s KILL "$service" ;;
start) $COMPOSE up -d --no-deps --wait --wait-timeout 60 "$service" ;;
status) $COMPOSE ps -a --format '{{.Service}} {{.State}} {{.Status}}' "$service" ;;
*) echo "usage: $0 stop|kill|start|status" >&2; exit 2 ;;
esac