-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathRoboFile.php
More file actions
1079 lines (958 loc) · 42.3 KB
/
RoboFile.php
File metadata and controls
1079 lines (958 loc) · 42.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
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
require_once 'vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Exception\RuntimeException;
/**
* This file provides commands to the robo CLI for managing development and
* testing of devshop.
* 1. Install robo CLI: http://robo.li/
* 2. Clone this repo and change into the directory.
* 3. Run `robo` to see the commands.
* 4. If you have drush, docker, and docker compose, you can launch a devshop
* with `robo up`
*
* Available commands:
*
* destroy Destroy all containers, docker volumes, and aegir
* configuration. help Displays help for a command launch
* Launch devshop after running prep:host and prep:source. Use --build to
* build new local containers. list Lists commands login
* Get a one-time login link to Devamster. logs Stream
* logs from the containers using docker-compose logs -f shell
* Enter a bash shell in the devmaster container. stop Stop
* devshop containers using docker-compose stop test Run all
* devshop tests on the containers. up Launch devshop
* containers using docker-compose up and follow logs. prepare
* prepare:containers Build aegir and devshop containers from the Dockerfiles. Detects your UID or you can pass as an argument. prepare:host Check for docker, docker-compose and drush. Install them if they are missing. prepare:sourcecode Clone all needed source code and build devmaster from the makefile.
*
* @see http://robo.li/
*/
class RoboFile extends \Robo\Tasks {
// Install this version first when testing upgrades.
const UPGRADE_FROM_VERSION = '1.0.0-rc4-testing';
const UPGRADE_FROM_PROVISION_VERSION = '7.x-3.10';
// The version of docker-compose to suggest the user install.
const DOCKER_COMPOSE_VERSION = '1.10.0';
// Defines where devmaster is installed. 'aegir-home/devmaster-$DEVSHOP_LOCAL_VERSION'
const DEVSHOP_LOCAL_VERSION = '1.x';
// Defines the URI we will use for the devmaster site.
const DEVSHOP_LOCAL_URI = 'devshop.local.computer';
// DevShop application user name.
protected $devshopInstall = "ansible-playbook /usr/share/devshop/docker/playbook.server.yml --tags install-devmaster --extra-vars \"devmaster_skip_install=false\"";
protected $devshopUsername = "aegir";
use \Robo\Common\IO;
/**
* @var The path to devshop root. Used for upgrades.
*/
private $devshop_root_path;
/**
* Map of $opts keys to $_SERVER variables.
*
* SERVER vars are set for the "docker-compose" process.
*
* @var array
*/
private $serverOptionsMap = [
'verbose' => 'ANSIBLE_VERBOSITY',
'vars' => 'ANSIBLE_EXTRA_VARS',
'tags' => 'ANSIBLE_TAGS',
'skip-tags' => 'ANSIBLE_SKIP_TAGS',
'playbook' => 'ANSIBLE_PLAYBOOK',
'roles-path' => 'ANSIBLE_ROLES_PATH',
'config' => 'ANSIBLE_CONFIG',
// Used in docker compose image.
'docker-image' => 'DEVSHOP_DOCKER_IMAGE',
'from' => 'FROM_IMAGE',
'os' => 'OS_VERSION',
'dockerfile' => 'DOCKERFILE',
];
/**
* Map of Symfony Console verbosity to Ansible Verbosity value.
* @var array
*/
private $ansibleVerbosityMap = [
OutputInterface::VERBOSITY_NORMAL => 0,
OutputInterface::VERBOSITY_VERBOSE => 1,
OutputInterface::VERBOSITY_VERY_VERBOSE => 2,
OutputInterface::VERBOSITY_DEBUG => 3,
];
/**
* Merge robo $opts and $_SERVER environment vars into the runtime environment
* of the docker-compose calls.
*
* @param array $opts
*/
/**
* @param array $opts The robo options array.
* @param array $env The initial environment.
*
* @return array
*/
private function generateEnvironment(array $opts, array $env = []) {
$env += $this->optionsToArray($opts['environment']);
$env['ANSIBLE_VERBOSITY'] = $this->ansibleVerbosityMap[$this->output()->getVerbosity()];
foreach ($this->serverOptionsMap as $opt_name => $var_name) {
// Use $_SERVER var if it exists...
$env[$var_name] = !empty($_SERVER[$var_name])? $_SERVER[$var_name]:
// or use --options value if it exists.
// If not, set to empty string.
(!empty($opts[$opt_name])? $opts[$opt_name]: '');
}
return $env;
}
/**
* Append _ARG to all variable names of an environment vars array.
* @param bool new Set to "true" to reset environment with only the new _ARG values.
* @return array
*/
private function generateEnvironmentArgs(array $opts, $new = false) {
// Convert opts to environment vars.
$environment = $this->generateEnvironment($opts);
// Load default environment, either empty or from existing.
$return_env = $new? []: $environment;
// Append _ARG to all environment variable names.
foreach ($environment as $name => $value) {
$return_env["{$name}_ARG"] = $value;
}
return $return_env;
}
public function __construct()
{
$this->git_ref = trim(str_replace('refs/heads/', '', shell_exec("git describe --tags --exact-match 2> /dev/null || git symbolic-ref -q HEAD 2> /dev/null")));
if (empty($this->git_ref) && !empty($_SERVER['GITHUB_REF'])) {
$this->git_ref = $_SERVER['GITHUB_REF'];
}
// Tell Provision power process to print output directly.
putenv('PROVISION_PROCESS_OUTPUT=direct');
}
//
// /**
// * Launch devshop after running prep:host and prep:source. Use --build to
// * build new local containers.
// *
// * If you only run one command, run this one.
// */
// public function launch($opts = ['build' => 0]) {
// $this->prepareHost();
// $this->prepareSourcecode();
//
// if ($opts['build']) {
// $this->prepareContainers();
// }
//
// $this->up(['follow' => TRUE]);
// }
/**
* Check for docker, docker-compose and drush. Install them if they are
* missing.
*/
public function prepareHost() {
// Check for docker
$this->say('Checking for Docker...');
if ($this->taskExec('docker info')
->printOutput(FALSE)
->run()
->wasSuccessful()) {
$this->_exec('docker -v');
$this->say('Docker detected.');
}
else {
$this->say('Could not run docker command. Find instructons for installing at https://www.docker.com/products/docker');
throw new RuntimeException('Unable to continue.');
}
// Check for docker-compose
$this->say('Checking for docker-compose...');
if ($this->_exec('docker-compose -v')->wasSuccessful()) {
$this->say('docker-compose detected.');
}
else {
$this->yell('Could not run docker-compose command.', 40, 'red');
$this->say("Run the following command as root to install it or see https://docs.docker.com/compose/install/ for more information.");
$this->say('curl -L "https://github.com/docker/compose/releases/download/' . self::DOCKER_COMPOSE_VERSION . '/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose');
throw new RuntimeException('Unable to continue.');
}
}
private $repos = [
'provision' => 'http://git.drupal.org/project/provision.git',
'aegir-home/.drush/commands/registry_rebuild' => 'http://git.drupal.org/project/registry_rebuild.git',
'documentation' => 'http://github.com/opendevshop/documentation.git',
];
/**
* Clone all needed source code and build devmaster from the makefile.
*
* @option no-dev Use build-devmaster.make instead of the development makefile.
* @option devshop-version The directory to put the
* @option make Defaults to "profile" to populate the makefile into the ./devmaster folder. Use --make=drupal to build the entire ./aegir-home/devmaster-1.x folder.
*/
public function prepareSourcecode($opts = [
'no-dev' => FALSE,
'devshop-version' => '1.x',
'test-upgrade' => FALSE,
'make' => 'profile',
]) {
if (empty($this->git_ref)) {
parent::yell("Preparing Sourcecode: Branch Unknown.");
}
else {
parent::yell("Preparing Sourcecode: Branch $this->git_ref");
}
if ($opts['devshop-version'] == NULL) {
$opts['devshop-version'] = $this->git_ref;
}
$this->devshop_root_path = __DIR__;
// Create the Aegir Home directory.
if (file_exists($this->devshop_root_path . "/aegir-home/.drush/commands")) {
$this->say($this->devshop_root_path . "/aegir-home/.drush/commands already exists.");
}
else {
$this->taskExecStack()
->exec("mkdir -p {$this->devshop_root_path}/aegir-home/.drush/commands")
->run();
}
// Clone all git repositories.
foreach ($this->repos as $path => $url) {
if (file_exists($this->devshop_root_path . '/' . $path)) {
$this->say("$path already exists.");
}
else {
$this->taskGitStack()
->cloneRepo($url, $this->devshop_root_path . '/' . $path)
->run();
}
// Checkout provision to the 7.x-3.x-devshop branch.
if ($path == 'provision') {
$this->taskGitStack()
->dir($this->devshop_root_path . '/' . $path)
->checkout('7.x-3.x-devshop')
->run();
}
}
// If we want to just populate modules into /devmaster folder...
if ($opts['make'] == 'profile') {
// Populate devmaster install profile with contrib code.
$makefile_path = 'devmaster/drupal-org.make';
$make_destination = 'devmaster/';
if (file_exists('devmaster/modules/contrib')) {
$this->say("Path 'devmaster/modules/contrib' already exists.");
}
else {
$this->yell("Populating devmaster profile with contrib code from $makefile_path ...");
$result = $this->_exec("bin/drush make {$makefile_path} {$make_destination} --working-copy --no-gitinfofile --no-core --contrib-destination=.");
if (!$result->wasSuccessful()) {
throw new \RuntimeException("Drush make failed with the exit code " . $result->getExitCode());
}
}
}
// Or if a whole Drupal build is needed.
elseif ($opts['make'] == 'drupal') {
// Run drush make to build the devmaster stack.
$makefile_path = $opts['no-dev']? 'build-devmaster.make': "build-devmaster-dev.make.yml";
$make_destination = $this->devshop_root_path . "/aegir-home/devmaster-" . $opts['devshop-version'];
// Append the desired devshop root path.
$makefile_path = $this->devshop_root_path . '/' . $makefile_path;
if (file_exists($make_destination)) {
$this->say("Path {$make_destination} already exists.");
}
else {
$this->yell("Building devmaster from makefile $makefile_path to $make_destination");
$result = $this->_exec("bin/drush make {$makefile_path} {$make_destination} --working-copy --no-gitinfofile");
if (!$result->wasSuccessful()) {
throw new \RuntimeException("Drush make failed with the exit code " . $result->getExitCode());
}
}
}
// Set git remote urls
if ($opts['no-dev'] == FALSE) {
$devshop_ssh_git_url = "[email protected]:opendevshop/devshop.git";
if ($this->taskExec("git remote set-url origin $devshop_ssh_git_url")->run()->wasSuccessful()) {
$this->yell("Set devshop git remote 'origin' to $devshop_ssh_git_url!");
}
else {
$this->say("<comment>Unable to set devshop git remote to $devshop_ssh_git_url !</comment>");
}
// if ($this->taskExec("cd {$make_destination}/profiles/devmaster && git remote set-url origin $devmaster_ssh_git_url && git remote set-url origin --add $devmaster_drupal_git_url")->run()->wasSuccessful()) {
// $this->yell("Set devmaster git remote 'origin' to $devmaster_ssh_git_url and added remote drupal!");
// }RuntimeException
// else {
// $this->say("<comment>Unable to set devmaster git remote to $devmaster_ssh_git_url !</comment>");
// }
// // Check for drupal remote
// if ($this->taskExec("cd {$make_destination}/profiles/devmaster && git remote get-url drupal")->run()->wasSuccessful()) {
// $this->say('Git remote "drupal" already exists in devmaster.');
// }
// // If remote does not exist, add it.
// elseif ($this->taskExec("cd {$make_destination}/profiles/devmaster && git remote add drupal $devmaster_drupal_git_url")->run()->wasSuccessful()) {
// $this->yell("Added 'drupal' git remote and added git.drupal.org as a second push target on origin!");
// }
// else {
// $this->say("<comment>Unable to add 'drupal' git remote and add git.drupal.org as a second push target on origin!</comment>");
// }
}
}
/**
* Build devshop containers.
*
* By default, `robo prepare:containers` will build a new container image
* using the 'Dockerfile' using FROM 'devshop/server:latest'. This shortens
* build times because the image was pre-built on docker hub.
*
* To force a new local build of the 'devshop/server' container image from
* scratch, use the '--from' option to specify a full docker image string or
* the `--os` option to use a `geerlingguy/docker-*-ansible` image.
*
* For example:
*
* robo up --os centos7
*
* will build the container from geerlingguy/docker-centos7-ansible.
*
* @example bin/robo prepare:containers
*
* @param $user_uid Pass a UID to build the image with. Defaults to the UID of the user running `robo`
*
* @option $tag The string to tag the resulting container with.
* @option $from The image to use to build the docker image FROM. Ignored if "os" is set.
* @option $dockerfile The dockerfile to use.
* @option $os An OS "slug" for any of the geerlingguy/docker-*-ansible images: https://hub.docker.com/u/geerlingguy/
* @option $vars Ansible vars to pass to --extra-vars option.
* @option $tags Ansible tags to pass to --tags option.
* @option $skip_tags Ansible tags to pass to --skip-tags option.
* @option $playbook Ansible tags to pass to ansible-playbook command.
* @option install-at-runtime Launch bare containers and then install devshop.
*/
public function prepareContainers($user_uid = NULL, $hostname = 'devshop.local.computer', $opts = [
'docker-image' => 'devshop/server:local',
'from' => NULL,
'dockerfile' => 'Dockerfile',
'os' => 'ubuntu1804',
'vars' => '',
'tags' => '',
'skip-tags' => '',
'playbook' => 'roles/server.playbook.yml',
'environment' => [],
'roles-path' => '/usr/share/devshop/roles',
'config' => '/usr/share/devshop/ansible.cfg',
'install-at-runtime' => FALSE,
]) {
// Define docker-image (name for the "image" in docker-compose)
// Set FROM_IMAGE and DEVSHOP_DOCKER_IMAGE if --os option is used. (and --from was not used)
if (empty($opts['from']) && !empty($opts['os'])) {
$opts['from'] = "geerlingguy/docker-{$opts['os']}-ansible";
$opts['docker-image'] = 'devshop/server:local-' . $opts['os'];
}
// Append the absolute path in the container.
$opts['playbook'] = '/usr/share/devshop/' . $opts['playbook'] ;
$this->yell('Building DevShop Container from: ' . $opts['from'], 40, 'blue');
// Block anything from running on build.
// @TODO: Figure out why centos can't enable service in build phase.
if ($opts['os'] == 'centos7' || $opts['install-at-runtime']) {
$opts['tags'] = $_SERVER['ANSIBLE_TAGS'] = 'none';
$opts['skip-tags'] = $_SERVER['ANSIBLE_SKIP_TAGS'] = '';
if ($opts['os'] == 'centos7') {
$this->yell('CENTOS DETECTED in RUNTIME. Running full playbook in container.', 40, 'red');
}
else {
$this->yell('--install-at-runtime option detected. Skipping build in container.', 40, 'red');
}
$this->yell('CENTOS DETECTED in BUILDTIME. Skipping playbook run in image build.', 40, 'red');
}
// Runtime Environment for the docker-compose build command.
$env_build = $this->generateEnvironmentArgs($opts);
// Determine current UID.
if (is_null($user_uid)) {
$env_build['DEVSHOP_USER_UID_ARG'] = trim(shell_exec('id -u'));
}
$provision_io = new \ProvisionOps\Tools\Style($this->input(), $this->output());
$process = new \ProvisionOps\Tools\PowerProcess('docker-compose build --pull --no-cache', $provision_io);
$process->setEnv($env_build);
$process->disableOutput();
$process->setTimeout(null);
$process->setTty(!empty($_SERVER['XDG_SESSION_TYPE']) && $_SERVER['XDG_SESSION_TYPE'] == 'tty');
// @TODO: Figure out why PowerProcess::mustRun() fails so miserably: https://github.com/opendevshop/devshop/pull/541/checks?check_run_id=518074346#step:7:45
$process->run();
}
/**
* Launch devshop in a variety of ways. Useful for local development and CI
* testing.
*
* Builds a container to match the local user to allow write permissions to
* Aegir Home.
*
* Examples:
*
* robo up
* Launch a devshop in containers using docker-compose.
*
* robo up --test
* Launch then test a devshop in a single process.
*
* robo up --test
* Launch, upgrade, then test a devshop in a single process.
*
* robo up --mode=install.sh --test
* Launch an OS container, then install devshop using install.sh, then run
* tests.
*
* robo up --mode=manual
* Just launch the container. Allows you to manually run the install.sh script.
*
* @option $test Run tests after containers are up and devshop is installed.
* @option $test-upgrade Install an old version, upgrade it to this version,
* then run tests.
* @option $mode Set to 'install.sh' to use the install.sh script for setup.
* @option $user-uid Override the detected current user's UID when building
* containers.
* @option $xdebug Set this option to launch with an xdebug container.
* @option no-dev Use build-devmaster.make instead of the development makefile.
* @option $build Run `robo prepare:containers` to rebuild the container first.
* @option os-version An OS "slug" for any of the geerlingguy/docker-*-ansible images: https://hub.docker.com/u/geerlingguy/
* @option environment pass an environment variable to docker-compose in the form --environment NAME=VALUE
* @option volumes Set to TRUE to use the docker-compose.volumes.yml file to map local folders into the container.
* @option install-at-runtime Launch bare containers and then install devshop.
*/
public function up($docker_command = 'devshop-ansible-playbook', $opts = [
'follow' => 1,
'test' => FALSE,
'test-upgrade' => FALSE,
// Set 'mode' => 'install.sh' to run a traditional OS install.
'mode' => 'docker-compose',
'user-uid' => NULL,
'disable-xdebug' => TRUE,
'no-dev' => FALSE,
'devshop-version' => '1.x',
'build' => FALSE,
'skip-source-prep' => FALSE,
'skip-install' => FALSE,
'os' => 'ubuntu1804',
'docker-image' => 'devshop/server:local',
'from' => NULL,
'vars' => '',
'tags' => '',
'skip-tags' => '',
'file' => 'Dockerfile',
'playbook' => 'roles/server.playbook.yml',
'roles-path' => '/usr/share/devshop/roles',
'config' => '/usr/share/devshop/ansible.cfg',
'local' => FALSE,
'environment' => [],
'volumes' => FALSE,
'install-at-runtime' => FALSE,
]) {
// Define docker-image (name for the "image" in docker-compose.
// Set FROM_IMAGE and DEVSHOP_DOCKER_IMAGE if --os option is used. (and --from was not used)
if (empty($opts['from']) && !empty($opts['os'])) {
$opts['from'] = "geerlingguy/docker-{$opts['os']}-ansible";
$opts['docker-image'] = 'devshop/server:local-' . $opts['os'];
}
// Check for tools
$this->prepareHost();
if (empty($this->devshop_root_path)) {
$this->devshop_root_path = __DIR__;
}
if (empty($this->git_ref)) {
parent::yell("Launching DevShop: Branch Unknown.");
}
else {
parent::yell("Launching DevShop: Branch $this->git_ref");
}
if ($opts['devshop-version'] == NULL) {
$opts['devshop-version'] = $this->git_ref;
}
// Determine current UID.
if (is_null($opts['user-uid'])) {
$opts['user-uid'] = trim(shell_exec('id -u'));
}
// Build the image if --build option specified, or if the image doesn't exist yet locally.
// If we don't, docker-compose up will automatically build it, but without these options.
// Run a "docker-compose pull" here confirms that the remote container by this name exists, and gets us a local copy.
$docker_image_exists_remotely = $this->_exec("docker pull {$opts['docker-image']}")->wasSuccessful();
// The image was just pulled, so this should always be true if $docker_image_exists_remotely is true.
$docker_image_exists_locally = $this->_exec("docker inspect {$opts['docker-image']} > /dev/null")->wasSuccessful();
// If --build option is used, or if docker image does not exist anywhere, build it with "local-$OS" tag
if ($opts['build'] || !$docker_image_exists_remotely && !$docker_image_exists_locally) {
$this->yell("Docker Image {$opts['docker-image']} was not found on this system or on docker hub. Building it...");
$this->prepareContainers($opts['user-uid'], 'devshop.local.computer', $opts);
}
// Warn the user that this container is not being built.
elseif (!$opts['build'] && $docker_image_exists_locally) {
$this->yell("Docker image {$opts['docker-image']} was found locally. Launching that container image. Use --build to rebuild it.", 40, "yellow");
}
// @TODO: Figure out why centos can't enable service in build phase.
if ($opts['os'] == 'centos7' || $opts['install-at-runtime']) {
// Set tags to all so it does a full install at runtime.
$opts['tags'] = $_SERVER['ANSIBLE_TAGS'] = 'all';
$opts['skip-tags'] = $_SERVER['ANSIBLE_SKIP_TAGS'] = 'none';
if ($opts['os'] == 'centos7') {
$this->yell('CENTOS DETECTED in RUNTIME. Running full playbook in container.', 40, 'red');
}
else {
$this->yell('--install-at-runtime option detected. Running full playbook in container.', 40, 'red');
}
}
if ($opts['mode'] == 'docker-compose') {
// Volumes
if ($opts['volumes']) {
$this->yell('Volume mounts requested. Adding docker-compose.volumes.yml');
$this->say(' - ' . __DIR__ . '/aegir-home to /var/aegir');
$this->say(' - ' . __DIR__ . '/devmaster to /var/aegir/devmaster-1.x/profiles/devmaster');
// Set COMPOSE_FILE to include volumes.
putenv('COMPOSE_FILE=docker-compose.yml:docker-compose.volumes.yml');
if (!file_exists('aegir-home/devmaster-' . $this::DEVSHOP_LOCAL_VERSION) && !$opts['skip-source-prep']) {
$this->io()->warning('The aegir-home folder not present. Running prepare source code command.');
$this->prepareSourcecode($opts);
}
}
$cmd[] = "docker-compose up --detach --force-recreate";
// Test commands must be run as application user.
// The `--test` command is run in GitHub Actions.
$test_command = '';
if ($opts['test']) {
// Do not run a playbook on docker-compose up, because it will launch as a separate process and we won't know when it ends.
$cmd[]= "docker-compose exec -T devshop service supervisord stop";
$test_command = "/usr/share/devshop/tests/devshop-tests.sh";
}
// @TODO: The `--test-upgrade` command is NOT YET run in GitHub Actions.
// The PR with the update hook can be used to finalize upgrade tests: https://github.com/opendevshop/devshop/pull/426
elseif ($opts['test-upgrade']) {
$test_command = "/usr/share/devshop/tests/devshop-tests-upgrade.sh";
}
else {
if ($opts['follow']) {
$cmd[] = "docker-compose logs -f";
}
else {
$cmd[] = "docker-compose logs";
}
}
// Runtime Environment for the $cmd list.
$env_run = $this->generateEnvironmentArgs($opts);
// Run a secondary command after the docker command.
if ($test_command) {
$env_run['DOCKER_COMMAND_POST'] = $test_command;
}
// Override the docker commmand.
$env_run['DOCKER_COMMAND'] = $docker_command;
// @TODO: Write to .env file so user does not have to keep using CLI args.
if (!empty($cmd)) {
foreach ($cmd as $command) {
$provision_io = new \ProvisionOps\Tools\Style($this->input, $this->output);
$process = new \ProvisionOps\Tools\PowerProcess($command, $provision_io);
$process->setEnv($env_run);
$isTty = !empty($_SERVER['XDG_SESSION_TYPE']) && $_SERVER['XDG_SESSION_TYPE'] == 'tty';
$process->setTty($isTty);
$process->setTimeout(NULL);
$process->disableOutput();
// @TODO: Figure out why PowerProcess::mustRun() fails so miserably: https://github.com/opendevshop/devshop/pull/541/checks?check_run_id=518074346#step:7:45
// $process->mustRun();
$process->run();
}
return;
}
}
// @TODO: Leaving here until the "Upgrade Test" is migrated to the new pattern.
// elseif ($opts['mode'] == 'install.sh' || $opts['mode'] == 'manual') {
//
// $init_map = [
// 'centos:7' => '/usr/lib/systemd/systemd',
// 'ubuntu:14.04' => '/sbin/init',
// 'geerlingguy/docker-ubuntu1404-ansible' => '/sbin/init',
// 'geerlingguy/docker-ubuntu1604-ansible' => '/lib/systemd/systemd',
// 'geerlingguy/docker-ubuntu1804-ansible' => '/lib/systemd/systemd',
// 'geerlingguy/docker-centos7-ansible' => '/usr/lib/systemd/systemd',
// ];
//
// $init = isset($init_map[$opts['install-sh-image']])? $init_map[$opts['install-sh-image']]: '/sbin/init';
//
// # This is the list of test sites, set in .travis.yml.
// # This is so requests to these sites go back to localhost.
// if (empty($_SERVER['SITE_HOSTS'])) {
// $_SERVER['SITE_HOSTS'] = 'devshop.local.computer';
// }
//
// # Launch Server container
// if (!$this->taskDockerRun($opts['install-sh-image'])
// ->name('devshop_container')
// ->volume($this->devshop_root_path, '/usr/share/devshop')
// ->volume($this->devshop_root_path . '/aegir-home', '/var/aegir')
// ->volume($this->devshop_root_path . '/roles', '/etc/ansible/roles')
// ->volume($this->devshop_root_path . '/provision', '/var/aegir/.drush/commands/provision')
// ->option('--hostname', 'devshop.local.computer')
// ->option('--add-host', '"' . $_SERVER['SITE_HOSTS'] . '":127.0.0.1')
// ->option('--volume', '/sys/fs/cgroup:/sys/fs/cgroup:ro')
// ->option('-t')
// ->publish(80,80)
// ->detached()
// ->privileged()
// ->env('COMPOSE_FILE', 'docker-compose.tests.yml')
// ->env('GITHUB_TOKEN', $_SERVER['GITHUB_TOKEN']?: '')
// ->env('TERM', 'xterm')
// ->env('GITHUB_REF', $_SERVER['GITHUB_REF'])
// ->env('AEGIR_USER_UID', $opts['user-uid'])
// ->env('PATH', "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/share/devshop/bin")
// ->exec('/usr/share/devshop/tests/run-tests.sh')
// ->exec($init)
// ->run()
// ->wasSuccessful()) {
// throw new RuntimeException('Docker Run failed.');
// }
//
// # Install mysql first to ensure it is started.
// if ($opts['install-sh-image'] == 'ubuntu:14.04') {
// if (!$this->taskDockerExec('devshop_container')
// ->exec("sed -i 's/101/0/' /usr/sbin/policy-rc.d")
// ->run()
// ->wasSuccessful()
// ) {
// throw new RuntimeException('Set init policy failed.');
// }
// }
// elseif ($opts['install-sh-image'] == 'geerlingguy/docker-ubuntu1604-ansible') {
//// @TODO: If this is the cause of wonkiness, let's not install dbus just for testing. There are better ways to set hostname.
// // Hostname install fails without dbus, so I am told: https://github.com/ansible/ansible/issues/25543
//// if (!(
//// $this->taskDockerExec('devshop_container')
//// ->exec("apt-get update")
//// ->run()
//// ->wasSuccessful()
//// && $this->taskDockerExec('devshop_container')
//// ->exec("apt-get install dbus -y")
//// ->env('DEBIAN_FRONTEND', 'noninteractive')
//// ->run()
//// ->wasSuccessful()
////
//// // @TODO: Hack attempt to fix failing apache restarts: https://travis-ci.org/opendevshop/devshop/jobs/608769926#L2447
//// // Idea from: https://unix.stackexchange.com/questions/239489/dbus-system-failed-to-activate-service-org-freedesktop-login1-timed-out
//// && $this->taskDockerExec('devshop_container')
//// ->exec("systemctl restart systemd-logind")
//// ->run()
//// ->wasSuccessful()
//// )) {
//// $this->say('Unable to install dbus. Setting hostname wont work. See https://github.com/ansible/ansible/issues/25543');
////
//// exit(1);
//// }
// }
//
// // Display home folder.
// $this->taskDockerExec('devshop_container')
// ->exec('ls -la /var/aegir')
// ->run();
//
// // Try to set ownership of home folder to AEGIR_UID.
// $this->taskDockerExec('devshop_container')
// ->exec("chown {$opts['user-uid']} /var/aegir -R")
// ->run();
//
// # If test-upgrade requested, install older version first, then run devshop upgrade $VERSION
// if ($opts['test-upgrade']) {
//
//// // This is needed because the old playbook has an incompatibility with newer ansible.
// // UPDATE: Seems to be not needed now?? This was triggering sh: 1: cannot create /root/.ansible.cfg: Permission denied
//// $this->taskDockerExec('devshop_container')
//// ->exec('echo "invalid_task_attribute_failed = false" >> /root/.ansible.cfg')
//// ->run();
//
// // get geerlingguy.git role, it's not in the old release but it needs to be there because the aegir-apache role has it listed as a dependency.
// $this->taskDockerExec('devshop_container')
// ->exec('ansible-galaxy install geerlingguy.git geerlingguy.apache')
// ->run();
//
// $this->yell("Running install.sh for old version...");
//
// // Run install.sh old version.
// $version = self::UPGRADE_FROM_VERSION;
// $this->_exec("curl -fsSL https://raw.githubusercontent.com/opendevshop/devshop/{$version}/install.sh -o {$this->devshop_root_path}/install.{$version}.sh");
//
// // Set makefile and devshop install path options because they need to be different than the defaults for upgrading.
// $install_path = "/usr/share/devshop-{$version}";
// $makefile_filename = $opts['no-dev']? 'build-devmaster.make': "build-devmaster-dev.make.yml";
//
// $opts['install-sh-options'] .= " --makefile=https://raw.githubusercontent.com/opendevshop/devshop/{$version}/{$makefile_filename}" ;
// $opts['install-sh-options'] .= " --install-path={$install_path}";
// $opts['install-sh-options'] .= " --force-ansible-role-install";
//
// if (!empty($opts['user-uid'])) {
// $opts['install-sh-options'] .= " --aegir-uid={$opts['user-uid']}";
// }
//
// if (!$this->taskDockerExec('devshop_container')
// ->exec("bash /usr/share/devshop/install.{$version}.sh " . $opts['install-sh-options'])
// ->run()
// ->wasSuccessful()) {
// throw new RunException("Installation of devshop $version failed.");
// };
//
// // Run devshop upgrade. This command runs:
// $this->yell("Running devshop upgrade...");
// // - self-update, which checks out the branch being tested and installs the roles.
// // - verify:system, which runs the playbook with those roles, along with a devmaster:upgrade
// $upgrade_to_branch = !empty($_SERVER['GITHUB_REF'])? $_SERVER['GITHUB_REF']: '1.x';
// $upgrade_command = '/usr/share/devshop/bin/devshop upgrade -n ' . $upgrade_to_branch;
// if (!$this->taskDockerExec('devshop_container')
// ->exec($upgrade_command)
// ->run()
// ->wasSuccessful()) {
// throw new RuntimeException("Command $upgrade_command failed.");
// };
//
// if (!$this->taskDockerExec('devshop_container')
// ->exec('/usr/share/devshop/bin/devshop status')
// ->run()
// ->wasSuccessful()) {
// throw new RuntimeException("Command 'devshop status' failed.");
// };
// }
// else {
// # Run install script on the container.
// $this->yell("Running install.sh ...");
// $install_command = '/usr/share/devshop/install.sh ' . $opts['install-sh-options'];
// if ($opts['mode'] != 'manual' && ($this->input()
// ->getOption('no-interaction') || $this->confirm('Run install.sh script?')) && !$this->taskDockerExec('devshop_container')
// ->exec($install_command)
// // ->option('tty')
// ->run()
// ->wasSuccessful()) {
// throw new RuntimeException('Docker Exec install.sh failed.');
// }
// }
//
// if ($opts['test']) {
//
// $this->yell("Running devshop-tests.sh ...");
//
// # Run test script on the container.
// if (!$this->taskDockerExec('devshop_container')
// ->exec('su - aegir -c - /usr/share/devshop/tests/devshop-tests.sh')
// ->run()
// ->wasSuccessful()
// ) {
// throw new RuntimeException('Docker Exec devshop-tests.sh failed.');
// }
// }
// }
}
/**
* Convert this: to this:
*
* array( array(
* "this=that" "this" => "that"
* ); );
*
* @param $options_list
*
* @return array
*/
private function optionsToArray($options_list) {
$vars = [];
foreach ($options_list as $options_string) {
list($name, $value) = explode("=", $options_string);
$vars[$name] = $value;
}
return $vars;
}
/**
* Run a command in the devshop container.
*/
public function exec($cmd = "devshop-ansible-playbook") {
return $this->_exec("docker-compose exec -T \
--env ANSIBLE_TAGS \
--env ANSIBLE_SKIP_TAGS \
--env ANSIBLE_VARS \
devshop $cmd")->getExitCode();
}
/**
* Stop devshop containers using docker-compose stop
*/
public function stop() {
$this->_exec('docker-compose stop');
}
/**
* Destroy all containers, docker volumes, and aegir configuration.
*
* Running with --no-interaction will keep the drupal devmaster codebase in
* place.
*
* Running with --force
*/
public function destroy($opts = ['force' => 0]) {
if ($opts['no-interaction'] || $this->confirm("Destroy all local data? (docker containers, volumes, config)")) {
$this->_exec('docker-compose kill');
$this->_exec('docker-compose rm -fv');
// Remove devmaster site folder
$version = self::DEVSHOP_LOCAL_VERSION;
$uri = self::DEVSHOP_LOCAL_URI;
$this->_exec("sudo rm -rf aegir-home/.drush");
$this->_exec("sudo rm -rf aegir-home/config");
$this->_exec("sudo rm -rf aegir-home/clients");
$this->_exec("sudo rm -rf aegir-home/projects");
$this->_exec("sudo rm -rf aegir-home/devmaster-{$version}/sites/{$uri}");
$this->_exec("sudo rm -rf aegir-home/devmaster-1.0.0-beta10/sites/{$uri}");
}
// Don't run when -n is specified,
if ($opts['force'] || !$opts['no-interaction'] && $this->confirm("Destroy local source code? (aegir-home)")) {
if ($this->_exec("sudo rm -rf aegir-home")->wasSuccessful()) {
$this->say("Entire aegir-home folder deleted.");
}
}
elseif ($opts['no-interaction']) {
$this->say("Local source code was retained. Use 'robo destroy --force' option to remove it, or run 'rm -rf aegir-home'.");
}
}
/**
* Stream logs from the containers using docker-compose logs -f
*/
public function logs() {
$this->_exec('docker-compose logs -f');
}
/**
* Stream watchdog logs from drupal
*/
public function watchdog() {
$user = 'aegir';
$this->_exec("docker-compose exec --user $user -T devshop drush @hostmaster wd-show --tail --extended");
}
/**
* Restart the containers.
*/
public function restart() {
$this->_exec('docker-compose restart');
$this->logs();
}
/**
* Enter a bash shell in the devmaster container.
*/
public function shell($user = 'aegir') {
if ($user) {
$process = new \Symfony\Component\Process\Process("docker-compose exec --user $user devshop bash");
}
else {
$process = new \Symfony\Component\Process\Process("docker-compose exec devshop bash");
}
$process->setTty(TRUE);
$process->setTimeout(NULL);
$process->run();
}
/**
* Run all devshop tests on the containers.
*/
public function test($user = 'aegir', $opts = array(
'compose-file' => 'docker-compose.yml',
)) {
$is_tty = !empty($_SERVER['XDG_SESSION_TYPE']) && $_SERVER['XDG_SESSION_TYPE'] == 'tty';
$no_tty = !$is_tty? '-T': '';
$command = "docker-compose exec $no_tty --user $user devshop /usr/share/devshop/tests/devshop-tests.sh";
$provision_io = new \ProvisionOps\Tools\Style($this->input, $this->output);
$process = new \ProvisionOps\Tools\PowerProcess($command, $provision_io);
$process->setTty(!empty($_SERVER['XDG_SESSION_TYPE']) && $_SERVER['XDG_SESSION_TYPE'] == 'tty');
$process->setEnv([
'COMPOSE_FILE' => $opts['compose-file'],
]);
$process->setTimeout(NULL);
$process->disableOutput();
// @TODO: Figure out why PowerProcess::mustRun() fails so miserably: https://github.com/opendevshop/devshop/pull/541/checks?check_run_id=518074346#step:7:45
// $process->mustRun();
$process->run();
}
/**
* Get a one-time login link to Devamster.
*/
public function login($user = 'aegir') {
$this->_exec("docker-compose exec --user $user -T devshop drush @hostmaster uli");
}
/**
* Create a new release of DevShop.
*/
public function release($version = NULL, $drupal_org_version = NULL) {
if (empty($version)) {
// @TODO Verify version string.
$version = $this->ask('What is the new version? ');
}
$version = trim($version);
if (!$this->confirm("Are you sure you want the version number to be $version?")) {
$this->release();
return;
}
if (empty($drupal_org_version)) {
$drupal_org_version = $this->ask("What should the Drupal.org version be? (Do not include 7.x or the second dot of the semantic version. ie 1.00-rc1 for 1.0.0-rc1)");
}
if (empty($drupal_org_version)) {
$this->release($version);
return;
}
if (!$this->confirm("Are you sure you want the Drupal.org tag to be 7.x-$drupal_org_version?")) {
$this->release();
return;
}
$drupal_org_tag = "7.x-$drupal_org_version";
$this->yell("The new version shall be $version!!!");
$release_branch = "release-{$version}";
$not_ready = TRUE;
while ($not_ready) {
$not_ready = !$this->confirm("Are you absolutely sure all contrib modules and drupal core are up to date in ./aegir-home/devmaster-1.x/profiles/devmaster/devmaster.make?? Go check. I'll wait.");
}