forked from kalamuna/terminatur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminatur.drush.inc
625 lines (553 loc) · 20.2 KB
/
terminatur.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
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
<?php
/**
* @file
* Terminatur: Drush commands to build pantheon sites locally
*/
// Constants
define('TERMINATUR_TMP_DIR', sys_get_temp_dir() . "/");
define('TERMINATUR_ENV', _terminatur_get_environment());
// Load up environment dependent stuff. Right now this is basiaclly just for
// kalastack/kalabox
// @todo: Eventually we will want to provide some basic handling for
// MAMP/Meglodon/etc
include_once('environment/terminatur.' . TERMINATUR_ENV . '.inc');
// Now that we have an environment load up other core stuff
include_once('terminatur.bootstrap.inc');
include_once('terminatur.code.inc');
include_once('terminatur.database.inc');
include_once('terminatur.files.inc');
include_once('terminatur.settings.inc');
include_once('terminatur.utils.inc');
/**
* Implementation of hook_drush_command().
*
* @See drush_parse_command() for a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
function terminatur_drush_command() {
$items = array();
// Some generic options needed for DB based commands
$db_options = array(
'user' => array(
'description' => 'A MySQL user with creds to create DBz.',
'example-value' => 'root',
),
'password' => array(
'description' => 'The password for the MySQL user.',
'example-value' => 'password',
),
'host' => array(
'description' => 'The host of the MySQL server.',
'example-value' => 'localhost',
),
'port' => array(
'description' => 'The port of the MySQL server.',
'example-value' => '3306',
),
);
$items['terminatur-code'] = array(
'description' => dt('Pulls down the code for a Pantheon site.'),
'aliases' => array('pullcode', 'pc'),
'examples' => array(
'drush pullcode mysite.dev' => 'Pulls down the code for the site at @terminatur.mysite.dev.',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['terminatur-data'] = array(
'description' => dt('Pulls down the database for a Pantheon site.'),
'aliases' => array('pulldata', 'pd'),
'examples' => array(
'drush pulldata sitename.dev' => 'Pulls down the database for a site at @terminatur.mysite.dev.',
),
'options' => array(
'db-backup-bucket' => array(
'description' => 'A specific backup bucket to download from, \'latest\', or \'new\'.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['terminatur-data']['options'] += $db_options;
$items['terminatur-files'] = array(
'description' => dt('Pulls down the files for a Pantheon site.'),
'aliases' => array('pullfiles', 'pf'),
'examples' => array(
'drush pullfiles sitename.dev' => 'Pulls down the files for a site at @terminatur.mysite.dev.',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['terminatur-pullsite'] = array(
'description' => dt('Pulls down the code, data and files for a Pantheon site. If on Kalabox creates hosts entry, vhost, alias and edits settings.php.'),
'aliases' => array('pullsite', 'ps'),
'examples' => array(
'drush pullsite sitename.dev' => 'Pulls down the code and data for a site at @terminatur.mysite.dev. Sets up server side conf on Kalabox.',
'drush pullsite sitename.dev --files' => 'Pulls down the code, data and files for a site at @terminatur.mysite.dev. Sets up server side conf on Kalabox.',
),
'options' => array(
'files' => array(
'description' => 'Use this flag to also pull down your files.',
),
'db-backup-bucket' => array(
'description' => 'A specific backup bucket to download from, \'latest\', or \'new\'.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['terminatur-pullsite']['options'] += $db_options;
$items['terminatur-crushsite'] = array(
'description' => dt('Removes a site completely.'),
'aliases' => array('crushsite', 'crush', 'wmd', 'cs'),
'examples' => array(
'drush crushsite sitename.dev' => 'Removes a site at @terminatur.mysite.dev or @kalabox.mysite.dev completely.',
),
'options' => array(),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['terminatur-crushsite']['options'] += $db_options;
$items['terminatur-newsite'] = array(
'description' => dt('Builds a new site locally.'),
'aliases' => array('newsite', 'ns'),
'examples' => array(
'drush newsite sitename' => 'Builds a new Panopoly site called sitename.',
'drush newsite sitename --profile=drupal7' => 'Builds a new vanilly Drupal 7 site called sitename.',
'drush newsite sitename --profile=openatrium --site-name="My Awesome Site"' => 'Builds a new Open Atrium site called sitename with human readable name "My Awesome Site".',
),
'options' => array(
'profile' => array(
'description' => 'Which distribution to install (use drupal7 for regular Drupal).',
'example-value' => 'panopoly,openatrium,drupal7',
),
'site-name' => array(
'description' => 'Human readable meta data',
'example-value' => '"My Awesome Site",BeastMode',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['terminatur-newsite']['options'] += $db_options;
// Include standard arguments for above commands
$common_args = array(
'sitename' => 'The sitename.dev part of @terminatur.sitename.dev.',
);
$common_options = array(
'destination' => array(
'description' => 'The destination of your webroot.',
'example-value' => '/var/www/',
),
);
foreach ($items as $key => $array) {
if (isset($array['arguments'])) {
$items[$key]['arguments'] = array_merge($items[$key]['arguments'], $common_args);
}
else {
$items[$key]['arguments'] = $common_args;
}
if (isset($array['options'])) {
$items[$key]['options'] = array_merge($items[$key]['options'], $common_options);
}
else {
$items[$key]['options'] = $common_options;
}
}
$items['terminatur-aliases'] = array(
'description' => dt('Grabs your Pantheon aliases and parses them for use with terminatur.'),
'aliases' => array('talias', 'ta'),
'examples' => array(
'drush talias [email protected] ' => 'Grabs your Pantheon aliases and Terminates them.',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['terminatur-set-git'] = array(
'description' => dt('Sets your gitconfig with your Pantheon profile info'),
'aliases' => array('tset'),
'examples' => array(
'drush tset' => 'Grabs your Pantheon aliases and Terminates them.',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
return $items;
}
/**
* Gets the codebase for the Pantheon site
*
* @todo: Right now this assumes you have git, maybe STFP or WGET in the future?
*
* @param boolean $sitename This is the "mysite.dev" part of @terminatur.mysite.dev
*/
function drush_terminatur_code($sitename = FALSE) {
// Grabs info about the site
if (!$sitename) {
$sitename = drush_get_option('sitename');
}
if (!$site = _terminatur_get_site($sitename)) {
return;
}
// Gets the webroot
$destination = drush_get_option('destination');
if (!$destination) {
$destination = TERMINATUR_DEFAULT_WEBROOT;
}
drush_log(dt("Downloading code..."), 'warning');
// Build code callback function
$get_code_func = '_terminatur_code_' . $site['terminatur']['code']['method'];
if (!$get_code_func($site, $destination)) {
return;
}
drush_log(dt("Code downloaded."), 'success');
}
/**
* Gets the database for the Pantheon site
*
* @param boolean $sitename This is the "mysite.dev" part of @terminatur.mysite.dev
*/
function drush_terminatur_data($sitename = FALSE) {
// Grabs info about the site
if (!$sitename) {
$sitename = drush_get_option('sitename');
}
if (!$site = _terminatur_get_site($sitename)) {
return;
}
// Gets some options or sets defaults
$destination = drush_get_option('destination');
if (!$destination) {
$destination = TERMINATUR_DEFAULT_WEBROOT;
}
$db_user = drush_get_option('user');
if (!$db_user) {
$db_user = TERMINATUR_DEFAULT_DB_USER;
}
$db_pass = drush_get_option('password');
if (!$db_pass) {
$db_pass = TERMINATUR_DEFAULT_DB_PASS;
}
$db_host = drush_get_option('host');
if (!$db_host) {
$db_host = TERMINATUR_DEFAULT_DB_HOST;
}
$db_port = drush_get_option('port');
if (!$db_port ) {
$db_port = TERMINATUR_DEFAULT_DB_PORT;
}
// Get the backup bucket to use.
_terminatur_get_data_bucket($site);
drush_log(dt("Downloading data..."), 'warning');
// Build code callback function
$get_data_func = '_terminatur_data_' . $site['terminatur']['database']['method'];
if (!$get_data_func($site, $destination, $db_user, $db_pass, $db_host, $db_port)) {
return;
}
drush_log(dt("Data downloaded."), 'success');
}
/**
* Downloads or refreshs pantheon files
* @param array or string $site an array of the sites info or the uparsed machine argument
* @return if fails
*/
function drush_terminatur_files($sitename = FALSE) {
// Grabs info about the site
if (!$sitename) {
$sitename = drush_get_option('sitename');
}
if (!$site = _terminatur_get_site($sitename)) {
return;
}
// Gets some options or sets defaults
$destination = drush_get_option('destination');
if (!$destination) {
$destination = TERMINATUR_DEFAULT_WEBROOT;
}
drush_log(dt("Downloading files..."), 'warning');
// Build files callback function
$get_files_func = '_terminatur_files_' . $site['terminatur']['files']['method'];
$get_files_func($site, $destination);
drush_log(dt("Files downloaded."), 'success');
}
/**
* Create a new and fully instantiated site or build one that already exist on pantheon
* @return when complete
*/
function drush_terminatur_pullsite($sitename = FALSE) {
// Grabs info about the site
if (!$sitename) {
$sitename = drush_get_option('sitename');
}
if (!$site = _terminatur_get_site($sitename)) {
return;
}
// Need MySQL
if ($site['terminatur']['database']['method'] != 'mysql') {
return drush_set_error('TERMINATUR_ERROR', 'You need at least MySQL for this to work.');
}
// Gets some options or sets defaults
$destination = drush_get_option('destination');
if (!$destination) {
$destination = TERMINATUR_DEFAULT_WEBROOT;
}
$db_user = drush_get_option('user');
if (!$db_user) {
$db_user = TERMINATUR_DEFAULT_DB_USER;
}
$db_pass = drush_get_option('password');
if (!$db_pass) {
$db_pass = TERMINATUR_DEFAULT_DB_PASS;
}
$db_host = drush_get_option('host');
if (!$db_host) {
$db_host = TERMINATUR_DEFAULT_DB_HOST;
}
$db_port = drush_get_option('port');
if (!$db_port ) {
$db_port = TERMINATUR_DEFAULT_DB_PORT;
}
$files = drush_get_option('files');
// Get the backup bucket to use.
_terminatur_get_data_bucket($site);
// Build callback functions
$get_data_func = '_terminatur_data_' . $site['terminatur']['database']['method'];
$get_code_func = '_terminatur_code_' . $site['terminatur']['code']['method'];
// Code & Data
if (!$get_code_func($site, $destination)) {
return;
}
if (!$get_data_func($site, $destination, $db_user, $db_pass, $db_host, $db_port)) {
return;
}
// Build vhost add callback function
$get_vhost_add_func = '_terminatur_vhost_add_' . TERMINATUR_ENV;
$get_vhost_add_func($site['machine-name'], $destination);
// Build host add callback function
$get_host_add_func = '_terminatur_host_add_' . TERMINATUR_ENV;
$get_host_add_func($site['machine-name'] . TERMINATUR_LOCAL_SUFFIX);
// Run additional environnment specific pullsite tasks
if (function_exists($get_pull_more_func = '_terminatur_pullsite_' . TERMINATUR_ENV)) {
$get_pull_more_func($site);
}
// If applicable, run this here so the user has a working site before we download files
if ($files) {
$get_files_func = '_terminatur_files_' . $site['terminatur']['files']['method'];
$get_files_func($site, $destination);
}
else {
// We need these directories set up for cache clear!
_terminatur_files_set_up_dirs($site, $destination);
}
// Clear cache
drush_invoke_process('@' . TERMINATUR_LOCAL_PREFIX . '.' . $site['machine-name'] . TERMINATUR_LOCAL_SUFFIX, 'cache-clear', array('all'), array('y'));
// Finish
drush_log('Site built!', 'success');
}
/**
* Obliterate a local site
*/
function drush_terminatur_crushsite($sitename = FALSE) {
// Confirm crush site
$confirm_choice = drush_confirm(dt('Do you really want to crush this site?'));
if (!$confirm_choice) {
return drush_set_error('TERMINATUR_ERROR', dt('Command cancelled.'));
}
// Grabs info about the site
if (!$sitename) {
$sitename = drush_get_option('sitename');
}
if (!$site = _terminatur_get_site($sitename, TRUE)) {
return false;
}
// Do a different check here because a local site does not have a DB method set
if (!drush_shell_exec('mysql --version')) {
return drush_set_error('TERMINATUR_ERROR', 'You need at least MySQL for this to work.');
}
// Gets some options or sets defaults
$destination = drush_get_option('destination');
if (!$destination) {
$destination = TERMINATUR_DEFAULT_WEBROOT;
}
$db_user = drush_get_option('user');
if (!$db_user) {
$db_user = TERMINATUR_DEFAULT_DB_USER;
}
$db_pass = drush_get_option('password');
if (!$db_pass) {
$db_pass = TERMINATUR_DEFAULT_DB_PASS;
}
$db_host = drush_get_option('host');
if (!$db_host) {
$db_host = TERMINATUR_DEFAULT_DB_HOST;
}
$db_port = drush_get_option('port');
if (!$db_port ) {
$db_port = TERMINATUR_DEFAULT_DB_PORT;
}
drush_log(dt("Removing site..."), 'warning');
// @todo: to be consistence maybe we want these to be abstracted out a bit?
if (!_terminatur_code_remove($site, $destination)) {
return;
}
if (!_terminatur_data_remove($site, $destination, $db_user, $db_pass, $db_host, $db_port)) {
return;
}
if (!_terminatur_files_remove($site, $destination)) {
return;
}
// Build vhost remove callback function
$get_vhost_remove_func = '_terminatur_vhost_remove_' . TERMINATUR_ENV;
$get_vhost_remove_func($site['machine-name']);
// Build host remove callback function
$get_host_remove_func = '_terminatur_host_remove_' . TERMINATUR_ENV;
// If we don't check for this and pass in just the suffix we can wipe
// needed entries
if (isset($site['machine-name'])) {
$get_host_remove_func($site['machine-name'] . TERMINATUR_LOCAL_SUFFIX);
}
// Remove local alias
// Build alias callback function
$get_alias_func = '_terminatur_alias_remove_' . TERMINATUR_ENV;
$get_alias_func($site['machine-name']);
// Done
drush_log('Site removed.', 'success');
}
/**
* Obliterate a local site
*/
function drush_terminatur_newsite($sitename = FALSE) {
// Grabs info about the site
if (!$sitename) {
$sitename = drush_get_option('sitename');
}
if (preg_match('/[^a-z0-9-]/', $sitename)) {
return drush_set_error('TERMINATUR_ERROR', 'New sitename can only be lowercase letters, numbers and hyphens.');
}
// Do a different check here because a local site does not have a DB method set
if (!drush_shell_exec('mysql --version')) {
return drush_set_error('TERMINATUR_ERROR', 'You need at least MySQL for this to work.');
}
// Gets some options or sets defaults
$destination = drush_get_option('destination');
if (!$destination) {
$destination = TERMINATUR_DEFAULT_WEBROOT;
}
$profile = drush_get_option('profile');
if (!$profile) {
$profile = TERMINATUR_DEFAULT_PROFILE;
}
$site_name = drush_get_option('site-name');
if (!$site_name) {
$site_name = TERMINATUR_DEFAULT_SITENAME;
}
$db_user = drush_get_option('user');
if (!$db_user) {
$db_user = TERMINATUR_DEFAULT_DB_USER;
}
$db_pass = drush_get_option('password');
if (!$db_pass) {
$db_pass = TERMINATUR_DEFAULT_DB_PASS;
}
$db_host = drush_get_option('host');
if (!$db_host) {
$db_host = TERMINATUR_DEFAULT_DB_HOST;
}
$db_port = drush_get_option('port');
if (!$db_port ) {
$db_port = TERMINATUR_DEFAULT_DB_PORT;
}
// Set options for Vanilla D7
$download = $profile;
if ($profile == 'drupal7') {
$download = 'drupal';
}
$install = $profile;
if ($profile == 'drupal7') {
$install = 'standard';
}
// This check has to happen here because we need $destination
if (is_dir($destination . $sitename)) {
return drush_set_error('TERMINATUR_ERROR', 'Already a site there, try another name.');
}
drush_log(dt("Building new site..."), 'warning');
// @todo: depending on what we do in the future, maybe NEWSITE creation should be abstracted out as well?
// This should verify a DB connection and create a DB
// We need to run this first before there is a settings.php
// We will run it again later to validate a new settings file
// We also need to build a faux site array to get it to do what we want.
$site = array();
$site['machine-name'] = $sitename;
if (!$databases = _terminatur_data_get_db_creds($site, $destination, $db_user, $db_pass, $db_host, $db_port)) {
return drush_set_error('TERMINATUR_ERROR', 'You have entered incorrect MySQL credentials.');
}
// Download the profile
// Set download command arguments
$dl_args = array($download);
// DL options
$dl_options = array(
'destination' => $destination,
'drupal-project-rename' => $sitename,
'y' => TRUE,
);
if (!drush_invoke_process('', 'pm-download', $dl_args, $dl_options)) {
return drush_set_error('TERMINATUR_ERROR', 'Could not download chosen profile.');
}
// Install options
$db_url = 'mysql://' . $databases['default']['default']['username'] . ':' . $databases['default']['default']['password'] . '@' . $databases['default']['default']['host'] . ':' . $databases['default']['default']['port'] . '/' . $databases['default']['default']['database'];
$account_pass = TERMINATUR_DEFAULT_USERPASS;
// Sadly have to do it this way because site-install doesnt let us specify a dir
// @todo: add in more options for admin name/email, etc
drush_shell_exec("cd " . $destination . $sitename . " && drush site-install " . $install . " -y --db-url=" . $db_url . " --account-pass=" . $account_pass . " --site-name='" . $site_name . "'");
// Update the new settings file
if (!$databases = _terminatur_data_get_db_creds($site, $destination, $db_user, $db_pass, $db_host, $db_port)) {
return drush_set_error('TERMINATUR_ERROR', 'You have entered incorrect MySQL credentials.');
}
// Now that we have an alias entry we should be able to grab the site array
$site = _terminatur_aliases_get_local_site($sitename . TERMINATUR_LOCAL_SUFFIX);
// Build file dirs to prevent htaccess errors
_terminatur_files_set_up_dirs($site, $destination);
// Build vhost add callback function
$get_vhost_add_func = '_terminatur_vhost_add_' . TERMINATUR_ENV;
$get_vhost_add_func($site['machine-name'], $destination);
// Build host add callback function
$get_host_add_func = '_terminatur_host_add_' . TERMINATUR_ENV;
$get_host_add_func($site['machine-name'] . TERMINATUR_LOCAL_SUFFIX);
// Clear cache
drush_invoke_process('@' . TERMINATUR_LOCAL_PREFIX . '.' . $site['machine-name'] . TERMINATUR_LOCAL_SUFFIX, 'cache-clear', array('all'), array('y'));
// Finish
drush_log('New site built!', 'success');
}
/**
* Refresh and reparse Pantheon alias file.
*/
function drush_terminatur_aliases() {
return terminatur_bootstrap(TRUE);
}
/**
* Set's your gitconfig with Pantheon profile info
*/
function drush_terminatur_set_git() {
// Do a different check here because a local site does not have a DB method set
if (!drush_shell_exec('git --version')) {
return drush_set_error('TERMINATUR_ERROR', 'You need git for this shit.');
}
$user = array();
$user = _terminatur_get_user();
// Set git config
drush_shell_exec("git config --global user.name \"" . $user->profile->firstname . " " . $user->profile->lastname . "\"");
drush_shell_exec("git config --global user.email " . $user->email);
// Yay!
drush_log('Git username and email have been set!', 'success');
}
/**
* Determine what environmental file to load
*/
function _terminatur_get_environment() {
// Check for Kalastack
if (isset($_SERVER['KALABOX']) && $_SERVER['KALABOX'] === 'on') {
define('KALABOX', TRUE);
return 'kalastack';
}
// elseif @todo some check for mamp
// elseif @todo some check for meglodon
// elseif @todo some check for devstack
// elseif @todo some check for drupalpro
else {
return 'default';
}
}