-
Notifications
You must be signed in to change notification settings - Fork 6
/
hosting.module
1425 lines (1286 loc) · 45 KB
/
hosting.module
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Hosting module.
*
* Contains just about all the interface magic of hostmaster.
*/
/**
* The maximum length for a site alias.
* Leaving room to append '.alias.drushrc.php' and keep filenames below 255.
*/
define('HOSTING_MAX_ALIAS_LENGTH', 235);
/**
* A serial queue type.
*
* This queue will be processed at a specified interval. When the interval has
* elapsed a configurable number of the items will be processed.
*/
define('HOSTING_QUEUE_TYPE_SERIAL', 'serial');
/**
* A batch queue type.
*
* This queue type aims to process repeatable tasks spread out over a
* configurable interval.
*
* See the processing code in hosting_get_queues() for the exact
* implementation.
*
* This queue type will:
* - Process items in the queue at some small number of specific points, evenly spaced throughout
* the configurable interval.
* - Process as many items at those intervals so that all the items are
* processed at least once over the configurable interval.
*
* @see HOSTING_QUEUE_TYPE_SPREAD
*/
define('HOSTING_QUEUE_TYPE_BATCH', 'batch');
/**
* A spread queue type.
*
* This queue type aims to process repeatable tasks spread out over a
* configurable interval.
*
* See the processing code in hosting_get_queues() for the exact
* implementation.
*
* This queue type will attempt to:
* - Process items at as many points as possible evenly spread over the
* configurable interval.
* - Points will be at least 1 minute apart to allow for the default dispatcher to execute them.
* - At each point process the minimum number of items needed to result in all
* items being processed at least once over the configurable interval.
*
* @see HOSTING_QUEUE_TYPE_BATCH
*/
define('HOSTING_QUEUE_TYPE_SPREAD', 'spread');
/**
* Not split for performance reasons. Just to keep code together.
*/
include_once dirname(__FILE__) . '/hosting.inc';
include_once dirname(__FILE__) . '/hosting.queues.inc';
include_once dirname(__FILE__) . '/hosting.features.inc';
/**
* Implements hook_menu().
*/
function hosting_menu() {
global $user;
$items = array();
$items['hosting/disabled'] = array(
'title' => 'Site disabled',
'page callback' => 'hosting_disabled_site',
'access arguments' => array('access disabled sites'),
'type' => MENU_CALLBACK,
);
$items['hosting/js'] = array(
'title' => 'ahah callback',
'page callback' => 'hosting_js_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['hosting/maintenance'] = array(
'title' => 'Site maintenance',
'page callback' => 'hosting_site_maintenance',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['admin/hosting'] = array(
'title' => 'Hosting',
'description' => 'Configure and manage the hosting system',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_features_form'),
'access arguments' => array('administer hosting'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/hosting/features'] = array(
'title' => 'Features',
'description' => 'Configure the exposed functionality of the Hosting system',
'weight' => -100,
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_features_form'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'access arguments' => array('administer hosting features'),
);
$items['admin/hosting/queues'] = array(
'title' => 'Queues',
'description' => 'Configure the frequency that cron, backup and task events are process',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_queues_configure'),
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer hosting queues'),
);
$items['admin/hosting/settings'] = array(
'title' => 'Settings',
'description' => 'Configure general Hosting settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_settings'),
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer hosting settings'),
);
$items['hosting/queues'] = array(
'page callback' => 'hosting_queues',
'type' => MENU_CALLBACK,
'access arguments' => array('access task logs'),
);
return $items;
}
/**
* Page callback for a page to be rendered in modal frame.
*/
function hosting_js_page() {
modalframe_child_js();
$args = func_get_args();
$path = implode('/', $args);
// In case Aegir is running in a sub-directory, the base_path should be
// stripped away here. Remove the leading slash from base_path as $path
// doesn't have one either.
$path = str_replace(ltrim(base_path(), '/'), '', $path);
menu_set_active_item($path);
if ($router_item = menu_get_item($path)) {
if ($router_item['access']) {
if ($router_item['file']) {
require_once $router_item['file'];
}
$return = call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
}
else {
$return = drupal_access_denied();
}
}
// Menu status constants are integers; page content is a string.
if (is_int($return) && module_exists('overlay')) {
overlay_close_dialog();
}
return $return;
}
/**
* Implements hook_menu_alter().
*/
function hosting_menu_alter(&$items) {
$items['node/add']['page callback'] = '_hosting_node_add';
$types = hosting_feature_node_types(TRUE);
foreach ($types as $feature => $type) {
$path = sprintf('node/add/%s', str_replace('_', '-', $type));
$items[$path]['access callback'] = 'hosting_menu_access';
$items[$path]['access arguments'] = array(
str_replace('_', ' ', $type),
$feature,
);
}
// These node types should remain hidden, and provide no user interface.
$items['node/add/package']['page callback'] = 'drupal_access_denied';
$items['node/add/task']['page callback'] = 'drupal_access_denied';
$items['node/add/package']['type'] = MENU_CALLBACK;
$items['node/add/task']['type'] = MENU_CALLBACK;
}
/**
* Menu access callback for creating a node provided by a Hosting feature.
*
* @param string $type
* The node type that the user wants to create.
* @param string $feature
* The machine name of the host feature that should be additionally checked to
* see if it's enabled.
*
* @return bool
* TRUE if the user can access, FALSE otherwise.
*/
function hosting_menu_access($type, $feature) {
global $user;
return (($user->uid == 1) || user_access('create ' . $type)) && (hosting_feature($feature) != HOSTING_FEATURE_DISABLED);
}
/**
* Page callback for a site that has been disabled.
*/
function hosting_disabled_site() {
drupal_set_breadcrumb(array());
return t("This site has been disabled by the site administrators");
}
/**
* Page callback for a site that is undergoing maintenance.
*/
function hosting_site_maintenance() {
drupal_set_breadcrumb(array());
return t("This site is currently in maintenance. Check back later.");
}
/**
* Implements hook_node_delete().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_delete($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'delete');
}
/**
* Implements hook_node_insert().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_insert($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'insert');
}
/**
* Implements hook_node_load().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_load($nodes, $types) {
// Decide whether any of $types are relevant to our purposes.
// Gather our extra data for each of these nodes.
$result = db_query("SELECT nid, name AS hosting_name FROM {hosting_context} WHERE nid IN(:nids)", array(':nids' => array_keys($nodes)))->fetchAllKeyed();
// Add our extra data to the node objects.
foreach ($result as $nid => $hosting_name) {
$nodes[$nid]->hosting_name = $hosting_name;
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($nodes[$nid], 'load');
}
}
/**
* Implements hook_node_prepare().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_prepare($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'prepare');
}
/**
* Implements hook_node_presave().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_presave($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'presave');
}
/**
* Implements hook_node_revision_delete().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_revision_delete($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'delete revision');
}
/**
* Implements hook_node_search_result().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_search_result($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'search result');
}
/**
* Implements hook_node_update().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_update($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'update');
}
/**
* Implements hook_node_update_index().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_update_index($node) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'update index');
}
/**
* Implements hook_node_validate().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_validate($node, $form, &$form_state) {
// Redispatch through our custom nodeapi implementation.
hosting_nodeapi($node, 'validate', $form);
}
/**
* Implements hook_node_view().
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_node_view($node, $view_mode, $langcode) {
// Redispatch through our custom nodeapi implementation.
if ($view_mode == 'teaser') {
hosting_nodeapi($node, 'view', TRUE, FALSE);
}
elseif ($view_mode == 'full') {
hosting_nodeapi($node, 'view', FALSE, TRUE);
}
}
/**
* Redirects to hosting_nodeapi_$nodetype_$op calls.
*
* To save ourselves from an incessant amount of intricately nested code,
* and allow easier extension and maintenance.
*
* @see hook_nodeapi_TYPE_OP()
*/
function hosting_nodeapi(&$node, $op, $arg1 = NULL, $arg2 = NULL) {
// We need to practically re-invent module_invoke_all here because
// we have to pass the node as a reference.
$return = array();
$hook = "nodeapi_" . $node->type . "_" . str_replace(" ", "_", $op);
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$result = $function($node, $arg1, $arg2);
if (isset($result) && is_array($result)) {
$return = array_merge_recursive($return, $result);
}
elseif (isset($result)) {
$return[] = $result;
}
}
return $return;
}
/**
* Implements hook_permission().
*/
function hosting_permission() {
return array(
'access hosting wizard' => array(
'title' => t('access hosting wizard'),
'description' => t("TODO Remove 'access hosting wizard', as it is not checked anywhere."),
),
'administer hosting queues' => array(
'title' => t('administer hosting queues'),
'description' => t('Configure the frequency that cron, backup and task events are process.'),
),
'administer hosting features' => array(
'title' => t('administer hosting features'),
'description' => t('Configure the exposed functionality of the hosting system.'),
),
'administer hosting' => array(
'title' => t('administer hosting'),
'description' => t('Configure and manage the hosting system.'),
),
'access disabled sites' => array(
'title' => t('access disabled sites'),
'description' => t('Access disabled sites.'),
),
'administer hosting settings' => array(
'title' => t('administer hosting settings'),
'description' => t('Configure general hosting settings.'),
),
);
}
/**
* Per node type description text. To be stored in the node_type table.
*
* @param string $type
* The node type.
*
* @return string
* Description text for the node type.
*/
function hosting_node_help($type) {
switch ($type) {
case 'site':
return t("<strong>An instance of a hosted site.</strong>\nIt contains information relating to the site, most notably the domain name, database server and platform it is being published on. A site may also have several aliases for additional domains the site needs to be accessible on.");
case 'platform':
return t("<strong>The file system location on a specific web server on which to publish sites.</strong>\nMultiple platforms can co-exist on the same web server, and need to do so for upgrades to be managed, as this is accomplished by migrating the site between platforms. Platforms are most commonly built for specific releases of Drupal.");
case 'client':
return t("<strong>The person or group that runs the site.</strong>\nThis information is usually required for billing and access purposes, to ensure that only certain people are able to view the information for sites they run. If you do not intend on having more than one client access the system, you will not need to create any additional clients for your purposes.");
case 'server':
return t("<strong>The physical machine which will provide various services to your site.</strong>\n Each server can have multiple services associated to it. To host a site you require a database service and a web service, which can either be provided by a single or multiple servers.");
case 'task':
return t("<strong>The mechanism whereby Hostmaster keeps track of all changes that occur to the system.</strong>\nEach task acts as a command for the back-end, and contains a full log of all changes that have occurred. If a task should fail, the administrator will be notified with an explanation of exactly what went wrong, and how to fix it.");
case 'package':
return t("PLACEHOLDER");
}
}
/**
* Implements hook_init().
*/
function hosting_init() {
static $already_called = FALSE;
if ($already_called) {
return;
}
$already_called = TRUE;
// Definitions for the default platforms, clients etc.
// Done to avoid using 'magic numbers'
define('HOSTING_DEFAULT_CLIENT', variable_get('hosting_default_client', 1));
define('HOSTING_DEFAULT_DB_SERVER', variable_get('hosting_default_db_server', 4));
define('HOSTING_DEFAULT_WEB_SERVER', variable_get('hosting_default_web_server', 3));
// This client has access to everything, see hosting_client.access.inc
define('HOSTING_ADMIN_CLIENT', variable_get('hosting_admin_client', 1));
define('HOSTING_OWN_DB_SERVER', variable_get('hosting_own_db_server', 2));
define('HOSTING_OWN_WEB_SERVER', variable_get('hosting_own_web_server', 3));
define('HOSTING_OWN_PLATFORM', variable_get('hosting_own_platform', 6));
// Find the base URL, this is used by the initial 'hosting-setup' Drush
// command. This gets defined in the bootstrap, so just using the global
// definition.
define('HOSTING_DEFAULT_BASE_URL', $GLOBALS['base_url']);
// Moved from hook_menu()
drupal_add_css(drupal_get_path('module', 'hosting') . '/hosting.css');
}
/**
* Implements hook_theme().
*/
function hosting_theme() {
return array(
'hosting_queues_configure' => array(
'file' => 'hosting.module',
'render element' => 'form',
),
'hosting_feature_dependencies' => array(
'file' => 'hosting.features.inc',
'arguments' => array(
'dependencies' => NULL,
'prefix' => NULL,
'features' => NULL,
),
),
);
}
/**
* Generates a link to a node.
*/
function _hosting_node_link($nid, $title = NULL) {
if (is_null($nid)) {
return t("None");
}
$node = node_load($nid);
if (!isset($title)) {
$title = isset($node->title) ? $node->title : NULL;
// Override with the human-readable name, when available.
$title = isset($node->human_name) && strlen($node->human_name) ? $node->human_name : $title;
}
$title = filter_xss($title);
if (isset($node->nid)) {
return node_access('view', $node) ? l($title, "node/" . $node->nid) : $title;
}
}
/**
* Implements hook_block_info().
*/
function hosting_block_info() {
$blocks['hosting_summary'] = array(
'info' => t('Hosting summary'),
'enabled' => 1,
'status' => 1,
'region' => 'sidebar_second',
'weight' => 10,
);
$blocks['hosting_queues'] = array(
'info' => t('Hosting queues'),
'enabled' => 1,
'status' => 1,
'region' => 'sidebar_first',
'weight' => 0,
);
$blocks['hosting_queues_summary'] = array(
'info' => t('Hosting queues summary'),
'enabled' => 1,
'status' => 1,
'region' => 'sidebar_first',
'weight' => 1,
'cache' => 'BLOCK_NO_CACHE',
);
$blocks['hosting_supporting_aegir'] = array(
'info' => t('Support Aegir'),
'enabled' => 1,
'status' => 1,
'region' => 'sidebar_second',
'weight' => 1,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function hosting_block_view($delta) {
$out = NULL;
switch ($delta) {
case 'hosting_queues':
$out = array(
'title' => t('Queues'),
'content' => hosting_queue_block(),
);
break;
case 'hosting_queues_summary':
$out = array(
'title' => t('Queues summary'),
'content' => hosting_queue_summary_block(),
);
break;
case 'hosting_supporting_aegir':
$out = array(
'title' => t('Support Aegir'),
'content' => hosting_supporting_aegir_block(),
);
break;
}
return $out;
// @todo where is 'hosting_summary'?
}
/**
* Build a block summarising the hosting queues.
*/
function hosting_queue_summary_block() {
if (user_access('administer hosting queues')) {
$queues = hosting_get_queues();
$output = '';
foreach ($queues as $queue => $info) {
$disp = array();
// Special case.
if (!$info['enabled']) {
$disp[] = t('Status: disabled');
continue;
}
$disp[] = t('Status: enabled');
foreach (array(
'description' => t('Description'),
'frequency' => t('Frequency'),
'items' => t('Items per run'),
'total_items' => t('Items in queue'),
'last_run' => t('Last run'),
) as $key => $title) {
if ($key == 'last_run') {
$info[$key] = hosting_format_interval($info[$key]);
}
elseif ($key == 'frequency') {
$info[$key] = t('every @interval', array('@interval' => format_interval($info[$key])));
}
$disp[] = $title . ": " . $info[$key];
}
$output .= theme('item_list', array('items' => $disp, 'title' => $info['name']));
}
return $output;
}
}
/**
* Build a block of the hosting queues.
*
* @see hosting_TASK_SINGULAR_summary()
*/
function hosting_queue_block() {
if (user_access('access task logs')) {
$queues = hosting_get_queues();
$output = '';
foreach ($queues as $queue => $info) {
// Invoke hosting_TASK_SINGULAR_summary().
$func = 'hosting_' . $info['singular'] . '_summary';
if (function_exists($func)) {
$output .= $func();
}
}
return $output;
}
}
/**
* Build a 'Support Aegir' block.
*/
function hosting_supporting_aegir_block() {
if (user_access('administer hosting')) {
$output = "
<p>Aegir will always cost you nothing to use, but that doesn't mean it costs us nothing to make.</p>
<p><strong>Aegir needs your continued support to grow and thrive.</strong></p>
<p>If you feel that Aegir has added significant value to your business or endeavour, please consider donating!</p>
<p><strong>How much is Aegir worth to you?</strong></p>
<a href=\"http://www.aegirproject.org/#support\" target=_blank>Donate Now ... </a>
<a href=\"http://www.aegirproject.org/#support\" target=_blank>or get more info.</a>
<footer style=\"font-size: smaller;\">This block is only displayed to you, the superuser.</footer>
";
return $output;
}
}
/**
* Check site URL is allowed.
*
* This function hooks into hook_allow_domain to let contrib modules
* weigh in on whether the site should be created.
*
* All the hooks must return true for the domain to be allowed.
*
* @see hook_allow_domain()
*/
function hosting_domain_allowed($url, $params = array()) {
$results = module_invoke_all('allow_domain', $url, $params);
$return = !in_array(FALSE, $results);
return $return;
}
/**
* Initial hosting setup drush command.
*
* Runs the 'hosting-dispatch' command, and sets up the crontab.
*/
function drush_hosting_setup() {
if (drush_confirm("This command will replace your crontab for this user. continue?")) {
variable_set('hosting_dispatch_enabled', FALSE);
// Attempt to run the dispatch command, to make sure it runs without the
// queue being enabled.
variable_set('hosting_dispatch_enabled', TRUE);
drush_print(_hosting_dispatch_cmd());
exec(_hosting_dispatch_cmd(), $return, $code);
variable_set('hosting_dispatch_enabled', FALSE);
$return = implode("\n", $return);
$data = unserialize($return);
if ($code == DRUSH_SUCCESS) {
variable_set('hosting_dispatch_enabled', TRUE);
drush_log(t("Dispatch command was run successfully"), 'success');
_hosting_setup_cron();
}
else {
drush_set_error('DRUSH_FRAMEWORK_ERROR', dt("Dispatch command could not be run. Returned: \n@return", array('@return' => $return)));
}
if (drush_get_error()) {
drush_log(t("The command did not complete successfully, please fix the issues and re-run this script"), 'error');
}
}
}
/**
* Set up the hosting-dispatch command in the aegir user's crontab.
*
* Replace the crontab entry if it exists, else create it from scratch.
*/
function _hosting_setup_cron($add = TRUE) {
$existing = FALSE;
$cron = '';
$cron_lines = array();
// Load any existing crontab.
exec('crontab -l 2> /dev/null', $cron_lines);
variable_set('hosting_cron_backup', $cron_lines);
if (count($cron_lines)) {
drush_log(dt("Replacing existing crontab"), 'notice');
$cron_lines = hosting_queues_cron_removal($cron_lines);
$cron = implode("\n", $cron_lines);
}
else {
drush_log(dt("No existing crontab was found"), 'message');
}
if ($add) {
// Append the fresh Aegir specific lines.
$cron .= "\n" . hosting_queues_cron_cmd();
drush_log(dt("Adding hosting-dispatch cron entry to run every minute"), 'ok');
}
// Write a new crontab.
$fp = popen('crontab -', 'w');
$success = TRUE;
if ($fp) {
if (fwrite($fp, $cron) && fwrite($fp, "\n")) {
$success = TRUE;
}
else {
$success = FALSE;
}
// 'pclose' returns shell exit codes (ie. 0 is success).
if (pclose($fp)) {
$success = FALSE;
}
}
else {
$success = FALSE;
}
if ($success) {
drush_log(dt("Installed a new crontab entry."), 'success');
}
else {
drush_set_error('PROVISION_CRON_FAILED', dt('Failed to install cron job'));
}
}
/**
* Replacement node/add page.
*
* Major kludge to remove the hidden node types from node/add page.
*
* Copied from node.module.
*/
function _hosting_node_add($type = '') {
global $user;
$types = node_type_get_types();
$type = ($type) ? str_replace('-', '_', $type) : NULL;
// If a node type has been specified, validate its existence.
if (isset($types[$type]) && user_access('create ' . $type) && (hosting_feature($type) !== HOSTING_FEATURE_DISABLED)) {
// Initialize settings:
$node = array(
'uid' => $user->uid,
'name' => $user->name,
'type' => $type,
);
drupal_set_title(t('Submit @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
// TODO $node needs to have $form as its first parameter.
$output = drupal_get_form($type . '_node_form', $node);
}
else {
// If no (valid) node type has been provided, display a node type overview.
foreach ($types as $type) {
if (function_exists($type->module . '_form') && user_access('create ' . $type->type) && (hosting_feature($type->type) !== HOSTING_FEATURE_DISABLED)) {
$type_url_str = str_replace('_', '-', $type->type);
$title = t('Add a new @s.', array('@s' => $type->name));
$out = '<dt>' . l(drupal_ucfirst($type->name), "node/add/$type_url_str", array('attributes' => array('title' => $title))) . '</dt>';
$out .= '<dd>' . filter_xss_admin($type->description) . '</dd>';
$item[$type->name] = $out;
}
}
if (isset($item)) {
uksort($item, 'strnatcasecmp');
$output = t('Choose the appropriate item from the list:') . '<dl>' . implode('', $item) . '</dl>';
}
else {
$output = t('No content types available.');
}
}
return $output;
}
/**
* List queues or tasks in a queue if a key is provided.
*
* @see hosting_TASK_SINGULAR_list()
*/
function hosting_queues($key = '') {
$queues = hosting_get_queues();
$output = '';
if ($queues[$key]) {
if ($queues[$key]['name']) {
$output .= "<h1>" . $queues[$key]['name'] . "</h1>";
}
// Invoke hosting_TASK_SINGULAR_list().
$func = 'hosting_' . $queues[$key]['singular'] . '_list';
if (function_exists($func)) {
$output .= $func();
}
}
else {
foreach ($queues as $key => $queue) {
$item[] = l($queue['name'], 'hosting/queues/' . $key);
}
$output .= theme('item_list', array('items' => $item, 'title' => t('Queues')));
}
return $output;
}
/**
* Generate context sensitive breadcrumbs.
*
* @param object $node
* A node object to use for context of the breadcrumbs.
*/
function hosting_set_breadcrumb($node) {
$breadcrumbs[] = l(t('Home'), NULL);
switch ($node->type) {
case 'task':
$breadcrumbs[] = _hosting_node_link($node->rid);
break;
case 'platform':
$breadcrumbs[] = _hosting_node_link($node->web_server);
break;
case 'site':
$breadcrumbs[] = _hosting_node_link($node->platform);
break;
case 'server':
$breadcrumbs[] = l(t('Servers'), 'hosting/servers');
break;
}
drupal_set_breadcrumb($breadcrumbs);
}
/**
* Form to configure the frequency of queue execution.
*/
function hosting_queues_configure($form, &$form_state) {
drupal_add_css(drupal_get_path('module', 'hosting') . '/hosting.css');
$units = array(
strtotime("1 second", 0) => t("Seconds"),
strtotime("1 minute", 0) => t("Minutes"),
strtotime("1 hour", 0) => t("Hours"),
strtotime("1 day", 0) => t("Days"),
strtotime("1 week", 0) => t("Weeks"),
);
$queues = hosting_get_queues();
$form['#tree'] = TRUE;
foreach ($queues as $queue => $info) {
$form[$queue]['description'] = array(
'#type' => 'item',
'#value' => $info['name'],
'#description' => $info['description'],
);
$form[$queue]["enabled"] = array(
'#type' => 'checkbox',
'#default_value' => $info['enabled'],
);
$form[$queue]["last_run"] = array(
'#markup' => hosting_format_interval(variable_get('hosting_queue_' . $queue . '_last_run', FALSE)),
);
$form[$queue]['frequency']['#prefix'] = "<div class='hosting-queue-frequency'>";
$form[$queue]['frequency']['#suffix'] = "</div>";
if ($info['type'] == HOSTING_QUEUE_TYPE_BATCH) {
$form[$queue]['frequency']['items'] = array(
'#markup' => t('%count %items every', array(
"%count" => $info['total_items'],
"%items" => format_plural($info['total_items'], $info['singular'], $info['plural']),
)),
'#prefix' => "<div class='hosting-queue-frequency-items'>",
'#suffix' => "</div>",
);
}
elseif ($info['type'] == HOSTING_QUEUE_TYPE_SPREAD) {
$form[$queue]['frequency']['items'] = array(
'#markup' => t('%count %items every', array(
"%count" => $info['total_items'],
"%items" => format_plural($info['total_items'], $info['singular'], $info['plural']),
)),
'#prefix' => "<div class='hosting-queue-frequency-items'>",
'#suffix' => "</div>",
);
}
else {
$form[$queue]['frequency']['items'] = array(
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => $info['items'],
// @ignore i18n_11
'#suffix' => ' ' . t('%items every', array('%items' => $info['plural'])) . ' ',
'#attributes' => array(
'class' => array('hosting-select-frequency-items'),
),
);
}
foreach (array_reverse(array_keys($units)) as $length) {
$unit = $units[$length];
if (!($info['frequency'] % $length)) {
$frequency_ticks = $info['frequency'] / $length;
$frequency_length = $length;
break;
}
}
$form[$queue]['frequency']["ticks"] = array(
'#type' => 'textfield',
'#default_value' => $frequency_ticks,
'#maxlength' => 5,
'#size' => 5,
'#attributes' => array(
'class' => array('hosting-select-frequency-ticks'),
),
);
$form[$queue]['frequency']["unit"] = array(
'#type' => 'select',
'#options' => $units,
'#default_value' => $frequency_length,
'#attributes' => array(
'class' => array('hosting-select-frequency-unit'),
),
);
}
$form['help'] = array(
'#type' => 'item',
'#description' => t("Increasing the queue frequency to every 1 second will not cause them to be dispatched every second, as the dispatcher can only run once per minute via cron. However, with such a short frequency, executing the hosting-dispatch command manually from the CLI will allow you to 'force' the queues to be dispatched between cron runs. This may be useful for development purposes."),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
);
return $form;
}
/**
* Theme function to render the queue configuration form.
*
* @see hosting_queues_configure()
*/
function theme_hosting_queues_configure($variables) {
$form = $variables['form'];
$queues = hosting_get_queues();
$rows = array();
$header = array(
'',
t('Description'),
array(
'data' => t('Frequency'),
'class' => array('hosting-queue-frequency-head'),
),
t('Last run'),
);
foreach ($queues as $key => $info) {
$row = array();
$row[] = drupal_render($form[$key]['enabled']);
$row[] = drupal_render($form[$key]['description']);
$row[] = drupal_render($form[$key]['frequency']);
$row[] = drupal_render($form[$key]['last_run']);
$rows[] = $row;