forked from mtusi2026/mtusi2026lab5
66 lines
2.2 KiB
Bash
Executable File
66 lines
2.2 KiB
Bash
Executable File
#!/bin/sh
|
|
# Идемпотентно создаёт .env и secrets/* для локального стенда. Значения не печатает.
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
|
|
if [ ! -f .env ]; then
|
|
(
|
|
umask 077
|
|
pg=$(openssl rand -hex 24)
|
|
mt=$(openssl rand -hex 16)
|
|
sed \
|
|
-e "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=${pg}|" \
|
|
-e "s|^DATABASE_URL=.*|DATABASE_URL=postgresql://messenger:${pg}@postgres:5432/messenger|" \
|
|
-e "s|^METRICS_TOKEN=.*|METRICS_TOKEN=${mt}|" \
|
|
.env.example > .env
|
|
)
|
|
echo "created .env"
|
|
else
|
|
echo "kept .env"
|
|
fi
|
|
|
|
mkdir -p secrets
|
|
chmod 700 secrets
|
|
|
|
write_secret() {
|
|
name=$1
|
|
value=$2
|
|
if [ -s "secrets/$name" ]; then
|
|
echo "kept secrets/$name"
|
|
return
|
|
fi
|
|
(umask 133; printf '%s' "$value" > "secrets/$name")
|
|
chmod 0644 "secrets/$name"
|
|
echo "created secrets/$name"
|
|
}
|
|
|
|
write_secret minio_root_user "root-$(openssl rand -hex 6)"
|
|
write_secret minio_root_password "$(openssl rand -hex 24)"
|
|
write_secret s3_api_access_key_id "api-$(openssl rand -hex 8)"
|
|
write_secret s3_api_secret_access_key "$(openssl rand -hex 24)"
|
|
write_secret s3_worker_access_key_id "worker-$(openssl rand -hex 8)"
|
|
write_secret s3_worker_secret_access_key "$(openssl rand -hex 24)"
|
|
|
|
# Broker password lives in one place and is rendered into both files, so the
|
|
# URL used by the app and the RabbitMQ config cannot drift apart.
|
|
if [ -s secrets/broker_url ] && [ -s secrets/rabbitmq.conf ]; then
|
|
echo "kept secrets/broker_url secrets/rabbitmq.conf"
|
|
else
|
|
bp=$(openssl rand -hex 24)
|
|
(umask 133; printf '%s' "amqp://messenger:${bp}@broker:5672/messenger" > secrets/broker_url)
|
|
chmod 0644 secrets/broker_url
|
|
(
|
|
umask 133
|
|
{
|
|
printf 'default_vhost = messenger\n'
|
|
printf 'default_user = messenger\n'
|
|
printf 'default_pass = %s\n' "$bp"
|
|
printf 'default_permissions.configure = .*\n'
|
|
printf 'default_permissions.read = .*\n'
|
|
printf 'default_permissions.write = .*\n'
|
|
} > secrets/rabbitmq.conf
|
|
)
|
|
chmod 0644 secrets/rabbitmq.conf
|
|
echo "created secrets/broker_url secrets/rabbitmq.conf"
|
|
fi
|