policy.drush.inc

  1. 8.0.x examples/policy.drush.inc
  2. 6.x examples/policy.drush.inc
  3. 7.x examples/policy.drush.inc
  4. 4.x examples/policy.drush.inc
  5. 5.x examples/policy.drush.inc
  6. master examples/policy.drush.inc

Example policy commandfile. Modify as desired.

Validates commands as they are issued and returns an error or changes options when policy is violated.

You can copy this file to any of the following 1. A .drush folder in your HOME folder. 2. Anywhere in a folder tree below an active module on your site. 3. /usr/share/drush/commands (configurable) 4. In an arbitrary folder specified with the --include option. 5. Drupal's sites/all/drush folder (note: sql-sync validation won't work here).

Functions

File

examples/policy.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Example policy commandfile. Modify as desired.
  5. *
  6. * Validates commands as they are issued and returns an error
  7. * or changes options when policy is violated.
  8. *
  9. * You can copy this file to any of the following
  10. * 1. A .drush folder in your HOME folder.
  11. * 2. Anywhere in a folder tree below an active module on your site.
  12. * 3. /usr/share/drush/commands (configurable)
  13. * 4. In an arbitrary folder specified with the --include option.
  14. * 5. Drupal's sites/all/drush folder (note: sql-sync validation won't work here).
  15. */
  16. /**
  17. * Implement of drush_hook_COMMAND_validate().
  18. *
  19. * Prevent catastrophic braino. Note that this file has to be local to the machine
  20. * that intitiates sql-sync command.
  21. */
  22. function drush_policy_sql_sync_validate($source = NULL, $destination = NULL) {
  23. if ($destination == '@prod') {
  24. return drush_set_error(dt('Per examples/policy.drush.inc, you may never overwrite the production database.'));
  25. }
  26. }
  27. /**
  28. * Implement hook help alter.
  29. *
  30. * When a hook extends a command with additional options, it must
  31. * implement help alter and declare the option(s). Doing so will add
  32. * the option to the help text for the modified command, and will also
  33. * allow the new option to be specified on the command line. Without
  34. * this, Drush will fail with an error when a user attempts to use
  35. * the option.
  36. */
  37. function policy_drush_help_alter($command) {
  38. if ($command['command'] == 'updatedb') {
  39. $command['options']['token'] = 'Per site policy, you must specify a token in the --token option for all commands.';
  40. }
  41. }
  42. /**
  43. * Implementation of drush_hook_COMMAND_validate().
  44. *
  45. * To test this example without copying, execute `drush --include=./examples updatedb`
  46. * from within your drush directory.
  47. *
  48. * Unauthorized users may view pending updates but not execute them.
  49. */
  50. function drush_policy_updatedb_validate() {
  51. // Check for a token in the request. In this case, we require --token=secret.
  52. if (!drush_get_option('token') == 'secret') {
  53. drush_log(dt('Per site policy, you must add a secret --token complete this command. See examples/policy.drush.inc. If you are running a version of drush prior to 4.3 and are not sure why you are seeing this message, please see http://drupal.org/node/1024824.'), 'warning');
  54. drush_set_context('DRUSH_AFFIRMATIVE', FALSE);
  55. drush_set_context('DRUSH_NEGATIVE', TRUE);
  56. }
  57. }
  58. /**
  59. * Implementation of drush_hook_COMMAND_validate().
  60. *
  61. * Only sudo tells me to make a sandwich: http://xkcd.com/149/
  62. */
  63. function drush_policy_make_me_a_sandwich_validate() {
  64. if (drush_is_windows()) {
  65. // $name = drush_get_username();
  66. // TODO: implement check for elevated process using w32api
  67. // as sudo is not available for Windows
  68. // http://php.net/manual/en/book.w32api.php
  69. // http://social.msdn.microsoft.com/Forums/en/clr/thread/0957c58c-b30b-4972-a319-015df11b427d
  70. }
  71. else {
  72. $name = posix_getpwuid(posix_geteuid());
  73. if ($name['name'] !== 'root') {
  74. return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
  75. }
  76. }
  77. }