lab5: брокер RabbitMQ, worker и асинхронная обработка

This commit is contained in:
2026-11-05 01:17:58 +03:00
parent 1982bdb690
commit d19824f546
44 changed files with 3486 additions and 150 deletions
+39 -9
View File
@@ -7,6 +7,8 @@ IFS= read -r root_user < /run/secrets/minio_root_user || [ -n "$root_user" ]
IFS= read -r root_password < /run/secrets/minio_root_password || [ -n "$root_password" ]
IFS= read -r api_key < /run/secrets/s3_api_access_key_id || [ -n "$api_key" ]
IFS= read -r api_secret < /run/secrets/s3_api_secret_access_key || [ -n "$api_secret" ]
IFS= read -r worker_key < /run/secrets/s3_worker_access_key_id || [ -n "$worker_key" ]
IFS= read -r worker_secret < /run/secrets/s3_worker_secret_access_key || [ -n "$worker_secret" ]
bucket="${S3_BUCKET:?}"
export MC_CONFIG_DIR=/tmp/.mc
@@ -15,14 +17,42 @@ export MC_HOST_stand="http://${root_user}:${root_password}@s3:9000"
mc mb --ignore-existing "stand/$bucket"
mc anonymous set none "stand/$bucket"
printf '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]}]}' "$bucket" "$bucket" > /tmp/messenger-api-policy.json
mc admin policy create stand messenger-api /tmp/messenger-api-policy.json
# The API owns the staging prefix only: it writes the accepted upload there and
# can remove it after the worker commits the result. The final prefix is read
# for variant/original downloads and legacy lab4 originals, but never written.
printf '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/staging/*"]},{"Effect":"Allow","Action":["s3:GetObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]}]}' "$bucket" "$bucket" "$bucket" > /tmp/messenger-api-policy.json
mc admin user add stand "$api_key" "$api_secret"
info=$(mc admin user info stand "$api_key" --json)
case "$info" in
*'"messenger-api"'*) ;;
*) mc admin policy attach stand messenger-api --user "$api_key" ;;
esac
# The worker reads staging, writes the final original and variants, and removes
# staging after a safe commit. It never deletes the final prefix.
printf '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/staging/*"]},{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]}]}' "$bucket" "$bucket" "$bucket" > /tmp/messenger-worker-policy.json
echo "bootstrap ok: bucket=$bucket policy=messenger-api"
ensure_user() {
name=$1
secret=$2
if ! mc admin user info stand "$name" >/dev/null 2>&1; then
mc admin user add stand "$name" "$secret"
fi
}
# `mc admin policy create` does not overwrite and a policy attached to a user
# cannot be removed. Detach, remove and recreate makes the script converge on
# the current policy even when the stand volume predates lab5.
install_policy() {
name=$1
file=$2
user=$3
if mc admin policy info stand "$name" >/dev/null 2>&1; then
mc admin policy detach stand "$name" --user "$user" >/dev/null 2>&1 || true
mc admin policy remove stand "$name" >/dev/null 2>&1 || true
fi
mc admin policy create stand "$name" "$file"
mc admin policy attach stand "$name" --user "$user"
}
ensure_user "$api_key" "$api_secret"
ensure_user "$worker_key" "$worker_secret"
install_policy messenger-api /tmp/messenger-api-policy.json "$api_key"
install_policy messenger-worker /tmp/messenger-worker-policy.json "$worker_key"
echo "bootstrap ok: bucket=$bucket policies=messenger-api,messenger-worker"