drush-ops / drush

Drush is a command-line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those who spend their working hours hacking away at the command prompt.
https://www.drush.org
2.33k stars 1.08k forks source link

drush_policy_sql_sync_validate not being picked up by drush #2406

Open tassoskoutlas opened 7 years ago

tassoskoutlas commented 7 years ago

Hi,

As per instructions I have the following snippet ~/.drush/policy.drush.inc file:

/**
 * Implements drush_hook_COMMAND_validate().
 *
 * Prevent catastrophic braino. Note that this file has to be local to the
 * machine that intitiates sql-sync command.
 */
function drush_policy_sql_sync_validate($source = NULL, $destination = NULL) {
  return drush_set_error('POLICY_DENY', dt('You may never overwrite the production database.'));
}

When I am issuing a command like drush sql-sync @self @some-remote-alias the code never executes. I am using the policy file automatically generated by drupal-composer. It includes pm-* commands with deny messages that execute normally.

Am I missing something?

tassoskoutlas commented 7 years ago

Local version: 8.1.6 Remote version: 8.1.5

weitzman commented 7 years ago

Try running with the --show-invoke global option. That will show what functions are being called.

tassoskoutlas commented 7 years ago

@weitzman this is very weird. When I run sql-sync with --show-invoke it goes straight into the Do you really want to continue? (y/n) question.

drush8 sql-sync @self @prod --show-invoke
You will destroy data in URL and replace with data from project.

Do you really want to continue? (y/n): 

I've also tried with pm-updatecode which came back with a list like the following:

This codebase is assembled with Composer instead of Drush. Use `composer update` and `composer require` instead of `drush pm-updatecode` and `drush pm-download`. You may override this error by using the --pm-force option.

Available drush_invoke() hooks for pm-updatecode:

drush_pm_updatecode_pre_validate
drush_acapi_pm_updatecode_pre_validate
drush_archive_pm_updatecode_pre_validate
drush_cache_pm_updatecode_pre_validate
drush_cli_pm_updatecode_pre_validate
drush_config_pm_updatecode_pre_validate
drush_core_pm_updatecode_pre_validate
drush_docs_pm_updatecode_pre_validate
drush_druplicon_pm_updatecode_pre_validate
drush_field_pm_updatecode_pre_validate
drush_help_pm_updatecode_pre_validate
drush_helpsingle_pm_updatecode_pre_validate
drush_image_pm_updatecode_pre_validate
drush_init_pm_updatecode_pre_validate
drush_locale_pm_updatecode_pre_validate
drush_make_pm_updatecode_pre_validate
drush_notify_pm_updatecode_pre_validate
drush_outputformat_pm_updatecode_pre_validate
drush_policy_pm_updatecode_pre_validate
drush_queue_pm_updatecode_pre_validate
drush_registry_rebuild_pm_updatecode_pre_validate
drush_role_pm_updatecode_pre_validate
drush_runserver_pm_updatecode_pre_validate
drush_search_pm_updatecode_pre_validate
drush_shellalias_pm_updatecode_pre_validate
drush_site_install_pm_updatecode_pre_validate
drush_sitealias_pm_updatecode_pre_validate
drush_sql_pm_updatecode_pre_validate
drush_sqlsync_pm_updatecode_pre_validate
drush_ssh_pm_updatecode_pre_validate
drush_state_pm_updatecode_pre_validate
drush_topic_pm_updatecode_pre_validate
drush_usage_pm_updatecode_pre_validate
drush_user_pm_updatecode_pre_validate
drush_variable_pm_updatecode_pre_validate
drush_views_pm_updatecode_pre_validate
drush_watchdog_pm_updatecode_pre_validate
drush_xh_pm_updatecode_pre_validate
drush_pm_updatecode_validate [* Defined in /Users/XXX/work/PROJECT/website/vendor/drush/drush/commands/pm/pm.drush.inc]
drush_acapi_pm_updatecode_validate
drush_archive_pm_updatecode_validate
drush_cache_pm_updatecode_validate
drush_cli_pm_updatecode_validate
drush_config_pm_updatecode_validate
drush_core_pm_updatecode_validate
drush_docs_pm_updatecode_validate
drush_druplicon_pm_updatecode_validate
drush_field_pm_updatecode_validate
drush_help_pm_updatecode_validate
drush_helpsingle_pm_updatecode_validate
drush_image_pm_updatecode_validate
drush_init_pm_updatecode_validate
drush_locale_pm_updatecode_validate
drush_make_pm_updatecode_validate
drush_notify_pm_updatecode_validate
drush_outputformat_pm_updatecode_validate
drush_policy_pm_updatecode_validate [* Defined in /Users/XXX/.drush/policy.drush.inc]

Available rollback hooks for pm-updatecode:                                                                                                  

drush_pm_updatecode_validate_rollback
drush_policy_pm_updatecode_validate_rollback
tassoskoutlas commented 7 years ago

@weitzman is this in any way expected behaviour?

frederickjh commented 7 years ago

@tassoskoutlas Just a thought here, your policy function does not include an if statement to select when the policy should be applied. All the example policies do have an if statement that selects when it should be applied.

weitzman commented 7 years ago

Make sure you are running your command from the /web dir or something below it. Or you can pass the --root option (or use a site alias). If you dont do those things then the /drush folder is not read and policy.drush.inc is not found.

weitzman commented 7 years ago

Oh wait, you put it in ~/.drush. That should work, unless --local is specified in a drush.wrapper file, or in a site alias.

bkosborne commented 7 years ago

I thought I was having this problem as well because I was getting the "are you sure" prompt for the command even though I had a validate hook that prevented it. Turns out that Drush implements the validate hook to create that prompt and the drush implementation is invoked BEFORE mine. So once I select y/n to the prompt, then my validation hook runs as expected.

greg-1-anderson commented 7 years ago

You could try using the pre-validate hook if you wanted to come in prior to the prompt.