From f703f9865f2ca4e9aa7c11d18d13462e5a9ef4c1 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Thu, 29 Apr 2021 13:45:02 +0200 Subject: [PATCH] Add 'call-update-php-hack.sh' This commit adds a hacky script that can manually call update.php. --- distillery/call-update-php-hack.sh | 36 ++++++++++++++++++ distillery/utils/set_trusted_host.sh | 10 +++-- distillery/utils/settings_php_get.sh | 17 +++++++++ distillery/utils/settings_php_set.sh | 56 ++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100755 distillery/call-update-php-hack.sh create mode 100755 distillery/utils/settings_php_get.sh create mode 100755 distillery/utils/settings_php_set.sh diff --git a/distillery/call-update-php-hack.sh b/distillery/call-update-php-hack.sh new file mode 100755 index 0000000..cca3e2f --- /dev/null +++ b/distillery/call-update-php-hack.sh @@ -0,0 +1,36 @@ +#!/bin/bash +set -e + +# read the lib/shared.sh and read the slug argument. +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +cd "$DIR" +source "$DIR/lib/lib.sh" +require_slug_argument + + +# if the site doesn't exist, I can't open a shell. +if ! sql_bookkeep_exists "$SLUG"; then + log_error "=> Site '$SLUG' does not exist in bookeeping table. " + echo "I can't rebuild it. " + exit 1 +fi; + +# Read everything from the database +read -r INSTANCE_BASE_DIR MYSQL_DATABASE MYSQL_USER GRAPHDB_REPO GRAPHDB_USER <<< "$(sql_bookkeep_load "${SLUG}" "filesystem_base,sql_database,sql_user,graphdb_repository,graphdb_user" | tail -n +2)" + +# cd into the right directory +cd "$INSTANCE_BASE_DIR" + +log_info " => Enabling 'update.php' access" +docker-compose exec barrel bash /utils/settings_php_set.sh update_free_access true + +log_info " => Calling update.php" + +IP=`docker-compose exec barrel awk 'END{print $1}' /etc/hosts | tr -d '\r'` +URL="http://$IP:8080/update.php/selection" + +# TODO: This should really call update.php sensibly +curl -H "Host: $INSTANCE_DOMAIN" $URL + +log_info "=> Disabling update.php access" +docker-compose exec barrel bash /utils/settings_php_set.sh update_free_access false diff --git a/distillery/utils/set_trusted_host.sh b/distillery/utils/set_trusted_host.sh index 2c28a7f..a859554 100755 --- a/distillery/utils/set_trusted_host.sh +++ b/distillery/utils/set_trusted_host.sh @@ -5,7 +5,9 @@ INSTANCE_DOMAIN="$(hostname -f)" INSTANCE_DOMAIN="${INSTANCE_DOMAIN%.wisski}" -chmod u+w web/sites/default/settings.php -echo "" >> web/sites/default/settings.php -echo "\$settings['trusted_host_patterns'] = ['${INSTANCE_DOMAIN//\./\\\.}'];" >> web/sites/default/settings.php -chmod u-w web/sites/default/settings.php \ No newline at end of file + +TRUSTED_HOST_PATTERN="${INSTANCE_DOMAIN//\./\\\\.}" +TRUSTED_HOST_PATTERNS='["'$TRUSTED_HOST_PATTERN'"]' + +echo "Setting 'trusted_host_patterns' to $TRUSTED_HOST_PATTERNS" +bash /utils/settings_php_set.sh 'trusted_host_patterns' "$TRUSTED_HOST_PATTERNS" \ No newline at end of file diff --git a/distillery/utils/settings_php_get.sh b/distillery/utils/settings_php_get.sh new file mode 100755 index 0000000..e4e1dae --- /dev/null +++ b/distillery/utils/settings_php_get.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# settings_php_get.sh name +# Gets the 'settings_php_get.php' setting 'name' as json-encoded value, or null when it does not exist. + +NAME=$1 + +if [ -z "$NAME" ]; then + echo "Usage: get_settings_setting.sh NAME" + exit 1 +fi; + +echo "$NAME" | drush php:eval ' + use \Drupal\Core\Site\Settings; + $name=trim(file_get_contents("php://stdin")); + echo json_encode(Settings::get($name)); +'; \ No newline at end of file diff --git a/distillery/utils/settings_php_set.sh b/distillery/utils/settings_php_set.sh new file mode 100755 index 0000000..28fff46 --- /dev/null +++ b/distillery/utils/settings_php_set.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# settings_php_set.sh name value +# Sets the 'settings.php' setting 'name' to 'value'. +# Value must be json-encoded. + +NAME=$1 +VALUE=$2 + +if [ -z "$NAME" ]; then + echo "Usage: settings_php_set.sh NAME VALUE" + exit 1 +fi; + +if [ -z "$VALUE" ]; then + echo "Usage: settings_php_set.sh NAME VALUE" + exit 1 +fi; + +cd /var/www/data/project +chmod u+w web/sites/default/settings.php + +(echo "$NAME"; echo "$VALUE" ) | drush php:eval ' + include_once DRUPAL_ROOT . "/core/includes/install.inc"; + + // read NAME and VALUE from STDIN + $content=file_get_contents("php://stdin"); + $newline=strpos($content, "\n"); + $name=trim(substr($content, 0, $newline)); + $jvalue=trim(substr($content, $newline + 1)); + + // decode json values + $value = @json_decode($jvalue); + if ($data === null && json_last_error() !== JSON_ERROR_NONE) { + echo "Invalid JSON, cannot update settings.php. \n"; + return 1; + } + + // make parameters to drush_rewrite_settings + $settings["settings"][$name] = (object)[ + "value" => $value, + "required" => TRUE, + ]; + + // find the actual settings.php file to rewrite + $filename = DRUPAL_ROOT . "/" . \Drupal::service("site.path") . "/settings.php"; + drupal_rewrite_settings($settings, $filename); + + echo "Wrote " . $filename . "\n"; + return 0; +'; +EXIT=$? + +chmod u-w web/sites/default/settings.php + +exit $? \ No newline at end of file