migrate.hostmaster.inc 8.19 KB
Newer Older
1 2 3 4 5
<?php

/**
 * @file manual site migrate system
 */
6 7 8 9 10 11 12 13 14 15 16 17 18

/**
 * Convert a directory name to a drush aliasname, stripping unwanted chars.
 *
 * @param string $name The directory name.
 *
 * @return string
 *   A drush alias name for the platform.
 */
function _provision_path_to_platform_alias($name) {
  return '@platform_' . preg_replace("/[!\W]/", "", basename(rtrim($name, '/')));
}

19
function drush_provision_hostmaster_migrate_validate($site, $platform) {
20 21 22 23 24 25
  // XXX: this doesn't actually document the pre alpha9 upgrade path
  drush_print('
This command will operate the following changes in your system:

1. verify the server and the hostmaster site (provision-verify)
2. verify the new platform (which will download it if missing)
26 27 28
3. stop the queue (hosting-pause)
4. attempt a migration to the new platform (provision-migrate)
5. start the queue (hosting-resume)
29 30 31 32 33 34

We are making the following assumptions:
 * you have read UPGRADE.txt
 * you are executing this script as your "aegir" user
');

35 36
  if (!drush_confirm("Do you really want to proceed with the upgrade?")) {
    return drush_set_error('PROVISION_CANCEL_MIGRATE', 'Upgrade canceled by user');
37 38
  }

39
  $data = drush_get_merged_options();
40

41
  $alias_file = drush_server_home() . '/.drush/server_master.alias.drushrc.php';
42

43 44
  if (!file_exists($platform)) {
    $dir = realpath(dirname($platform));
45
    if (is_dir($dir)) {
46 47 48 49
      $platform = $dir . '/' . basename($platform);
      drush_log("changed relative platform path to absolute");
    }
  }
50

51
  // If the alias file exists, it means we are migrating from a post alpha9 release.
52
  if (is_readable($alias_file)) {
53 54
    provision_backend_invoke('@server_master', 'provision-verify');
    provision_backend_invoke('@hostmaster', 'provision-verify');
55

56
    drush_set_option('site_name', '@hostmaster');
57
    drush_set_option('old_platform', d('@hostmaster')->platform->name);
58

59
    $new_platform = _provision_path_to_platform_alias($platform);
60
    drush_invoke_process('@none', "provision-save", array($new_platform), array(
61 62 63 64
      'context_type' => 'platform',
      'server' => '@server_master',
      'web_server' => '@server_master',
      'root' => $platform,
65
      'makefile' => drush_get_option('makefile', dirname(__FILE__) . '/aegir.make'),
66
    ));
67 68 69 70 71 72
    // propagate working-copy args downward
    $options = array();
    if (drush_get_option('working-copy')) {
      $options['working-copy'] = 1;
    }
    provision_backend_invoke($new_platform, 'provision-verify', array(), $options);
73
    drush_set_option('new_platform', $new_platform);
omega8cc's avatar
omega8cc committed
74
    // exit if an error has occured.
75 76 77
    if (drush_get_error()) {
      return false;
    }
78
  }
79 80 81 82
  else {

    // add a server record :
    $server = '@server_master';
83 84 85 86
    /**
     * @deprecated in drush3 it's 'options', in drush 4 it's 'cli', drop
     * 'options' when we drop drush3 support
     */
87
    $context = drush_get_context('cli') ? 'cli' : 'options';
88
    drush_invoke_process('@none', "provision-save", array($server), array(
89 90 91 92 93
      'context_type' => 'server',
      'aegir_root' => dirname(rtrim($data['config_path'], '/')),
      'remote_host' => php_uname('n'),
      'script_user' => $data['script_user'],
     // apache or nginx or..
94
      'http_service_type' => drush_get_option('http_service_type', 'apache', $context),
95 96 97 98 99 100 101
      'web_group' => $data['web_group'],
      'master_url' => $data['master_url'],
      'restart_cmd' => $data['restart_cmd'],
     // mysql
      'db_service_type' => 'mysql',
      'master_db' => $data['master_db'],
    ));
102

103 104
    provision_backend_invoke($server, 'provision-verify');
    drush_set_option('server_name', $server);
omega8cc's avatar
omega8cc committed
105
    // exit if an error has occured.
106 107 108
    if (drush_get_error()) {
      return false;
    }
109

omega8cc's avatar
omega8cc committed
110

111
    $old_platform = _provision_path_to_platform_alias($data['publish_path']);
112
    drush_invoke_process('@none', "provision-save", array($old_platform), array(
113 114 115 116 117 118 119
      'context_type' => 'platform',
      'server' => $server,
      'web_server' => $server,
      'root' => $data['publish_path'],
    ));
    provision_backend_invoke($old_platform, 'provision-verify');
    drush_set_option('old_platform', $old_platform);
omega8cc's avatar
omega8cc committed
120
    // exit if an error has occured.
121 122 123 124
    if (drush_get_error()) {
      return false;
    }

125
    $new_platform = _provision_path_to_platform_alias($platform);
126
    drush_invoke_process('@none', "provision-save", array($new_platform), array(
127 128 129 130
      'context_type' => 'platform',
      'server' => $server,
      'web_server' => $server,
      'root' => $platform,
131
      'makefile' => drush_get_option('makefile', dirname(__FILE__) . '/aegir.make'),
132 133 134
    ));
    provision_backend_invoke($new_platform, 'provision-verify');
    drush_set_option('new_platform', $new_platform);
omega8cc's avatar
omega8cc committed
135
    // exit if an error has occured.
136 137 138 139 140 141
    if (drush_get_error()) {
      return false;
    }


    $site_name = '@hostmaster';
142
    drush_invoke_process('@none', "provision-save", array($site_name), array(
143 144 145 146 147
      'context_type' => 'site',
      'platform' => $old_platform,
      'db_server' => $server,
      'uri' => $site,
      'profile' => 'hostmaster',
148
      'drush_aliases' => 'hm',
149 150 151
    ));
    provision_backend_invoke($site_name, 'provision-verify');
    drush_set_option('site_name', $site_name);
omega8cc's avatar
omega8cc committed
152
    // exit if an error has occured.
153 154 155 156
    if (drush_get_error()) {
      return false;
    }
  }
157 158
}

159 160 161
/**
 * Prepare the hostmaster site for migration
 */
162
function drush_provision_pre_hostmaster_migrate($site, $platform) {
163 164
  // we ignore errors here through integrate = FALSE
  provision_backend_invoke(drush_get_option('site_name'), 'hosting-pause', array(), array('#integrate' => FALSE));
165 166
}

167
function drush_provision_hostmaster_migrate($site, $platform) {
168 169
  // See: https://www.drupal.org/node/2762701.
  $platform_version = provision_backend_invoke(drush_get_option('site_name'), 'status', array(), array('fields' => 'drupal-version', 'field-labels' => 0));
170
  $major_version_upgrade = version_compare('7.0', trim($platform_version['output'])) > 0 ? TRUE : FALSE;
171 172 173
  if ($major_version_upgrade) {
    drush_log('Detected major version upgrade. Disabling error-reporting.', 'warning');
    $local_settings_file = d('@hostmaster')->site_path . '/local.settings.php';
174 175 176
    if (!file_exists($local_settings_file)) {
      file_put_contents($local_settings_file, "<?php # local settings.php \n");
    }
177 178 179 180
    $local_settings_backup = $local_settings_file . '.bak';
    provision_file()->copy($local_settings_file, $local_settings_backup)
      ->succeed('Backed up local.settings.php.')
      ->fail('Failed to backup of local.settings.php.');
181
    provision_file()->chmod($local_settings_file, 02640);
182 183 184 185 186 187 188 189
    $lines_to_append = "
    // These lines were added to overcome a false-positive error thrown in Drupal 7.50+
    error_reporting(0);
    ini_set('display_errors', FALSE);
    ini_set('display_startup_errors', FALSE);";
    file_put_contents($local_settings_file, $lines_to_append, FILE_APPEND);
  }

Adrian Rossouw's avatar
Adrian Rossouw committed
190
  provision_backend_invoke(drush_get_option('site_name'), 'provision-migrate', array(drush_get_option('new_platform')));
191 192 193 194 195 196

  if ($major_version_upgrade) {
    drush_log('Re-enabling error-reporting.', 'notice');
    // The site path has changed, so update file paths.
    $local_settings_file = $platform . '/sites/' . d('@hostmaster')->uri . '/local.settings.php';
    $local_settings_backup = $local_settings_file . '.bak';
197
    provision_file()->chmod($local_settings_file, 02640);
198 199 200 201 202
    provision_file()->copy($local_settings_backup, $local_settings_file)
      ->succeed('Restored backup of local.settings.php.')
      ->fail('Failed to restore backup of local.settings.php.');
    provision_file()->unlink($local_settings_backup);
  }
203 204 205
}

function drush_provision_post_hostmaster_migrate($site, $platform) {
206
  provision_backend_invoke(drush_get_option('site_name'), 'cache-clear', array('drush'));
207 208
  // we pass the context names we generated to the task so we can enforce that the names
  // stay the same.
209
  provision_backend_invoke(drush_get_option('site_name'), 'hosting-resume', array(), array(
210 211
    'old_platform_name' => drush_get_option('old_platform'),
    'new_platform_name' => drush_get_option('new_platform'),
Adrian Rossouw's avatar
Adrian Rossouw committed
212
  ));
213 214 215
  // We need to re-verify @server_master via frontend to re-generate
  // its drush alias and to update Nginx configuration files.
  provision_backend_invoke('@hostmaster', 'hosting-task', array(@server_master, 'verify'), array('force' => TRUE));
216 217
}