legacy/genkeys.sh

59 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# ----- Start commands -----
fatal_err() {
echo -e "\033[31mFatal error $*\033[0m"
}
# ----- End commands -----
# ----- Start safeguards -----
if [[ "$1" != '-a' ]]; then
fatal_err
echo " This script will potentially break any existing instance of Pairent."
echo " To execute this script, re-run it with option -a as first argument."
exit -1
fi
if ! [ -f .env ]; then
fatal_err
echo -e " No .env file was found."
echo -e " Please use the \033[32m.env.example\033[0m to create a dotenv file:"
echo -e " 1. \033[34mcp .env.example .env\033[0m"
echo -e " 2. Edit your \033[32m.env\033[0m in your favourite editor"
exit -1
fi
if ! [ -x "$(command -v python3)" ]; then
fatal_err
echo -e " Python is not installed."
echo -e " Please install Python 3.11 on your system"
exit -1
fi
# ----- End safeguards -----
# ----- Start bootstrap back -----
echo Generating keys...
KEY=$(python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())')
sed -Ei "s/^DJANGO_KEY=.*$/DJANGO_KEY=/" .env
sed -Ei "s/^DJANGO_KEY=.*$/DJANGO_KEY='$KEY'/" .env
DB_PASS=$(tr -dc A-Za-z0-9 </dev/urandom | head -c $(( $RANDOM % 32 + 32 )))
sed -Ei "s/DB_PASSWORD=\w*/DB_PASSWORD=$DB_PASS/" .env
echo Using these keys:
echo -e " \033[32mDjango key: \033[0m$KEY"
echo -e " \033[32mDatabase key: \033[0m$DB_PASS"
cp .env pairent_backend/.env
# ----- End bootstrap back -----
exit