4 * Provision/Drush hooks for git commands.
8 * Implements hook_drush_init()
10 function devshop_provision_drush_init(){
11 $list = drush_commandfile_list();
12 $provision_drush_inc = $list['provision'];
13 include_once($provision_drush_inc);
14 include_once('devshop_provision.context.project.inc');
17 function devshop_provision_drush_exit(){
20 //include_once('devshop_provision.context.project.inc');
23 * Implementation of hook_drush_command().
25 function devshop_provision_drush_command() {
26 //$items['provision-devshop-create'] = array(
27 // 'description' => 'Create a DevShop Project with three platforms and three sites for dev, test, and live.',
28 // 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
29 // 'arguments' => array(
30 // 'git-url' => 'The URL of a git repo to use to create the platforms.',
31 // 'path' => 'Desired project path.',
32 // 'branch' => 'Branch (not yet implemented!)',
33 // 'retry' => 'Retry the create.',
35 // 'aliases' => array('create-project'),
37 $items['provision-devshop-delete'] = array(
38 'description' => 'Delete a DevShop Project and all associated sites and platforms.',
39 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH
,
42 'aliases' => array('delete-project'),
44 $items['provision-devshop-platform-create'] = array(
45 'description' => 'Create a new platform and add it to an existing DevShop Project.',
46 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH
,
48 'platform-name' => 'The name of the platform to be created.',
49 'git-url' => 'The URL of a git repo to use to create the platforms.',
50 'branch' => 'Git branch to clone.',
52 'aliases' => array('create-platform'),
54 $items['provision-devshop-pull'] = array(
55 'description' => 'Pull & verify platform code and (optionally) run update.php, clear cache, and revert features.',
56 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH
,
58 //'sync' => 'Sync content from default source site before code pull.',
59 'update' => 'Run update.php after code pull.',
60 'revert' => 'Revert all features after code pull.',
61 'cache' => 'Clear all caches after code pull.',
63 'aliases' => array('pdp'),
65 $items['provision-devshop-sync'] = array(
66 'description' => 'Sync database (and files, coming soon) from a chosen source site.',
67 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH
,
69 'update' => 'Run update.php after content sync.',
70 'revert' => 'Revert all features after content sync.',
71 'cache' => 'Clear all caches after content sync.',
72 'files' => 'Sync site files.',
75 'from' => 'Site alias to sync from.',
76 'to' => 'Site alias to sync to.',
78 'aliases' => array('pds'),
80 $items['provision-devshop-commit'] = array(
81 'description' => 'Export the site\'s Features and commit the result.',
82 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH
,
84 'revert' => ' Force revert all features after exporting and committing.',
86 'aliases' => array('pdc'),
88 'from' => 'Site alias to sync from.',
91 $items['provision-devshop-dl'] = array(
92 'description' => 'Download modules. This is just a wrapper for dl, it is here for hostmaster.',
93 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH
,
95 'modules' => 'The list of modules to DL.',
97 'aliases' => array('pdl'),
100 $items['provision-devshop-test'] = array(
101 'description' => 'Run a group of SimpleTest tests.',
102 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH
,
104 'tests-to-run' => 'The list of tests to run.',
105 'sync-from-live' => 'Sync contents from Live before running tests.',
107 'aliases' => array('pdt'),
114 * Implements drush_HOOK_pre_COMMAND()
116 * This runs for each tasks during the command
117 * drush @hostmaster hosting-tasks
119 * NOTE: This ONLY runs when being called this way.
120 * This hook should ONLY be used to pass Options from a hostmaster task form to
123 function drush_devshop_provision_pre_hosting_task() {
124 $task =& drush_get_context('HOSTING_TASK');
127 // For our platforms, we have to clone it if it has a git_remote
128 // If it has a git branch, we should checkout as well.
129 if ($task->ref
->type
== 'platform' && $task->task_type
== 'verify' && !empty($task->ref
->git_url
)) {
131 $platform = $task->ref
;
132 $root = $platform->publish_path
;
133 $git_remote = $platform->git_url
;
134 $git_branch = $platform->git_branch
;
136 // Check if a repo exists
137 if (!is_dir($root) || !drush_shell_cd_and_exec($root, 'git status')){
138 drush_log(dt("[DEVSHOP] No Repo found at !root. Cloning from !git", array('!git' => $platform->git_url
, '!root' => $root)));
140 // Build the command string
141 $command = "git clone $git_remote $root";
143 $command .
= " --branch $git_branch";
146 // If the platform has been verified and has a branch and git url
148 $root = $platform->publish_path
;
149 $git_remote = $platform->git_url
;
150 $git_branch = $platform->git_branch
;
152 // Build the command string
153 $command = "git checkout $git_branch";
157 if (!empty($command)){
158 drush_log('[DEVSHOP] Running: ' .
$command);
160 // @TODO: Create a d()->server->shell_cd_and_exec() function
161 // server->shell_exec() uses escapeshellcmd(), so we cannot cd_and_exec!
162 // So instead, this is the code from d()->server->shell_exec
163 // d()->server->shell_exec($cmd);
164 if (provision_is_local_host(d()->server
->remote_host
)) {
165 return drush_shell_cd_and_exec($root, escapeshellcmd($command));
168 return drush_shell_cd_and_exec($root, 'ssh ' .
drush_get_option('ssh-options', '-o PasswordAuthentication=no') .
' %s %s', $this->script_user .
'@' .
d()->server
->remote_host
, escapeshellcmd($command));
171 $output = drush_shell_exec_output();
172 drush_log('Shell Output: ' .
implode("\n", $output) , 'notice');
178 if ($task->ref
->type
== 'platform' && $task->task_type
== 'devshop-pull') {
179 $task->options
['no-update'] = !$task->task_args
['update'];
180 $task->options
['no-revert'] = !$task->task_args
['revert'];
181 $task->options
['no-cache'] = !$task->task_args
['cache'];
182 $task->options
['sites'] = explode('|', $task->task_args
['sites']);
186 if ($task->ref
->type
== 'site' && $task->task_type
== 'devshop-commit') {
187 $task->options
['message'] = $task->task_args
['message'];
188 $task->options
['push'] = $task->task_args
['push'];
189 $task->options
['revert'] = $task->task_args
['revert'];
193 if ($task->ref
->type
== 'site' && $task->task_type
== 'devshop-sync') {
194 $task->options
['source'] = $task->task_args
['source'];
195 $task->options
['pull'] = $task->task_args
['pull'];
196 $task->options
['no-update'] = !$task->task_args
['update'];
197 $task->options
['no-revert'] = !$task->task_args
['revert'];
198 $task->options
['no-cache'] = !$task->task_args
['cache'];
202 if ($task->ref
->type
== 'project' && $task->task_type
== 'devshop-create') {
203 $task->options
['retry'] = $task->task_args
['retry'];
207 if ($task->ref
->type
== 'project' && $task->task_type
== 'devshop-platform-create') {
208 $task->options
['platform-name'] = $task->task_args
['platform-name'];
209 $task->options
['git-url'] = $task->task_args
['git-url'];
210 $task->options
['branch'] = $task->task_args
['branch'];
214 if ($task->ref
->type
== 'site' && $task->task_type
== 'devshop-dl') {
215 $task->options
['modules'] = $task->task_args
['modules'];
219 if ($task->ref
->type
== 'site' && $task->task_type
== 'devshop-test') {
222 foreach(explode("\n", $task->task_args
['tests_to_run']) as
$test) {
224 if (strlen($test) > 0) {
228 $task->options
['tests-to-run'] = implode($tests, ",");
229 $task->options
['sync-from-live'] = $task->task_args
['sync'];