Skip to content

Commit

Permalink
Prevent cache collisions in multisite install tasks (acquia#3171)
Browse files Browse the repository at this point in the history
* Refactor post-install hook to use temp cache dir per site, use requests instead of ENV.

* Refresh Factory Hooks from template for 9002001.

* Update version number for hook.

* Remove redundant factory hook update.

* Removed unused use statements.

* Style and phpcs updates.
  • Loading branch information
lcatlett authored and grasmash committed Dec 15, 2018
1 parent ee4680a commit 73d7eb0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 22 deletions.
100 changes: 89 additions & 11 deletions scripts/factory-hooks/post-install/post-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,98 @@
* in your subscription. Unlike most API-based hooks, this hook does not
* take arguments, but instead executes the PHP code it is provided.
*
* This is used so that an ACSF site install will match a local BLT site
* install. After a local site install, the update functions are run.
* This is used so that an ACSF site install is identical to the local BLT site
* install, with the environment, site, and uri CLI runtime arguments overriding
* all other configuration.
*
*/

$site = $_ENV['AH_SITE_GROUP'];
$env = $_ENV['AH_SITE_ENVIRONMENT'];
$target_env = $site . $env;
use Drush\Drush;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Core\Database\Database;
use Drupal\Core\Site\Settings;

// The public domain name of the website.
// Run updates against requested domain rather than acsf primary domain.
$domain = $_SERVER['HTTP_HOST'];
global $acsf_site_name;

// Acquia hosting site / environment names
/*$site = getenv('AH_SITE_GROUP');
$env = getenv('AH_SITE_ENVIRONMENT');
$uri = FALSE;*/

$site = 'SANDBOX';
$env = 'local';
$uri = 'local.sandbox.com'

// ACSF Database Role
if (!empty($GLOBALS['gardens_site_settings']['conf']['acsf_db_name'])) {
$db_role = $GLOBALS['gardens_site_settings']['conf']['acsf_db_name'];
}

$docroot = sprintf('/var/www/html/%s.%s/docroot', $site, $env);

// BLT executable
$blt = sprintf('/var/www/html/%s.%s/vendor/bin/blt', $site, $env);

/**
* Exit on error.
*
* @param string $message
* A message to write to sdderr.
*/
function error($message) {
fwrite(STDERR, $message);
exit(1);
}

fwrite(STDERR, sprintf("Running updates on: site: %s; env: %s; db_role: %s; name: %s;\n", $site, $env, $db_role, $acsf_site_name));

include_once $docroot . '/sites/g/sites.inc';
$sites_json = gardens_site_data_load_file();
if (!$sites_json) {
error('The ACSF site registry could not be loaded from the server.');
}

foreach ($sites_json['sites'] as $site_domain => $site_info) {
if ($site_info['conf']['acsf_db_name'] === $db_role && !empty($site_info['flags']['preferred_domain'])) {
$uri = $site_domain;
fwrite(STDERR, "Site domain: $uri;\n");
break;
}
}
if (!$uri) {
error('Could not find the preferred domain that belongs to the site.');
}

$docroot = sprintf('/var/www/html/%s.%s/docroot', $site, $env);

//$cache_directory = sprintf('/mnt/tmp/%s.%s/drush_tmp_cache/%s', $site, $env, md5($uri));
//cacheDir=`/usr/bin/env php /mnt/www/html/$site.$env/vendor/acquia/blt/scripts/blt/drush/cache.php $site $env $uri`
$cache_directory = exec("/mnt/www/html/$site.$env/vendor/acquia/blt/scripts/blt/drush/cache.php $site $env $uri");

shell_exec(sprintf('mkdir -p %s', escapeshellarg($cache_directory)));

// Execute the updates
$command = sprintf(
'DRUSH_PATHS_CACHE_DIRECTORY=%s %s drupal:update --environment=%s --site=%s --define drush.uri=%s --verbose --yes --no-interaction',
escapeshellarg($cache_directory),
escapeshellarg($blt),
escapeshellarg($env),
escapeshellarg($acsf_site_name),
escapeshellarg($uri)
);
fwrite(STDERR, "Executing: $command with cache dir $cache_directory;\n");

$result = 0;
$output = array();
exec($command, $output, $result);
print join("\n", $output);

// Clean up the drush cache directory.
shell_exec(sprintf('rm -rf %s', escapeshellarg($cache_directory)));

if ($result) {
fwrite(STDERR, "Command execution returned status code: $result!\n");
exit($result);
}

$domain_fragments = explode('.', $_SERVER['HTTP_HOST']);
$site_name = array_shift($domain_fragments);

exec("/mnt/www/html/$site.$env/vendor/acquia/blt/bin/blt drupal:update --environment=$env --site=$site_name --define drush.uri=$domain --verbose --yes");
14 changes: 3 additions & 11 deletions settings/blt.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
* Setup BLT utility variables, include required files.
*/

use Acquia\Blt\Robo\Config\ConfigInitializer;
use Acquia\Blt\Robo\Config\ConfigInitializer;
use Drupal\Component\Utility\Bytes;
use Drupal\Core\DrupalKernel;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
* Host detection.
Expand Down Expand Up @@ -92,15 +88,11 @@
* Site directory detection.
*/
if (!isset($site_path)) {
try {
$site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
}
catch (BadRequestHttpException $e) {
$site_path = 'sites/default';
}
$site_path = \Drupal::service('site.path');
}
$site_dir = str_replace('sites/', '', $site_path);


/*******************************************************************************
* Acquia Cloud Site Factory settings.
******************************************************************************/
Expand Down

0 comments on commit 73d7eb0

Please sign in to comment.