Drush Alias sub-group auto creation

Public

For a more complex solution utilizing this one, try http://dropbucket.org/node/749

Add this to the end of any drush alias file to automatically produce sub-groups. This automatically creates a sub-group so you could call (as per the example) drush @stack1-all cc all and have it clear the cache on all sites in stack1. As you add new sites to your file, placing them in stack1.newsite will automatically add it to the stack1-all call group so you don't have to add it to another location. Creating new groupings will also have them be automatically picked up.

Now combine this approach with something like http://dropbucket.org/node/84 and you can automate lots of different grouping clusters for possible multiple, multi-site deployments and management all from 1 file on your laptop while sitting on the beach.

</> CopyGet raw version
php
  1. /**
  2.  * Magic to auto produce additional alias sub-groups
  3.  */
  4. $modifier = '-all';
  5. foreach ($aliases as $key => $values) {
  6. $parts = explode('.', $key);
  7. if (count($parts) >= 2) {
  8. // something that's in a subgroup
  9. array_push($aliases[$parts[0] . $modifier]['site-list'], '@' . $key);
  10. }
  11. else {
  12. // something is group-able
  13. $aliases[$key . $modifier] = array('site-list' => array('@' . $key));
  14. }
  15. }

Example file structure for aliases.drushrc.php

</> CopyGet raw version
php
  1. <?php
  2. /**
  3.  * Server config for everything at highest level
  4.  */
  5. $server = array(
  6. 'remote-user' => 'USER',
  7. 'ssh-options' => '-p PORT',
  8. 'remote-host' => 'ADDRESS.ADDRESS', // comment out for local server
  9. );
  10.  
  11. /**
  12.  * Stack1
  13.  */
  14. $aliases['stack1'] = $server + array(
  15. 'root' => 'LOCATION',
  16. 'uri' => 'ADDRESS',
  17. );
  18. $aliases['stack1.site2'] = array(
  19. 'parent' => '@stack1',
  20. 'uri' => 'ADDRESS',
  21. );
  22. $aliases['stack1.site3'] = array(
  23. 'parent' => '@stack1',
  24. 'uri' => 'ADDRESS',
  25. );
  26.  
  27. /**
  28.  * Stack2
  29.  */
  30. $aliases['stack2'] = $server + array(
  31. 'root' => 'LOCATION',
  32. );
  33. $aliases['stack2.site1'] = array(
  34. 'parent' => '@stack2',
  35. 'uri' => 'ADDRESS',
  36. );
  37.  
  38. /**
  39.  * Magic to auto produce additional alias sub-groups
  40.  */
  41. $modifier = '-all';
  42. foreach ($aliases as $key => $values) {
  43. $parts = explode('.', $key);
  44. if (count($parts) >= 2) {
  45. // something that's in a subgroup
  46. array_push($aliases[$parts[0] . $modifier]['site-list'], '@' . $key);
  47. }
  48. else {
  49. // something is group-able
  50. $aliases[$key . $modifier] = array('site-list' => array('@' . $key));
  51. }
  52. }

Example result

</> CopyGet raw version
php
  1. Array
  2. (
  3. [stack1] => Array
  4. (
  5. [remote-user] => USER
  6. [ssh-options] => -p PORT
  7. [remote-host] => ADDRESS.ADDRESS
  8. [root] => LOCATION
  9. [uri] => ADDRESS
  10. )
  11.  
  12. [stack1.site2] => Array
  13. (
  14. [parent] => @stack1
  15. [uri] => ADDRESS
  16. )
  17.  
  18. [stack1.site3] => Array
  19. (
  20. [parent] => @stack1
  21. [uri] => ADDRESS
  22. )
  23.  
  24. [stack2] => Array
  25. (
  26. [remote-user] => USER
  27. [ssh-options] => -p PORT
  28. [remote-host] => ADDRESS.ADDRESS
  29. [root] => LOCATION
  30. )
  31.  
  32. [stack2.site1] => Array
  33. (
  34. [parent] => @stack2
  35. [uri] => ADDRESS
  36. )
  37.  
  38. [stack1-all] => Array
  39. (
  40. [site-list] => Array
  41. (
  42. [0] => @stack1
  43. [1] => @stack1.site2
  44. [2] => @stack1.site3
  45. )
  46.  
  47. )
  48.  
  49. [stack2-all] => Array
  50. (
  51. [site-list] => Array
  52. (
  53. [0] => @stack2
  54. [1] => @stack2.site1
  55. )
  56.  
  57. )
  58.  
  59. )