forked from developmentseed/FeatureServer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfserver.drush.inc
More file actions
607 lines (557 loc) · 19.3 KB
/
fserver.drush.inc
File metadata and controls
607 lines (557 loc) · 19.3 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?php
// $Id$
/**
@TODO:
(10:38:30 AM) Adrian Rossouw: if you need drush to be able to modify the files dir
(10:38:47 AM) Adrian Rossouw: you need to add : umask(0002) to your settings.php
(10:38:53 AM) Adrian Rossouw: drupal 6.x has a crazy bug in it
(10:39:10 AM) Adrian Rossouw: that all uploaded files are 600 , owned by the web server user
(10:39:15 AM) Adrian Rossouw: so your drush script can't modify it
(10:39:20 AM) Adrian Rossouw: or change the perms, or anythin
*/
/**
* Implementation of hook_drush_command().
*
* @See drush_parse_command() for a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
function fserver_drush_command() {
$items = array();
$items['fserver-status'] = array(
'callback' => 'fserver_drush_status',
'description' => 'Determine the packaging status of a project.',
);
$items['fserver-package'] = array(
'callback' => 'fserver_drush_package',
'description' => 'Update packaging for a project.',
);
$items['fserver-distro'] = array(
'callback' => 'fserver_drush_distro',
'description' => 'Update packaging for a distribution.',
);
return $items;
}
/**
* Implementation of hook_drush_help().
*/
function fserver_drush_help($section) {
switch ($section) {
case 'drush:fserver-status':
return dt("Determine the packaging status of a project.");
case 'drush:fserver-package':
return dt("Update packaging for a project.");
case 'drush:fserver-distro':
return dt("Package a new Drupal distribution from a distribution node.");
}
}
/**
* Package new releases for a specified project or all.
*/
function fserver_drush_package() {
if (!drush_get_option('uri') && !drush_get_option('l')) {
drush_die('You must use the --uri option to specify the location of this site.');
}
$projects = _fserver_drush_get_projects();
// Filter the project list by the argument.
$args = func_get_args();
$arg = array_shift($args);
if (!empty($arg)) {
if (isset($projects[$arg])) {
$projects = array($arg => $projects[$arg]);
}
else {
drush_die('The project '. $arg .' could not be found.');
}
}
$rows = array();
foreach ($projects as $project) {
$new_tags = $project->get_new_tags();
if (!empty($new_tags)) {
foreach ($new_tags as $tag_id => $tag) {
$release = $project->create_release($tag_id);
$rows[] = array($project->node->title, $tag_id, $release->field_fserver_file[0]['filename']);
}
}
}
if (!empty($rows)) {
array_unshift($rows, array(dt('Project'), dt('Release'), dt('Filename')));
drush_print_table($rows, TRUE);
}
else {
drush_print(dt('All projects are up to date. No packages were created.'));
}
}
/**
* Display project packaging status.
*/
function fserver_drush_status() {
$projects = _fserver_drush_get_projects();
// Filter the project list by the argument.
$args = func_get_args();
$arg = array_shift($args);
if (!empty($arg)) {
if (isset($projects[$arg])) {
$projects = array($arg => $projects[$arg]);
}
else {
drush_die('The project '. $arg .' could not be found.');
}
}
$rows = array(array(dt('Method'), dt('Project'), dt('New tags')));
foreach ($projects as $project) {
$new_tags = $project->get_new_tags();
$rows[] = array(
$project->method,
$project->node->title,
!empty($new_tags) ? implode(', ',array_keys($new_tags)) : dt("Up to date"),
);
}
drush_print_table($rows, TRUE);
}
/**
* Retrieve all fserver projects from the DB.
*/
function _fserver_drush_get_projects() {
static $projects;
if (!isset($projects)) {
$projects = array();
$result = db_query("SELECT nid, title FROM {node} WHERE type = 'fserver_project' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
$project = new FserverProject($node, $node->field_fserver_name[0]['value'], $node->field_fserver_method[0]['value'], $node->field_fserver_repository[0]['value']);
$projects[$project->name] = $project;
}
}
return $projects;
}
/**
* Project class. Contains helpful methods for detecting tags, packaging, etc.
*/
class FserverProject {
var $node, $name, $method, $repository;
/**
* Constructor.
*/
function __construct($node, $name, $method, $repository) {
$this->node = $node;
$this->name = $name;
$this->method = $method;
$this->repository = $repository;
}
/**
* Destructor.
*/
function __destruct() {
if (isset($this->path)) {
drush_op('drush_shell_exec', "rm -rf {$this->path}");
}
}
/**
* Initialize a working copy for this project.
*/
function init_wc() {
if (!isset($this->path)) {
$this->path = file_directory_temp() . '/fserver_'. $this->name .'_'. time();
switch ($this->method) {
case 'git':
drush_op('drush_shell_exec', "git clone {$this->repository} {$this->path}");
break;
}
}
}
/**
* Get new tags that have no corresponding release nodes.
*/
function get_new_tags() {
$new_tags = array();
$releases = $this->get_releases();
foreach ($this->get_tags() as $tag_id => $tag) {
if (!isset($releases[$tag_id])) {
$new_tags[$tag_id] = $tag;
}
}
return $new_tags;
}
/**
* Get release nodes for this project.
*/
function get_releases() {
if (!isset($this->releases)) {
$this->releases = array();
$result = db_query("SELECT nid FROM {node} WHERE type = 'fserver_release' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
if ($node && isset($node->field_fserver_project[0]['nid']) && $node->field_fserver_project[0]['nid'] == $this->node->nid) {
$this->releases[fserver_generate_version($node, TRUE)] = $node;
}
}
}
return $this->releases;
}
function get_tag($tag_id) {
$tags = $this->get_tags();
if (isset($tags[$tag_id])) {
$vc = $tags[$tag_id];
$tag = fserver_parse_tag_name($vc);
$this->init_wc();
// Retrieve tag info
_drush_shell_exec_output_set(' '); // Clear output cache.
drush_op('drush_shell_exec', "git --git-dir={$this->path}/.git show {$vc}");
$info = drush_shell_exec_output();
if ($info[0] == "tag {$vc}") {
$info = implode("\n", $info);
$info = explode("\n\n", $info);
list($basic, $commit, $message) = $info;
$basic = explode("\n", $basic);
array_shift($basic); // Removes tag line
array_shift($basic); // Removes author line
// Set timestamp on the tag
$date = array_shift($basic);
$date = strtotime(trim(substr($date, strpos($date, ':') + 1)));
$tag['timestamp'] = is_numeric($date) ? $date : time();
}
return $tag;
}
}
/**
* Get tags for this project.
*/
function get_tags() {
if (!isset($this->tags)) {
$this->tags = array();
switch ($this->method) {
case 'git':
_drush_shell_exec_output_set(' '); // Clear output cache.
drush_op('drush_shell_exec', "git ls-remote --tags {$this->repository} | awk '{print $2;}'");
$lines = drush_shell_exec_output();
while ($line = array_shift($lines)) {
$vc = array_pop(explode('/', $line));
$parsed = fserver_parse_tag_name($vc);
if (!empty($parsed)) {
$this->tags[$parsed['version']] = $vc;
}
}
break;
}
}
return $this->tags;
}
/**
* Create a release node for the given tag.
*/
function create_release($tag_id) {
$tag = $this->get_tag($tag_id);
if ($tag && $file = $this->create_package($tag_id)) {
$node = new stdClass();
$node->type = 'fserver_release';
$node->status = 1;
$node->created = !empty($tag['timestamp']) ? $tag['timestamp'] : time();
$node->uid = $this->node->uid;
$node->title = "{$this->name} {$tag_id}";
$node->body = !empty($tag['message']) ? $tag['message'] : '';
$node->field_fserver_file = array(0 => (array) $file);
$node->field_fserver_project = array(0 => array('nid' => $this->node->nid));
$node->field_fserver_api = array(0 => array('value' => $tag['core']));
$node->field_fserver_versionmajor = array(0 => array('value' => $tag['major']));
$node->field_fserver_versionpatch = array(0 => array('value' => $tag['patch']));
$node->field_fserver_versionextra = array(0 => array('value' => $tag['extra']));
// @TODO
$node->field_fserver_recommended = array(0 => array('value' => 1));
$node->field_fserver_security = array(0 => array('value' => 0));
node_save($node);
}
return $node;
}
/**
* Create an archive package for the given tag.
*/
function create_package($tag_id) {
$this->init_wc();
$tag = $this->get_tag($tag_id);
if ($tag) {
switch ($this->method) {
case 'git':
$base = "{$this->name}-{$tag_id}";
$file_path = file_directory_path() .'/fserver';
// Create the directory if it doesn't exist.
if (file_check_directory($file_path, TRUE)) {
drush_op('drush_shell_exec', "git --git-dir={$this->path}/.git checkout {$vc}");
$this->write_package_info($tag);
drush_op('drush_shell_exec', "tar -Pcf - --exclude='.git' --transform='s,{$this->path},{$this->name},' {$this->path} | gzip > {$file_path}/{$base}.tgz");
// @TODO
// We need to properly chown and permission these directories.
// Is it possible for drush to know the web server user?
drush_op('chmod', $file_path, 0775);
drush_op('chgrp', $file_path, fserver_posix_groupname());
drush_op('chmod', "{$file_path}/{$base}.tgz", 0775);
drush_op('chgrp', "{$file_path}/{$base}.tgz", fserver_posix_groupname());
$file = new stdClass();
$file->uid = $this->node->uid;
$file->filename = "{$base}.tgz";
$file->filepath = "{$file_path}/{$base}.tgz";
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize($file->filepath);
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
drupal_write_record('files', $file);
return $file;
}
}
}
return FALSE;
}
/**
* Write packaging information for a release.
*/
function write_package_info($tag) {
$fserver_url = url("fserver", array('purl' => array('disabled' => TRUE), 'absolute' => TRUE));
$packaging = array();
$packaging[] = "; Information added by fserver";
$packaging[] = "core = \"{$tag['core']}\"";
$packaging[] = "datestamp = \"{$tag['timestamp']}\"";
$packaging[] = "project = \"{$this->name}\"";
$packaging[] = "project status url = \"{$fserver_url}\"";
$packaging[] = "version = \"{$tag['version']}\"";
$packaging = "\n" . implode("\n", $packaging);
$files = file_scan_directory($this->path, '.info', array('.git'));
foreach ($files as $path => $file) {
// @TODO: Parse the info file or not?
$info_file = file_get_contents($file->filename);
$info_file .= $packaging;
file_put_contents($file->filename, $info_file);
}
}
}
/**
* Discover the web server group. Taken from provision.
*/
function fserver_posix_groupname() {
$info = posix_getgrgid(posix_getgid());
$common_groups = array(
'httpd',
'www-data',
'apache',
'nogroup',
'nobody',
$info['name']);
foreach ($common_groups as $group) {
$groupname = '';
if (is_numeric($group)) {
$info = posix_getgrgid($group);
$groupname = $info['name'];
}
else {
$info = posix_getgrnam($group);
$groupname = $info['name'];
}
if ($groupname) {
return $groupname;
break;
}
}
return NULL;
}
function fserver_drush_distro() {
if (!drush_get_option('version')) {
drush_die('No version supplied, please use --version to specify a version for your release.');
}
$distros = _fserver_drush_get_distros();
// Filter the project list by the argument.
$args = func_get_args();
$arg = array_shift($args);
$version = drush_get_option('version');
if (!empty($arg)) {
if (isset($distros[$arg])) {
$distros = array($arg => $distros[$arg]);
}
else {
drush_die('The distribution '. $arg .' could not be found.');
}
}
$rows = array();
foreach ($distros as $distro) {
$release = $distro->create_release($version);
$rows[] = array($distro->node->title, $release->field_fserver_file[0]['filename']);
}
if (!empty($rows)) {
array_unshift($rows, array(dt('Project'), dt('Filename')));
drush_print_table($rows, TRUE);
}
else {
drush_print(dt('No distributions to update.'));
}
}
function _fserver_drush_get_distros() {
static $distros;
if (!isset($distros)) {
$projects = array();
$result = db_query("SELECT nid, title FROM {node} WHERE type = 'fserver_distro' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
$distro = new FserverDistro($node, $node->field_fserver_name[0]['value']);
$distros[$distro->name] = $distro;
}
}
return $distros;
}
/**
* Project class. Contains helpful methods for detecting tags, packaging, etc.
*/
class FserverDistro {
var $node, $name;
/**
* Constructor.
*/
function __construct($node, $name, $version = NULL) {
$this->node = $node;
$this->name = $name;
//$this->version = $version;
}
/**
* Destructor.
*/
function __destruct() {
if (isset($this->path)) {
drush_op('drush_shell_exec', "rm -rf {$this->path}");
}
}
/**
* Initialize a working copy for this project.
*/
function init_wc() {
if (!isset($this->raw_version)) {
drush_die('Version was not parsed properly. Please supply versions in the drupal-6--1-0-alpha1 format.');
}
if (!isset($this->path)) {
$this->path = file_directory_temp() . '/fserver_'. $this->name .'_'. time();
if (file_check_directory($this->path, TRUE)) {
if(!file_save_data($this->node->field_fserver_drushmake[0]['value'], $this->path .'/'. $this->name .'-'. $this->raw_version .'.make', FILE_EXISTS_REPLACE)) {
return FALSE;
}
// @TODO
// We need to properly chown and permission these directories.
// Is it possible for drush to know the web server user?
drush_op('chmod', $this->path, 0775);
drush_op('chgrp', $this->path, fserver_posix_groupname());
drush_op('chmod', "{$this->path}/{$this->name}-{$this->raw_version}.make", 0775);
drush_op('chgrp', "{$this->path}/{$this->name}-{$this->raw_version}.make", fserver_posix_groupname());
}
}
return TRUE;
}
/**
* Get release nodes for this project.
*/
function get_releases() {
if (!isset($this->releases)) {
$this->releases = array();
$result = db_query("SELECT nid FROM {node} WHERE type = 'fserver_release' AND status = 1");
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
if ($node && isset($node->field_fserver_project[0]['nid']) && $node->field_fserver_project[0]['nid'] == $this->node->nid) {
$this->releases[fserver_generate_version($node, TRUE)] = $node;
}
}
}
return $this->releases;
}
function parse_version($version) {
if (isset($version)) {
$this->version = array();
$parsed = fserver_parse_tag_name($version);
if (!empty($parsed)) {
$extra = $parsed['extra'] ? $parsed['extra'] : 0;
$view = views_get_view('fserver_release_lookup');
$view->set_display('default');
$args = array();
$args[] = $this->node->nid;
$args[] = $parsed['core'];
$args[] = $extra;
$view->set_arguments($args);
$preview = $view->preview($form_state['display_id'], $args);
$query = db_prefix_tables($view->build_info['query']);
_db_query_callback($view->build_info['query_args'], TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
// Next line required because views passes 0 as "IS NULL" to the query, and we actually want 0, so it's been hacked in manually
$query .= " AND (node_data_field_fserver_project.field_fserver_versionmajor_value = %d) AND (node_data_field_fserver_api.field_fserver_versionpatch_value = %d)";
$items = db_query($query, $parsed['major'], $parsed['patch']);
while ($result = db_fetch_object($items)) {
$results[] = $result->nid;
}
if (!$results[0]) {
$this->version = $parsed;
$this->raw_version = $parsed['version'];
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}
return $this->version;
}
/**
* Create a release node for the given tag.
*/
function create_release($version = NULL) {
$version = $this->parse_version($version);
if ($file = $this->create_package()) {
//if ($version) {
$node = new stdClass();
$node->type = 'fserver_release';
$node->status = 1;
$node->created = !empty($tag['timestamp']) ? $tag['timestamp'] : time();
$node->uid = $this->node->uid;
$node->title = "{$this->name} {$tag_id}";
$node->body = !empty($tag['message']) ? $tag['message'] : '';
$node->field_fserver_file = array(0 => (array) $file);
$node->field_fserver_project = array(0 => array('nid' => $this->node->nid));
$node->field_fserver_api = array(0 => array('value' => $version['core']));
$node->field_fserver_versionmajor = array(0 => array('value' => $version['major']));
$node->field_fserver_versionpatch = array(0 => array('value' => $version['patch']));
$node->field_fserver_versionextra = array(0 => array('value' => $version['extra']));
// @TODO
$node->field_fserver_recommended = array(0 => array('value' => 1));
$node->field_fserver_security = array(0 => array('value' => 0));
node_save($node);
}
return $node;
}
/**
* Create an archive package for the given tag.
*/
function create_package() {
if (!$this->init_wc()) {
return FALSE;
}
$base = "{$this->name}-{$this->raw_version}";
$file_path = file_directory_path() .'/fserver';
// Create the directory if it doesn't exist.
if (file_check_directory($file_path, TRUE)) {
drush_backend_invoke('make', array("{$this->path}/{$base}.make", "{$file_path}/{$base}", "--tar"));
drush_op('drush_shell_exec', "mv {$file_path}/{$base}.tar.gz {$file_path}/{$base}.tgz");
// @TODO
// We need to properly chown and permission these directories.
// Is it possible for drush to know the web server user?
drush_op('chmod', $file_path, 0775);
drush_op('chgrp', $file_path, fserver_posix_groupname());
drush_op('chmod', "{$file_path}/{$base}.tgz", 0775);
drush_op('chgrp', "{$file_path}/{$base}.tgz", fserver_posix_groupname());
$file = new stdClass();
$file->uid = $this->node->uid;
$file->filename = "{$base}.tgz";
$file->filepath = "{$file_path}/{$base}.tgz";
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize($file->filepath);
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
drupal_write_record('files', $file);
return $file;
}
return FALSE;
}
}