Commit 5b3da29a authored by Adrian Rossouw's avatar Adrian Rossouw

Committed path 11 from #736706 - thanks drumm

This gets rid of the path function, and introduces method chaining for the file service, which results in far cleaner code.
parent 15d8e38f
......@@ -6,8 +6,9 @@ function drush_db_pre_provision_backup($url = NULL) {
}
function drush_db_pre_provision_backup_rollback($url = NULL) {
provision_service('file')->path("unlink", drush_get_option('sites_path') . "/$url/database.sql", TRUE, dt("Deleted mysql dump from sites directory"),
dt("Could not delete mysql dump from sites directory"));
provision_service('file')->unlink(drush_get_option('sites_path') . '/' . $url . '/database.sql')
->succeed('Deleted mysql dump from sites directory')
->fail('Could not delete mysql dump from sites directory');
}
function drush_db_post_provision_backup($url = NULL) {
......
......@@ -194,14 +194,15 @@ class provisionService_db extends provisionService {
$creds = $this->fetch_site_credentials();
$exists = provision_service('file')->path("exists", $dump_file, TRUE,
dt('Found database dump at @path.'),
dt('No database dump was found at @path.'),
'PROVISION_DB_DUMP_NOT_FOUND');
$exists = provision_service('file')->exists($dump_file)
->succeed('Found database dump at @path.')
->fail('No database dump was found at @path.', 'PROVISION_DB_DUMP_NOT_FOUND')
->status();
if ($exists) {
$readable = provision_service('file')->path("readable", $dump_file, TRUE, dt('Database dump at @path is readable'),
dt('The database dump at @path could not be read.'),
'PROVISION_DB_DUMP_NOT_READABLE');
$readable = provision_service('file')->readable($dump_file)
->succeed('Database dump at @path is readable')
->fail('The database dump at @path could not be read.', 'PROVISION_DB_DUMP_NOT_READABLE')
->status();
if ($readable) {
$this->import_dump($dump_file, $creds);
}
......
......@@ -17,7 +17,7 @@ function drush_db_provision_deploy_rollback($url = NULL) {
// Rollback doesn't apply here yet. Unless we trigger a deploy of the first dump
// made. Which could go on infinitely if something is really long.
function drush_db_post_provision_deploy($url) {
provision_service('file')->path('unlink', drush_get_option('sites_path') .'/'. $url .'/database.sql', TRUE,
dt("Removed dump file @path after restoring from it"),
dt("Could not remove dump file @path"), 'DRUSH_PERM_ERROR');
provision_service('file')->unlink(drush_get_option('sites_path') .'/'. $url .'/database.sql')
->succeed('Removed dump file @path after restoring from it')
->fail('Could not remove dump file @path', 'DRUSH_PERM_ERROR');
}
......@@ -30,9 +30,9 @@ function drush_db_pre_provision_restore_rollback($url = NULL) {
// 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($url = NULL) {
provision_service('file')->path('unlink', drush_get_option('sites_path') . '/' . $url .'/database.sql', TRUE,
dt("Removed dump file @path after restoring from it"),
dt("Could not remove dump file @path"), 'DRUSH_PERM_ERROR');
provision_service('file')->unlink(drush_get_option('sites_path') . '/' . $url . '/database.sql')
->succeed('Removed dump file @path after restoring from it')
->fail('Could not remove dump file @path', 'DRUSH_PERM_ERROR');
provision_service('db')->destroy_site_database($url);
......
This diff is collapsed.
......@@ -26,10 +26,11 @@ function drush_provision_drupal_provision_backup_validate($url = NULL, $backup_f
}
if ($backup_file) {
if (provision_service('file')->path("exists", $backup_file, FALSE,
dt("Backing site up to @path."),
dt("Back up file @path already exists."),
'PROVISION_BACKUP_ALREADY_EXISTS')) {
if (provision_service('file')->exists($backup_file)->status()) {
drush_set_error('PROVISION_BACKUP_ALREADY_EXISTS', dt('Back up file @path already exists.', array('@path' => $backup_file)));
}
else {
drush_log(dt('Backing site up to @path.', array('@path' => $backup_file)), 'message');
drush_set_option('backup_file', $backup_file);
}
}
......@@ -77,7 +78,8 @@ function drush_provision_drupal_provision_backup($url) {
function drush_provision_drupal_provision_backup_rollback() {
$backup_file = drush_get_option('backup_file');
if (file_exists($backup_file)) {
provision_service('file')->path('unlink', $backup_file, TRUE,
dt("Removed stale backup file $backup_file"), dt("Failed deleting backup file $backup_file"));
provision_service('file')->unlink($backup_file)
->succeed('Removed stale backup file @path')
->fail('Failed deleting backup file @path');
}
}
......@@ -27,7 +27,9 @@ function drush_provision_drupal_pre_provision_clone($url, $new_url, $platform =
* Remove the extracted site directory
*/
function drush_provision_drupal_pre_provision_clone_rollback($url, $new_url, $platform = null) {
$success = provision_service('file')->path("unlink", drush_get_option('backup_file'), TRUE, dt('Removed unused clone site package'), dt('Could not remove unused clone site package'));
$success = provision_service('file')->unlink(drush_get_option('backup_file'))
->succeed('Removed unused clone site package')
->fail('Could not remove unused clone site package');
}
/**
......
......@@ -21,10 +21,10 @@
function drush_provision_drupal_provision_deploy_validate($url = null, $backup_file = null) {
_provision_drupal_url_required();
$exists = provision_service('file')->path("exists", $backup_file, TRUE,
dt("Deploying site from @path"),
dt("Could not find backup file @path"),
'PROVISION_BACKUP_NOT_FOUND');
$exists = provision_service('file')->exists($backup_file)
->succeed('Deploying site from @path')
->fail('Could not find backup file @path', 'PROVISION_BACKUP_NOT_FOUND')
->status();
if ($exists) {
drush_set_option('backup_file', $backup_file);
}
......@@ -36,10 +36,10 @@ function drush_provision_drupal_provision_deploy_validate($url = null, $backup_f
function drush_provision_drupal_pre_provision_deploy($url, $backup_file) {
// the url is likely to have changed in the deployment
drush_set_option('site_url', $url);
$extracted = provision_service('file')->path("extract", drush_get_option('backup_file'), drush_get_option('sites_path') ."/$url",
dt('Successfully extracted the contents of @path'),
dt('Failed to extract the contents of @path'),
'PROVISION_BACKUP_EXTRACTION_FAILED');
$extracted = provision_service('file')->extract(drush_get_option('backup_file'), drush_get_option('sites_path') . '/' . $url)
->succeed('Successfully extracted the contents of @path')
->fail('Failed to extract the contents of @path', 'PROVISION_BACKUP_EXTRACTION_FAILED')
->status();
if ($extracted) {
drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
......
......@@ -69,7 +69,8 @@ function drush_provision_drupal_provision_install_rollback($url) {
function drush_provision_drupal_post_provision_install($url) {
drush_set_option('aliases', drush_get_option('aliases'), 'site');
drush_set_option('installed', TRUE, 'site');
provision_service('file')->path("chmod", "./sites/$url/settings.php", 0440, dt("Secured settings.php with safe permissions"));
provision_service('file')->chmod('./sites/' . $url . '/settings.php', 0440)
->succeed('Secured settings.php with safe permissions');
_provision_drupal_rebuild_caches($url);
drush_set_option('packages', _scrub_object(provision_drupal_system_map()), 'site');
}
......
......@@ -49,7 +49,9 @@ function drush_provision_drupal_pre_provision_migrate_rollback($url, $platform =
drush_log(dt("Bringing site out of maintenance"));
drush_set_option('site_offline', 0);
_provision_drupal_create_settings_file($url);
$success = provision_service('file')->path("unlink", drush_get_option('backup_file'), TRUE, dt('Removed unused migration site package'), dt('Could not remove unused migration site package'));
$success = provision_service('file')->unlink(drush_get_option('backup_file'))
->succeed('Removed unused migration site package')
->fail('Could not remove unused migration site package');
_provision_apache_create_vhost_config($url);
_provision_apache_restart_apache();
}
......
......@@ -194,10 +194,10 @@ function _provision_drupal_create_settings_file($url = NULL) {
$options['extra_config'] .= join("\n", drush_command_invoke_all('provision_drupal_config', $url, $options));
drush_log(dt("Generate settings.php file"));
if (provision_service('file')->path("exists", "sites/$url/settings.php")) {
provision_service('file')->path("chmod", "sites/$url/settings.php", 0640,
dt('Changed permissions of settings.php to @confirm'),
dt('Could not change permissions of settings.php to @confirm'));
if (provision_service('file')->exists('sites/' . $url . '/settings.php')->status()) {
provision_service('file')->chmod('sites/' . $url . '/settings.php', 0640)
->succeed('Changed permissions of settings.php to @perm')
->fail('Could not change permissions of settings.php to @perm');
}
$fp = provision_service('file')->path('fopen', 'sites/' . $url . '/settings.php', 'w');
......@@ -206,13 +206,13 @@ function _provision_drupal_create_settings_file($url = NULL) {
fclose($fp);
# Change the permissions of the file
provision_service('file')->path("chmod", "sites/$url/settings.php", 0440,
dt('Changed permissions of settings.php to @confirm'),
dt('Could not change permissions of settings.php to @confirm'));
provision_service('file')->chmod('sites/' . $url . '/settings.php', 0440)
->succeed('Changed permissions of settings.php to @perm')
->fail('Could not change permissions of settings.php to @perm');
provision_service('file')->path("chgrp", "sites/$url/settings.php", drush_get_option('web_group'),
dt('Change group ownership of settings.php to @confirm'),
dt('Could not change group ownership of settings.php to @confirm'));
provision_service('file')->chgrp('sites/' . $url . '/settings.php', drush_get_option('web_group'))
->succeed('Change group ownership of settings.php to @gid')
->fail('Could not change group ownership of settings.php to @gid');
}
/**
......@@ -251,27 +251,24 @@ function _provision_drupal_create_directories($url, $profile = NULL) {
foreach ($mkdir as $path => $perm) {
if (!is_dir($path)) {
provision_service('file')->path("mkdir", $path, TRUE,
dt("Created <code>@path</code>"),
dt("Could not create <code>@path</code>"),
'DRUSH_PERM_ERROR');
provision_service('file')->mkdir($path)
->succeed('Created <code>@path</code>')
->fail('Could not create <code>@path</code>', 'DRUSH_PERM_ERROR');
}
provision_service('file')->path("chmod_recursive", $path, $perm,
dt("Changed permissions of <code>@path</code> to @confirm"),
dt("Could not change permissions <code>@path</code> to @confirm")
);
provision_service('file')->chmod($path, $perm, TRUE)
->succeed('Changed permissions of <code>@path</code> to @perm')
->fail('Could not change permissions <code>@path</code> to @perm');
}
foreach ($chown as $path => $owner) {
provision_service('file')->path("chown_recursive", $path, $owner,
dt("Changed ownership of <code>@path</code>"),
dt("Could not change ownership <code>@path</code>"),
'DRUSH_PERM_ERROR' );
provision_service('file')->chown($path, $owner, TRUE)
->succeed('Changed ownership of <code>@path</code>')
->fail('Could not change ownership <code>@path</code>', 'DRUSH_PERM_ERROR');
}
foreach ($chgrp as $path => $group) {
provision_service('file')->path("chgrp_recursive", $path, $group,
dt("Changed group ownership of <code>@path</code>"),
dt("Could not change group ownership <code>@path</code>"));
provision_service('file')->chgrp($path, $group, TRUE)
->succeed('Changed group ownership of <code>@path</code>')
->fail('Could not change group ownership <code>@path</code>');
}
}
......@@ -391,9 +388,9 @@ function _provision_drupal_maintain_aliases($url) {
}
foreach($aliases as $alias) {
if (trim($alias)) {
provision_service('file')->path("symlink", $url, drush_get_option('docroot_path') . "/sites/" . $alias,
dt("Created symlink for alias @alias", array("@alias" => $alias)),
dt("Could not create symlink for alias @alias", array("@alias" => $alias)));
provision_service('file')->symlink($url, drush_get_option('docroot_path') . '/sites/' . $alias)
->succeed('Created symlink for alias @target')
->fail('Could not create symlink for alias @target');
}
}
}
......@@ -409,13 +406,15 @@ function _provision_drupal_delete_aliases($aliases, $silent = false) {
}
if ($silent) {
$error = NULL;
} else {
$error = dt("Could not remove symlink for alias @alias", array("@alias" => $alias));
}
else {
$error = 'Could not remove symlink for alias @path';
}
foreach ($aliases as $alias) {
if ($alias = trim($alias)) {
provision_service('file')->path("unlink", drush_get_option('docroot_path') . "/sites/" . $alias, TRUE,
dt("Removed symlink for alias @alias", array("@alias" => $alias)), $error);
provision_service('file')->unlink(drush_get_option('docroot_path') . '/sites/' . $alias)
->succeed('Removed symlink for alias @path')
->fail($error);
}
}
}
......
......@@ -26,10 +26,10 @@
function drush_provision_drupal_provision_restore_validate($url = null, $restore_file = null) {
drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
$exists = provision_service('file')->path("exists", $restore_file, TRUE,
dt("Restoring site from @path"),
dt("Could not find backup file @path"),
'PROVISION_BACKUP_NOT_FOUND');
$exists = provision_service('file')->exists($restore_file)
->succeed('Restoring site from @path')
->fail('Could not find backup file @path', 'PROVISION_BACKUP_NOT_FOUND')
->status();
if ($exists) {
drush_set_option('restore_file', $restore_file);
}
......@@ -41,10 +41,9 @@ function drush_provision_drupal_provision_restore_validate($url = null, $restore
function drush_provision_drupal_pre_provision_restore($url, $restore_file) {
drush_invoke('provision-backup', $url);
provision_service('file')->path("extract", drush_get_option('restore_file'), drush_get_option('sites_path') ."/$url.restore",
dt('Successfully extracted the contents of @path'),
dt('Failed to extract the contents of @path'),
'PROVISION_BACKUP_EXTRACTION_FAILED');
provision_service('file')->extract(drush_get_option('restore_file'), drush_get_option('sites_path') . '/' . $url . '.restore')
->succeed('Successfully extracted the contents of @path')
->fail('Failed to extract the contents of @path', 'PROVISION_BACKUP_EXTRACTION_FAILED');
}
/**
......@@ -58,12 +57,11 @@ function drush_provision_drupal_pre_provision_restore_rollback($url) {
* Switch the restore directories around now that we have the new db installed
*/
function drush_provision_drupal_provision_restore($url) {
$old = drush_get_option('sites_path') ."/$url.restore";
$new = drush_get_option('sites_path') ."/$url";
provision_service('file')->path("switch_paths", $old, $new ,
dt('Swapping out the @path and @confirm directories was successful.'),
dt('Swapping the @path and @confirm directories has failed.'),
'DRUSH_PERM_ERROR');
$old = drush_get_option('sites_path') . '/' . $url . '.restore';
$new = drush_get_option('sites_path') . '/' . $url;
provision_service('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($url);
provision_save_site_data($url);
......
......@@ -27,7 +27,7 @@ function drush_provision_drupal_provision_verify($url = null) {
if (PROVISION_CONTEXT_PLATFORM) {
provision_service('file')->create_dir(drush_get_option('config_path'), dt('Provision configuration'), 0711);
provision_service('file')->create_dir(drush_get_option('config_path') . '/includes', dt('Provision PHP configuration'), 0711);
if (!provision_service('file')->path('exists', drush_get_option('config_path') . '/includes/global.inc', TRUE, dt("Global configuration file exists"))) {
if (!provision_service('file')->exists(drush_get_option('config_path') . '/includes/global.inc')->succeed('Global configuration file exists')->status()) {
# create an empty global.inc so the include doesn't fail with
# open_basedir restrictions
if (!$file = provision_service('file')->path('fopen', drush_get_option('config_path') . '/includes/global.inc', 'a')) {
......@@ -38,8 +38,9 @@ function drush_provision_drupal_provision_verify($url = null) {
}
}
provision_service('file')->create_dir(drush_get_option('backup_path'), dt('Backup'), 0700);
provision_service('file')->path("writable", drush_get_option('sites_path'), TRUE, dt("Drupal sites directory is writable by the provisioning script"),
dt("Drupal sites directory is not writable by the provisioning script"), 'PROVISION_SITES_DIR_NOT_WRITABLE');
provision_service('file')->writable(drush_get_option('sites_path'))
->succeed('Drupal sites directory @path is writable by the provisioning script')
->fail('Drupal sites directory @path is not writable by the provisioning script', 'PROVISION_SITES_DIR_NOT_WRITABLE');
drush_set_option('sites', array_keys((array) provision_drupal_find_sites()), 'drupal');
drush_log(dt("This platform is running @short_name @version", array('@short_name' => 'drupal', '@version' => VERSION)));
......
......@@ -201,20 +201,20 @@ function drush_provision_hostmaster_make($platform) {
* Generate a provision.settings.php file to configure provision
*/
function _provision_generate_config() {
$exists = provision_service('file')->path('exists', drush_get_option('docroot_path') . '/drushrc.php');
$exists = provision_service('file')->exists(drush_get_option('docroot_path') . '/drushrc.php')->status();
if ($exists) {
drush_log(dt("Found existing drushrc.php file"));
provision_service('file')->path("chmod", drush_get_option('docroot_path') . '/drushrc.php', 0600,
dt('Changed permissions of drushrc.php to @confirm'),
dt('Could not change permissions of drushrc.php to @confirm'));
provision_service('file')->chmod(drush_get_option('docroot_path') . '/drushrc.php', 0600)
->succeed('Changed permissions of drushrc.php to @perm')
->fail('Could not change permissions of drushrc.php to @perm');
}
else {
drush_log(dt("Generating drushrc.php file"));
}
provision_save_platform_data();
provision_service('file')->path("chmod", drush_get_option('docroot_path') . '/drushrc.php', 0400,
dt('Changed permissions of drushrc.php to @confirm'),
dt('Could not change permissions of drushrc.php to @confirm'));
provision_service('file')->chmod(drush_get_option('docroot_path') . '/drushrc.php', 0400)
->succeed('Changed permissions of drushrc.php to @perm')
->fail('Could not change permissions of drushrc.php to @perm');
return TRUE;
}
......
......@@ -64,7 +64,7 @@ function provision_load_site_data($url) {
$conf_file = "sites/$url/site.php";
if (file_exists($conf_file)) {
provision_service('file')->path('chmod', $conf_file, 0400);
provision_service('file')->chmod($conf_file, 0400);
require($conf_file);
// The provision 0.1 config data might contain older data,
......@@ -81,6 +81,13 @@ function provision_load_site_data($url) {
*/
function provision_save_site_data() {
$context = 'site';
drush_set_option('context-path', drush_get_option('context-path', null, 'site'));
drush_unset_option('context-path', 'site');
drush_set_option('config-file', drush_get_option('config-file', null, 'site'));
drush_unset_option('config-file', 'site');
drush_save_config($context);
$filename = _drush_config_file($context);
......@@ -99,9 +106,9 @@ function provision_save_site_data() {
fclose($fp);
}
provision_service('file')->path("chmod", $filename, 0600,
dt('Changed permissions of drushrc.php to @confirm'),
dt('Could not change permissions of drushrc.php to @confirm'));
provision_service('file')->chmod($filename, 0600)
->succeed('Changed permissions of drushrc.php to @perm')
->fail('Could not change permissions of drushrc.php to @perm');
}
/**
......@@ -117,6 +124,13 @@ function provision_save_site_data() {
* Boolean. The value returned by drush_save_config().
*/
function provision_save_platform_data() {
drush_set_option('context-path', drush_get_option('context-path', null, 'drupal'));
drush_unset_option('context-path', 'drupal');
drush_set_option('config-file', drush_get_option('config-file', null, 'drupal'));
drush_unset_option('config-file', 'drupal');
return drush_save_config('drupal');
}
......@@ -180,15 +194,17 @@ function _provision_recursive_delete($path) {
}
}
$rm = provision_service('file')->path('rmdir', $path, TRUE,
dt('Deleting @path directory successful.', array('@path' => $path)),
dt('Deleting @path directory failed.', array('@path' => $path)));
$rm = provision_service('file')->rmdir($path)
->succeed('Deleting @path directory successful.')
->fail('Deleting @path directory failed.')
->status();
$ret = $ret && $rm;
}
else {
$rm = provision_service('file')->path('unlink', $path, TRUE, NULL,
dt('Deleting @path file failed.', array('@path' => $path)));
$rm = provision_service('file')->unlink($path)
->fail('Deleting @path file failed.')
->status();
$ret = $ret && $rm;
}
return $ret;
......
<?php
class provisionService {
protected $last_status;
protected $tokens;
function __construct() {
$this->init();
}
......@@ -13,6 +16,70 @@ class provisionService {
return TRUE;
}
/**
* Clear internal state
*/
protected function _clear_state() {
$this->last_status = NULL;
$this->tokens = NULL;
}
/**
* Return the status of the last operation.
*
* @return
* TRUE or FALSE for success or failure; NULL if there was not a previous
* operation.
*/
function status() {
return $this->last_status;
}
/**
* Log a notice into the logging system, if the last operation completed
* succesfully.
*
* @param $message
* The message to log, a string.
*/
function succeed($message) {
if ($this->last_status === TRUE) {
drush_log(dt($message, $this->tokens), 'message');
}
return $this;
}
/**
* Log a notice into the logging system, if the last operation did not
* complete succesfully.
*
* @param $message
* Log this as a error to the logging system, if the $error_codes parameter
* has been set, otherwise, log this as a warning. If the operation
* specifies an additional reason for the operation failing, it will be
* appended to this message.
*
* @param error_codes
* Generate these system level errors using the provision error bitmasks.
*/
function fail($message, $error_codes = NULL) {
if (!empty($this->tokens['@reason'])) {
$message .= ' (@reason)';
}
if ($this->last_status === FALSE) {
if (is_null($error_codes)) {
// Trigger a warning
drush_log(dt($message, $this->tokens), 'warning');
}
else {
// Trigger a sysem halting error
drush_set_error($error_codes, dt($message, $this->tokens));
}
}
return $this;
}
}
class provisionConfig {
......@@ -86,9 +153,8 @@ class provisionConfig {
}
function unlink() {
return provision_service('file')->path('unlink', $this->filename());
return provision_service('file')->unlink($this->filename())->status();
}
}
......
......@@ -24,7 +24,9 @@ function provision_ssl_provision_apache_vhost_config($url, $options) {
if ($options['ssl_redirect']) {
// That's pretty bad, but if we *don't* do that, the vhost is never updated after the first write
// XXX: we need a better way to identify if this is legit
provision_service('file')->path('exists', drush_get_option('vhost_path') . '/' . $url . '_80', FALSE, NULL, dt("Overwriting existing vhost %url:80 with redirection", array('%url' => $url)));
if (provision_service('file')->exists(drush_get_option('vhost_path') . '/' . $url . '_80')->status()) {
drush_log(dt('Overwriting existing vhost %url:80 with redirection', array('%url' => $url)), 'warning');
}
$newoptions = $options;
// in the redirection template, the ServerName is the first alias in the list
array_push($newoptions['aliases'], $options['site_url']);
......@@ -44,6 +46,7 @@ function provision_ssl_provision_apache_vhost_config($url, $options) {
*/
function provision_ssl_provision_apache_delete_vhost($url, $options) {
if ($options['ssl'] && $options['ssl_redirect']) {
provision_service('file')->path('unlink', drush_get_option('vhost_path') . '/' . $url . '_80', TRUE, NULL, dt("Failed deleting redirection vhost."));
provision_service('file')->unlink(drush_get_option('vhost_path') . '/' . $url . '_80')
->succeed('Failed deleting redirection vhost.');
}
}
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