migrate.provision.inc 9.59 KB
Newer Older
1 2
<?php
/**
3
 * @file
4
 * Migrate command implementation
5 6 7 8
 */

/**
 * Make sure we have a valid site being migrated, and that the file being migrated from exists
9 10
 *
 * Implementation of drush_hook_COMMAND_validate().
11
 */
12
function drush_provision_drupal_provision_migrate_validate($platform = NULL) {
13
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
14 15 16 17
}

/**
 * Make a backup before making any changes, and add extract the file we are restoring from
18 19
 *
 * Implementation of drush_hook_pre_COMMAND().
20
 */
21
function drush_provision_drupal_pre_provision_migrate($platform, $new_name = NULL) {
omega8cc's avatar
omega8cc committed
22 23
  // Set offline mode to true and re-generate the settings.php file.
  // This will write a $conf['site_offline'] = 1; to the settings.php file.
24
  drush_log(dt("Putting site under maintenance"));
25
  d()->site_enabled = FALSE;
26
  _provision_drupal_create_settings_file();
27
  drush_invoke('provision-backup');
28
  provision_drupal_push_site();
29 30

  drush_set_option('old_platform', d()->platform->name);
31 32

  // If we are renaming the site, pick this up early.
33 34
  if (!is_null($new_name) && ($new_name != d()->name)) {
    drush_set_option('target_name', $new_name);
35
    drush_set_option('source_name', d()->uri);
36 37 38
  }
  else {
    drush_set_option('target_name', d()->name);
39

40
    // Detect if the platform stays the same.
41 42 43 44 45
    if (d()->platform->name == $platform) {
      drush_log('Replacing the existing site on the same platform');
      // deploying over existing site.
      drush_set_option('deploy_replace_site', TRUE);
    }
46
  }
47 48 49 50
}

/**
 * Remove the extracted site directory
51
 * Restore the vhost conf per the original platform
52 53
 *
 * Implementation of drush_hook_pre_COMMAND_rollback().
54
 */
55
function drush_provision_drupal_pre_provision_migrate_rollback($platform, $new_name = NULL) {
omega8cc's avatar
omega8cc committed
56
  // Set site_offline to false and regenerate the config.
57
  drush_log(dt("Bringing site out of maintenance"));
58
  d()->site_enabled = true;
59
  _provision_drupal_create_settings_file();
60
  provision_drupal_push_site();
61

62
  $success =  provision_file()->unlink(drush_get_option('backup_file'))
63 64
    ->succeed('Removed unused migration site package')
    ->fail('Could not remove unused migration site package');
65
  d()->service('http')->create_config('site');
Adrian Rossouw's avatar
Adrian Rossouw committed
66
  d()->service('http')->parse_configs();
67 68 69 70
}

/**
 * Switch the migrate directories around now that we have the new db installed
71 72
 *
 * Implementation of drush_hook_COMMAND().
73
 */
74
function drush_provision_drupal_provision_migrate($platform, $new_name = NULL) {
75 76 77

  $target = drush_get_option('target_name');
  $options = array();
78 79
  $options['uri'] = d()->uri;

80 81 82
  // If the site is migrated between platforms and not just renamed,
  // we should update the info collected about source and target platform first.
  // Note that we have to exclude Hostmaster platform from this extra verify.
83
  if (!provision_is_hostmaster_site() && drush_get_option('provision_verify_platforms_before_migrate', TRUE)) {
84 85 86 87 88 89 90 91
    if (!is_null(d($platform)->name) && (d($platform)->name != d()->platform->name)) {
      provision_backend_invoke('@hostmaster', 'hosting-task', array(d()->platform->name, 'verify'), array('force' => TRUE));
      sleep(5); // A small trick to avoid high load and race conditions.
      provision_backend_invoke('@hostmaster', 'hosting-task', array(d($platform)->name, 'verify'), array('force' => TRUE));
      sleep(5); // A small trick to avoid high load and race conditions.
    }
  }

92
  if (!is_null($new_name) && ($new_name != d()->name)) {
93
    $options = d()->options;
94
    $options['uri'] = ltrim($new_name, '@');
95
    $hash_name = drush_get_option('#name') ? '#name' : 'name';
96
    $options[$hash_name] = $new_name;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    $new_aliases = array();
    $old_aliases = (is_array($options['aliases'])) ? $options['aliases'] : FALSE;
    $pre_old_uri = ltrim(d()->uri, '@');
    $pre_new_uri = ltrim($new_name, '@');
    $raw_old_uri = preg_replace('/^www\./', '', $pre_old_uri);
    $raw_new_uri = preg_replace('/^www\./', '', $pre_new_uri);
    $www_old_uri = "www." . $raw_old_uri;
    $www_new_uri = "www." . $raw_new_uri;
    if (is_array($old_aliases) && !empty($old_aliases)) {
      foreach ($old_aliases as $alias) {
        if (!preg_match("/^www\./", $pre_old_uri) && preg_match("/^www\./", $pre_new_uri)) {
          $new_aliases[] = str_replace($www_old_uri, $raw_new_uri, $alias);
        }
        elseif (preg_match("/^www\./", $pre_old_uri) && !preg_match("/^www\./", $pre_new_uri)) {
          $new_aliases[] = str_replace($raw_old_uri, $www_new_uri, $alias);
        }
        elseif (!preg_match("/^www\./", $pre_old_uri) && !preg_match("/^www\./", $pre_new_uri)) {
          $new_aliases[] = str_replace($www_old_uri, $www_new_uri, $alias);
        }
        elseif (preg_match("/^www\./", $pre_old_uri) && preg_match("/^www\./", $pre_new_uri)) {
          $new_aliases[] = str_replace($raw_old_uri, $raw_new_uri, $alias);
        }
      }
      $unique_aliases = array_unique($new_aliases); // Make sure there are no duplicates.
      $options['aliases'] = array(); // Reset original aliases array.
      $options['aliases'] = $unique_aliases; // Use rewritten aliases array.
      if (!is_null(d($platform)->name) && (d($platform)->name == d()->platform->name)) {
        drush_set_option('rename_only', TRUE);
      }
      drush_set_option('aliases', $unique_aliases);
    }
    else {
      $detected_aliases = provision_drupal_find_aliases();
      drush_set_option('aliases', $detected_aliases);
    }
    // Warning: do not try to re-verify the original site here
    // even with backend-only verify, because it would create
    // (and never delete) duplicate vhost with old domain
    // and all aliases included - see issue #1067702.
  }
  else {
    // We have to exclude Hostmaster site from any extra verify steps.
139
    if (!provision_is_hostmaster_site()) {
140 141 142 143 144 145
      // We should update also the info collected about the site before running migrate task.
      // Doing this is safe only when the site is migrated with the same name - see issue #1067702.
      $local_uri_verify = '@' . d()->uri;
      provision_backend_invoke('@hostmaster', 'hosting-task', array($local_uri_verify, 'verify'), array('force' => TRUE));
      sleep(5); // A small trick to avoid high load and race conditions.
    }
146 147 148 149
  }
  $options['platform'] = $platform;
  $options['root'] = d($platform)->root;

150 151 152 153
  if ($profile = drush_get_option('profile', FALSE)) {
    $options['profile'] = $profile;
  }

154 155 156 157
  if ($db_server = drush_get_option('new_db_server', FALSE)) {
    $options['db_server'] = $db_server;
  }

158 159
  drush_set_option('new_site_path', "{$options['root']}/sites/{$options['uri']}");

160
  drush_invoke_process('@none', 'provision-save', array($target), $options);
161 162 163 164 165 166 167 168 169 170

  // Build an options array to pass to the deploy task. This allows injection
  // of data into the deploy task. See: https://www.drupal.org/node/2471805
  $deploy_options = array(
    'old_uri' => d()->uri,
    'strict' => 0,
  );
  drush_command_invoke_all_ref('provision_deploy_options_alter', $deploy_options, 'migrate');

  provision_backend_invoke($target, 'provision-deploy', array(drush_get_option('backup_file')), $deploy_options);
171
  if (!drush_get_error()) {
172
    drush_log(dt("Bringing site out of maintenance"));
173 174
    d()->site_enabled = TRUE;
    drush_set_option('provision_save_config', FALSE);
175
    provision_backend_invoke($target, 'provision-verify');
176
  }
177 178
}

179 180
/**
 * Something went wrong
181 182
 *
 * Implementation of drush_hook_COMMAND_rollback().
183 184
 */
function drush_provision_drupal_provision_migrate_rollback($platform) {
185
  drush_invoke_process('@none', 'provision-save', array(d()->name), array('platform' => drush_get_option('old_platform'), 'root' => NULL, 'uri' => NULL));
186
}
187

188 189
/**
 * Delete the old site directory and recreate the settings file
190 191
 *
 * Implementation of drush_hook_post_COMMAND().
192
 */
193
function drush_provision_drupal_post_provision_migrate($platform, $new_name = NULL) {
194
  drush_set_option('installed', FALSE);
195 196 197 198

  if (!drush_get_option('deploy_replace_site', FALSE)) {
    // we remove the aliases even if redirection is enabled as a precaution
    // if redirection is enabled, keep silent about errors
199
    _provision_drupal_delete_aliases();
200 201 202
    _provision_recursive_delete(d()->site_path);
    d()->service('http')->sync(d()->site_path);
  }
203

204
  if (!is_null($new_name) && ($new_name != d()->name)) {
205 206 207
    // Use correct context for cleanup operations.
    $source_name = drush_get_option('source_name');
    // Remove the existing drush alias.
208
    $config = new Provision_Config_Drushrc_Alias(d()->name);
209
    $config->unlink();
210 211 212 213 214
    // Delete the old site directory.
    _provision_recursive_delete(d($source_name)->site_path);
    d()->service('http')->sync(d($source_name)->site_path);
    // Remove old site name aliases.
    _provision_drupal_delete_aliases();
215 216
    // Remove the symlink in the clients directory.
    _provision_client_delete_symlink();
217
  }
218

219 220 221
  $rename_only = drush_get_option('rename_only');
  if ($rename_only) {
    // We run this extra tricky backend verify to avoid issue #1004526.
222
    if (!provision_is_hostmaster_site()) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236
      $deploy_replace_site = drush_get_option('deploy_replace_site');
      $final_aliases = drush_get_option('aliases');
      provision_reload_config('site', drush_get_option('new_site_path', d($new_name)->site_path) . '/drushrc.php');
      d($new_name)->aliases = drush_set_option('aliases', $final_aliases);
      d($new_name)->write_alias();
      drush_set_option('aliases', drush_get_option('aliases'),  'site');
      drush_set_option('provision_save_config', false);
      provision_backend_invoke($new_name, 'provision-verify');
    }
  }
  else {
    // Load the config file of the newly migrated site and return it to hosting.
    provision_reload_config('site', drush_get_option('new_site_path', d()->site_path) . '/drushrc.php');
  }
237
}