legacy/genkeys.sh

97 lines
2.1 KiB
Bash
Raw Normal View History

2023-05-16 22:20:28 +02:00
#!/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"
ln -s .env pairent_backend/.env
# ----- End bootstrap back -----
exit
# ----- Start bootstrap front -----
echo Building frontend static files...
cd pairent_frontend_react
NPMS=('npm' 'pnpm' 'yarn')
NPM='None'
for n in ${NPMS[@]}; do
if [ -x "$(command -v "$n")" ]; then
NPM=$n
fi
done
if [[ "$NPM" == 'None' ]]; then
fatal_err
echo ' No node package manager was found in your system.'
echo ' Please install one of the following:'
for n in ${NPMS[@]}; do
echo -e " $n"
done
exit -1
fi
rm -rf node_modules
$NPM install
$NPM run build
DIST_FOLDER=$(realpath dist)
cd ..
cp -r $DIST_FOLDER/* static
echo Done building frontend static files
# ----- End bootstrap front -----