518 setup env command (#519)

* Created command and cleaned

* Simplified docker setup, created script to generate .env

* add git ignore file back
This commit is contained in:
Julien Nahum
2024-08-08 16:33:01 +02:00
committed by GitHub
parent 7888990e84
commit fd9067b470
11 changed files with 225 additions and 196 deletions

View File

@@ -40,8 +40,6 @@ RUN chmod 777 -R storage
RUN php artisan package:discover --ansi
COPY docker/php-fpm-entrypoint /usr/local/bin/opnform-entrypoint
COPY docker/generate-api-secret.sh /usr/local/bin/
RUN ln -s /secrets/api.env .env
RUN chmod a+x /usr/local/bin/*

View File

@@ -26,7 +26,6 @@ FROM node:20-alpine
WORKDIR /app
COPY --from=javascript-builder /app/.output/ /app/
RUN ls /app/
RUN ln -s /secrets/client.env .env
ADD ./docker/node-entrypoint /entrypoint.sh
RUN chmod a+x /entrypoint.sh

View File

@@ -1,42 +0,0 @@
#!/bin/bash -e
main() {
generate_api_secrets
}
generate_api_secrets() {
if ! is_configured; then
echo "Generating shared secret..."
SECRET="$(random_string)"
add_secret_to_env_file /secrets/client.env NUXT_API_SECRET "$SECRET"
add_secret_to_env_file /secrets/api.env FRONT_API_SECRET "$SECRET"
fi
}
random_string() {
array=()
for i in {a..z} {A..Z} {0..9};
do
array[$RANDOM]=$i
done
printf %s ${array[@]::8} $'\n'
}
add_secret_to_env_file() {
FILE=$1
TEMP_FILE=/tmp/env.$$
VAR=$2
VAL=$3
grep -q "^$VAR=" "$FILE" 2>/dev/null || ( echo "$VAR=" >> "$FILE" )
cp $FILE $TEMP_FILE
sed "s/^$VAR=.*$/$VAR=$VAL/" -i $TEMP_FILE
cat $TEMP_FILE > $FILE
}
is_configured() {
grep -q "FRONT_API_SECRET=.\+" .env 2>/dev/null
}
main

View File

@@ -1,13 +1,9 @@
#!/bin/sh
main() {
if [ "$1" == "bash" ]; then
if [ "$1" = "bash" ]; then
"$@"
else
wait_for_api_secret
if [ ! -f .env ] && [ -f /secrets/client.env ]; then
ln -sf /secrets/client.env .env
fi
if [ -f .env ]; then
. .env
else
@@ -16,15 +12,10 @@ main() {
run_server "$@"
fi
}
wait_for_api_secret() {
until [ -f /secrets/configured ]; do
echo "Waiting for api secret..."
sleep 1
done
}
run_server() {
echo "Running " node "$@"
echo "Running node $@"
"$@"
}
main "$@"
main "$@"

View File

@@ -1,61 +1,22 @@
#!/bin/bash
main() {
read_env
prep_file_permissions
prep_storage
if is_master "$@"; then
prep_laravel_secrets
wait_for_db
apply_db_migrations
run_init_project
mark_ready
else
wait_for_ready
wait_for_db
fi
read_env
wait_for_db
apply_db_migrations
run_init_project
run_server "$@"
}
is_master() {
echo "$@" | grep -q php-fpm
}
read_env() {
#set +x
[ -f .env ] || touch .env
. .env
#set -x
}
prep_file_permissions() {
chmod a+x ./artisan
}
prep_laravel_secrets() {
read_env
[ "x$APP_KEY" != "x" ] || {
echo "Generating Laravel key..."
grep -q "APP_KEY=" .env || {
echo "APP_KEY=" >> .env
}
./artisan key:generate
read_env
}
[ "x$JWT_SECRET" != "x" ] || {
echo "Generating Laravel Secret..."
./artisan jwt:secret -f
read_env
}
[ "x$FRONT_API_SECRET" != "x" ] || {
echo "Generating Shared Client Secret..."
/usr/local/bin/generate-api-secret.sh
read_env
}
echo "Done with secrets"
}
apply_db_migrations() {
echo "Running DB Migrations"
./artisan migrate
@@ -66,28 +27,15 @@ run_init_project() {
./artisan app:init-project
}
wait_for_ready() {
echo "Checking keys have been generated"
until [ -f /secrets/configured ]; do
sleep 1;
echo "Waiting for keys to generate"
done
}
mark_ready() {
touch /secrets/configured
}
wait_for_db() {
echo "Waiting for DB to be ready"
until ./artisan migrate:status 2>&1 | grep -q -E "(Migration table not found|Migration name)"; do
echo "Waiting for DB to bootup"
sleep 1
done
}
run_server() {
echo "Booting $@"
read_env
echo "Starting server $@"
/usr/local/bin/docker-php-entrypoint "$@"
}
@@ -112,4 +60,4 @@ prep_storage() {
ln -t . -sf /persist/storage
}
main "$@"
main "$@"