getting rid of the git pm post task
[project/devshop_provision.git] / devshop_provision.drush.inc
1 <?php
2 /**
3 * @file
4 * Provision/Drush hooks for git commands.
5 */
6
7 /**
8 * Implements hook_drush_init()
9 */
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');
15 }
16
17 function devshop_provision_drush_exit(){
18 }
19
20 //include_once('devshop_provision.context.project.inc');
21
22 /**
23 * Implementation of hook_drush_command().
24 */
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.',
34 // ),
35 // 'aliases' => array('create-project'),
36 //);
37 $items['provision-devshop-delete'] = array(
38 'description' => 'Delete a DevShop Project and all associated sites and platforms.',
39 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
40 'arguments' => array(
41 ),
42 'aliases' => array('delete-project'),
43 );
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,
47 'arguments' => array(
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.',
51 ),
52 'aliases' => array('create-platform'),
53 );
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,
57 'options' => array(
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.',
62 ),
63 'aliases' => array('pdp'),
64 );
65 $items['provision-devshop-sync'] = array(
66 'description' => 'Sync database (and files, coming soon) from a chosen source site.',
67 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
68 'options' => array(
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.',
73 ),
74 'arguments' => array(
75 'from' => 'Site alias to sync from.',
76 'to' => 'Site alias to sync to.',
77 ),
78 'aliases' => array('pds'),
79 );
80 $items['provision-devshop-commit'] = array(
81 'description' => 'Export the site\'s Features and commit the result.',
82 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
83 'options' => array(
84 'revert' => ' Force revert all features after exporting and committing.',
85 ),
86 'aliases' => array('pdc'),
87 'arguments' => array(
88 'from' => 'Site alias to sync from.',
89 ),
90 );
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,
94 'options' => array(
95 'modules' => 'The list of modules to DL.',
96 ),
97 'aliases' => array('pdl'),
98 );
99
100 $items['provision-devshop-test'] = array(
101 'description' => 'Run a group of SimpleTest tests.',
102 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
103 'options' => array(
104 'tests-to-run' => 'The list of tests to run.',
105 'sync-from-live' => 'Sync contents from Live before running tests.',
106 ),
107 'aliases' => array('pdt'),
108 );
109 return $items;
110
111 }
112
113 /**
114 * Implements drush_HOOK_pre_COMMAND()
115 *
116 * This runs for each tasks during the command
117 * drush @hostmaster hosting-tasks
118 *
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
121 * the $task object.
122 */
123 function drush_devshop_provision_pre_hosting_task() {
124 $task =& drush_get_context('HOSTING_TASK');
125
126 // Verify Platform
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)) {
130
131 $platform = $task->ref;
132 $root = $platform->publish_path;
133 $git_remote = $platform->git_url;
134 $git_branch = $platform->git_branch;
135
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)));
139
140 // Build the command string
141 $command = "git clone $git_remote $root";
142 if ($git_branch) {
143 $command .= " --branch $git_branch";
144 }
145 }
146 // If the platform has been verified and has a branch and git url
147 else {
148 $root = $platform->publish_path;
149 $git_remote = $platform->git_url;
150 $git_branch = $platform->git_branch;
151
152 // Build the command string
153 $command = "git checkout $git_branch";
154 }
155
156 // Execute
157 if (!empty($command)){
158 drush_log('[DEVSHOP] Running: ' . $command);
159
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));
166 }
167 else {
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));
169 }
170
171 $output = drush_shell_exec_output();
172 drush_log('Shell Output: ' . implode("\n", $output) , 'notice');
173 }
174
175 }
176
177 // Pull
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']);
183 }
184
185 // Commit
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'];
190 }
191
192 // Sync
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'];
199 }
200
201 // Projectt Create
202 if ($task->ref->type == 'project' && $task->task_type == 'devshop-create') {
203 $task->options['retry'] = $task->task_args['retry'];
204 }
205
206 // Platform Create
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'];
211 }
212
213 // Download
214 if ($task->ref->type == 'site' && $task->task_type == 'devshop-dl') {
215 $task->options['modules'] = $task->task_args['modules'];
216 }
217
218 // Run tests
219 if ($task->ref->type == 'site' && $task->task_type == 'devshop-test') {
220 $tests = array();
221
222 foreach(explode("\n", $task->task_args['tests_to_run']) as $test) {
223 $test = trim($test);
224 if (strlen($test) > 0) {
225 $tests[] = $test;
226 }
227 }
228 $task->options['tests-to-run'] = implode($tests, ",");
229 $task->options['sync-from-live'] = $task->task_args['sync'];
230 }
231 }