-
Notifications
You must be signed in to change notification settings - Fork 6
/
feather.drush.inc
297 lines (253 loc) · 7.45 KB
/
feather.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* Feather is a light-weight, minimal configuration script for running an Apache
* http server in a development environment.
*/
/**
* Implementation of hook_drush_help().
*/
function feather_drush_help($section) {
switch ($section) {
case 'meta:feather:title':
return dt("Feather HTTP server commands");
case 'meta:feather:summary':
return dt('Start and stop the server, add and remove virtual hosts.');
}
}
/**
* Implementation of hook_drush_command().
*/
function feather_drush_command() {
$items['feather'] = array(
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-start'] = array(
'description' => 'Start the web server.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-stop'] = array(
'description' => 'Stop the web server.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-status'] = array(
'description' => 'Display the current status (running/not running) of the server.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-check-config'] = array(
'description' => 'Check the syntax of the generated configuration files.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-error-log'] = array(
'description' => 'Display the error log using `tail`.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-list'] = array(
'description' => 'Display all configured virtual hosts.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-add'] = array(
'description' => 'Add a virtual host configuration.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['feather-remove'] = array(
'description' => 'Remove a virtual host configuration.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
return $items;
}
/**
* Include a file from the feather includes directory.
*/
function feather_include($filename) {
include_once dirname(__FILE__) .'/includes/'. $filename;
}
/**
* Command callback for `drush feather`.
*/
function drush_feather() {
$args = func_get_args();
$command = array_shift($args);
if (empty($command)) {
$command = 'status';
}
$commands = drush_get_commands();
$command = $commands['feather-'. $command];
drush_dispatch($command, $args);
}
/**
* Command callback for `drush feather-start`.
*/
function drush_feather_start() {
$server = feather_get_server();
$vhosts = $server->get_vhosts();
if (empty($vhosts)) {
return drush_set_error('DRUSH_SERVER_NO_VHOSTS', dt('Create at least one virtual host using drush vhost-add before you can start the server.'));
}
if ($server->start()) {
drush_log(dt('Server started with the following virtual hosts:'), 'ok');
drush_feather_list();
}
else {
return drush_set_error('FEATHER_SERVER_START_ERROR', dt('Unable to start the server.'));
}
}
/**
* Command callback for `drush feather-stop`.
*/
function drush_feather_stop() {
$server = feather_get_server();
if ($server->stop()) {
drush_log(dt('Server stopped.'), 'ok');
}
else {
return drush_set_error('FEATHER_SERVER_STOP_ERROR', dt('Unable to stop the server.'));
}
}
/**
* Command callback for `drush feather-status`.
*/
function drush_feather_status() {
$server = feather_get_server();
$vhosts = $server->get_vhosts();
if (empty($vhosts)) {
return drush_set_error('DRUSH_SERVER_NO_VHOSTS', dt('Create at least one virtual host using drush vhost-add before you can start the server.'));
}
if ($server->is_running()) {
drush_log(dt('The server is running, with these virtual hosts:'), 'ok');
drush_feather_list();
}
else {
drush_log(dt('The server is stopped.'), 'ok');
}
}
/**
* Command callback for `drush feather-check-config`.
*/
function drush_feather_check_config() {
$server = feather_get_server();
$status = drush_shell_exec($server->apachectl("-t"));
if (!$status) {
drush_log(dt('The following errors were found in the server configuration:'), 'error');
$output = drush_shell_exec_output();
if (!empty($output)) {
foreach ($output as $line) {
drush_print($line);
}
}
}
else {
drush_log(dt('No errors found in the server configuration.'), 'ok');
}
}
/**
* Command callback for `drush feather-error-log`.
*/
function drush_feather_error_log() {
$server = feather_get_server();
$log_file = $server->log_path .'/error_log';
drush_shell_exec_interactive("tail -f {$log_file}");
}
/**
* Command callback for `drush feather-list`.
*/
function drush_feather_list() {
$server = feather_get_server();
$vhosts = $server->get_vhosts();
if (empty($vhosts)) {
drush_log(dt('No virtual host configurations found.'), 'warning');
return;
}
foreach ($vhosts as $name => $vhost) {
$rows[] = array(
$vhost->uri,
'->',
$vhost->doc_root,
);
}
drush_print_table($rows);
}
/**
* Command callback for `drush feather-add`.
*/
function drush_feather_add($alias = NULL) {
$server = feather_get_server();
if (!empty($alias)) {
$settings = drush_sitealias_get_record($alias);
}
$uri = !empty($settings['uri']) ? $settings['uri'] : drush_get_context('DRUSH_URI', FALSE);
if (empty($uri)) {
$host = drush_prompt('Host name', 'localhost');
$port = drush_prompt('Port', '8080');
$uri = 'http://'. $host .':'. $port;
}
$vhost = $server->vhost_get($uri);
if ($vhost->exists()) {
return drush_set_error('DRUSH_TOOLBOX_VHOST_EXISTS', dt('The specified virtual host already exists.'));
}
$vhost->doc_root = !empty($settings['root']) ? $settings['root'] : drush_get_context('DRUSH_DRUPAL_ROOT', FALSE);
if (empty($vhost->doc_root)) {
$vhost->doc_root = drush_prompt('Document root', getcwd());
}
$vhost->save();
drush_log("[Added] {$vhost->name} -> {$vhost->doc_root}", 'ok');
if ($server->is_running()) {
$server->stop();
$server->start();
}
}
/**
* Command callback for `drush feather-remove`.
*/
function drush_feather_remove($alias = NULL) {
$server = feather_get_server();
if (!empty($alias)) {
$settings = drush_sitealias_get_record($alias);
}
$uri = !empty($settings['uri']) ? $settings['uri'] : drush_get_context('DRUSH_URI', FALSE);
// If a uri is passed in, ask for confirmation.
if (!empty($uri)) {
$vhost = $server->vhost_get($uri);
if (!$vhost->exists()) {
return drush_set_error('DRUSH_TOOLBOX_VHOST_DOES_NOT_EXIST', dt('The specified virtual host configuration does not exist.'));
}
if (!drush_confirm(dt('Delete configuration for !uri?', array('!uri' => $vhost->uri)))) {
// Cancelled.
return;
}
}
// No uri passed in, prompt the user.
else {
$vhosts = $server->get_vhosts();
if (empty($vhosts)) {
drush_log(dt('No virtual host configurations found.'), 'warning');
return;
}
$options = array();
foreach ($vhosts as $name => $vhost) {
$options[$name] = $vhost->uri;
}
$choice = drush_choice($options, 'Choose a virtual host configuration to delete');
// Cancelled.
if (empty($choice)) {
return;
}
$vhost = $vhosts[$choice];
}
if ($vhost->exists()) {
$vhost->delete();
drush_log(dt('!host:!port removed.', array('!host' => $vhost->host, '!port' => $vhost->port)), 'ok');
}
}
function feather_get_server() {
static $server;
if (!isset($server)) {
feather_include('server.inc');
$server = new FeatherServer();
}
return $server;
}
function feather_get_conf_file() {
feather_include('conf-file.inc');
$conf_file = new FeatherHttpdConfFile();
return $conf_file;
}