Skip to content

Instantly share code, notes, and snippets.

@gapple
Last active December 18, 2017 21:20

Revisions

  1. gapple revised this gist Dec 18, 2017. No changes.
  2. gapple revised this gist Dec 1, 2016. 1 changed file with 52 additions and 11 deletions.
    63 changes: 52 additions & 11 deletions file_sync.drush.inc
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,13 @@
    <?php

    /**
    * @file
    * Provide a file-sync command to sync files directories between environments.
    */

    /**
    * Implements hook_drush_command().
    */
    function file_sync_drush_command() {
    $commands = array();

    @@ -8,7 +16,7 @@ function file_sync_drush_command() {
    'aliases' => array('fsync'),
    'arguments' => array(
    'source' => 'Source alias',
    'destination' => 'Destination alias',
    'destination' => 'Destination alias', // TODO make this optional and default to @self
    ),
    'options' => array(
    'only-public' => 'Only sync the public files directory',
    @@ -25,6 +33,23 @@ function file_sync_drush_command() {
    return $commands;
    }

    /**
    * Implements hook_drush_help_alter().
    */
    function file_sync_drush_help_alter(&$command) {
    $commands = drush_get_commands();

    // Append all of core-rsync's options to file-sync.
    // This is needed to prevent drush_core_rsync() from sending the additional
    // options for core-rsync through to rsync as well.
    if ($command['command'] === 'file-sync' && isset($commands['core-rsync'])) {
    $command['options'] += $commands['core-rsync']['options'];
    }
    }

    /**
    * File sync command callback.
    */
    function drush_file_sync($source, $destination) {

    $only_public = drush_get_option('only-public', FALSE);
    @@ -35,28 +60,34 @@ function drush_file_sync($source, $destination) {
    return false;
    }

    // drush_core_rsync() loads all of the arguments and options originally
    // passed to drush, so using drush_unset_option('private') is ineffective and
    // we need to hack the context instead.
    // drush_unset_option() is needed to prevent the options for file-sync getting
    // passed through to core-rsync.
    // core-rync loads the original command line options to pass to rsync, so some
    // additional context-hacking is needed.
    // see drush_core_rsync() and drush_get_original_cli_args_and_options().
    $args = &drush_get_context('DRUSH_COMMAND_ARGS', array());
    $original_args = $args;

    // TODO load all options from command definition.
    foreach (array('only-public', 'only-private') as $option) {
    $command = drush_get_command();
    foreach (array_keys($command['options']) as $option) {
    drush_unset_option($option, 'cli');

    $arg_index = array_search('--' . $option, $args);
    if ($arg_index !== FALSE) {
    unset($args[$arg_index]);
    }
    }
    $args = array_values($args);

    $public_result = FALSE;
    if (!$only_private) {
    drush_log(dt('Syncing public files'), 'ok');
    drush_log(dt('Syncing public files'), 'status');
    $public_result = _drush_file_sync_path_alias($source, $destination, '%files');
    }

    $private_result = FALSE;
    if (!$only_public) {
    drush_log(dt('Syncing private files'), 'ok');
    drush_log(dt('Syncing private files'), 'status');
    $private_result = _drush_file_sync_path_alias($source, $destination, '%private', $only_private? 'error': 'warning');
    }

    @@ -68,17 +99,25 @@ function drush_file_sync($source, $destination) {
    ||
    ($only_private && !$private_result)
    ) {
    // If public transfer is attempted but fails, it's always an error.
    // If private transfer is attempted but fails, it's an error if only
    // transferring private files was attempted.
    drush_log(dt('Sync could not complete successfully'), 'error');
    }
    else if (!$only_public && !$only_private && !$private_result){
    else if (!$only_public && !$only_private && !$private_result) {
    // A private file path may not be defined, so it's only a warning if the
    // private transfer could not be completed while attempting to transfer all
    // files.
    drush_log(dt('Sync partially completed'), 'warning');
    }
    else {
    drush_log(dt('Sync completed successfully'), 'ok');
    drush_log(dt('Sync completed successfully'), 'success');
    }
    }

    /**
    * Helper function to sync a single path between two environments.
    *
    * @param $source
    * @param $destination
    * @param $path_alias
    @@ -107,5 +146,7 @@ function _drush_file_sync_path_alias($source, $destination, $path_alias, $error_

    drush_invoke('rsync', array($source_path, $destination_path));

    return TRUE;
    // drush_command() doesn't pass along the return value from rsync for
    // drush_invoke() to make use of, so it's necessary to check the error log.
    return drush_get_error() === DRUSH_SUCCESS;
    }
  3. gapple revised this gist Dec 4, 2014. 1 changed file with 78 additions and 15 deletions.
    93 changes: 78 additions & 15 deletions file_sync.drush.inc
    Original file line number Diff line number Diff line change
    @@ -4,20 +4,21 @@ function file_sync_drush_command() {
    $commands = array();

    $commands['file-sync'] = array(
    'description' => 'Sync files directory between sites',
    'description' => 'Sync files directories between sites',
    'aliases' => array('fsync'),
    'arguments' => array(
    'source' => 'Source alias',
    'destination' => 'Destination alias',
    ),
    'options' => array(
    'private' => 'Sync private files',
    'only-public' => 'Only sync the public files directory',
    'only-private' => 'Only sync the private files directory',
    ),
    'required-arguments' => TRUE,
    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
    'examples' => array(
    'drush fsync @source @destination' => 'Sync public files directory from source to destination alias',
    'drush fsync --private @source @destination' => 'Sync private files directory from source to destination alias',
    'drush fsync @source @destination' => 'Sync both the public and private files directories',
    'drush fsync --only-public @source @destination' => 'Only sync the public files directory',
    ),
    );

    @@ -26,23 +27,85 @@ function file_sync_drush_command() {

    function drush_file_sync($source, $destination) {

    $additional_options = array();
    $only_public = drush_get_option('only-public', FALSE);
    $only_private = drush_get_option('only-private', FALSE);

    if ($only_public && $only_private) {
    drush_log(dt('Only one of --only-public or --only-private can be set'), 'error');
    return false;
    }

    // drush_core_rsync() loads all of the arguments and options originally
    // passed to drush, so using drush_unset_option('private') is ineffective and
    // we need to hack the context instead.
    $args = &drush_get_context('DRUSH_COMMAND_ARGS', array());
    $original_args = $args;

    // TODO load all options from command definition.
    foreach (array('only-public', 'only-private') as $option) {
    $arg_index = array_search('--' . $option, $args);
    if ($arg_index !== FALSE) {
    unset($args[$arg_index]);
    }
    }
    $args = array_values($args);

    if (!$only_private) {
    drush_log(dt('Syncing public files'), 'ok');
    $public_result = _drush_file_sync_path_alias($source, $destination, '%files');
    }

    if (!$only_public) {
    drush_log(dt('Syncing private files'), 'ok');
    $private_result = _drush_file_sync_path_alias($source, $destination, '%private', $only_private? 'error': 'warning');
    }

    $path_alias = drush_get_option('private', FALSE)? '%private' : '%files';
    // Restore the original arguments just to be safe.
    $args = $original_args;

    // Check that the path aliases can be resolved properly before starting the transfer.
    $source_alias_info = drush_sitealias_evaluate_path($source . ':'. $path_alias, $additional_options);
    if (
    (!$only_private && !$public_result)
    ||
    ($only_private && !$private_result)
    ) {
    drush_log(dt('Sync could not complete successfully'), 'error');
    }
    else if (!$only_public && !$only_private && !$private_result){
    drush_log(dt('Sync partially completed'), 'warning');
    }
    else {
    drush_log(dt('Sync completed successfully'), 'ok');
    }
    }

    /**
    * @param $source
    * @param $destination
    * @param $path_alias
    * @param $error_level
    * If sync fails, what error level should be used.
    * @return bool
    */
    function _drush_file_sync_path_alias($source, $destination, $path_alias, $error_level = 'error') {

    $additional_options = array();

    // Check that the path alias can be resolved properly before starting the transfer.
    $source_path = $source . ':'. $path_alias;
    $source_alias_info = drush_sitealias_evaluate_path($source_path, $additional_options);
    if (empty($source_alias_info['path-aliases'][$path_alias])) {
    return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate source path !path.', array('!path' => $source)));
    drush_log(dt('Could not evaluate source path !path.', array('!path' => $source_path)), $error_level);
    return FALSE;
    }

    $destination_alias_info = drush_sitealias_evaluate_path($destination . ':' . $path_alias, $additional_options);
    $destination_path = $destination . ':' . $path_alias;
    $destination_alias_info = drush_sitealias_evaluate_path($destination_path, $additional_options);
    if (empty($destination_alias_info['path-aliases'][$path_alias])) {
    return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate destination path !path.', array('!path' => $destination)));
    drush_log(dt('Could not evaluate destination path !path.', array('!path' => $destination_path)), $error_level);
    return FALSE;
    }

    drush_log('Starting rsync', 'ok');
    drush_unset_option('private');
    drush_invoke('rsync', array($source . ':' . $path_alias, $destination . ':' . $path_alias));
    drush_log('Sync complete', 'ok');
    drush_invoke('rsync', array($source_path, $destination_path));

    return TRUE;
    }
  4. gapple renamed this gist Dec 3, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. gapple created this gist Dec 3, 2014.
    48 changes: 48 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?php

    function file_sync_drush_command() {
    $commands = array();

    $commands['file-sync'] = array(
    'description' => 'Sync files directory between sites',
    'aliases' => array('fsync'),
    'arguments' => array(
    'source' => 'Source alias',
    'destination' => 'Destination alias',
    ),
    'options' => array(
    'private' => 'Sync private files',
    ),
    'required-arguments' => TRUE,
    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
    'examples' => array(
    'drush fsync @source @destination' => 'Sync public files directory from source to destination alias',
    'drush fsync --private @source @destination' => 'Sync private files directory from source to destination alias',
    ),
    );

    return $commands;
    }

    function drush_file_sync($source, $destination) {

    $additional_options = array();

    $path_alias = drush_get_option('private', FALSE)? '%private' : '%files';

    // Check that the path aliases can be resolved properly before starting the transfer.
    $source_alias_info = drush_sitealias_evaluate_path($source . ':'. $path_alias, $additional_options);
    if (empty($source_alias_info['path-aliases'][$path_alias])) {
    return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate source path !path.', array('!path' => $source)));
    }

    $destination_alias_info = drush_sitealias_evaluate_path($destination . ':' . $path_alias, $additional_options);
    if (empty($destination_alias_info['path-aliases'][$path_alias])) {
    return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate destination path !path.', array('!path' => $destination)));
    }

    drush_log('Starting rsync', 'ok');
    drush_unset_option('private');
    drush_invoke('rsync', array($source . ':' . $path_alias, $destination . ':' . $path_alias));
    drush_log('Sync complete', 'ok');
    }