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
```
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_KEY=<insert your generated key in here>
APP_DEBUG=true
MULTI_DB_ENABLED=false
DB_HOST1=db
DB_PORT1=3306
DB_USERNAME1=ninja
DB_PASSWORD1=ninja
DB_DATABASE1=ninja
PHANTOMJS_PDF_GENERATION=false
REQUIRE_HTTPS=false
IN_USER_EMAIL=
IN_PASSWORD=
```
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
```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`
if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$@"
set -- supervisord "$@"
fi
# create storage volume
if [ ! -d /var/www/app/storage ] && [ -d "$BAK_STORAGE_PATH" ]; then
mv "$BAK_STORAGE_PATH" /var/www/app/storage
elif [ -d "$BAK_STORAGE_PATH" ]; then
# copy missing folders in storage
IN_STORAGE_BACKUP="$(ls "$BAK_STORAGE_PATH")"
for path in $IN_STORAGE_BACKUP; do
if [ ! -e "/var/www/app/storage/$path" ]; then
cp -Rp "$BAK_STORAGE_PATH/$path" "/var/www/app/storage/"
fi
done
if [ -d "$BAK_STORAGE_PATH" ]; then
if [ ! -d /var/www/app/storage ]; then
mv "$BAK_STORAGE_PATH" /var/www/app/storage
else
# copy missing folders in storage
IN_STORAGE_BACKUP="$(ls "$BAK_STORAGE_PATH")"
for path in $IN_STORAGE_BACKUP; do
if [ ! -e "/var/www/app/storage/$path" ]; then
cp -Rp "$BAK_STORAGE_PATH/$path" "/var/www/app/storage/"
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
rm -rf "$BAK_STORAGE_PATH"
# create public volume
if [ ! -d /var/www/app/public ] && [ -d "$BAK_PUBLIC_PATH" ]; then
mv "$BAK_PUBLIC_PATH" /var/www/app/public
elif [ ! -e /var/www/app/public/version ] || [ "$INVOICENINJA_VERSION" != "$(cat /var/www/app/public/version)" ]; then
# version mismatch, update all
cp -au "$BAK_PUBLIC_PATH/"* /var/www/app/public
echo "$INVOICENINJA_VERSION" >/var/www/app/public/version
elif [ ! -d /var/www/app/public/logo ] && [ -d "$BAK_PUBLIC_PATH/logo" ]; then
# missing logo folder only, copy folder
cp -a "$BAK_PUBLIC_PATH/logo" /var/www/app/public/logo
elif [ -d "$BAK_PUBLIC_PATH/logo" ]; then
# copy missing folders in logo
IN_LOGO_BACKUP="$(ls "$BAK_PUBLIC_PATH/logo")"
for path in $IN_LOGO_BACKUP; do
if [ ! -e "/var/www/app/public/logo/$path" ]; then
cp -a "$BAK_PUBLIC_PATH/logo/$path" "/var/www/app/public/logo/"
fi
done
if [ -d "$BAK_PUBLIC_PATH" ]; then
if [ ! -d /var/www/app/public ]; then
mv "$BAK_PUBLIC_PATH" /var/www/app/public
elif [ ! -f /var/www/app/public/version ] || [ "$INVOICENINJA_VERSION" != "$(cat /var/www/app/public/version)" ]; then
# version mismatch, update all
cp -au "$BAK_PUBLIC_PATH/"* /var/www/app/public
echo "$INVOICENINJA_VERSION" >/var/www/app/public/version
elif [ ! -d /var/www/app/public/logo ] && [ -d "$BAK_PUBLIC_PATH/logo" ]; then
# missing logo folder only, copy folder
cp -a "$BAK_PUBLIC_PATH/logo" /var/www/app/public/logo
elif [ -d "$BAK_PUBLIC_PATH/logo" ]; then
# copy missing folders in logo
IN_LOGO_BACKUP="$(ls "$BAK_PUBLIC_PATH/logo")"
for path in $IN_LOGO_BACKUP; do
if [ ! -e "/var/www/app/public/logo/$path" ]; then
cp -a "$BAK_PUBLIC_PATH/logo/$path" "/var/www/app/public/logo/"
fi
done
fi
rm -rf "$BAK_PUBLIC_PATH"
fi
rm -rf "$BAK_PUBLIC_PATH"
# Initialize values that might be stored in a file
file_env 'APP_KEY'

View File

@@ -1,5 +1,45 @@
#!/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 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
# 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_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_MAILER=log
MAIL_HOST=smtp.mailtrap.io