1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @file |
||
5 | * Drush commands for Deployotron! |
||
6 | */ |
||
7 | |||
8 | 10 | define('DEPLOYOTRON_VERSION', '2.1.2'); |
|
9 | |||
10 | 10 | require_once 'deployotron.actions.inc'; |
|
11 | |||
12 | use Deployotron\ActionFactory; |
||
13 | |||
14 | /** |
||
15 | * Implements hook_drush_command(). |
||
16 | */ |
||
17 | function deployotron_drush_command() { |
||
18 | 10 | $items['deploy'] = array( |
|
19 | 10 | 'description' => 'Deploy site to environment.', |
|
20 | 10 | 'command-hook' => 'deployotron_run', |
|
21 | 10 | 'callback arguments' => array('deploy'), |
|
22 | 'arguments' => array( |
||
23 | 10 | 'site-alias' => 'Site alias to deploy to.', |
|
24 | 10 | ), |
|
25 | 'options' => array( |
||
26 | 10 | 'no-confirm' => 'Do not require confirmation before running.', |
|
27 | 10 | ), |
|
28 | 10 | 'sub-options' => array(), |
|
29 | 10 | 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, |
|
30 | ); |
||
31 | 10 | foreach (ActionFactory::getActions('deploy') as $action) { |
|
32 | 10 | if ($options = $action->getOptions()) { |
|
33 | 10 | $items['deploy']['options'] += $options['options']; |
|
34 | 10 | foreach ($options['sub-options'] as $sub => $sub_options) { |
|
35 | 10 | if (!isset($items['deploy']['sub-options'][$sub])) { |
|
36 | 10 | $items['deploy']['sub-options'][$sub] = array(); |
|
37 | 10 | } |
|
38 | 10 | $items['deploy']['sub-options'][$sub] += $sub_options; |
|
39 | 10 | } |
|
40 | 10 | } |
|
41 | 10 | } |
|
42 | |||
43 | 10 | $items['oh-my-god'] = array( |
|
44 | 10 | 'description' => 'Try to restore a site from a backup.', |
|
45 | 10 | 'command-hook' => 'deployotron_run', |
|
46 | 10 | 'callback arguments' => array('omg'), |
|
47 | 'arguments' => array( |
||
48 | 10 | 'site-alias' => 'Site alias to try and save.', |
|
49 | 10 | ), |
|
50 | 'options' => array( |
||
51 | 10 | 'no-confirm' => 'Do not require confirmation before running.', |
|
52 | 10 | ), |
|
53 | 10 | 'sub-options' => array(), |
|
54 | 10 | 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, |
|
55 | 'aliases' => array( |
||
56 | 10 | 'omg', |
|
57 | 10 | ), |
|
58 | ); |
||
59 | 10 | foreach (ActionFactory::getActions('omg') as $action) { |
|
60 | 10 | if ($options = $action->getOptions()) { |
|
61 | 10 | $items['oh-my-god']['options'] += $options['options']; |
|
62 | 10 | foreach ($options['sub-options'] as $sub => $sub_options) { |
|
63 | 10 | if (!isset($items['oh-my-god']['sub-options'][$sub])) { |
|
64 | 10 | $items['oh-my-god']['sub-options'][$sub] = array(); |
|
65 | 10 | } |
|
66 | 10 | $items['oh-my-god']['sub-options'][$sub] += $sub_options; |
|
67 | 10 | } |
|
68 | 10 | } |
|
69 | 10 | } |
|
70 | |||
71 | 10 | $items['deployotron-actions'] = array( |
|
72 | 10 | 'description' => 'Display the actions run by deployotron.', |
|
73 | 10 | 'hidden' => TRUE, |
|
74 | 10 | 'topic' => TRUE, |
|
75 | 10 | 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, |
|
76 | 10 | 'callback' => 'deployotron_print_actions', |
|
77 | ); |
||
78 | |||
79 | 10 | return $items; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Implements hook_drush_help(). |
||
84 | */ |
||
85 | function deployotron_drush_help($section) { |
||
86 | switch ($section) { |
||
87 | 3 | case 'meta:deployotron:title': |
|
88 | 1 | return dt('Deployotron'); |
|
89 | |||
90 | 3 | case 'meta:deployotron:summary': |
|
91 | 1 | return dt('Deploys site.'); |
|
92 | |||
93 | 3 | case 'drush:deploy': |
|
94 | 1 | return dt("Deploy site to a specific environment."); |
|
95 | |||
96 | 3 | case 'drush:oh-my-god': |
|
97 | 1 | return dt("Try to find a backup, and restore it to the site."); |
|
98 | |||
99 | } |
||
100 | 3 | } |
|
101 | |||
102 | /** |
||
103 | * Verify arguments to the command. |
||
104 | */ |
||
105 | function drush_deployotron_run_validate($name, $site_alias = NULL) { |
||
106 | // We'd prefer to use something like drush_set_context, but it claims it's for |
||
107 | // private use, so we'll have to fall back to good old global vars. |
||
108 | 9 | global $_deployotron_actions; |
|
109 | |||
110 | 9 | if (empty($site_alias)) { |
|
111 | 1 | return drush_set_error('NO_ALIAS', dt('No alias given.')); |
|
112 | |||
113 | } |
||
114 | 9 | $site = drush_sitealias_get_record($site_alias); |
|
115 | 9 | if (empty($site)) { |
|
116 | 1 | return drush_set_error('BAD_ALIAS', dt('Invalid alias.')); |
|
117 | } |
||
118 | |||
119 | // Copy options from the deployotron key in as command line options. |
||
120 | 9 | if (!empty($site['deployotron'])) { |
|
121 | 9 | foreach ($site['deployotron'] as $option => $value) { |
|
122 | 9 | drush_set_default($option, $value); |
|
123 | 9 | } |
|
124 | 9 | } |
|
125 | |||
126 | 9 | $_deployotron_actions = ActionFactory::getActions($name, $site); |
|
127 | 9 | foreach ($_deployotron_actions as $action) { |
|
128 | 9 | if ($action->enabled()) { |
|
129 | 9 | if (!$action->validate()) { |
|
130 | 1 | if (!drush_get_error()) { |
|
131 | drush_set_error(dt('@action validation failed without a message.', array('@action' => $action->getShort()))); |
||
132 | } |
||
133 | 1 | return FALSE; |
|
134 | } |
||
135 | 9 | } |
|
136 | 9 | } |
|
137 | 9 | } |
|
138 | |||
139 | /** |
||
140 | * Command callback. |
||
141 | */ |
||
142 | function drush_deployotron_run($name, $site_alias) { |
||
143 | 9 | global $_deployotron_actions, $_deployotron_rollbacks; |
|
144 | 9 | $_deployotron_rollbacks = array(); |
|
145 | |||
146 | 9 | $message = "About to:\n\n"; |
|
147 | 9 | $i = 1; |
|
148 | 9 | foreach ($_deployotron_actions as $action) { |
|
149 | 9 | if ($action->enabled()) { |
|
150 | 9 | $message .= ($i++) . ': ' . strtr($action->getDescription(), array("\n" => "\n ")) . "\n\n"; |
|
151 | 9 | } |
|
152 | 9 | } |
|
153 | |||
154 | 9 | $confirm_message = trim(drush_get_option('message', NULL) . "\n\n" . |
|
155 | 9 | drush_get_option('confirm_message', NULL) . "\n\n" . |
|
156 | 9 | drush_get_option('confirm_message_' . $name, NULL)); |
|
157 | 9 | if (!empty($confirm_message)) { |
|
158 | 1 | $message .= $confirm_message . "\n\n"; |
|
159 | 1 | } |
|
160 | |||
161 | 9 | if (drush_get_option('no-confirm', FALSE)) { |
|
162 | 1 | drush_print($message); |
|
163 | 1 | } |
|
164 | else { |
||
165 | 9 | $message .= "\nContinue?"; |
|
166 | 9 | if (!drush_confirm($message)) { |
|
167 | 2 | return drush_user_abort(); |
|
168 | } |
||
169 | } |
||
170 | |||
171 | 8 | $success = FALSE; |
|
172 | // Using an ArrayObject instead of a plain array, as arrays are passed by copy |
||
173 | // and objects aren't. Relying on the callee to remember the & is error-prone. |
||
174 | 8 | $state = new ArrayObject(); |
|
175 | 8 | foreach ($_deployotron_actions as $action) { |
|
176 | 8 | if ($action->enabled()) { |
|
177 | 8 | $short = $action->getShort(); |
|
178 | 8 | drush_log($action->getRunMessage(), 'status'); |
|
179 | 8 | $success = $action->run($state); |
|
180 | 8 | $_deployotron_rollbacks[] = $action; |
|
181 | 8 | if (!$success) { |
|
182 | 2 | drush_log(dt('@action failed.', array('@action' => $short)), 'error'); |
|
183 | 2 | drush_print(); |
|
184 | 2 | return FALSE; |
|
185 | } |
||
186 | else { |
||
187 | 8 | drush_log('', 'ok'); |
|
188 | 8 | drush_print(); |
|
189 | } |
||
190 | 8 | } |
|
191 | 8 | } |
|
192 | |||
193 | 8 | $done_message = trim(drush_get_option('message', NULL) . "\n\n" . |
|
194 | 8 | drush_get_option('done_message', NULL) . "\n\n" . |
|
195 | 8 | drush_get_option('done_message_' . $name, NULL)); |
|
196 | 8 | if (!empty($done_message)) { |
|
197 | 1 | drush_print("\n\n" . $done_message); |
|
198 | 1 | } |
|
199 | |||
200 | 8 | drush_print("Done."); |
|
201 | 8 | } |
|
202 | |||
203 | /** |
||
204 | * Rollback deploy command. |
||
205 | */ |
||
206 | function drush_deployotron_run_rollback($name) { |
||
207 | 4 | global $_deployotron_rollbacks; |
|
208 | |||
209 | 4 | foreach ($_deployotron_rollbacks as $action) { |
|
210 | 2 | if ($action->enabled()) { |
|
211 | 2 | if ($action->rollback() !== 'no rollback') { |
|
212 | 2 | drush_log(dt('Rolled back @action.', array('@action' => $action->getShort())), 'ok'); |
|
213 | 2 | } |
|
214 | 2 | } |
|
215 | 4 | } |
|
216 | 4 | } |
|
217 | |||
218 | /** |
||
219 | * Callback for topic depoloyotron-actions. |
||
220 | */ |
||
221 | function deployotron_print_actions() { |
||
222 | 1 | ActionFactory::getHelp(); |
|
223 | } |
||
224 |