clone.provision.inc 4.2 KB
Newer Older
1 2 3
<?php

/**
4
 * Clone command implementation
5
 *
6
 * This command when called will
7
 *   1. Make a backup of the current site, before modifications are made.
8
 *   2. Execute 'provision deploy' to build the new site using the backup of the old site.
9 10 11 12 13 14
 *
 */

/**
 * Make sure we have a valid site being cloned, and that the file being cloned from exists
 */
15
function drush_provision_drupal_provision_clone_validate($new_name = null, $platform = null) {
16 17 18 19 20 21
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
}

/**
 * Make a backup before making any changes, and add extract the file we are restoring from
 */
22
function drush_provision_drupal_pre_provision_clone($new_name, $platform = null) {
23
  drush_invoke('provision-backup');
24 25 26 27 28
}

/**
 * Remove the extracted site directory
 */
29
function drush_provision_drupal_pre_provision_clone_rollback($new_name, $platform = null) {
30
  $success =  provision_file()->unlink(drush_get_option('backup_file'))
31 32
    ->succeed('Removed unused clone site package')
    ->fail('Could not remove unused clone site package');
33 34 35 36 37
}

/**
 * Switch the clone directories around now that we have the new db installed
 */
38
function drush_provision_drupal_provision_clone($new_name, $platform = null) {
39 40
  drush_set_option('old_platform', d()->platform->name);

41 42 43 44 45 46 47 48 49 50 51 52 53
  // If the site is cloned between platforms and not just in the same platform,
  // we should update the info collected about source and target platform first.
  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.
  }
  // We should update also the info collected about the site before running clone.
  $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.

54
  $options = d()->options;
55
  $options['uri'] = ltrim($new_name, '@');
56
  $hash_name = drush_get_option('#name') ? '#name' : 'name';
57
  $options[$hash_name] = $new_name;
58
  $options['platform'] = (isset($platform)) ? $platform : $options['platform'];
59
  $options['root'] = d($options['platform'])->root;
60 61
  $options['aliases'] = array();
  $options['redirection'] = 0;
62

63 64 65 66 67 68 69
  // XXX: right now we just drop SSL configuration when cloning, because
  // we do not check if the site name change is compatible with the
  // certificate. This can be removed when we a) check for wildcard
  // certs and b) allow the user to change the cert from the UI.
  $options['ssl_enabled'] = 0;
  unset($options['ssl_key']);

70 71 72 73
  // Do not copy CDN aliases to the cloned site --CDN vhost
  $options['cdn'] = 0;
  unset($options['cdn']);

74 75 76 77
  if ($profile = drush_get_option('profile', FALSE)) {
    $options['profile'] = $profile;
  }

78 79 80 81
  if ($db_server = drush_get_option('new_db_server', FALSE)) {
    $options['db_server'] = $db_server;
  }

82
  drush_invoke_process('@none', 'provision-save', array($new_name), $options);
83
  # note that we reset the aliases so they don't conflict with the original site
84

85 86 87 88 89 90 91 92 93
  // 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, 'clone');

  provision_backend_invoke($new_name, 'provision-deploy', array(drush_get_option('backup_file')), $deploy_options);
94 95

  if (!drush_get_error()) {
96
    // Verify the newly cloned site.
97
    provision_backend_invoke($new_name, 'provision-verify');
98 99 100
    sleep(5); // A small trick to avoid high load and race conditions.
    // Verify again also original site via frontend to avoid issue #1004526
    provision_backend_invoke('@hostmaster', 'hosting-task', array($local_uri_verify, 'verify'), array('force' => TRUE));
101
  }
102

103 104
  provision_reload_config('site', d()->site_path . '/drushrc.php');

105
}