-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebfact.module
More file actions
573 lines (521 loc) · 21 KB
/
Copy pathwebfact.module
File metadata and controls
573 lines (521 loc) · 21 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
<?php
//require DRUPAL_ROOT . '/vendor/autoload.php';
/**
* Implementation of hook_init().
*/
function webfact_init() {
drupal_add_css(drupal_get_path('module', 'webfact') . '/css/webfact.css',
array('group' => CSS_DEFAULT, 'every_page' => TRUE));
//require __DIR__ . '/vendor/autoload.php';
}
//function webfact_install(){
//}
function webfact_permission() {
return array(
'access websites' => array(
'title' => t('Access websites'),
'description' => t('Allow users to access website details'),
),
'access templates' => array(
'title' => t('Access templates'),
'description' => t('Allow users to access website templates'),
),
'manage containers' => array(
'title' => t('Manage Containers'),
'description' => t('Allow user full access to docker containers'),
),
);
}
function webfact_menu() {
$items['website/%/%'] = array(
'page callback' => 'webfact_arguments',
'page arguments' => array(0, 1, 2),
'access arguments' => array('access websites'),
'type' => MENU_CALLBACK, // no menu
);
// admin form in Configuration > Development
$items['admin/config/development/webfact'] = array(
'title' => t('Webfactory'),
'description' => t('Webfactory settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('webfact_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
// entityfieldquery tests
#$items['bob'] = array(
# 'page callback' => 'webfact_bootstrap',
# 'access arguments' => array('access websites'),
# 'type' => MENU_CALLBACK, // no menu
#);
#$items['site/%/%'] = array(
# 'page callback' => 'webfact_bootstrap',
# 'page arguments' => array(0, 1, 2),
# 'access arguments' => array('access websites'),
# 'type' => MENU_CALLBACK, // no menu
#);
#// angular tests
#$items['webang'] = array(
# 'title' => 'Angular test',
# 'page callback' => 'webfact_angular',
# 'access arguments' => array('access content'),
# 'type' => MENU_CALLBACK, // no menu
#);
return $items;
}
// admin form
function webfact_admin_settings() {
$form['webfact_fieldset_serverapi'] = array(
'#title' => t('Server API'),
'#description' => t('The Webfactory can manage containers on either a Mesos or Docker server.'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webfact_fieldset_serverapi']['webfact_container_api'] = array(
'#title' => t('Server API type'),
'#type' => 'select',
'#options' => array(
0 => t('Docker API'),
1 => t('Mesos/Marathon API'),
),
'#default_value' => variable_get('webfact_container_api', 0),
);
$form['webfact_fieldset_serverapi']['webfact_dserver'] = array(
'#title' => t('Docker API URL'),
'#default_value' => variable_get('webfact_dserver', array('')),
'#description' => t('e.g. unix:///var/run/docker.sock or http://dock.example.ch:2375'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 100,
);
$form['webfact_fieldset_serverapi']['webfact_mserver'] = array(
'#title' => t('Mesos/Marathon API URL'),
'#default_value' => variable_get('webfact_mserver', array('')),
'#description' => t('e.g. http://marathon.example.ch:8080. Leave empty if mesos is not used to manage containers. Note: ensure a trailing slash.'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 100,
);
$form['webfact_fieldset_serverapi']['webfact_bserver'] = array(
'#title' => t('Mesos/Bamboo API URL'),
'#default_value' => variable_get('webfact_bserver', array('')),
'#description' => t('e.g. http://bamboo.example.ch:8000. Leave empty if mesos is not used to manage containers. Note: ensure a trailing slash.'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 100,
);
$form['webfact_fieldset_serverapi']['webfact_mesosserver'] = array(
'#title' => t('Mesos/Mesos URL'),
'#default_value' => variable_get('webfact_mesosserver', array('')),
'#description' => t('e.g. http://mesos.example.ch:5050. Used to provide links to job outputs. Leave empty if mesos is not used to manage containers. Note: ensure a trailing slash.'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 100,
);
$form['webfact_fieldset_server'] = array(
'#title' => t('Webfactory UI container'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webfact_fieldset_server']['webfact_loglines'] = array(
'#title' => t('How many lines to tail on logs'),
'#default_value' => variable_get('webfact_loglines', array('')),
'#description' => t('e.g. 90'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 100,
);
$form['webfact_fieldset_server']['webfact_druplogs_count'] = array(
'#title' => t('How many lines to tail on drupal logs'),
'#default_value' => variable_get('webfact_druplogs_count', 200),
'#description' => t('e.g. 200'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 100,
);
$form['webfact_fieldset_server']['webfact_server_sitesdir'] = array(
'#title' => t('Folder within the webfact container, where sites are mounted'),
'#default_value' => variable_get('webfact_server_sitesdir', array('/opt/sites/')),
'#description' => t('Default is /opt/sites/. When new websites are added, a subfolder is created with the site name. e.g. /data in containers could be mapped to /opt/sites/CONTAINERNAME. Note: ensure a trailing slash.'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_cont'] = array(
'#title' => t('New containers'),
'#description' => t('Settings for new containers'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webfact_fieldset_cont']['webfact_fserver'] = array(
'#title' => t('Domain postfix'),
'#default_value' => variable_get('webfact_fserver', array('')),
'#description' => t('The part of the URL after the hostname, e.g. webfact.example.ch'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_cont']['webfact_cont_image'] = array(
'#title' => t('Default base image for creating containers (can be overridden in templates and websites)'),
'#default_value' => variable_get('webfact_cont_image', array('')),
'#description' => t('e.g. boran/drupal'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_cont']['webfact_restartpolicy'] = array(
'#title' => t('New containers should start automatically when the docker server is rebooted?'),
'#default_value' => variable_get('webfact_restartpolicy', array('on-failure')),
'#description' => t('The site default behaviour to apply when the container exits, or docker is booted. Either "always" or "on-failure" to restart only when the container exit code is non-zero. This can also be specified per container.'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 30,
);
$form['webfact_fieldset_cont']['webfact_env_server'] = array(
'#title' => t('An environment variable to pass to all containers'),
'#default_value' => variable_get('webfact_env_server', array('')),
'#description' => t('To identify where containers were created. e.g. WEBFACT_SERVER=foo.example.ch'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_cont']['webfact_www_volume_path'] = array(
'#title' => t('The webroot, default /var/www/html'),
'#default_value' => variable_get('webfact_www_volume_path', '/var/www/html'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_cont']['webfact_server_sitesdir_host'] = array(
'#title' => t('Host folder for container mountpoints'),
'#default_value' => variable_get('webfact_server_sitesdir_host', array('/opt/sites/')),
'#description' => t('Default is /opt/sites/, used as a prefix for the volumes below. Note: ensure a trailing slash.'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_cont']['webfact_www_volume'] = array(
'#title' => t('Create a volume for the webroot above mapped to a subfolder www within the hostfolder above'),
'#default_value' => variable_get('webfact_www_volume', 1),
'#description' => t('Automatically add a standard volume for web files, e.g. /opt/sites/CONTAINERNAME/www. It will be created if necessary.'),
'#type' => 'checkbox',
'#return_value' => 1,
);
$form['webfact_fieldset_cont']['webfact_data_volume'] = array(
'#title' => t('Create a volume /data mapped to /opt/sites/CONTAINERNAME/data on the docker server'),
'#default_value' => variable_get('webfact_data_volume', 1),
'#description' => t('Automatically add a standard volume for sharing backups or other infos from container to server. /opt/sites/CONTAINERNAME/data will be created if necessary.'),
'#type' => 'checkbox',
'#return_value' => 1,
);
$form['webfact_fieldset_rproxy'] = array(
'#title' => t('Docker API - Reverse proxy'),
'#description' => t('Docker API: Settings for the proxy container responsbile for virtual name mapping.'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webfact_fieldset_rproxy'] = array(
'#title' => t('Docker Reverse proxy'),
'#description' => t('Docker API: Settings for the proxy container responsbile for virtual name mapping.'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webfact_fieldset_rproxy']['webfact_rproxy'] = array(
'#title' => t('Reverse proxy container ID'),
'#default_value' => variable_get('webfact_rproxy', array('nginx')),
'#description' => t('default nginx'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_rproxy']['webfact_rproxy_conf'] = array(
'#title' => t('Reverse proxy config file'),
'#default_value' => variable_get('webfact_rproxy_conf', array('/etc/nginx/conf.d/default.conf')),
'#description' => t('default /etc/nginx/conf.d/default.conf'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_rproxy']['webfact_rproxygen'] = array(
'#title' => t('Reverse proxy generator container ID'),
'#default_value' => variable_get('webfact_rproxygen', array('nginx-gen')),
'#description' => t('default nginx-gen'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_rproxy']['webfact_rproxy_loglines'] = array(
'#title' => t('How many lines to tail on nginx logs'),
'#default_value' => variable_get('webfact_rproxy_loglines', array('1000')),
'#description' => t('default 1000'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 100,
);
// webfact_manage_db_host webfact_manage_db_user webfact_manage_db_pw
$form['webfact_fieldset_db'] = array(
'#title' => t('Database management'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webfact_fieldset_db']['webfact_manage_db'] = array(
'#title' => t('Manage an external DB for all containers'),
'#default_value' => variable_get('webfact_manage_db', 0),
'#description' => t('Create a new DB for each new container in a dedicated DB instance. The stored procedure CreateAppDB() is called and the DB name is d_containername and username u_containername. <br>If this tickbox is not set, assume that DBs are created within containers themselves.'),
'#type' => 'checkbox',
'#return_value' => 1,
);
$form['webfact_fieldset_db']['webfact_manage_db_host'] = array(
'#title' => t('Hostname or IP of the DB server'),
'#default_value' => variable_get('webfact_manage_db_host', array('mysql')),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_db']['webfact_manage_db_user'] = array(
'#title' => t('Sql username'),
'#default_value' => variable_get('webfact_manage_db_user', array('webfact_create')),
'#description' => t('This user must have the right to create DBs'),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_db']['webfact_manage_db_pw'] = array(
'#title' => t('Sql password'),
'#default_value' => variable_get('webfact_manage_db_pw', array('')),
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 100,
);
$form['webfact_fieldset_UI'] = array(
'#title' => t('User interface and debugging'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webfact_fieldset_UI']['webfact_msglevel1'] = array(
'#title' => t('Level 1 messages to user: standard'),
'#default_value' => variable_get('webfact_msglevel1', true),
'#description' => t('Should always be enabled'),
'#type' => 'checkbox',
'#return_value' => 1,
);
$form['webfact_fieldset_UI']['webfact_msglevel2'] = array(
'#title' => t('Level 2 messages to user: verbose'),
'#default_value' => variable_get('webfact_msglevel2', true),
'#type' => 'checkbox',
'#return_value' => 1,
);
$form['webfact_fieldset_UI']['webfact_msglevel3'] = array(
'#title' => t('Level 3 messages to user: debugging'),
'#default_value' => variable_get('webfact_msglevel3', true),
'#type' => 'checkbox',
'#return_value' => 1,
);
$form['webfact_fieldset_UI']['webfact_rebuild_backups'] = array(
'#title' => t('Create automatic backups when rebuilding. Disable this on dev/test servers to save time and diskspace'),
'#default_value' => variable_get('webfact_rebuild_backups', true),
'#type' => 'checkbox',
'#return_value' => 1,
);
$form['webfact_fieldset_UI']['webfact_log_refresh'] = array(
'#title' => t('Refresh delay (secs) for the container log UI'),
#'#default_value' => variable_get('webfact_log_refresh', array('')),
'#default_value' => variable_get('webfact_log_refresh', array('30')),
'#description' => t('default 30'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 10,
);
return system_settings_form($form);
}
function webfact_theme() {
return array(
'bootstrap' => array(
'template' => 'bootstrap',
),
'webfacts' => array(
'template' => 'webfacts',
),
);
}
function webfact_angular() {
//$info = libraries_load('angularjs');
//dpm($info);
//drupal_add_library('angularjs', 'angularjs');
$path = drupal_get_path('module', 'webfact');
drupal_add_js($path . '/js/angular.min.js');
drupal_add_js($path . '/js/app.js');
return theme('webfacts');
}
/* (deprecated) example to build a list of nodes with EntityFieldQuery()
* and show in bootstrap. see bootstrap.tpl.php and
*/
function webfact_bootstrap($path='x', $action='y', $id=0) {
//dpm('webfact_bootstrap');
//drupal_set_message($action);
if (!is_numeric($id)) { // only numbers: security
#drupal_access_denied();
return array('#markup' => '<p>' . t('Error: arguments not valid') . ",
path=$path, 1=$action, 2=$id" . '</p>');
}
return theme('bootstrap');
}
/*
* hook for the /website/%/% page
*/
function webfact_arguments($path, $action, $id) {
//drupal_set_message($action);
if (!is_numeric($id)) { // only numbers: security
#drupal_access_denied();
return array('#markup' => '<p>' . t('Error: arguments not valid') . ",
path=$path, 1=$action, 2=$id" . '</p>');
}
#watchdog('webfact', 'id='. $id . ' action='. $action );
$w= new WebfactController;
//return $w->helloWorldPage();
return $w->arguments($action, $id); // send back render array
}
/*
* hook_node_delete()
* After deleting website meta data, delete the container too
* TODO:
* a) what about backup images?
* b) Only delete if category<>production, but the node will still be gone?
* ideally there needs to be a pre and post delete hook.
* Note: disabled in favour of using the batchAPI, e.g. see 'deleteall' in controller.php.
*/
/*
function webfact_node_delete($node) {
if ($node->type == 'website') {
//dpm($node);
$w = new WebfactController;
$w->deleteContainer($node->title);
drupal_set_message("Note: backup images of $node->title have NOT been deleted (should be removed automatically later)");
// todo: if there is a destination back to /something/nid, delete it
//if (isset($_GET['destination'])) { // go back where we were
// $from = drupal_parse_url($_GET['destination']);
//}
}
}*/
/*
* catch website or template edits
*/
function webfact_form_alter (&$form, &$form_state, $form_id) {
if ( ($form_id == 'website_node_form')
|| ($form_id == 'template_node_form') ) {
$form['#validate'][] = 'webfact_form_validate';
#dpm($form['#validate']);
}
}
/*
* validate fields: correct values
* todo: how to active this code only for new nodes?
*/
function webfact_form_validate($form, &$form_state) {
#dpm($form_state);
// hostname: force lowercase, strip everything except a-z/0-9
if (isset($form_state['values']['field_hostname']['und'][0]['value'])) {
$form_state['values']['field_hostname']['und'][0]['value']
#= preg_replace('/\s+/', '',
= preg_replace('/[^A-Za-z0-9]/', '',
strtolower($form_state['values']['field_hostname']['und'][0]['value']));
// Could also warn user:
// then set form_set_error('title', t('Invalid hostname.'));
}
#dpm($form_state['values']['field_hostname']);
// strip double quotes (which break nginx-gen) and trim
if (isset($form_state['values']['field_docker_environment']['und'])) {
#dpm($form_state['values']['field_docker_environment']['und']);
foreach ($form_state['values']['field_docker_environment']['und'] as &$envline) {
if (isset($envline['value']) && (strlen($envline['value'])>0) ) {
$envline['value'] = trim($envline['value']); // leading/trailing space
#dpm('old:' . $envline['value']);
$envline['value'] = preg_replace('/[\"]/', '', $envline['value']);
#dpm('new:' . $envline['value'] );
}
}
}
// remove leading/trailing space from fields with arrays
if (isset($form_state['values']['field_docker_volumes']['und'])) {
foreach ($form_state['values']['field_docker_volumes']['und'] as &$envline) {
if (isset($envline['value']) && (strlen($envline['value'])>0) ) {
$envline['value'] = trim($envline['value']);
}
}
}
if (isset($form_state['values']['field_docker_ports']['und'])) {
foreach ($form_state['values']['field_docker_ports']['und'] as &$envline) {
if (isset($envline['value']) && (strlen($envline['value'])>0) ) {
$envline['value'] = trim($envline['value']);
}
}
}
// fields with just one value:
$fields=array('field_hostname', 'field_docker_image', 'field_site_email', 'field_hostname');
foreach ($fields as $field) {
if (isset($form_state['values'][$field]['und'][0]['value'])) {
$form_state['values'][$field]['und'][0]['value'] =
trim($form_state['values'][$field]['und'][0]['value']);
#dpm('trimmed ' . $field);
}
}
}
/*
* hook_node_insert()
* when new websites are added, ensure a mountpoint
* such as /opt/sites/SITE exist
* Now /opt/sites must be mounted from the docker server itself,
* since this gui is running in a container too.
* todo: Developed for a specific use-case, make more generic?
*/
function webfact_node_insert($node) {
if ($node->type == 'website') {
// automatic volumes enabled?
if ( (variable_get('webfact_data_volume', 1) !== 1 ) &&
(variable_get('webfact_www_volume', 1) !== 1 ) ) {
return;
}
$sitesdir = variable_get('webfact_server_sitesdir', '/opt/sites/');
#dpm($node);
if (! file_exists($sitesdir)) {
drupal_set_message("No server folder $sitesdir exists (or is not accessible) for container mounts");
return;
} else if (! is_writable($sitesdir)) {
drupal_set_message("Server folder (for container mounts) $sitesdir cannot be written to.");
return;
}
$sitename = $node->field_hostname['und'][0]['value'];
if (! file_exists($sitesdir . '/' . $sitename) ) {
if (! mkdir($sitesdir . '/' . $sitename, 0775) ) {
drupal_set_message("Server folder $sitesdir/$sitename could not be created.");
}
}
// create folders for each volume: this is also done in controller at create time
// todo: remove it here?
if (variable_get('webfact_data_volume', 1) == 1 ) {
$folder = $sitesdir . '/' . $sitename . '/data';
if (! file_exists($folder) ) {
watchdog('webfact', "Create $folder");
if (! mkdir($folder, 0775) ) {
drupal_set_message("Server folder $folder could not be created.");
}
}
}
if (variable_get('webfact_www_volume', 0) == 1 ) {
$folder = $sitesdir . '/' . $sitename . '/www';
if (! file_exists($folder) ) {
watchdog('webfact', "Create $folder");
if (! mkdir($folder, 0775) ) {
drupal_set_message("Server folder $folder could not be created.");
}
}
}
} // if website
}