Skip to content

Commit

Permalink
Refactor how we set/restore ownership of drupal settings file to allo…
Browse files Browse the repository at this point in the history
…w reuse... (#179)

* Refactor how we set/restore ownership of drupal settings file to allow reuse in containers based on this one.

* Clarify comment.

* Adjustments.
  • Loading branch information
jasonhildebrand authored Jan 13, 2022
1 parent f240a68 commit 78e2348
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions drupal/rootfs/etc/islandora/utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -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.
Expand Down

0 comments on commit 78e2348

Please sign in to comment.