You are here

function drush_acsf_init in Acquia Cloud Site Factory Connector 8

Command callback: executes the required changes to this repository.

1 string reference to 'drush_acsf_init'
acsf_init_drush_command in acsf_init/acsf_init.drush.inc
Implements hook_drush_command().

File

acsf_init/acsf_init.drush.inc, line 129
Provides drush commands to set up a site for Acquia Site Factory.

Code

function drush_acsf_init() {
  $drupal_root = realpath(DRUPAL_ROOT);
  if (basename($drupal_root) !== 'docroot') {

    // If for some reason we wanted to enable this command to run on directory
    // structures where no /docroot exists, that's possible in theory... but we
    // need to to audit / change the code in this file to
    // - derive the repo root in a good way;
    // - properly distinguish the repo root from the drupal root;
    // - figure out what to do with remaining instances of "docroot", if any;
    // - figure out what to do when drupal root and repo root are equal... like
    //   not copy hook files?
    drush_set_error('INCORRECT DIRECTORY STRUCTURE', dt("Drupal must be installed in a subdirectory of your code repository, named 'docroot'."));
    return;
  }
  $repo_root = dirname($drupal_root);
  $skip_default_settings = drush_get_option('skip-default-settings');
  drush_print(dt('Installing ACSF requirements.'));

  // Create the required directories.
  foreach (acsf_init_get_required_dirs($repo_root) as $name => $dir) {

    // Skip '../sites/default' if --skip-default-settings is set.
    if ($skip_default_settings && $name == 'site env default') {
      continue;
    }
    drush_print(dt('Creating directory for !name at !dir', [
      '!name' => $name,
      '!dir' => $dir,
    ]));
    if (!file_exists($dir)) {
      if (mkdir($dir, 0755, TRUE)) {
        drush_log(dt('Success'), 'success');
      }
      else {
        drush_log(dt('Error'), 'error');
      }
    }
    else {
      drush_log(dt('Already Exists'), 'ok');
    }
  }

  // Copy the required files.
  $lib_path = sprintf('%s/lib', dirname(__FILE__));
  foreach (acsf_init_get_required_files($repo_root) as $location) {
    $file = $location['filename'];
    $dest = sprintf('%s/%s', $location['dest'], $file);

    // Some files only contain a destination as they are already in place.
    if (isset($location['source']) && isset($location['dest'])) {
      $source = sprintf('%s/%s/%s', $lib_path, $location['source'], $file);
      drush_print(dt('Copying !file to !dest.', [
        '!file' => $source,
        '!dest' => $dest,
      ]));
      if (file_exists($dest)) {
        $confirm = drush_confirm(dt('Destination file exists, continue?'));
        if ($confirm === FALSE) {
          continue;
        }
      }

      // Copy the file into the destination.
      if (copy($source, $dest)) {
        drush_log(dt('Copy Success: !file', [
          '!file' => $file,
        ]), 'success');
      }
      else {
        drush_log(dt('Copy Error: !file', [
          '!file' => $file,
        ]), 'error');
      }

      // If the file exists, it could be set to 0444, so we have to ensure that
      // it is writable before overwriting it. The copy would fail otherwise.
      if (!is_writable($dest)) {
        if (!chmod($dest, 0666)) {
          drush_log(dt('Chmod Error: !file', [
            '!file' => $file,
          ]), 'error');
        }
      }
    }

    // Chmod the file if required.
    $mod = isset($location['mod']) ? $location['mod'] : FALSE;
    if ($mod && chmod($dest, $mod)) {
      drush_log(dt('Chmod Success: !file', [
        '!file' => $file,
      ]), 'success');
    }
    elseif ($mod) {
      drush_log(dt('Chmod Error: !file', [
        '!file' => $file,
      ]), 'error');
    }
  }
  try {
    drush_acsf_init_patch_htaccess();
  } catch (AcsfInitHtaccessException $e) {
    drush_log($e
      ->getMessage(), 'error');
  }

  // The default settings.php file needs special handling. On the ACSF
  // infrastructure our own business logic needs to execute while on ACE or on
  // a local environment the default settings.php could be used to drive other
  // sites. For this reason the ACSF specific code will be included in the file
  // instead of rewriting it to contain only our code.
  if (!$skip_default_settings) {
    drush_print(dt('Updating the default settings.php file with the ACSF specific business logic.'));
    $edit_allowed = TRUE;
    $default_settings_php_path = $repo_root . '/docroot/sites/default/settings.php';
    if (file_exists($default_settings_php_path)) {
      $edit_allowed = drush_confirm(dt('Destination file exists, continue?'));
    }
    if ($edit_allowed !== FALSE) {

      // If the file exists, it could be set to 0444, so we have to ensure that
      // it is writable.
      if (file_exists($default_settings_php_path) && !is_writable($default_settings_php_path)) {
        if (!chmod($default_settings_php_path, 0666)) {
          drush_log(dt('Chmod Error: !file', [
            '!file' => $file,
          ]), 'error');
        }
      }
      if (file_exists($default_settings_php_path)) {

        // If the current default settings.php file has the same content as
        // acsf.legacy.default.settings.php then this file can be rewritten
        // according to the new approach. A simple strict equality check should
        // be enough since the acsf-init-verify checked the deployed files by
        // comparing md5 hashes, so even a single character difference would
        // have caused an error in the code deployment process.
        $current_default_settings_php = file_get_contents($default_settings_php_path);
        $legacy_default_settings_php = file_get_contents($lib_path . '/sites/default/acsf.legacy.default.settings.php');
        if ($current_default_settings_php === $legacy_default_settings_php) {
          acsf_init_default_settings_php_create($default_settings_php_path);
        }
        else {

          // Update the default settings.php file with the latest ACSF code.
          acsf_init_default_settings_php_update($default_settings_php_path);
        }
      }
      else {

        // The default settings.php file does not exist yet, so create a new
        // file with the necessary include.
        acsf_init_default_settings_php_create($default_settings_php_path);
      }
    }
  }

  // Verify that the files are in sync.
  clearstatcache();
  $result = drush_acsf_init_verify();
  if ($result) {
    drush_print(dt("Be sure to commit any changes to your repository before deploying. This includes files like sites/default/settings.php; you can use 'git status --ignored' to make sure no changes are inadvertantly ignored by a custom git configuration."));
  }
}