Files
mtusi2026lab2/scripts/init-secrets.sh
T

49 lines
1.6 KiB
Bash

#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
DIR=secrets
DB_USER="${POSTGRES_USER:-messenger}"
DB_NAME="${POSTGRES_DB:-messenger}"
umask 077
mkdir -p "$DIR"
if [ ! -s "$DIR/postgres_password" ]; then
# hex, чтобы пароль не требовал URL-экранирования в DSN.
od -An -tx1 -N24 /dev/urandom | tr -d ' \n' > "$DIR/postgres_password"
echo "init-secrets: created $DIR/postgres_password"
fi
if [ ! -s "$DIR/database_url" ]; then
pw="$(cat "$DIR/postgres_password")"
printf 'postgres://%s:%s@postgres:5432/%s?sslmode=disable' "$DB_USER" "$pw" "$DB_NAME" > "$DIR/database_url"
echo "init-secrets: created $DIR/database_url"
fi
if [ ! -s "$DIR/valkey_password" ]; then
od -An -tx1 -N24 /dev/urandom | tr -d ' \n' > "$DIR/valkey_password"
echo "init-secrets: created $DIR/valkey_password"
fi
if [ ! -s "$DIR/cache_url" ]; then
pw="$(cat "$DIR/valkey_password")"
printf 'redis://:%s@valkey:6379/0' "$pw" > "$DIR/cache_url"
echo "init-secrets: created $DIR/cache_url"
fi
if [ ! -s "$DIR/valkey_cache_password" ]; then
od -An -tx1 -N24 /dev/urandom | tr -d '
' > "$DIR/valkey_cache_password"
echo "init-secrets: created $DIR/valkey_cache_password"
fi
if [ ! -s "$DIR/history_cache_url" ]; then
pw="$(cat "$DIR/valkey_cache_password")"
printf 'redis://:%s@valkey-cache:6379/0' "$pw" > "$DIR/history_cache_url"
echo "init-secrets: created $DIR/history_cache_url"
fi
chmod 0700 "$DIR"
chmod 0644 "$DIR/postgres_password" "$DIR/database_url" "$DIR/valkey_password" "$DIR/cache_url" "$DIR/valkey_cache_password" "$DIR/history_cache_url"