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

/**
4
 * Deploy command implementation
5 6
 *
 * This command when called will 
7 8 9 10 11 12 13 14 15
 *   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.
16 17 18 19
 */

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

  $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');
38
    drush_set_option('old_db_name', drush_get_option('db_name', ''));
39 40 41 42 43 44
  }
  else {
    drush_set_option('extract_path', d()->site_path);
  }

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

/**
 * Make a backup before making any changes, and add extract the file we are restoring from
49 50
 *
 * Implementation of drush_hook_pre_COMMAND().
51
 */
52
function drush_provision_drupal_pre_provision_deploy($backup_file) {
53
  // the url is likely to have changed in the deployment
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();
58
  if ($extracted) {
59 60 61 62
    // 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');
63 64 65
    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');
66 67 68
    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');
69

70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    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 {
        return false;
      }
    }

87 88 89 90 91
    // 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.
92
    drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
93
    _provision_drupal_create_settings_file();
94
    provision_drupal_push_site($override_slave_authority = TRUE);
95 96 97

    $site_packages = drush_get_option('packages', array(), 'site');
    $drupal_packages = drush_get_option('packages', array(), 'drupal');
98
    $merged_modules = isset($drupal_packages['base']['modules']) ? $drupal_packages['base']['modules'] : array();
99 100 101 102 103 104 105 106 107 108 109
    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) {
110 111 112 113
          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)) {
114
            drush_log(dt("Could not find a version of the !name module", array('!name' => $name)), 'warning');
Adrian Rossouw's avatar
Adrian Rossouw committed
115 116
          }
          else {
117 118 119 120 121 122 123 124 125
            if (($merged_modules[$name]['schema_version'] > 0) && ($module['schema_version'] > $merged_modules[$name]['schema_version'])) {
              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)", 
                array('!name' => $name, '!versionA' => $module['schema_version'], '!versionB' => $merged_modules[$name]['schema_version'])));
            }
            else {
              drush_log(dt("Found a valid version of the !name module with schema version !schema_version", 
                array('!name' => $name, '!schema_version' => $merged_modules[$name]['schema_version'])));
            }
Adrian Rossouw's avatar
Adrian Rossouw committed
126 127 128 129
          }
        }
      }
    }
130 131 132 133 134
  }
}

/**
 * Remove the extracted site directory
135 136
 *
 * Implementation of drush_hook_pre_COMMAND_rollback().
137
 */
138
function drush_provision_drupal_pre_provision_deploy_rollback() {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  if (drush_get_option('deploy_replace_site', FALSE)) {
    if (drush_get_option('site_dirs_swapped', FALSE)) {

      // swap the site directories back if necessary.
      $old = d()->site_path . '.restore';
      $new = d()->site_path;

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

       _provision_drupal_create_settings_file();
       provision_save_site_data();
    }
  }
      
  if (provision_file()->exists(drush_get_option('extract_path'))->status()) {
    _provision_recursive_delete(drush_get_option('extract_path'));
157
  }
158 159
}

160 161 162
/**
 * Implementation of drush_hook_COMMAND().
 */
163
function drush_provision_drupal_provision_deploy() {
drumm's avatar
drumm committed
164
  _provision_drupal_maintain_aliases();
165
  _provision_drupal_create_directories();
166 167
}

168 169 170
/**
 * Implementation of drush_hook_post_COMMAND().
 */
171
function drush_provision_drupal_post_provision_deploy() {
172
  // call the drush updatedb command.
173
  provision_backend_invoke(d()->name, 'updatedb');
174 175 176 177
  // 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');
178
    _provision_drupal_rebuild_caches();
179 180

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


  // remove the restore directory 
  if (!drush_get_error() && drush_get_option('deploy_replace_site', FALSE)) {
    _provision_recursive_delete(drush_get_option('extract_path'));
  }

195 196 197 198 199 200 201 202 203 204 205
  // 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');
      }
    }
  }
206
}