migrate.provision.inc 7.75 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 22
function drush_provision_drupal_pre_provision_migrate($platform, $new_uri = NULL) {
  /* Set offline mode to true and re-generate the settings.php. This will write a
23 24 25
   * $conf['site_offline'] = 1; to the settings.php
   */
  drush_log(dt("Putting site under maintenance"));
26
  d()->site_enabled = FALSE;
27
  _provision_drupal_create_settings_file();
28
  drush_invoke('provision-backup');
29
  provision_drupal_push_site();
30 31

  drush_set_option('old_platform', d()->platform->name);
32 33 34 35 36 37

  // If we are renaming the site, pick this up early.
  if (!is_null($new_uri) && ($new_uri != d()->uri)) {
    drush_set_option('target_name', $new_uri);
  }
  else {
38
    // the site url stays the same
39
    drush_set_option('target_name', d()->name);
40 41 42 43 44 45 46

    // The platform stays the same
    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);
    }
47
  }
48 49 50 51
}

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

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

69 70 71 72
}

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

  $target = drush_get_option('target_name');

  $options = array();

82 83
  $options['uri'] = d()->uri;

84 85 86 87 88 89 90 91 92 93 94 95
  // 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.
  if (d()->profile != 'hostmaster') {
    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.
    }
  }

96 97 98
  if (!is_null($new_uri) && ($new_uri != d()->uri)) {
    $options = d()->options;
    $options['uri'] = ltrim($new_uri, '@');
99 100
    $hash_name = drush_get_option('#name') ? '#name' : 'name';
    $options[$hash_name] = $new_uri;
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 139 140 141
    $new_aliases = array();
    $old_aliases = (is_array($options['aliases'])) ? $options['aliases'] : FALSE;
    $pre_old_uri = ltrim(d()->uri, '@');
    $pre_new_uri = ltrim($new_uri, '@');
    $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)) {
      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.
    }
    // 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.
    if (d()->profile != 'hostmaster') {
      // 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.
    }
142 143 144 145
  }
  $options['platform'] = $platform;
  $options['root'] = d($platform)->root;

146 147 148 149
  if ($profile = drush_get_option('profile', FALSE)) {
    $options['profile'] = $profile;
  }

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

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

156
  drush_invoke_process('@none', 'provision-save', array($target), $options);
157
  provision_backend_invoke($target, 'provision-deploy', array(drush_get_option('backup_file')), array('old_uri' => d()->uri));
158
  if (!drush_get_error()) {
159
    drush_log(dt("Bringing site out of maintenance"));
160 161
    d()->site_enabled = TRUE;
    drush_set_option('provision_save_config', FALSE);
162
    provision_backend_invoke($target, 'provision-verify');
163
  }
164 165
}

166 167
/**
 * Something went wrong
168 169
 *
 * Implementation of drush_hook_COMMAND_rollback().
170 171
 */
function drush_provision_drupal_provision_migrate_rollback($platform) {
172
  drush_invoke_process('@none', 'provision-save', array(d()->name), array('platform' => drush_get_option('old_platform'), 'root' => NULL, 'uri' => NULL));
173
}
174

175 176
/**
 * Delete the old site directory and recreate the settings file
177 178
 *
 * Implementation of drush_hook_post_COMMAND().
179
 */
180
function drush_provision_drupal_post_provision_migrate($platform, $new_uri = NULL) {
181
  drush_set_option('installed', FALSE);
182 183 184 185

  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
186
    _provision_drupal_delete_aliases();
187 188 189
    _provision_recursive_delete(d()->site_path);
    d()->service('http')->sync(d()->site_path);
  }
190 191

  if (!is_null($new_uri) && ($new_uri != d()->uri)) {
192
    // remove the existing alias
193
    $config = new Provision_Config_Drushrc_Alias(d()->name);
194
    $config->unlink();
195 196 197

    // Remove the symlink in the clients directory.
    _provision_client_delete_symlink();
198
  }
199

200 201 202
  // 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');
}