summaryrefslogtreecommitdiffstats
path: root/platform/clone.provision.inc
blob: c70cc6f3a177d7eb60792bedf5ae1159c4cb1133 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

/**
 * Clone command implementation
 *
 * This command when called will 
 *   1. Make a backup of the current site, before modifications are made.
 *   2. Execute 'provision deploy' to build the new site using the backup of the old site.
 *
 */

/**
 * Make sure we have a valid site being cloned, and that the file being cloned from exists
 */
function drush_provision_drupal_provision_clone_validate($new_uri = null, $platform = null) {
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
}

/**
 * Make a backup before making any changes, and add extract the file we are restoring from
 */
function drush_provision_drupal_pre_provision_clone($new_uri, $platform = null) {
  drush_invoke('provision-backup');
}

/**
 * Remove the extracted site directory
 */
function drush_provision_drupal_pre_provision_clone_rollback($new_uri, $platform = null) {
  $success =  provision_file()->unlink(drush_get_option('backup_file'))
    ->succeed('Removed unused clone site package')
    ->fail('Could not remove unused clone site package');
}

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

  // 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.

  $options = d()->options;
  $options['uri'] = ltrim($new_uri, '@');
  $hash_name = drush_get_option('#name') ? '#name' : 'name';
  $options[$hash_name] = $new_uri;
  $options['platform'] = (isset($platform)) ? $platform : $options['platform'];
  $options['root'] = d($options['platform'])->root;
  $options['aliases'] = array();
  $options['redirection'] = 0;

  // 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']);

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

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

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

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

  provision_backend_invoke($new_uri, 'provision-deploy', array(drush_get_option('backup_file')), array('old_uri' => d()->uri));

  if (!drush_get_error()) {
    // Verify the newly cloned site.
    provision_backend_invoke($new_uri, 'provision-verify');
    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));
  }

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

}