From 78e234832557debb61a4ee3a89f02835b9fe97f5 Mon Sep 17 00:00:00 2001 From: Jason Hildebrand Date: Thu, 13 Jan 2022 17:07:34 -0600 Subject: [PATCH] Refactor how we set/restore ownership of drupal settings file to allow reuse... (#179) * Refactor how we set/restore ownership of drupal settings file to allow reuse in containers based on this one. * Clarify comment. * Adjustments. --- drupal/rootfs/etc/islandora/utilities.sh | 52 +++++++++++++++++++----- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/drupal/rootfs/etc/islandora/utilities.sh b/drupal/rootfs/etc/islandora/utilities.sh index 7f7b81a6..7a428cdd 100755 --- a/drupal/rootfs/etc/islandora/utilities.sh +++ b/drupal/rootfs/etc/islandora/utilities.sh @@ -284,6 +284,45 @@ function fedora_url { fi } + +# Allow modifications to settings.php by changing ownership and perms +function allow_settings_modifications { + local site="${1}"; shift + local drupal_root=$(drush drupal:directory) + local subdir=$(drupal_site_env "${site}" "SUBDIR") + local site_directory=$(realpath "${drupal_root}/sites/${subdir}") + + # send debug output to stderr because the caller typically captures output from this function. + #>&2 echo "adjusting ownership of ${site_directory}/settings.php" + if [ -f "${site_directory}/settings.php" ]; then + previous_owner_group=$(stat -c "%u:%g" "${site_directory}/settings.php") + chown 100:101 "${site_directory}/settings.php" + chmod a=rwx "${site_directory}/settings.php" + fi + if [ ! -z "${previous_owner_group}" ]; then + echo ${previous_owner_group} + fi +} + +# Restore ownership of settings.php so that it is readable/writable outside of docker +function restore_settings_ownership { + local site="${1}"; shift + local previous_owner_group="${1}"; shift + local drupal_root=$(drush drupal:directory) + local subdir=$(drupal_site_env "${site}" "SUBDIR") + local site_directory=$(realpath "${drupal_root}/sites/${subdir}") + + # Restore owner/group to previous value. + # When the codebase is bind-mounted, this ensures the file remains readable/writable by the host user. + if [ ! -z "${previous_owner_group}" ]; then + chown "${previous_owner_group}" "${site_directory}/settings.php" + fi + + # Restrict access to settings.php + chmod 444 "${site_directory}/settings.php" +} + + # Regenerate / Update settings.php function update_settings_php { local site="${1}"; shift @@ -311,11 +350,7 @@ function update_settings_php { fi # Allow modifications to settings.php - if [ -f "${site_directory}/settings.php" ]; then - previous_owner_group=$(stat -c "%u:%g" "${site_directory}/settings.php") - chown 100:101 "${site_directory}/settings.php" - chmod a=rwx "${site_directory}/settings.php" - fi + local previous_owner_group=$(allow_settings_modifications ${site}) drush -l "${site_url}" islandora:settings:create-settings-if-missing drush -l "${site_url}" islandora:settings:set-hash-salt "${salt}" @@ -336,12 +371,7 @@ function update_settings_php { fi # Restore owner/group to previous value - if [ ! -z "${previous_owner_group}" ]; then - chown "${previous_owner_group}" "${site_directory}/settings.php" - fi - - # Restrict access to settings.php - chmod 444 "${site_directory}/settings.php" + restore_settings_ownership ${site} ${previous_owner_group} } # Enable module and apply configuration.