forked from mtusi2026/mtusi2026lab5
47 lines
1.9 KiB
Bash
Executable File
47 lines
1.9 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
umask 077
|
|
|
|
cd "$(dirname "$0")/.."
|
|
command -v openssl >/dev/null 2>&1 || { echo 'openssl is required' >&2; exit 1; }
|
|
mkdir -p secrets
|
|
|
|
if [ ! -e secrets/postgres_password ] && [ ! -e secrets/database_url ]; then
|
|
password=$(openssl rand -hex 24)
|
|
printf '%s' "$password" > secrets/postgres_password
|
|
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable' "$password" > secrets/database_url
|
|
elif [ ! -s secrets/postgres_password ] || [ ! -s secrets/database_url ]; then
|
|
echo 'incomplete PostgreSQL secrets; restore the matching pair' >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -e secrets/rustfs_root_access_key ] && [ ! -e secrets/rustfs_root_secret_key ]; then
|
|
openssl rand -hex 16 > secrets/rustfs_root_access_key
|
|
openssl rand -hex 32 > secrets/rustfs_root_secret_key
|
|
elif [ ! -s secrets/rustfs_root_access_key ] || [ ! -s secrets/rustfs_root_secret_key ]; then
|
|
echo 'incomplete RustFS root secrets; restore the matching pair' >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -e secrets/s3_access_key_id ] && [ ! -e secrets/s3_secret_access_key ]; then
|
|
openssl rand -hex 16 > secrets/s3_access_key_id
|
|
openssl rand -hex 32 > secrets/s3_secret_access_key
|
|
elif [ ! -s secrets/s3_access_key_id ] || [ ! -s secrets/s3_secret_access_key ]; then
|
|
echo 'incomplete API S3 secrets; restore the matching pair' >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -e secrets/rabbitmq_password ] && [ ! -e secrets/broker_url ]; then
|
|
broker_password=$(openssl rand -hex 24)
|
|
printf '%s' "$broker_password" > secrets/rabbitmq_password
|
|
printf 'amqp://messenger:%s@rabbitmq:5672/messenger' "$broker_password" > secrets/broker_url
|
|
elif [ ! -s secrets/rabbitmq_password ] || [ ! -s secrets/broker_url ]; then
|
|
echo 'incomplete RabbitMQ secrets; restore the matching pair' >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -e .env ]; then
|
|
cp .env.example .env
|
|
fi
|
|
echo 'Local .env and secret files are present. Existing values were kept.'
|