deploy.provision.inc 8.67 KB
Newer Older
1 2 3
<?php

/**
helmo's avatar
helmo committed
4
 * @file
5
 * Deploy command implementation
6
 *
helmo's avatar
helmo committed
7
 * This command when called will
8 9 10 11 12 13 14 15 16
 *   1. Extract the backup that is being deployed to the target folder in the sites directory.
 *   2. Import the drushrc.php details.
 *   3. Do some diagnostics to make sure all the needed packages are available.
 *   4. Create a new database, belonging to the site's user, and import the database dump.
 *   5. Regenerate configuration files to show new db settings.
 *   6. Call the drush 'updatedb' command to update the database if neccesary.
 *   7. Update the file paths to the new sites directory.
 *   8. Rebuild the site's package manifest.
 *   9. Save the new drushrc.php with the newly generated settings.
17 18 19
 */

/**
helmo's avatar
helmo committed
20
 * Make sure we have a valid site being deployd, and that the file being deployd from exists.
21 22
 *
 * Implementation of drush_hook_COMMAND_validate().
23
 */
helmo's avatar
helmo committed
24
function drush_provision_drupal_provision_deploy_validate($backup_file = NULL) {
25
  $exists = provision_file()->exists($backup_file)
26 27 28
    ->succeed('Deploying site from @path')
    ->fail('Could not find backup file @path', 'PROVISION_BACKUP_NOT_FOUND')
    ->status();
29
  if ($exists) {
30
    drush_set_option('backup_file', $backup_file);
31
  }
32 33 34 35 36 37 38

  $exists = provision_file()->exists(d()->site_path)
    ->succeed('Replacing the existing site at @path')
    ->status();

  if ($exists) {
    drush_set_option('extract_path', d()->site_path . '.restore');
39
    drush_set_option('old_db_name', drush_get_option('db_name', ''));
40 41 42 43 44 45
  }
  else {
    drush_set_option('extract_path', d()->site_path);
  }

  drush_set_option('deploy_replace_site', $exists);
46 47 48
}

/**
helmo's avatar
helmo committed
49
 * Make a backup before making any changes, and add extract the file we are restoring from.
50
 *
helmo's avatar
helmo committed
51
 * Implements drush_hook_pre_COMMAND().
52
 */
53
function drush_provision_drupal_pre_provision_deploy($backup_file) {
helmo's avatar
helmo committed
54
  $extracted = provision_file()->extract($backup_file, drush_get_option('extract_path'))
55
    ->succeed('Successfully extracted the contents of @path')
56
    ->fail('Failed to extract the contents of @path to @target', 'PROVISION_BACKUP_EXTRACTION_FAILED')
57
    ->status();
helmo's avatar
helmo committed
58

59
  if ($extracted) {
60 61 62 63
    // Make sure the files in the files directory are accessible by the web server.
    provision_file()->chgrp(drush_get_option('extract_path') . '/files', d('@server_master')->web_group, TRUE)
      ->succeed('Changed group ownership of files in <code>@path</code> to @gid')
      ->fail('Could not change group ownership of files in <code>@path</code> to @gid');
64 65 66
    provision_file()->chgrp(drush_get_option('extract_path') . '/private/files', d('@server_master')->web_group, TRUE)
      ->succeed('Changed group ownership of private files in <code>@path</code> to @gid')
      ->fail('Could not change group ownership of private files in <code>@path</code> to @gid');
67 68 69
    provision_file()->chgrp(drush_get_option('extract_path') . '/private/temp', d('@server_master')->web_group, TRUE)
      ->succeed('Changed group ownership of temp files in <code>@path</code> to @gid')
      ->fail('Could not change group ownership of temp files in <code>@path</code> to @gid');
70 71 72
    provision_file()->chgrp(drush_get_option('extract_path') . '/private/config', d('@server_master')->web_group, TRUE)
      ->succeed('Changed group ownership of config files in <code>@path</code> to @gid')
      ->fail('Could not change group ownership of config files in <code>@path</code> to @gid');
73

74 75 76 77 78 79 80 81 82 83 84 85
    if (drush_get_option('deploy_replace_site', FALSE)) {
      $old = d()->site_path . '.restore';
      $new = d()->site_path;

      $swapped = provision_file()->switch_paths($old, $new)
        ->succeed('Swapping out the @path1 and @path2 directories was successful.')
        ->fail('Swapping the @path1 and @path2 directories has failed.', 'DRUSH_PERM_ERROR')
        ->status();
      if ($swapped) {
        drush_set_option('site_dirs_swapped', TRUE);
      }
      else {
helmo's avatar
helmo committed
86
        return FALSE;
87 88 89
      }
    }

90 91 92 93 94
    // We have already created a new database. Save the info to the config files.
    provision_prepare_environment();
    provision_save_site_data();

    // Load the newly created information, including re-loading the new db creds.
95
    drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
96
    _provision_drupal_create_settings_file();
97
    provision_drupal_push_site($override_slave_authority = TRUE);
98 99 100

    $site_packages = drush_get_option('packages', array(), 'site');
    $drupal_packages = drush_get_option('packages', array(), 'drupal');
101
    $merged_modules = isset($drupal_packages['base']['modules']) ? $drupal_packages['base']['modules'] : array();
102 103 104 105 106 107 108 109 110 111 112
    if (isset($site_packages['profiles'])) {
      $profiles = array_keys($site_packages['profiles']);
      $profile = $profiles[0];
      if (isset($drupal_packages['profiles'][$profile]['modules'])) {
        $merged_modules = array_merge($merged_modules, $drupal_packages['profiles'][$profile]['modules']);
      }
    }

    if (isset($site_packages['modules']) && is_array($site_packages['modules'])) {
      foreach ($site_packages['modules'] as $name => $module) {
        if ($module['status'] == 1) {
113 114 115 116
          if (isset($module['platform']) && $module['platform'] != '-1') {
            drush_log(dt("The !name module is installed in the site's modules directory. Skipping schema version check.", array('!name' => $name)));
          }
          elseif (!array_key_exists($name, $merged_modules)) {
117
            drush_log(dt("Could not find a version of the !name module", array('!name' => $name)), 'warning');
Adrian Rossouw's avatar
Adrian Rossouw committed
118 119
          }
          else {
120
            if (($merged_modules[$name]['schema_version'] > 0) && ($module['schema_version'] > $merged_modules[$name]['schema_version'])) {
helmo's avatar
helmo committed
121 122
              drush_set_error('PROVISION_SCHEMA_UPGRADE_FAILURE',
                dt("The version of the !name module found on this platform (!versionB) has a lower Schema version than the one the site has installed (!versionA)",
123 124 125
                array('!name' => $name, '!versionA' => $module['schema_version'], '!versionB' => $merged_modules[$name]['schema_version'])));
            }
            else {
helmo's avatar
helmo committed
126
              drush_log(dt("Found a valid version of the !name module with schema version !schema_version",
127 128
                array('!name' => $name, '!schema_version' => $merged_modules[$name]['schema_version'])));
            }
Adrian Rossouw's avatar
Adrian Rossouw committed
129 130 131 132
          }
        }
      }
    }
133 134 135 136
  }
}

/**
helmo's avatar
helmo committed
137
 * Remove the extracted site directory.
138 139
 *
 * Implementation of drush_hook_pre_COMMAND_rollback().
140
 */
141
function drush_provision_drupal_pre_provision_deploy_rollback() {
142 143 144
  if (drush_get_option('deploy_replace_site', FALSE)) {
    if (drush_get_option('site_dirs_swapped', FALSE)) {

helmo's avatar
helmo committed
145
      // Swap the site directories back if necessary.
146 147 148 149
      $old = d()->site_path . '.restore';
      $new = d()->site_path;

       provision_file()->switch_paths($old, $new)
helmo's avatar
helmo committed
150 151
         ->succeed('Swapping out the @path1 and @path2 directories was successful.')
         ->fail('Swapping the @path1 and @path2 directories has failed.', 'DRUSH_PERM_ERROR');
152 153 154 155 156

       _provision_drupal_create_settings_file();
       provision_save_site_data();
    }
  }
helmo's avatar
helmo committed
157

158 159
  if (provision_file()->exists(drush_get_option('extract_path'))->status()) {
    _provision_recursive_delete(drush_get_option('extract_path'));
160
  }
161 162
}

163
/**
helmo's avatar
helmo committed
164
 * Implements drush_hook_COMMAND().
165
 */
166
function drush_provision_drupal_provision_deploy() {
drumm's avatar
drumm committed
167
  _provision_drupal_maintain_aliases();
168
  _provision_drupal_create_directories();
169 170
}

171
/**
helmo's avatar
helmo committed
172
 * Implements drush_hook_post_COMMAND().
173
 */
174
function drush_provision_drupal_post_provision_deploy() {
helmo's avatar
helmo committed
175
  // Call the drush updatedb command.
176
  provision_backend_invoke(d()->name, 'updatedb');
177 178 179 180
  // We should be able to fully load Drupal now.
  if (drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
    drush_include_engine('drupal', 'deploy');
    drush_set_option('packages', _scrub_object(provision_drupal_system_map()), 'site');
181
    _provision_drupal_rebuild_caches();
182

helmo's avatar
helmo committed
183
    // Rebuild the node access tables only if necessary.
anarcat's avatar
anarcat committed
184
    if (!function_exists("node_access_needs_rebuild") || node_access_needs_rebuild()) {
185
      node_access_rebuild();
186
      drush_log(dt('Rebuilt node access table'));
187
    }
helmo's avatar
helmo committed
188 189
  }
  else {
190
    drush_log("could not bootstrap drupal after updatedb");
191
  }
192

helmo's avatar
helmo committed
193
  // Remove the restore directory.
194 195 196 197
  if (!drush_get_error() && drush_get_option('deploy_replace_site', FALSE)) {
    _provision_recursive_delete(drush_get_option('extract_path'));
  }

198 199 200 201 202 203 204 205 206 207 208
  // Remove the old database.
  if (!drush_get_error() && drush_get_option('deploy_replace_site', FALSE)) {
    if ($old_database = drush_get_option('old_db_name', '')) {
      if (!d()->service('db')->drop_database($old_database)) {
        drush_log(dt('Failed to drop database @old_database', array('@old_database' => $old_database)), 'warning');
      }
      else {
        drush_log(dt('Dropped the old database (@old_database).', array('@old_database' => $old_database)), 'info');
      }
    }
  }
209
}