Commit 111ad289 authored by Adrian Rossouw's avatar Adrian Rossouw

Merge the functionality of the restore command into the deploy command, and have it re-use that.

parent 771e4181
......@@ -4,47 +4,7 @@ function drush_db_provision_restore_validate() {
d()->service('db')->connect();
}
function drush_db_pre_provision_restore() {
drush_set_option('old_db_creds', d()->service('db')->fetch_site_credentials());
d()->service('db')->create_site_database();
}
function drush_db_provision_restore() {
d()->service('db')->import_site_database(d()->site_path . '.restore/database.sql');
}
function drush_db_pre_provision_restore_rollback() {
d()->service('db')->destroy_site_database();
$keys = array('db_name', 'db_passwd', 'db_user', 'db_host');
//Restore the original database credentials of the site.
// They were never truly lost, but the options in the 'process' context
// were overriding them. By unsetting the temporary options, the originals
// are automatically restored.
foreach ($keys as $key) {
drush_unset_option($key, 'process');
}
}
// Rollback doesn't apply here yet. Unless we trigger a restore of the first dump
// made. Which could go on infinitely if something is really long.
function drush_db_post_provision_restore() {
provision_file()->unlink(d()->site_path . '/database.sql')
->succeed('Removed dump file @path after restoring from it')
->fail('Could not remove dump file @path', 'DRUSH_PERM_ERROR');
d()->service('db')->destroy_site_database(drush_get_option('old_db_creds'));
// TODO - test this .. it's probably horribly broken .. deleting the newly restored db instead of the old one.
// The new database credentials will be saved against the site now.
drush_set_option('db_name', drush_get_option('db_name'), 'site');
drush_set_option('db_type', drush_get_option('db_type'), 'site');
drush_set_option('db_user', drush_get_option('db_user'), 'site');
drush_set_option('db_host', drush_get_option('db_host'), 'site');
drush_set_option('db_passwd', drush_get_option('db_passwd'), 'site');
d()->service('db')->destroy_site_database();
}
......@@ -12,3 +12,5 @@ function drush_http_post_provision_deploy_rollback() {
d()->service('http')->create_site_config();
d()->service('http')->parse_configs();
}
......@@ -4,9 +4,7 @@
* Implementation of hook_provision_pre_restore
*/
function drush_http_pre_provision_restore() {
drush_set_option('redirect_url', drush_get_option('web_maintenance_url'));
d()->service('http')->create_site_config();
d()->service('http')->parse_configs();
d()->service('http')->delete_site_config();
}
function drush_http_pre_provision_restore_rollback() {
......@@ -14,13 +12,3 @@ function drush_http_pre_provision_restore_rollback() {
d()->service('http')->parse_configs();
}
function drush_http_post_provision_restore() {
d()->service('http')->create_site_config();
d()->service('http')->parse_configs();
}
// The old db details should have been switched back by the mysql rollback at this point.
function drush_http_post_provision_restore_rollback() {
drush_http_post_provision_restore();
}
......@@ -26,6 +26,19 @@ function drush_provision_drupal_provision_deploy_validate($backup_file = null) {
if ($exists) {
drush_set_option('backup_file', $backup_file);
}
$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');
}
else {
drush_set_option('extract_path', d()->site_path);
}
drush_set_option('deploy_replace_site', $exists);
}
/**
......@@ -33,11 +46,28 @@ function drush_provision_drupal_provision_deploy_validate($backup_file = null) {
*/
function drush_provision_drupal_pre_provision_deploy($backup_file) {
// the url is likely to have changed in the deployment
$extracted = provision_file()->extract($backup_file, d()->site_path)
$extracted = provision_file()->extract($backup_file, drush_get_option('extract_path'))
->succeed('Successfully extracted the contents of @path')
->fail('Failed to extract the contents of @path to @target', 'PROVISION_BACKUP_EXTRACTION_FAILED')
->status();
if ($extracted) {
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);
_provision_drupal_create_settings_file();
}
else {
return false;
}
}
drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
$site_packages = drush_get_option('packages', array(), 'site');
......@@ -71,9 +101,29 @@ function drush_provision_drupal_pre_provision_deploy($backup_file) {
* Remove the extracted site directory
*/
function drush_provision_drupal_pre_provision_deploy_rollback() {
if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
_provision_recursive_delete($site_root);
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'));
}
}
function drush_provision_drupal_provision_deploy() {
......@@ -81,7 +131,9 @@ function drush_provision_drupal_provision_deploy() {
}
function drush_provision_drupal_post_provision_deploy() {
provision_prepare_environment();
_provision_drupal_create_directories();
_provision_drupal_create_settings_file();
provision_save_site_data();
// call the drush updatedb command.
......@@ -92,4 +144,11 @@ function drush_provision_drupal_post_provision_deploy() {
drush_set_option('packages', _scrub_object(provision_drupal_system_map()), 'site');
_provision_drupal_rebuild_caches();
}
// remove the restore directory
if (!drush_get_error() && drush_get_option('deploy_replace_site', FALSE)) {
_provision_recursive_delete(drush_get_option('extract_path'));
}
}
<?php
/**
* Restore command implementation
*
* This command when called will
* 1. Make a backup of the current site, before modifications are made.
* 2. Temporarily disable the site by causing apache to redirect to a help page. Restarting apache is required.
* 3. Extract the backup that is being migrated to to a temporary folder in the sites directory.
* 4. Create a new database, belonging to the site's user, and switch it around with the current site's database.
* 5. Import the old database and site.php details.
* 6. Switch around the sites directory of the current site and the backup being migrated.
* 7. Regenerate configuration files.
* 8. TODO: diagnostic to test that everything is ok?
* 9. Remove the temporary redirect and restart apache so the previous site is available again.
* 10. Remove the extranuous db and duplicate site directory.
*
* If at any time an error occurs, before step 9. It should reverse all the changes it has made,
* and leave the current site directory and database in the right place, and remove all cruft that
* was created by this process.
* Migrate command implementation
*/
/**
......@@ -38,6 +22,12 @@ function drush_provision_drupal_pre_provision_migrate($platform = null) {
drush_set_option('site_offline', 1);
_provision_drupal_create_settings_file();
drush_invoke('provision-backup');
drush_set_option('old_platform', d()->platform->name);
if (d()->platform->name == $platform) {
// deploying over existing site.
drush_set_option('deploy_replace_site', TRUE);
}
}
/**
......@@ -54,13 +44,13 @@ function drush_provision_drupal_pre_provision_migrate_rollback($platform) {
->fail('Could not remove unused migration site package');
d()->service('http')->create_site_config();
d()->service('http')->parse_configs();
}
/**
* Switch the migrate directories around now that we have the new db installed
*/
function drush_provision_drupal_provision_migrate($platform) {
drush_set_option('old_platform', d()->platform->name);
drush_backend_invoke_args('provision-save', array(d()->name), array('platform' => $platform, 'root' => null, 'uri' => null));
provision_backend_invoke(d()->name, 'provision-deploy', array(drush_get_option('backup_file')));
if (!drush_get_error()) {
......@@ -79,12 +69,15 @@ function drush_provision_drupal_provision_migrate_rollback($platform) {
* Delete the old site directory and recreate the settings file
*/
function drush_provision_drupal_post_provision_migrate() {
drush_set_option('installed', FALSE);
// we remove the aliases even if redirection is enabled as a precaution
// if redirection is enabled, keep silent about errors
_provision_drupal_delete_aliases(d()->aliases);
_provision_recursive_delete(d()->site_path);
d()->service('http')->sync(d()->site_path);
drush_set_option('installed', FALSE)
if (!drush_get_option('deploy_replace_site', FALSE)) {
// we remove the aliases even if redirection is enabled as a precaution
// if redirection is enabled, keep silent about errors
_provision_drupal_delete_aliases(d()->aliases);
_provision_recursive_delete(d()->site_path);
d()->service('http')->sync(d()->site_path);
}
}
<?php
/**
* Restore command implementation
*
* This command when called will
* 1. Make a backup of the current site, before modifications are made.
* 2. Temporarily disable the site by causing apache to redirect to a help page. Restarting apache is required.
* 3. Extract the backup that is being restored to to a temporary folder in the sites directory.
* 4. Create a new database, belonging to the site's user, and switch it around with the current site's database.
* 5. Import the old database and site.php details.
* 6. Switch around the sites directory of the current site and the backup being restored.
* 7. Regenerate configuration files.
* 8. TODO: diagnostic to test that everything is ok?
* 9. Remove the temporary redirect and restart apache so the previous site is available again.
* 10. Remove the extranuous db and duplicate site directory.
*
* If at any time an error occurs, before step 9. It should reverse all the changes it has made,
* and leave the current site directory and database in the right place, and remove all cruft that
* was created by this process.
*/
/**
* Make sure we have a valid site being restored, and that the file being restored from exists
*/
......@@ -39,47 +19,26 @@ function drush_provision_drupal_provision_restore_validate($restore_file = null)
* Make a backup before making any changes, and add extract the file we are restoring from
*/
function drush_provision_drupal_pre_provision_restore($restore_file) {
drush_log(dt("Putting site under maintenance"));
drush_set_option('site_offline', 1);
_provision_drupal_create_settings_file();
drush_invoke('provision-backup');
provision_file()->extract(drush_get_option('restore_file'), d()->site_path . '.restore')
->succeed('Successfully extracted the contents of @path')
->fail('Failed to extract the contents of @path', 'PROVISION_BACKUP_EXTRACTION_FAILED');
}
/**
* Remove the extracted site directory
*/
function drush_provision_drupal_pre_provision_restore_rollback() {
_provision_recursive_delete(d()->site_path . '.restore');
function drush_provision_drupal_pre_provision_restore_rollback($restore_file) {
$success = provision_file()->unlink(drush_get_option('backup_file'))
->succeed('Removed unused migration site package')
->fail('Could not remove unused migration site package');
}
/**
* Switch the restore directories around now that we have the new db installed
*/
function drush_provision_drupal_provision_restore() {
$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');
// make sure it has the latest site data available
_provision_drupal_create_settings_file();
provision_save_site_data();
}
// Luckily this is reversable =)
function drush_provision_drupal_provision_restore_rollback() {
drush_provision_drupal_provision_restore();
}
function drush_provision_drupal_provision_restore($restore_file) {
/**
* Delete the old site directory and recreate the settings file
*/
function drush_provision_drupal_post_provision_restore() {
_provision_recursive_delete(d()->site_path . '.restore');
// This is the actual drupal provisioning requirements.
_provision_drupal_create_directories();
_provision_drupal_create_settings_file();
provision_backend_invoke(d()->name, 'provision-deploy', array(drush_get_option('restore_file')));
if (!drush_get_error()) {
provision_backend_invoke(d()->name, 'provision-verify');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment