forked from pantheon-deprecated/terminus-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminus.drush.inc
2493 lines (2259 loc) · 76.3 KB
/
terminus.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
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
* Terminus: programatic access to the Pantheon Platform via Drush.
*/
/**
* Some constants we use.
*/
define('TERMINUS_HOST', 'terminus.getpantheon.com');
define('TERMINUS_PORT', 443);
date_default_timezone_set('UTC');
/**
* An easy function to override constants to allow development internally.
*
* Also bootstraps the included library functions, which are separate so people
* can use them outside the context of Drush.
*
* TODO: use for other settings.
*/
function terminus_bootstrap($validate = TRUE) {
// TODO: something to check that terminus is up to date!
include_once('terminus.backup.api.inc');
include_once('terminus.site.api.inc');
include_once('terminus.user.api.inc');
include_once('terminus.tunnel.inc');
include_once('terminus.workflow.api.inc');
include_once('terminus.code.api.inc');
include_once('terminus.organizations.api.inc');
include_once('terminus.art.inc');
$session_data = terminus_session_data();
// DRY session validation.
if ($validate) {
if (!isset($session_data['user_uuid'])) {
return drush_set_error("No user UUID found. Please authenticate first using 'drush pauth'");
}
}
// Common command flags.
$session_data['nocache'] = drush_get_option('nocache');
$session_data['json_output'] = drush_get_option('json');
if (isset($session_data['onebox']) || drush_get_option('onebox')) {
$session_data['onebox'] = TRUE;
}
return $session_data;
}
/**
* Implementation of hook_drush_command().
*
* List of available terminus commands.
*
* @return
* An associative array describing your command(s).
*/
function terminus_drush_command() {
$items = array();
$items['pantheon-logout'] = array(
'description' => "Clear any stored session data.",
'examples' => array(
'drush plogout',
),
'aliases' => array('plogout'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
// The 'pantheon-auth' command.
$items['pantheon-auth'] = array(
'description' => "Authenticate against the pantheon dashboard. Required before doing anything else.",
'arguments' => array(
'email' => 'Email address of dashboard account',
),
'options' => array(
'password' => array(
'description' => 'Optional: include password for script-friendly use.',
'example-value' => 'mypassword',
),
),
'examples' => array(
'drush pauth josh@getpantheon.com --password=mypassword' => 'Get authentication token.',
),
'aliases' => array('pauth'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
// The 'pantheon-sites' command.
$items['pantheon-sites'] = array(
'description' => 'List your Pantheon sites.',
'examples' => array(
'drush psites --nocache' => 'Get a fresh list of sites.',
),
'aliases' => array('psites'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-hostname-add'] = array(
'description' => "Add a hostname to a site you control.",
'arguments' => array(
'site' => 'The site UUID you are working on.',
'environment' => 'The environemnt (e.g. "live").',
'hostname' => 'The hostname (e.g. "www.mysite.com")',
),
'aliases' => array('psite-hostname-add'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-hostname-remove'] = array(
'description' => "Remove a hostname from a site you control.",
'arguments' => array(
'site' => 'The site UUID you are working on.',
'environment' => 'The environemnt (e.g. "live").',
'hostname' => 'The hostname (e.g. "www.mysite.com")',
),
'aliases' => array('psite-hostname-remove'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-products'] = array(
'description' => "Get the list of available Drupal product start-states.",
'aliases' => array('pproducts'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-create'] = array(
'description' => "Create a new site on Pantheon",
'arguments' => array(
'name' => 'Short name of the site to create. Will be part of the url.',
),
'options' => array(
'label' => array(
'description' => 'Human-friendly site label.',
),
'product' => array(
'description' => 'UUID of the product you want to start with (see pantheon-products).',
),
'organization' => array(
'description' => 'UUID of an organization you want the site to be a part of.',
),
'nopoll' => array(
'description' => 'Do not hang around and wait for the site to be ready. Useful for scripting a lot of spin-ups.',
),
),
'aliases' => array('psite-create'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-delete'] = array(
'description' => "Delete a site from Pantheon.",
'arguments' => array(
'site' => 'UUID of the site you want to delete.',
),
'aliases' => array('psite-delete'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-backups'] = array(
'description' => "List site backups (and exports).",
'arguments' => array(
'site' => 'UUID of the site you want to get backups for.',
'environment' => 'The environment (e.g. "live") you want to back up.',
),
'aliases' => array('psite-backups'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-get-backup'] = array(
'description' => "Get a download link to a specific site backup.",
'arguments' => array(
'site' => 'UUID of the site you want to get backups for.',
'environment' => 'The environment (e.g. "live") you want to back up.',
'bucket' => 'Bucket for the backup.',
'element' => 'Which part of the backup do you want?',
),
'aliases' => array('psite-get-backup'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-make-backup'] = array(
'description' => "Trigger an on-demand backup for a site/environment.",
'arguments' => array(
'site' => 'UUID of the site you want to make a backup for.',
'environment' => 'The environment (e.g. "live") you want to back up.',
),
'aliases' => array('psite-backup'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-download-backup'] = array(
'description' => "Download a backup file from a specific site.",
'arguments' => array(
'site' => 'UUID of the site you want to get backups for.',
'environment' => 'The environment (e.g. "live") you want to download a back up from.',
'bucket' => 'Bucket for the backup. Use "latest" for the most recent.',
'element' => 'Which part of the backup do you want? (e.g. database, files, code)',
'destination' => 'Where would you like the backup?'
),
'aliases' => array('psite-dl-backup'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-load-backup'] = array(
'description' => "Load db with a backup file from a specific site.",
'arguments' => array(
'site' => 'UUID or name of the site you want to get backups for.',
'environment' => 'The environment (e.g. "live") you want to use a backup from.',
'bucket' => 'Bucket for the backup. Use "latest" for the most recent.',
),
'aliases' => array('psite-load-backup'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-connection-mode'] = array(
'description' => "Set or retrieve the connection mode of a specific site/environment.",
'arguments' => array(
'site' => 'UUID or name of the site.',
'environment' => 'The dev or multidev environment you would like to target.',
'mode' => 'Connection mode like to set. (e.g. "sftp" or "git") Leave blank to retrieve the current mode.',
),
'aliases' => array('psite-cmode'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-commit'] = array(
'description' => 'Commit changes in an on-server-dev environment.',
'arguments' => array(
'site' => 'UUID or name of the site.',
'environment' => 'Environment to commit: a dev or multidev environment',
),
'options' => array(
'message' => 'Commit message',
),
'aliases' => array('psite-commit'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-diffstat'] = array(
'description' => 'Get a list of changes (diffstat) to be commited in a remote on-server-dev environment.',
'arguments' => array(
'site' => 'UUID or name of the site.',
'environment' => 'Environment to commit: a dev or multidev environment',
),
'aliases' => array('psite-diffs'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-notifications'] = array(
'description' => 'Get a list of notifications for a site to track ongoing jobs.',
'arguments' => array(
'site' => 'UUID or name of the site.',
),
'aliases' => array('psite-notifications'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-uuid'] = array(
'description' => "Get the site UUID based on the name.",
'arguments' => array(
'site' => 'UUID of the site.',
),
'examples' => array(
'drush psite-uuid mysite --nocache' => 'Get the UUID of your site.',
),
'aliases' => array('psite-uuid'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-name'] = array(
'description' => "Get the site name based on the UUID.",
'arguments' => array(
'name' => 'name of the site.',
),
'examples' => array(
'drush psite-name 12345678-1234-abcd-9876-fedcba09 --nocache' => 'Get the name of your site.',
),
'aliases' => array('psite-name'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-dashboard'] = array(
'description' => "Get the dashboard link for a site.",
'arguments' => array(
'site' => 'UUID of the site.',
),
'examples' => array(
'drush psite-dash mysite -y' => 'Open the dashboard for a site.',
),
'aliases' => array('psite-dash'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-service-level'] = array(
'description' => "Update the service level for the site.",
'arguments' => array(
'site' => 'UUID of the site.',
'service-level' => 'Service level to upgrade to.',
),
'examples' => array(
'drush psite-upgrade <site> <service-level>' => 'Open the dashboard for a site.',
),
'aliases' => array('psite-upgrade'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
/**
* Organization-scope functions.
*/
$items['pantheon-organizations'] = array(
'description' => "List your organization affiliations.",
'examples' => array(
'drush porgs --nocache' => 'Get a fresh list of sites.',
),
'aliases' => array('porgs'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-organization-sites'] = array(
'description' => "List the sites for an organization. Org admins only.",
'arguments' => array(
'organization' => 'UUID of the organization you want to use.',
),
'examples' => array(
'drush porg-sites --nocache' => 'Get a fresh list of sites.',
),
'aliases' => array('porg-sites'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-organization-site-add'] = array(
'description' => "Add a site to an organization. Org admins only.",
'arguments' => array(
'organization' => 'UUID of the organization.',
'site' => 'UUID of the organization.',
),
'examples' => array(
'drush porg-site-add <org> <site>' => 'Add a site to an organization.',
),
'aliases' => array('porg-site-add'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-organization-site-remove'] = array(
'description' => "Remove a site from an organization. Org admins only.",
'arguments' => array(
'organization' => 'UUID of the organization.',
'site' => 'UUID of the organization.',
),
'examples' => array(
'drush porg-site-remove <org> <site>' => 'Add a site to an organization.',
),
'aliases' => array('porg-site-remove'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
/**
* Tunnel Functions.
*/
$items['pantheon-site-tunnels'] = array(
'description' => "Get a list of open tunnels.",
'aliases' => array('psite-tunnels'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-tunnel'] = array(
'description' => "Opens a tunnel to a specific site/environment/service.",
'arguments' => array(
'site' => 'UUID or name of the site.',
'environment' => 'The target environment (e.g. "live").',
'service' => 'The service (e.g. "db" or "redis") to open a tunnel for. This defaults to "db".',
'port' => 'Local port to connect to.'
),
'aliases' => array('psite-tunnel'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-tunnel-close'] = array(
'description' => "Closes the tunnel to a specific site/environment/service.",
'arguments' => array(
'site' => 'UUID or name of the site. If left empty, all tunnels will be closed.',
'environment' => 'The target environment (e.g. "live"). If left empty, all site tunnels will be closed.',
'service' => 'The service (e.g. "mysql" or "redis") to open a tunnel for. If empty, all site/environment tunnels will be closed.'
),
'aliases' => array('psite-tclose'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-sql-dump'] = array(
'callback' => 'drush_sql_dump_execute',
'description' => 'Exports the Drupal DB as SQL using mysqldump or equivalent.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'examples' => array(
'drush sql-dump --result-file=../18.sql' => 'Save SQL dump to the directory above Drupal root.',
'drush sql-dump --skip-tables-key=common' => 'Skip standard tables. @see example.drushrc.php',
),
'options' => array(
'result-file' => array(
'description' => 'Save to a file. The file should be relative to Drupal root. If --result-file is provided with no value, then date based filename will be created under ~/drush-backups directory.',
'example-value' => '/path/to/file',
'value' => 'optional',
),
'skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
'structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
'tables-key' => 'A key in the $tables array. Optional.',
'skip-tables-list' => 'A comma-separated list of tables to exclude completely. Optional.',
'structure-tables-list' => 'A comma-separated list of tables to include for structure, but not data. Optional.',
'tables-list' => 'A comma-separated list of tables to transfer. Optional.',
'ordered-dump' => 'Use this option to output ordered INSERT statements in the sql-dump.Useful when backups are managed in a Version Control System. Optional.',
'create-db' => array('hidden' => TRUE, 'description' => 'Omit DROP TABLE statements. Postgres and Oracle only. Used by sql-sync, since including the DROP TABLE statements interfere with the import when the database is created.'),
'data-only' => 'Dump data without statements to create any of the schema.',
'ordered-dump' => 'Order by primary key and add line breaks for efficient diff in revision control. Also, faster rsync. Slows down the dump. Mysql only.',
'gzip' => 'Compress the dump using the gzip program which must be in your $PATH.',
),
'aliases' => array('psite-sql-dump'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-site-mount'] = array(
'description' => "Mounts an environment file system to a local directory.",
'arguments' => array(
'site' => 'UUID or name of the site. If left empty, all tunnels will be closed.',
'environment' => 'The target environment (e.g. "live"). If left empty, all site tunnels will be closed.',
'destination' => 'Where would you to mount the file system?'
),
'aliases' => array('psite-mount'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
/**
* Workflow.
*/
$items['pantheon-site-clone'] = array(
'description' => 'Clone content from one site environment to another.',
'arguments' => array(
'site' => 'UUID of the site containing content.',
'source' => 'The source environment (e.g. "live") from which content will be cloned.',
'target' => 'The target environment (e.g. "live") to which content will be cloned.',
),
'options' => array(
'db' => 'Clone database content.',
'files' => 'Clone files content.',
'update' => 'Run update.php after cloning database.',
),
'examples' => array(
'drush psite-clone site_uuid dev test' => 'Clone both database and files from dev to test.',
'drush psite-clone site_uuid dev test --db --update' => 'Clone only database from dev to test then run update.php.',
'drush psite-clone site_uuid dev test --files' => 'Clone only user files from dev to test.',
),
'aliases' => array('psite-clone'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
/**
* Utility Functions.
*/
$items['pantheon-aliases'] = array(
'description' => "Update the Pantheon Drush alias file at ~/.drush/pantheon.aliases.drushrc.php.",
'options' => array(
'destination' => array(
'description' => 'Specify the destination to save the alias file.',
),
),
'aliases' => array('paliases'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
$items['pantheon-pp'] = array(
'description' => "Direct pseudo-proxy interface. JSON only, for debugging.",
'arguments' => array(
'realm' => 'Are you asking about users or sites?',
'uuid' => 'The unique id of the thing you want to know about.',
'path' => 'Optional: path extension for more specific commands.',
),
'aliases' => array('pp'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
// Include standard options for all commands:
$common = array(
'nocache' => array(
'description' => 'Force a refresh of cached authentication session.',
),
'json' => array('description' => 'Return raw JSON if possible.'),
'onebox' => array('description' => 'Use onebox (Pantheon dev only).'),
);
foreach ($items as $key => $array) {
if (isset($array['options'])) {
$items[$key]['options'] = array_merge($items[$key]['options'], $common);
}
else {
$items[$key]['options'] = $common;
}
}
return $items;
}
/**
* @group Drush Commands
*
* These commands exercise various aspects of the API.
*/
/**
* Validate Pantheon site content clone.
*/
function drush_terminus_pantheon_site_clone_validate($site_uuid, $source, $target) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return drush_set_error('DRUSH_PSITE_CLONE_NO_SESSION', 'You must authenticate before using this command.');
}
if (!terminus_validate_uuid($site_uuid)) {
return drush_set_error('DRUSH_PSITE_CLONE_INVALID_UUID', 'You must specify a valid site uuid.');
}
if (!terminus_validate_environment($source)) {
return drush_set_error('DRUSH_PSITE_CLONE_INVALID_SOURCE', 'You must specify a valid source environment name.');
}
if (!terminus_validate_environment($target)) {
return drush_set_error('DRUSH_PSITE_CLONE_INVALID_TARGET', 'You must specify a valid target environment name.');
}
if (!drush_get_option('db') && drush_get_option('files') && drush_get_option('update')) {
return drush_set_error('DRUSH_PSITE_CLONE_INVALID_UPDATE', dt("You can't update the database if you're not cloning the database."));
}
}
/**
* Pantheon site content clone.
*/
function drush_terminus_pantheon_site_clone($site_uuid, $env_source, $env_target) {
// Figure out the various options.
$clone_db = $clone_files = FALSE;
if (!drush_get_option('db') && !drush_get_option('files')) {
$clone_db = $clone_files = TRUE;
}
else {
if (drush_get_option('db')) {
$clone_db = TRUE;
}
if (drush_get_option('files')) {
$clone_files = TRUE;
}
}
$clone_db_update = drush_get_option('update') ? 1 : 0;
// Build label to describe operation.
$content = array();
if ($clone_db) {
if ($clone_db_update) {
$content[] = 'database (then run update.php)';
}
else {
$content[] = 'database';
}
}
if ($clone_files) {
$content[] = 'user files';
}
$content_str = implode(' and ', $content);
// Confirm.
$confirm = drush_confirm(dt('Are you sure you want to clone @site_name @content_str from @source to @target?', array(
'@site_name' => terminus_site_uuid_to_name($site_uuid),
'@content_str' => $content_str,
'@source' => $env_source,
'@target' => $env_target,
)));
if (!$confirm) {
return;
}
// Clone database.
if ($clone_db) {
if ($clone_db_update) {
drush_log('Initiated database cloning then update.php.', 'notice');
}
else {
drush_log('Initiated database cloning.', 'notice');
}
terminus_api_environment_clone_database($site_uuid, $env_source, $env_target, $clone_db_update);
}
// Clone files.
if ($clone_files) {
drush_log('Initiated files cloning.', 'notice');
terminus_api_environment_clone_files($site_uuid, $env_source, $env_target);
}
}
/**
* User data about self.
*/
function drush_terminus_pantheon_whoami() {
$session_data = terminus_bootstrap();
// import to local scope variables
extract($session_data);
drush_print(dt("User UUID: !uuid", array('!uuid' => $user_uuid)));
drush_print(dt("Email: !email", array('!email' => $email)));
return TRUE;
}
/**
* Validation for the list of Pantheon sites.
*/
function drush_terminus_pantheon_sites_validate() {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return drush_set_error('DRUSH_PSITES_NO_SESSION', 'You must authenticate before using this command.');
}
drush_set_option('session_data', $session_data);
}
/**
* Get the list of Pantheon sites.
*/
function drush_terminus_pantheon_sites() {
extract(drush_get_option('session_data'));
$sites = terminus_user_site_list();
if (isset($json_output)) {
drush_print($sites);
return TRUE;
}
if (!terminus_validate_sites($sites)) {
return drush_set_error('DRUSH_PSITES_INVALID_RESPONSE', 'Unable to parse list of sites.');
}
// Display tabular output.
// TODO: refactor into re-usable display functions.
$sites = json_decode($sites);
$rows = array();
foreach ($sites as $site_uuid => $data) {
$i = $data->information;
$rows[$i->name] = array($i->name, $i->service_level, $site_uuid);
}
ksort($rows);
array_unshift($rows, array("Site", "Service Level", "UUID"));
drush_print_table($rows, TRUE);
}
/**
* Create a new site via the CLI.
*/
function drush_terminus_pantheon_site_create($name = FALSE) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return FALSE;
}
extract($session_data);
if (!$name) {
$name = terminus_session_select_data('site_name');
if (!$name) {
drush_log('You must supply a site name!', 'failed');
return FALSE;
}
}
// Construct payload. We don't want empty/null values.
$site_data = array();
$site_data['name'] = preg_replace("/[^A-Za-z0-9 -]/", '', $name);
if (drush_get_option('label')) {
$site_data['label'] = drush_get_option('label');
}
else {
$site_data['label'] = terminus_session_select_data('site_label');
}
if (drush_get_option('organization')) {
$site_data['organization'] = drush_get_option('organization');
}
else {
$site_data['organization'] = terminus_session_select_data('organization');
}
if (drush_get_option('product')) {
$site_data['product'] = drush_get_option('product');
}
else {
$site_data['product'] = terminus_session_select_data('product');
}
if (!$site_data['product']) {
drush_log('You must specify a starting state!', 'failed');
return FALSE;
}
drush_log("Queuing build.", "ok");
$result = terminus_request('user', $user_uuid, 'sites', 'POST', $site_data);
if (!$result) {
drush_log("Site create failed", "failed");
return FALSE;
}
if (isset($json_output) && isset($result['json']) && $result['json']) {
drush_print($result['json']);
return TRUE;
}
$site_uuid = json_decode($result['json']);
drush_log("Site is building now in your dashboard.", "ok");
drush_print("The new site's UUID is '". $site_uuid ."'");
drush_cache_clear_all('terminus-sites', 'pantheon', TRUE);
# Poll for completion.
if (drush_get_option('nopoll')) {
drush_log("No-poll option detected. Check your dashboard to see when this site is ready.");
return TRUE;
}
drush_print(dt('Waiting for site spinup to complete. You can cancel anytime and the process will continue in the background.'));
$ready = 0;
while (TRUE) {
echo ".";
sleep(5);
$result = terminus_api_site_bindings($site_uuid);
$bindings = json_decode($result['json']);
// Keep track of ready bindings.
if (!isset($bindings_ready)) {
foreach ($bindings as $bid => $data) {
$bindings_ready[$bid] = FALSE;
}
}
foreach ($bindings as $bid => $data) {
if ($data->ready && !$bindings_ready[$bid]) {
$bindings_ready[$bid] = TRUE;
echo ' ' . dt('@type step complete (@ready/@total).', array(
'@type' => $data->type,
'@ready' => ++$ready,
'@total' => count($bindings_ready),
));
}
}
$all_ready = TRUE;
foreach ($bindings_ready as $binding_ready) {
if (!$binding_ready) $all_ready = FALSE;
}
if ($all_ready) {
break;
}
}
# Poll for the sync code job to complete.
drush_print("Creating git repository and deploying code to dev.");
while (TRUE) {
echo ".";
sleep(5);
$notifications = terminus_notifications_list($site_uuid, 'chef_solo_sync_code');
if (count($notifications) > 0) {
break;
}
}
drush_print("Your site is now ready! Manage it in the dashboard:");
drush_print("https://dashboard.getpantheon.com/sites/$site_uuid");
return TRUE;
}
/**
* Get a site uuid based on the name.
*/
function drush_terminus_pantheon_site_uuid($site_name) {
terminus_bootstrap();
$site_uuid = terminus_get_site_uuid_by_name($site_name);
drush_print($site_uuid);
return TRUE;
}
/**
* Get a site name based on the uuid.
*/
function drush_terminus_pantheon_site_name($site_uuid) {
terminus_bootstrap();
$result = terminus_api_site_info($site_uuid);
$site = json_decode($result['json']);
drush_print($site->name);
return TRUE;
}
/**
* Get a dashboard link.
*/
function drush_terminus_pantheon_site_dashboard($site_uuid = FALSE) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return FALSE;
}
extract($session_data);
if (!$site_uuid = terminus_site_input($site_uuid, TRUE, FALSE)) {
drush_log('You must specify a site UUID', 'failed');
return FALSE;
}
drush_print("https://dashboard.getpantheon.com/sites/$site_uuid");
$open = drush_confirm("Would you like to open this link now?");
if ($open) {
exec("open https://dashboard.getpantheon.com/sites/$site_uuid");
}
return TRUE;
}
/**
* Delete a site via the CLI.
*/
function drush_terminus_pantheon_site_delete($site_uuid = FALSE) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return FALSE;
}
extract($session_data);
if (!$site_uuid = terminus_site_input($site_uuid, TRUE, FALSE)) {
drush_log('You must supply a site UUID', 'failed');
return FALSE;
}
$name = terminus_site_uuid_to_name($site_uuid);
$confirm = drush_confirm("Are you sure you want to delete $name ($site_uuid)?");
if (!$confirm) {
return TRUE;
}
$result = terminus_api_site_delete($site_uuid);
drush_log("Site deleted.", "ok");
drush_cache_clear_all('terminus-sites', 'pantheon');
return TRUE;
}
/**
* Add a hostname.
*/
function drush_terminus_pantheon_hostname_add($site_uuid = FALSE, $environment = FALSE, $hostname = FALSE) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return FALSE;
}
extract($session_data);
if (!$site_uuid = terminus_site_input($site_uuid, TRUE, TRUE)) {
drush_log('You must supply a site UUID', 'failed');
return FALSE;
}
if (!$environment) {
$environment = terminus_session_select_data('environment', $site_uuid);
if (!$environment) {
drush_log('You must supply an environment', 'failed');
return FALSE;
}
}
$hostname = drush_prompt(dt('Hostname (e.g. "www.mysite.com") you would like to add'), NULL, TRUE);
if (!preg_match('/(?=^.{1,254}$)(^(?:(?!\d|-)[a-z0-9\-]{1,63}(?<!-)\.)+(?:[a-z]{2,})$)/i', $hostname)) {
drush_log("'$hostname' is not a valid hostname.", 'failed');
return FALSE;
}
$result = terminus_api_site_environment_add_hostname($site_uuid, $environment, $hostname);
if ($result === FALSE) {
return FALSE;
}
drush_log("Added '$hostname' to the $environment environment", 'ok');
return TRUE;
}
/**
* Remove a hostname.
*/
function drush_terminus_pantheon_hostname_remove($site_uuid = FALSE, $environment = FALSE, $hostname = FALSE) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return FALSE;
}
extract($session_data);
if (!$site_uuid) {
$site_uuid = terminus_session_select_data('site_uuid');
if (!$site_uuid) {
drush_log('You must supply a site UUID', 'failed');
return FALSE;
}
}
if (!$environment) {
$environment = terminus_session_select_data('environment', $site_uuid);
if (!$environment) {
drush_log('You must supply an environment', 'failed');
return FALSE;
}
}
if (!$hostname) {
$hostname = terminus_session_select_data('hostname', $site_uuid, $environment);
if (!$hostname) {
drush_log('You must choose a hostname', 'failed');
return FALSE;
}
}
$result = terminus_api_site_environment_delete_hostname($site_uuid, $environment, $hostname);
if ($result === FALSE) {
return FALSE;
}
drush_log("Removed '$hostname' from the $environment environment", 'ok');
return TRUE;
}
/**
* Get a list of backups for a site.
*/
function drush_terminus_pantheon_site_backups($site_uuid = FALSE, $environment = FALSE) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return FALSE;
}
extract($session_data);
if (!$site_uuid = terminus_site_input($site_uuid, TRUE, TRUE)) {
drush_log('You must supply a site UUID', 'failed');
return FALSE;
}
if (!$environment) {
$environment = terminus_session_select_data('environment', $site_uuid);
if (!$environment) {
drush_log('You must supply an environment', 'failed');
return FALSE;
}
}
$result = terminus_api_backup_catalog($site_uuid, $environment);
$rows = array();
$backups = json_decode($result['json']);
foreach ($backups as $id => $a) {
if (!isset($a->filename)) {
continue;
}
$parts = explode('_', $id);
$type = strpos($id, 'backup') ? 'backup' : 'export';
$size = round($a->size/(1024*1024), 2) .' MB';
$rows[$a->filename] = array($type, date(DATE_RFC822, $a->timestamp), $parts[2], $parts[0] .'_'. $parts[1], $size);
}
krsort($rows);
if (drush_get_option('json')) {
drush_print(json_encode($rows));
return TRUE;
}
array_unshift($rows, array("Type", "Date", "Element", "Bucket", "Size"));
drush_print_table($rows, TRUE);
}
/**
* Get a list of backups for a site.
*/
function drush_terminus_pantheon_site_get_backup($site_uuid = FALSE, $environment = FALSE, $bucket = FALSE, $element = FALSE) {
$session_data = terminus_bootstrap();
if ($session_data === FALSE) {
return FALSE;
}
extract($session_data);
if (!$site_uuid = terminus_site_input($site_uuid, TRUE, TRUE)) {
drush_log('You must supply a site UUID', 'failed');
return FALSE;
}
if (!$environment) {
$environment = terminus_session_select_data('environment', $site_uuid);
if (!$environment) {
drush_log('You must supply an environment', 'failed');
return FALSE;
}
}
$result = terminus_api_backup_download_url($site_uuid, $environment, $bucket, $element);
if (isset($json_output) && isset($result['json']) && $result['json']) {
drush_print($result['json']);
return TRUE;
}
$data = json_decode($result['json']);
drush_print(dt('Signed url for the backup:'));
drush_print($data->url);
return TRUE;
}
/**
* Make a backup. Uses more generic backup api.
*/
function drush_terminus_pantheon_site_make_backup($site_uuid = FALSE, $environment = FALSE) {
$session_data = terminus_bootstrap();
if (!$session_data) {
drush_log('You are not authenticated', 'failed');
return FALSE;
}
extract($session_data);
if (!$site_uuid = terminus_site_input($site_uuid, TRUE, TRUE)) {
drush_log('You must supply a site UUID', 'failed');
return FALSE;
}
if (!$environment) {
$environment = terminus_session_select_data('environment', $site_uuid);
if (!$environment) {
drush_log('You must supply an environment', 'failed');
return FALSE;
}