From 107d9ea4e87a1858d54d59df0a2e6079f678633d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Tue, 4 Apr 2023 18:51:51 +0200 Subject: [PATCH 1/7] Added entrypoint hooks for your own custom scripts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dennis Vestergaard Værum --- Dockerfile-alpine.template | 5 +++++ Dockerfile-debian.template | 5 +++++ README.md | 30 ++++++++++++++++++++++++++++++ docker-entrypoint.sh | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/Dockerfile-alpine.template b/Dockerfile-alpine.template index 3d69ca5d4..905077bcf 100644 --- a/Dockerfile-alpine.template +++ b/Dockerfile-alpine.template @@ -99,6 +99,11 @@ RUN { \ } > "${PHP_INI_DIR}/conf.d/nextcloud.ini"; \ \ mkdir /var/www/data; \ + mkdir -p /docker-entrypoint-hooks.d/pre-installation \ + /docker-entrypoint-hooks.d/post-installation \ + /docker-entrypoint-hooks.d/pre-upgrade \ + /docker-entrypoint-hooks.d/post-upgrade \ + /docker-entrypoint-hooks.d/before-starting; \ chown -R www-data:root /var/www; \ chmod -R g=u /var/www diff --git a/Dockerfile-debian.template b/Dockerfile-debian.template index 4a9d975f2..f3df0472d 100644 --- a/Dockerfile-debian.template +++ b/Dockerfile-debian.template @@ -111,6 +111,11 @@ RUN { \ } > "${PHP_INI_DIR}/conf.d/nextcloud.ini"; \ \ mkdir /var/www/data; \ + mkdir -p /docker-entrypoint-hooks.d/pre-installation \ + /docker-entrypoint-hooks.d/post-installation \ + /docker-entrypoint-hooks.d/pre-upgrade \ + /docker-entrypoint-hooks.d/post-upgrade \ + /docker-entrypoint-hooks.d/before-starting; \ chown -R www-data:root /var/www; \ chmod -R g=u /var/www diff --git a/README.md b/README.md index 4128efc1e..36114de90 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,36 @@ To customize other PHP limits you can simply change the following variables: - `PHP_UPLOAD_LIMIT` (default `512M`) This sets the upload limit (`post_max_size` and `upload_max_filesize`) for big files. Note that you may have to change other limits depending on your client, webserver or operating system. Check the [Nextcloud documentation](https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/big_file_upload_configuration.html) for more information. +## Auto configuration via hook folders + +There are 5 hooks + +- `pre-installation` Executed before the Nextcloud is installed/initiated +- `post-installation` Executed after the Nextcloud is installed/initiated +- `pre-upgrade` Executed before the Nextcloud is upgraded +- `post-upgrade` Executed after the Nextcloud is upgraded +- `before-starting` Executed before the Nextcloud is started + +To use the hooks triggered by the entrypoint script... either added the script(s) to one/more of the hook folders, which are located at the path `/docker-entrypoint-hooks.d` in the container. It is also possible to use volume(s) to insert the script into the container. + +**Remember:** Only the script(s) markted with the executable flag are executed. + +**Example:** +```yaml +... + app: + image: nextcloud:stable + + volumes: + - ./app-hooks/pre-installation:/docker-entrypoint-hooks.d/pre-installation + - ./app-hooks/post-installation:/docker-entrypoint-hooks.d/post-installation + - ./app-hooks/pre-upgrade:/docker-entrypoint-hooks.d/pre-upgrade + - ./app-hooks/post-upgrade:/docker-entrypoint-hooks.d/post-upgrade + - ./app-hooks/before-starting:/docker-entrypoint-hooks.d/before-starting +... +``` + + ## Using the apache image behind a reverse proxy and auto configure server host and protocol The apache image will replace the remote addr (IP address visible to Nextcloud) with the IP address from `X-Real-IP` if the request is coming from a proxy in `10.0.0.0/8`, `172.16.0.0/12` or `192.168.0.0/16` by default. If you want Nextcloud to pick up the server host (`HTTP_X_FORWARDED_HOST`), protocol (`HTTP_X_FORWARDED_PROTO`) and client IP (`HTTP_X_FORWARDED_FOR`) from a trusted proxy, then disable rewrite IP and add the reverse proxy's IP address to `TRUSTED_PROXIES`. diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 37441fd8f..d23e9127d 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -19,6 +19,33 @@ run_as() { fi } +# Execute all executable files in a given directory in alphanumeric order +run_path() { + local hook_folder_path="/docker-entrypoint-hooks.d/$1" + local return_code=0 + + echo "Searching for scripts to run in the folder: ${hook_folder_path}" + + ( + cd "${hook_folder_path}" + find . -type f -print | while read script_file_path; do + if ! [ -x "${script_file_path}" ] && [ -f "${script_file_path}" ]; then + echo "The script \"${script_file_path}\" in the folder \"${hook_folder_path}\" was skipping, because it didn't have the executable flag" + continue + fi + + echo "Running the script: \"${script_file_path}\"" + + run_as "${script_file_path}" || return_code="$?" + + if [ "${return_code}" -ne "0" ]; then + echo "Failed at executing \"${script_file_path}\". Exit code: ${return_code}" + exit 1 + fi + done + ) +} + # usage: file_env VAR [DEFAULT] # ie: file_env 'XYZ_DB_PASSWORD' 'example' # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of @@ -182,6 +209,8 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP fi if [ "$install" = true ]; then + run_path pre-installation + echo "Starting nextcloud installation" max_retries=10 try=0 @@ -204,12 +233,16 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP NC_TRUSTED_DOMAIN_IDX=$((NC_TRUSTED_DOMAIN_IDX+1)) done fi + + run_path post-installation else echo "Please run the web-based installer on first connect!" fi fi # Upgrade else + run_path pre-upgrade + run_as 'php /var/www/html/occ upgrade' run_as 'php /var/www/html/occ app:list' | sed -n "/Enabled:/,/Disabled:/p" > /tmp/list_after @@ -217,6 +250,7 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP diff /tmp/list_before /tmp/list_after | grep '<' | cut -d- -f2 | cut -d: -f1 rm -f /tmp/list_before /tmp/list_after + run_path post-upgrade fi echo "Initializing finished" @@ -227,6 +261,8 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP run_as 'php /var/www/html/occ maintenance:update:htaccess' fi ) 9> /var/www/html/nextcloud-init-sync.lock + + run_path before-starting fi exec "$@" From 8d1c420de0bb307c83342327e8de8a89ef08cb7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Sat, 13 May 2023 09:04:11 +0200 Subject: [PATCH 2/7] =?UTF-8?q?Small=20changes:=20-=20Only=20execute=20she?= =?UTF-8?q?ll-scripts=20(mening=20files=20ending=20with=20.sh)=20-=20Sort?= =?UTF-8?q?=20the=20files=20before=20executing=20them,=20had=20forgotten?= =?UTF-8?q?=20=F0=9F=98=85=20-=20Added=20a=20message=20when=20a=20hook=20s?= =?UTF-8?q?cript=20finish=20-=20Added=20prefix=20arror=20to=20message=20to?= =?UTF-8?q?=20show=20the=20are=20related?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dennis Vestergaard Værum --- docker-entrypoint.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index d23e9127d..fb07f7be8 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -24,24 +24,26 @@ run_path() { local hook_folder_path="/docker-entrypoint-hooks.d/$1" local return_code=0 - echo "Searching for scripts to run in the folder: ${hook_folder_path}" + echo "=> Searching for scripts to run in the folder: ${hook_folder_path}" ( cd "${hook_folder_path}" - find . -type f -print | while read script_file_path; do + find . -type f -iname '*.sh' -print | sort | while read script_file_path; do if ! [ -x "${script_file_path}" ] && [ -f "${script_file_path}" ]; then - echo "The script \"${script_file_path}\" in the folder \"${hook_folder_path}\" was skipping, because it didn't have the executable flag" + echo "==> The script \"${script_file_path}\" in the folder \"${hook_folder_path}\" was skipping, because it didn't have the executable flag" continue fi - echo "Running the script: \"${script_file_path}\"" + echo "==> Running the script: \"${script_file_path}\"" run_as "${script_file_path}" || return_code="$?" if [ "${return_code}" -ne "0" ]; then - echo "Failed at executing \"${script_file_path}\". Exit code: ${return_code}" + echo "==> Failed at executing \"${script_file_path}\". Exit code: ${return_code}" exit 1 fi + + echo "==> Finished the script: \"${script_file_path}\"" done ) } From edea64b530ca145fb065be2e9178e7ce8a1edbca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Sat, 13 May 2023 09:08:34 +0200 Subject: [PATCH 3/7] Show in the search msg that it only searches for '*.sh' files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dennis Vestergaard Værum --- docker-entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index fb07f7be8..51c68a075 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -24,7 +24,7 @@ run_path() { local hook_folder_path="/docker-entrypoint-hooks.d/$1" local return_code=0 - echo "=> Searching for scripts to run in the folder: ${hook_folder_path}" + echo "=> Searching for scripts (*.sh) to run in the folder: ${hook_folder_path}" ( cd "${hook_folder_path}" From ac68d6c2092511348903a6dc52bd6f6ba06abfef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6rum?= <6872940+dvaerum@users.noreply.github.com> Date: Sun, 14 May 2023 08:28:02 +0200 Subject: [PATCH 4/7] Fixed spelling mistake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: J0WI Signed-off-by: Dennis Værum <6872940+dvaerum@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 36114de90..14c197623 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,7 @@ There are 5 hooks To use the hooks triggered by the entrypoint script... either added the script(s) to one/more of the hook folders, which are located at the path `/docker-entrypoint-hooks.d` in the container. It is also possible to use volume(s) to insert the script into the container. -**Remember:** Only the script(s) markted with the executable flag are executed. +**Remember:** Only the script(s) marked with the executable flag are executed. **Example:** ```yaml From a31036f574ffcc308f83704c9bc46efb35d4cd9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Sun, 14 May 2023 10:19:30 +0200 Subject: [PATCH 5/7] Updated the `README.md` file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dennis Vestergaard Værum --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 14c197623..89d9616c2 100644 --- a/README.md +++ b/README.md @@ -210,13 +210,15 @@ There are 5 hooks - `post-installation` Executed after the Nextcloud is installed/initiated - `pre-upgrade` Executed before the Nextcloud is upgraded - `post-upgrade` Executed after the Nextcloud is upgraded -- `before-starting` Executed before the Nextcloud is started +- `before-starting` Executed before the Nextcloud starts -To use the hooks triggered by the entrypoint script... either added the script(s) to one/more of the hook folders, which are located at the path `/docker-entrypoint-hooks.d` in the container. It is also possible to use volume(s) to insert the script into the container. +To use the hooks triggered by the `entrypoint` script, either +- Added your script(s) to the individual of the hook folder(s), which are located at the path `/docker-entrypoint-hooks.d` in the container +- Use volume(s) if you want to use script from the host system inside the container, see example. -**Remember:** Only the script(s) marked with the executable flag are executed. +**Note:** Only the script(s) ending with `.sh` and marked as executable will be executed. -**Example:** +**Example:** Mount using volumes ```yaml ... app: From e9e834b6bc457a3ca7c9f7fe4fe9fd0985ea4a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Sun, 11 Jun 2023 14:55:48 +0200 Subject: [PATCH 6/7] change from using find to using a for-loop to located the `.sh` files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dennis Vestergaard Værum --- README.md | 2 +- docker-entrypoint.sh | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 89d9616c2..31531767b 100644 --- a/README.md +++ b/README.md @@ -216,7 +216,7 @@ To use the hooks triggered by the `entrypoint` script, either - Added your script(s) to the individual of the hook folder(s), which are located at the path `/docker-entrypoint-hooks.d` in the container - Use volume(s) if you want to use script from the host system inside the container, see example. -**Note:** Only the script(s) ending with `.sh` and marked as executable will be executed. +**Note:** Only the script(s) located in a hook folder (not sub-folders), ending with `.sh` and marked as executable, will be executed. **Example:** Mount using volumes ```yaml diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 51c68a075..71fc03bb7 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -27,14 +27,13 @@ run_path() { echo "=> Searching for scripts (*.sh) to run in the folder: ${hook_folder_path}" ( - cd "${hook_folder_path}" - find . -type f -iname '*.sh' -print | sort | while read script_file_path; do + for script_file_path in "${hook_folder_path}/"*.sh; do if ! [ -x "${script_file_path}" ] && [ -f "${script_file_path}" ]; then echo "==> The script \"${script_file_path}\" in the folder \"${hook_folder_path}\" was skipping, because it didn't have the executable flag" continue fi - echo "==> Running the script: \"${script_file_path}\"" + echo "==> Running the script (cwd: $(pwd)): \"${script_file_path}\"" run_as "${script_file_path}" || return_code="$?" From 4a55cad211a738866f22b0b3f47ae0b4ee85fe82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Wed, 14 Jun 2023 23:48:54 +0200 Subject: [PATCH 7/7] Fix bug - that would make docker-entrypoint.sh failed, hook folders was empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dennis Vestergaard Værum --- docker-entrypoint.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 71fc03bb7..941dc3ee2 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -24,7 +24,12 @@ run_path() { local hook_folder_path="/docker-entrypoint-hooks.d/$1" local return_code=0 - echo "=> Searching for scripts (*.sh) to run in the folder: ${hook_folder_path}" + echo "=> Searching for scripts (*.sh) to run, located in the folder: ${hook_folder_path}" + + if [ -z "$(ls -A "${hook_folder_path}")" ]; then + echo "==> but the hook folder \"$(basename "${hook_folder_path}")\" is empty, so nothing to do" + return 0 + fi ( for script_file_path in "${hook_folder_path}/"*.sh; do