Automatically create user and skip setup page (#345)

* Added checks to docker-entrypoint

* Add init scripts feature

* Added backward compatibility

* Added auto user creation

* Updated README.md and env

* Use dbCheck for checking DB connection
This commit is contained in:
Ween Jiann
2021-05-13 09:41:08 +08:00
committed by GitHub
parent 8e9e7c47da
commit 6c68882449
5 changed files with 106 additions and 38 deletions

View File

@@ -31,21 +31,19 @@ git clone https://github.com/invoiceninja/dockerfiles.git
cd dockerfiles cd dockerfiles
``` ```
Instead of defining our environment variables inside our docker-compose.yml file we now define this in the `env` file, open this file up and insert your `APP_URL` and your `APP_KEY` Instead of defining our environment variables inside our docker-compose.yml file we now define this in the `env` file, open this file up and insert your `APP_URL`, `APP_KEY` and update the rest of the variables as required.
``` ```
APP_URL=http://in.localhost:8003/ APP_URL=http://in.localhost:8003/
APP_KEY=<insert your generated key in here> APP_KEY=<insert your generated key in here>
APP_DEBUG=true APP_DEBUG=true
MULTI_DB_ENABLED=false REQUIRE_HTTPS=false
DB_HOST1=db IN_USER_EMAIL=
DB_PORT1=3306 IN_PASSWORD=
DB_USERNAME1=ninja
DB_PASSWORD1=ninja
DB_DATABASE1=ninja
PHANTOMJS_PDF_GENERATION=false
``` ```
If `IN_USER_EMAIL` and `IN_PASSWORD` is not set the default user email and password is "admin@example.com" and "changeme!" respectively. You will use this for the initial login, thereafter, you can delete this two environment variables.
The `APP_KEY` can be generated by running The `APP_KEY` can be generated by running
```bash ```bash

View File

@@ -0,0 +1,14 @@
#!/bin/sh
php artisan db:seed --force
# Build up array of arguments...
if [[ ! -z "${IN_USER_EMAIL}" ]]; then
email="--email ${IN_USER_EMAIL}"
fi
if [[ ! -z "${IN_PASSWORD}" ]]; then
password="--password ${IN_PASSWORD}"
fi
php artisan ninja:create-account $email $password

View File

@@ -46,43 +46,52 @@ file_env() {
# first arg is `-f` or `--some-option` # first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$@" set -- supervisord "$@"
fi fi
# create storage volume # create storage volume
if [ ! -d /var/www/app/storage ] && [ -d "$BAK_STORAGE_PATH" ]; then if [ -d "$BAK_STORAGE_PATH" ]; then
mv "$BAK_STORAGE_PATH" /var/www/app/storage if [ ! -d /var/www/app/storage ]; then
elif [ -d "$BAK_STORAGE_PATH" ]; then mv "$BAK_STORAGE_PATH" /var/www/app/storage
# copy missing folders in storage else
IN_STORAGE_BACKUP="$(ls "$BAK_STORAGE_PATH")" # copy missing folders in storage
for path in $IN_STORAGE_BACKUP; do IN_STORAGE_BACKUP="$(ls "$BAK_STORAGE_PATH")"
if [ ! -e "/var/www/app/storage/$path" ]; then for path in $IN_STORAGE_BACKUP; do
cp -Rp "$BAK_STORAGE_PATH/$path" "/var/www/app/storage/" if [ ! -e "/var/www/app/storage/$path" ]; then
fi cp -Rp "$BAK_STORAGE_PATH/$path" "/var/www/app/storage/"
done fi
done
fi
rm -rf "$BAK_STORAGE_PATH"
fi
# prevent init scripts from running when upgrading from IN <= 5.1.62
if [ -f /var/www/app/public/version ] && [ "$INVOICENINJA_VERSION" != "$(cat /var/www/app/public/version)" ]; then
touch /var/www/app/storage/.initialized
fi fi
rm -rf "$BAK_STORAGE_PATH"
# create public volume # create public volume
if [ ! -d /var/www/app/public ] && [ -d "$BAK_PUBLIC_PATH" ]; then if [ -d "$BAK_PUBLIC_PATH" ]; then
mv "$BAK_PUBLIC_PATH" /var/www/app/public if [ ! -d /var/www/app/public ]; then
elif [ ! -e /var/www/app/public/version ] || [ "$INVOICENINJA_VERSION" != "$(cat /var/www/app/public/version)" ]; then mv "$BAK_PUBLIC_PATH" /var/www/app/public
# version mismatch, update all elif [ ! -f /var/www/app/public/version ] || [ "$INVOICENINJA_VERSION" != "$(cat /var/www/app/public/version)" ]; then
cp -au "$BAK_PUBLIC_PATH/"* /var/www/app/public # version mismatch, update all
echo "$INVOICENINJA_VERSION" >/var/www/app/public/version cp -au "$BAK_PUBLIC_PATH/"* /var/www/app/public
elif [ ! -d /var/www/app/public/logo ] && [ -d "$BAK_PUBLIC_PATH/logo" ]; then echo "$INVOICENINJA_VERSION" >/var/www/app/public/version
# missing logo folder only, copy folder elif [ ! -d /var/www/app/public/logo ] && [ -d "$BAK_PUBLIC_PATH/logo" ]; then
cp -a "$BAK_PUBLIC_PATH/logo" /var/www/app/public/logo # missing logo folder only, copy folder
elif [ -d "$BAK_PUBLIC_PATH/logo" ]; then cp -a "$BAK_PUBLIC_PATH/logo" /var/www/app/public/logo
# copy missing folders in logo elif [ -d "$BAK_PUBLIC_PATH/logo" ]; then
IN_LOGO_BACKUP="$(ls "$BAK_PUBLIC_PATH/logo")" # copy missing folders in logo
for path in $IN_LOGO_BACKUP; do IN_LOGO_BACKUP="$(ls "$BAK_PUBLIC_PATH/logo")"
if [ ! -e "/var/www/app/public/logo/$path" ]; then for path in $IN_LOGO_BACKUP; do
cp -a "$BAK_PUBLIC_PATH/logo/$path" "/var/www/app/public/logo/" if [ ! -e "/var/www/app/public/logo/$path" ]; then
fi cp -a "$BAK_PUBLIC_PATH/logo/$path" "/var/www/app/public/logo/"
done fi
done
fi
rm -rf "$BAK_PUBLIC_PATH"
fi fi
rm -rf "$BAK_PUBLIC_PATH"
# Initialize values that might be stored in a file # Initialize values that might be stored in a file
file_env 'APP_KEY' file_env 'APP_KEY'

View File

@@ -1,5 +1,45 @@
#!/bin/sh #!/bin/sh
# usage: docker_process_init_files [file [file [...]]]
# ie: docker_process_init_files /always-initdb.d/*
# process initializer files, based on file extensions
docker_process_init_files() {
echo
local f
for f; do
case "$f" in
*.sh)
# https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
# https://github.com/docker-library/postgres/pull/452
if [ -x "$f" ]; then
in_log "$0: running $f"
"$f"
else
in_log "$0: sourcing $f"
. "$f"
fi
;;
*) in_log "$0: ignoring $f" ;;
esac
echo
done
}
php artisan config:cache php artisan config:cache
php artisan optimize php artisan optimize
# Check if DB works, if not crash the app.
DB_READY=$(php artisan tinker --execute='echo app()->call("App\Utils\SystemHealth@dbCheck")["success"];')
if [ "$DB_READY" != "1" ]; then
echo "Error connecting to DB"
php artisan migrate:status # Print verbose error
exit 1
fi
php artisan migrate --force php artisan migrate --force
# If first IN run, it needs to be initialized
if [ ! -f /var/www/app/storage/.initialized ]; then
docker_process_init_files /docker-entrypoint-init.d/*
touch /var/www/app/storage/.initialized
fi

7
env
View File

@@ -12,6 +12,13 @@ DB_DATABASE=ninja
DB_USERNAME=ninja DB_USERNAME=ninja
DB_PASSWORD=ninja DB_PASSWORD=ninja
# Create initial user
# Default to these values if empty
# IN_USER_EMAIL=admin@example.com
# IN_PASSWORD=changeme!
IN_USER_EMAIL=
IN_PASSWORD=
# Mail options # Mail options
MAIL_MAILER=log MAIL_MAILER=log
MAIL_HOST=smtp.mailtrap.io MAIL_HOST=smtp.mailtrap.io