-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathWordPress.php
More file actions
1507 lines (1308 loc) · 55.3 KB
/
WordPress.php
File metadata and controls
1507 lines (1308 loc) · 55.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
declare( ticks=1 );
namespace EE\Site\Type;
use EE;
use EE\Model\Site;
use EE\Model\Site_Meta;
use Symfony\Component\Filesystem\Filesystem;
use function EE\Site\Utils\auto_site_name;
use function EE\Site\Utils\get_site_info;
use function EE\Site\Utils\get_public_dir;
use function EE\Utils\get_flag_value;
use function EE\Utils\trailingslashit;
use function EE\Utils\get_value_if_flag_isset;
/**
* Adds `wp` site type to `ee site` command.
*
* @package ee-cli
*/
class WordPress extends EE_Site_Command {
/**
* @var string $cache_type Type of caching being used.
*/
private $cache_type;
/**
* @var int $level The level of creation in progress. Essential for rollback in case of failure.
*/
private $level;
/**
* @var object $logger Object of logger.
*/
private $logger;
/**
* @var string $locale Language to install WordPress in.
*/
private $locale;
/**
* @var bool $skip_install To skip installation of WordPress.
*/
private $skip_install;
/**
* @var bool $skip_status_check To skip site status check pre-installation.
*/
private $skip_status_check;
/**
* @var bool $force To reset remote database.
*/
private $force;
/**
* @var string $vip_go_mu_plugins WordPress VIP Go mu-plugins repo.
*/
private $vip_go_mu_plugins = 'https://github.com/Automattic/vip-go-mu-plugins-built';
/**
* @var string $vip_go_mu_plugins WordPress VIP Go mu-plugins repo.
*/
private $vip_go_skeleton = 'https://github.com/Automattic/vip-go-skeleton.git';
/**
* @var bool $is_vip To check if site is setup for vip.
*/
private $is_vip = false;
public function __construct() {
parent::__construct();
$this->level = 0;
$this->logger = \EE::get_file_logger()->withName( 'site_wp_command' );
$this->site_data['site_type'] = 'wp';
}
/**
* Runs the standard WordPress Site installation.
*
* ## OPTIONS
*
* <site-name>
* : Name of website.
*
* [--cache]
* : Use redis cache for WordPress.
* ---
* options:
* - all
* - object
* - page
* ---
*
* [--vip]
* : Create WordPress VIP GO site using your vip repo which contains wp-content dir. Default it will use skeleton repo.
* ---
* default: https://github.com/Automattic/vip-go-skeleton.git
* ---
*
* [--mu=<subdir>]
* : WordPress sub-dir Multi-site.
*
* [--mu=<subdom>]
* : WordPress sub-domain Multi-site.
*
* [--title=<title>]
* : Title of your site.
*
* [--admin-user=<admin-user>]
* : Username of the administrator.
*
* [--admin-pass=<admin-pass>]
* : Password for the the administrator.
*
* [--admin-email=<admin-email>]
* : E-Mail of the administrator.
*
* [--local-db]
* : Create separate db container instead of using global db.
*
* [--with-local-redis]
* : Enable cache with local redis container.
*
* [--public-dir]
* : Set custom source directory for site inside htdocs.
*
* [--php=<php-version>]
* : PHP version for site. Currently only supports PHP 5.6 and latest.
* ---
* default: latest
* options:
* - 5.6
* - 7.2
* - latest
* ---
*
* [--dbname=<dbname>]
* : Set the database name.
*
* [--dbuser=<dbuser>]
* : Set the database user.
*
* [--dbpass=<dbpass>]
* : Set the database password.
*
* [--dbhost=<dbhost>]
* : Set the database host. Pass value only when remote dbhost is required.
*
* [--dbprefix=<dbprefix>]
* : Set the database table prefix.
*
* [--dbcharset=<dbcharset>]
* : Set the database charset.
* ---
* default: utf8mb4
* ---
*
* [--dbcollate=<dbcollate>]
* : Set the database collation.
*
* [--skip-check]
* : If set, the database connection is not checked.
*
* [--version=<version>]
* : Select which WordPress version you want to download. Accepts a version number, ‘latest’ or ‘nightly’.
*
* [--skip-content]
* : Download WP without the default themes and plugins.
*
* [--skip-install]
* : Skips wp-core install.
*
* [--skip-status-check]
* : Skips site status check.
*
* [--ssl]
* : Enables ssl on site.
* ---
* options:
* - le
* - self
* - inherit
* - custom
* ---
*
* [--ssl-key=<ssl-key-path>]
* : Path to the SSL key file.
*
* [--ssl-crt=<ssl-crt-path>]
* : Path to the SSL crt file.
*
* [--wildcard]
* : Gets wildcard SSL .
*
* [--yes]
* : Do not prompt for confirmation.
*
* [--force]
* : Resets the remote database if it is not empty.
*
* ## EXAMPLES
*
* # Create WordPress site
* $ ee site create example.com --type=wp
*
* # Create WordPress multisite subdir site
* $ ee site create example.com --type=wp --mu=subdir
*
* # Create WordPress multisite subdom site
* $ ee site create example.com --type=wp --mu=subdom
*
* # Create WordPress site with ssl from letsencrypt
* $ ee site create example.com --type=wp --ssl=le
*
* # Create WordPress site with wildcard ssl
* $ ee site create example.com --type=wp --ssl=le --wildcard
*
* # Create WordPress site with self signed certificate
* $ ee site create example.com --type=wp --ssl=self
*
* # Create WordPress site with remote database
* $ ee site create example.com --type=wp --dbhost=localhost --dbuser=username --dbpass=password
*
* # Create WordPress site with custom site title, locale, admin user, admin email and admin password
* $ ee site create example.com --type=wp --title=easyengine --locale=nl_NL --admin-email=easyengine@example.com --admin-user=easyengine --admin-pass=easyengine
*
* # Create WordPress site with custom source directory inside htdocs ( SITE_ROOT/app/htdocs/current )
* $ ee site create example.com --type=wp --public-dir=current
*
* # Create WordPress site with custom ssl certs
* $ ee site create example.com --ssl=custom --ssl-key='/path/to/example.com.key' --ssl-crt='/path/to/example.com.crt'
*
*/
public function create( $args, $assoc_args ) {
$this->check_site_count();
\EE\Utils\delem_log( 'site create start' );
$this->logger->debug( 'args:', $args );
$this->logger->debug( 'assoc_args:', empty( $assoc_args ) ? array( 'NULL' ) : $assoc_args );
$this->site_data['site_url'] = strtolower( \EE\Utils\remove_trailing_slash( $args[0] ) );
$mu = \EE\Utils\get_flag_value( $assoc_args, 'mu' );
if ( isset( $assoc_args['mu'] ) && ! in_array( $mu, [ 'subdom', 'subdir' ], true ) ) {
\EE::error( "Unrecognized multi-site parameter: $mu. Only `--mu=subdom` and `--mu=subdir` are supported." );
}
$this->site_data['app_sub_type'] = $mu ?? 'wp';
$vip_wp_content_repo = \EE\Utils\get_flag_value( $assoc_args, 'vip' );
if ( ! empty( $vip_wp_content_repo ) && is_bool( $vip_wp_content_repo ) ) {
$vip_wp_content_repo = $this->vip_go_skeleton;
\EE::log( 'VIP Skeleton repo will be used for wp-content directory: ' . $vip_wp_content_repo );
}
if ( ! empty( $vip_wp_content_repo ) ) {
$this->check_repo_access( $vip_wp_content_repo );
}
if ( Site::find( $this->site_data['site_url'] ) ) {
\EE::error( sprintf( "Site %1\$s already exists. If you want to re-create it please delete the older one using:\n`ee site delete %1\$s`", $this->site_data['site_url'] ) );
}
$this->site_data['site_fs_path'] = WEBROOT . $this->site_data['site_url'];
$this->cache_type = get_value_if_flag_isset( $assoc_args, 'cache', [ 'all', 'object', 'page' ], 'all' );
$this->site_data['site_ssl_wildcard'] = \EE\Utils\get_flag_value( $assoc_args, 'wildcard' );
$this->site_data['php_version'] = \EE\Utils\get_flag_value( $assoc_args, 'php', 'latest' );
$this->site_data['app_admin_url'] = \EE\Utils\get_flag_value( $assoc_args, 'title', $this->site_data['site_url'] );
$this->site_data['app_admin_username'] = \EE\Utils\get_flag_value( $assoc_args, 'admin-user', \EE\Utils\random_name_generator() );
$this->site_data['app_admin_password'] = \EE\Utils\get_flag_value( $assoc_args, 'admin-pass', \EE\Utils\random_password() );
$this->site_data['db_name'] = \EE\Utils\get_flag_value( $assoc_args, 'dbname', str_replace( [ '.', '-' ], '_', $this->site_data['site_url'] ) );
$this->site_data['db_host'] = \EE\Utils\get_flag_value( $assoc_args, 'dbhost', GLOBAL_DB );
$this->site_data['db_port'] = '3306';
$this->site_data['db_user'] = \EE\Utils\get_flag_value( $assoc_args, 'dbuser', $this->create_site_db_user( $this->site_data['site_url'] ) );
$this->site_data['db_password'] = \EE\Utils\get_flag_value( $assoc_args, 'dbpass', \EE\Utils\random_password() );
$this->locale = \EE\Utils\get_flag_value( $assoc_args, 'locale', \EE::get_config( 'locale' ) );
$local_cache = \EE\Utils\get_flag_value( $assoc_args, 'with-local-redis' );
$this->site_data['cache_host'] = '';
if ( $this->cache_type ) {
$this->site_data['cache_host'] = $local_cache ? 'redis' : 'global-redis';
}
$this->site_data['site_container_fs_path'] = get_public_dir( $assoc_args );
$this->site_data['site_ssl'] = get_value_if_flag_isset( $assoc_args, 'ssl', [ 'le', 'self', 'inherit', 'custom' ], 'le' );
if ( 'custom' === $this->site_data['site_ssl'] ) {
try {
$this->validate_site_custom_ssl( get_flag_value( $assoc_args, 'ssl-key' ), get_flag_value( $assoc_args, 'ssl-crt' ) );
} catch ( \Exception $e ) {
$this->catch_clean( $e );
}
}
$supported_php_versions = [ 5.6, 7.2, 'latest' ];
if ( ! in_array( $this->site_data['php_version'], $supported_php_versions ) ) {
$old_version = $this->site_data['php_version'];
$floor = (int) floor( $this->site_data['php_version'] );
if ( 5 === $floor ) {
$this->site_data['php_version'] = 5.6;
} elseif ( 7 === $floor ) {
$this->site_data['php_version'] = 7.2;
$old_version .= ' yet';
} else {
EE::error( 'Unsupported PHP version: ' . $this->site_data['php_version'] );
}
\EE::confirm( sprintf( 'EEv4 does not support PHP %s. Continue with PHP %s?', $old_version, $this->site_data['php_version'] ), $assoc_args );
}
if ( \EE\Utils\get_flag_value( $assoc_args, 'local-db' ) ) {
$this->site_data['db_host'] = 'db';
}
$this->site_data['db_root_password'] = ( 'db' === $this->site_data['db_host'] ) ? \EE\Utils\random_password() : '';
\EE\Service\Utils\nginx_proxy_check();
if ( $this->cache_type && ! $local_cache ) {
\EE\Service\Utils\init_global_container( GLOBAL_REDIS );
}
if ( GLOBAL_DB === $this->site_data['db_host'] ) {
\EE\Service\Utils\init_global_container( GLOBAL_DB );
try {
$user_data = \EE\Site\Utils\create_user_in_db( GLOBAL_DB, $this->site_data['db_name'], $this->site_data['db_user'], $this->site_data['db_password'] );
if ( ! $user_data ) {
throw new \Exception( sprintf( 'Could not create user %s. Please check logs.', $this->site_data['db_user'] ) );
}
} catch ( \Exception $e ) {
$this->catch_clean( $e );
}
$this->site_data['db_name'] = $user_data['db_name'];
$this->site_data['db_user'] = $user_data['db_user'];
$this->site_data['db_password'] = $user_data['db_pass'];
} elseif ( 'db' !== $this->site_data['db_host'] ) {
// If user wants to connect to remote database.
if ( ! isset( $assoc_args['dbuser'] ) || ! isset( $assoc_args['dbpass'] ) ) {
\EE::error( '`--dbuser` and `--dbpass` are required for remote db host.' );
}
$arg_host_port = explode( ':', $this->site_data['db_host'] );
$this->site_data['db_host'] = $arg_host_port[0];
$this->site_data['db_port'] = empty( $arg_host_port[1] ) ? '3306' : $arg_host_port[1];
}
$this->site_data['app_admin_email'] = \EE\Utils\get_flag_value( $assoc_args, 'admin-email', strtolower( 'admin@' . $this->site_data['site_url'] ) );
$this->skip_install = \EE\Utils\get_flag_value( $assoc_args, 'skip-install' );
$this->skip_status_check = \EE\Utils\get_flag_value( $assoc_args, 'skip-status-check' );
$this->force = \EE\Utils\get_flag_value( $assoc_args, 'force' );
if ( 'inherit' === $this->site_data['site_ssl'] && ( 'subdom' === $mu || $this->site_data['site_ssl_wildcard'] ) ) {
\EE::error( '--wildcard or --mu=subdom flag can not be passed together with --ssl=inherit flag.' );
}
\EE::log( 'Configuring project.' );
$this->create_site( $assoc_args );
\EE\Utils\delem_log( 'site create end' );
}
/**
* Enable object cache.
*/
private function enable_object_cache() {
$redis_host = $this->site_data['cache_host'];
$redis_plugin_constant = 'docker-compose exec --user=\'www-data\' php wp config set --type=variable redis_server "array(\'host\'=> \'' . $redis_host . '\',\'port\'=> 6379,)" --raw';
$activate_wp_redis_plugin = "docker-compose exec --user='www-data' php wp plugin install wp-redis --activate";
$enable_redis_cache = "docker-compose exec --user='www-data' php wp redis enable";
$obj_cache_key_prefix = $this->site_data['site_url'] . '_obj:';
$add_cache_key_salt = "docker-compose exec --user='www-data' php wp config set WP_CACHE_KEY_SALT $obj_cache_key_prefix --add=true --type=constant";
$this->docker_compose_exec( $redis_plugin_constant, 'Unable to download or activate wp-redis plugin.' );
$this->docker_compose_exec( $activate_wp_redis_plugin, 'Unable to download or activate wp-redis plugin.' );
$this->docker_compose_exec( $enable_redis_cache, 'Unable to enable object cache' );
$this->docker_compose_exec( $add_cache_key_salt, 'Unable to set cache key salt' );
}
/**
* Enable page cache.
*/
private function enable_page_cache() {
$activate_nginx_helper = 'docker-compose exec --user=\'www-data\' php wp plugin install nginx-helper --activate';
$nginx_helper_fail_msg = 'Unable to download or activate nginx-helper plugin properly.';
$page_cache_key_prefix = $this->site_data['site_url'] . '_page:';
$redis_host = $this->site_data['cache_host'];
$wp_cli_params = ( 'wp' === $this->site_data['app_sub_type'] ) ? 'option update' : 'network meta update 1';
$plugin_data = sprintf( '{
"log_level":"INFO",
"log_filesize":5,
"enable_purge":1,
"enable_map":0,
"enable_log":0,
"enable_stamp":0,
"purge_homepage_on_new":1,
"purge_homepage_on_edit":1,
"purge_homepage_on_del":1,
"purge_archive_on_new":1,
"purge_archive_on_edit":0,
"purge_archive_on_del":0,
"purge_archive_on_new_comment":0,
"purge_archive_on_deleted_comment":0,
"purge_page_on_mod":1,
"purge_page_on_new_comment":1,
"purge_page_on_deleted_comment":1,
"cache_method":"enable_redis",
"purge_method":"get_request",
"redis_hostname":"%s",
"redis_port":"6379",
"redis_prefix":"%s"
}',
$redis_host,
$page_cache_key_prefix
);
$add_hostname_constant = "docker-compose exec --user='www-data' php wp config set RT_WP_NGINX_HELPER_REDIS_HOSTNAME $redis_host --add=true --type=constant";
$add_port_constant = "docker-compose exec --user='www-data' php wp config set RT_WP_NGINX_HELPER_REDIS_PORT 6379 --add=true --type=constant";
$add_prefix_constant = "docker-compose exec --user='www-data' php wp config set RT_WP_NGINX_HELPER_REDIS_PREFIX $page_cache_key_prefix --add=true --type=constant";
$add_redis_maxttl = "docker-compose exec --user='www-data' php wp config set WP_REDIS_MAXTTL 14400 --add=true --type=constant";
$add_plugin_data = "docker-compose exec --user='www-data' php wp $wp_cli_params rt_wp_nginx_helper_options '$plugin_data' --format=json";
$this->docker_compose_exec( $add_hostname_constant, $nginx_helper_fail_msg );
$this->docker_compose_exec( $add_port_constant, $nginx_helper_fail_msg );
$this->docker_compose_exec( $add_prefix_constant, $nginx_helper_fail_msg );
$this->docker_compose_exec( $activate_nginx_helper, $nginx_helper_fail_msg );
$this->docker_compose_exec( $add_plugin_data, $nginx_helper_fail_msg );
$this->docker_compose_exec( $add_redis_maxttl, $nginx_helper_fail_msg );
}
/**
* Execute command with fail msg.
*
* @param string $command Command to execute.
* @param string $fail_msg failure message.
*/
private function docker_compose_exec( $command, $fail_msg = '' ) {
if ( empty( $command ) ) {
return;
}
if ( ! \EE::exec( $command ) ) {
\EE::warning( $fail_msg );
}
}
/**
* Creates database user for a site
*
* @param string $site_url URL of site.
*
* @return string Generated db user.
*/
private function create_site_db_user( string $site_url ): string {
if ( strlen( $site_url ) > 53 ) {
$site_url = substr( $site_url, 0, 53 );
}
return $site_url . '-' . \EE\Utils\random_password( 6 );
}
/**
* Display all the relevant site information, credentials and useful links.
*
* [<site-name>]
* : Name of the website whose info is required.
*
* ## EXAMPLES
*
* # Display site info
* $ ee site info example.com
*/
public function info( $args, $assoc_args ) {
\EE\Utils\delem_log( 'site info start' );
if ( ! isset( $this->site_data['site_url'] ) ) {
$args = auto_site_name( $args, 'wp', __FUNCTION__ );
$this->site_data = get_site_info( $args, false );
$this->cache_type = $this->site_data['cache_nginx_fullpage'];
}
$ssl = $this->site_data['site_ssl'] ? 'Enabled' : 'Not Enabled';
$prefix = ( $this->site_data['site_ssl'] ) ? 'https://' : 'http://';
$info = [ [ 'Site', $prefix . $this->site_data['site_url'] ] ];
if ( ! empty( $this->site_data['admin_tools'] ) ) {
$info[] = [ 'Access admin-tools', $prefix . $this->site_data['site_url'] . '/ee-admin/' ];
}
$info[] = [ 'Site Root', $this->site_data['site_fs_path'] ];
$info[] = [ 'Site Title', $this->site_data['app_admin_url'] ];
if ( ! empty( $this->site_data['app_admin_username'] ) && ! $this->skip_install ) {
$info[] = [ 'WordPress Username', $this->site_data['app_admin_username'] ];
$info[] = [ 'WordPress Password', $this->site_data['app_admin_password'] ];
}
$info[] = [ 'DB Host', $this->site_data['db_host'] ];
if ( ! empty( $this->site_data['db_root_password'] ) ) {
$info[] = [ 'DB Root Password', $this->site_data['db_root_password'] ];
}
$info[] = [ 'DB Name', $this->site_data['db_name'] ];
$info[] = [ 'DB User', $this->site_data['db_user'] ];
$info[] = [ 'DB Password', $this->site_data['db_password'] ];
$info[] = [ 'E-Mail', $this->site_data['app_admin_email'] ];
$info[] = [ 'SSL', $ssl ];
if ( $this->site_data['site_ssl'] ) {
$info[] = [ 'SSL Wildcard', $this->site_data['site_ssl_wildcard'] ? 'Yes' : 'No' ];
}
$info[] = [ 'Cache', $this->cache_type ? 'Enabled' : 'None' ];
\EE\Utils\format_table( $info );
\EE\Utils\delem_log( 'site info end' );
}
/**
* Function to configure site and copy all the required files.
*/
private function configure_site_files() {
$site_conf_dir = $this->site_data['site_fs_path'] . '/config';
$site_conf_env = $this->site_data['site_fs_path'] . '/.env';
$site_nginx_default_conf = $site_conf_dir . '/nginx/conf.d/main.conf';
$site_php_ini = $site_conf_dir . '/php/php/conf.d/custom.ini';
$server_name = ( 'subdom' === $this->site_data['app_sub_type'] ) ? $this->site_data['site_url'] . ' *.' . $this->site_data['site_url'] : $this->site_data['site_url'];
$custom_conf_dest = $site_conf_dir . '/nginx/custom/user.conf';
$custom_conf_source = SITE_WP_TEMPLATE_ROOT . '/config/nginx/user.conf.mustache';
$process_user = posix_getpwuid( posix_geteuid() );
\EE::log( 'Creating WordPress site ' . $this->site_data['site_url'] );
\EE::log( 'Copying configuration files.' );
$default_conf_content = $this->generate_default_conf( $this->site_data['app_sub_type'], $this->cache_type, $server_name );
$local = ( 'db' === $this->site_data['db_host'] ) ? true : false;
$db_host = $local ? $this->site_data['db_host'] : $this->site_data['db_host'] . ':' . $this->site_data['db_port'];
$env_data = [
'local' => $local,
'virtual_host' => $this->site_data['site_url'],
'root_password' => $this->site_data['db_root_password'],
'database_name' => $this->site_data['db_name'],
'database_user' => $this->site_data['db_user'],
'user_password' => $this->site_data['db_password'],
'wp_db_host' => $db_host,
'wp_db_user' => $this->site_data['db_user'],
'wp_db_name' => $this->site_data['db_name'],
'wp_db_pass' => $this->site_data['db_password'],
'user_id' => $process_user['uid'],
'group_id' => $process_user['gid'],
];
$php_ini_data = [
'admin_email' => $this->site_data['app_admin_email'],
];
$env_content = \EE\Utils\mustache_render( SITE_WP_TEMPLATE_ROOT . '/config/.env.mustache', $env_data );
$php_ini_content = \EE\Utils\mustache_render( SITE_WP_TEMPLATE_ROOT . '/config/php-fpm/php.ini.mustache', $php_ini_data );
try {
$this->dump_docker_compose_yml( [ 'nohttps' => true ] );
$this->fs->dumpFile( $site_conf_env, $env_content );
if ( ! IS_DARWIN ) {
\EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'postfix' ] );
}
\EE\Site\Utils\set_postfix_files( $this->site_data['site_url'], $this->site_data['site_fs_path'] . '/services' );
$this->fs->dumpFile( $site_nginx_default_conf, $default_conf_content );
$this->fs->copy( $custom_conf_source, $custom_conf_dest );
$this->fs->remove( $this->site_data['site_fs_path'] . '/app/html' );
$this->fs->dumpFile( $site_php_ini, $php_ini_content );
if ( IS_DARWIN ) {
if ( 'db' === $this->site_data['db_host'] ) {
$db_conf_file = $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf';
$this->fs->copy( SITE_WP_TEMPLATE_ROOT . '/my.cnf.mustache', $db_conf_file );
}
\EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php', 'postfix' ] );
} else {
\EE\Site\Utils\restart_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php' ] );
}
} catch ( \Exception $e ) {
$this->catch_clean( $e );
}
}
/**
* Generate and place docker-compose.yml file.
*
* @param array $additional_filters Filters to alter docker-compose file.
*
* @ignorecommand
*/
public function dump_docker_compose_yml( $additional_filters = [] ) {
$site_conf_dir = $this->site_data['site_fs_path'] . '/config';
$site_nginx_default_conf = $site_conf_dir . '/nginx/conf.d/main.conf';
$site_php_ini = $site_conf_dir . '/php/php/conf.d/custom.ini';
$volumes = [
'nginx' => [
[
'name' => 'htdocs',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/app',
'container_path' => '/var/www',
],
[
'name' => 'config_nginx',
'path_to_symlink' => dirname( dirname( $site_nginx_default_conf ) ),
'container_path' => '/usr/local/openresty/nginx/conf',
'skip_darwin' => true,
],
[
'name' => 'config_nginx',
'path_to_symlink' => $site_nginx_default_conf,
'container_path' => '/usr/local/openresty/nginx/conf/conf.d/main.conf',
'skip_linux' => true,
'skip_volume' => true,
],
[
'name' => 'log_nginx',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/nginx',
'container_path' => '/var/log/nginx',
],
],
'php' => [
[
'name' => 'htdocs',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/app',
'container_path' => '/var/www',
],
[
'name' => 'config_php',
'path_to_symlink' => $site_conf_dir . '/php',
'container_path' => '/usr/local/etc',
'skip_darwin' => true,
],
[
'name' => 'config_php',
'path_to_symlink' => $site_php_ini,
'container_path' => '/usr/local/etc/php/php/conf.d/custom.ini',
'skip_linux' => true,
'skip_volume' => true,
],
[
'name' => 'log_php',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/php',
'container_path' => '/var/log/php',
],
],
'postfix' => [
[
'name' => '/dev/log',
'path_to_symlink' => '/dev/log',
'container_path' => '/dev/log',
'skip_volume' => true,
'skip_darwin' => true,
],
[
'name' => 'data_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/spool',
'container_path' => '/var/spool/postfix',
],
[
'name' => 'ssl_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/ssl',
'container_path' => '/etc/ssl/postfix',
],
[
'name' => 'config_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/config/postfix',
'container_path' => '/etc/postfix',
'skip_darwin' => true,
],
],
];
if ( 'db' === $this->site_data['db_host'] ) {
$volumes['db'] = [
[
'name' => 'db_data',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/data',
'container_path' => '/var/lib/mysql',
],
[
'name' => 'db_conf',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf',
'container_path' => '/etc/mysql',
'skip_darwin' => true,
],
[
'name' => 'db_conf',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf',
'container_path' => '/etc/mysql/my.cnf',
'skip_linux' => true,
'skip_volume' => true,
],
[
'name' => 'db_logs',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/logs',
'container_path' => '/var/log/mysql',
],
];
}
if ( ! IS_DARWIN && empty( \EE_DOCKER::get_volumes_by_label( $this->site_data['site_url'] ) ) ) {
foreach ( $volumes as $volume ) {
\EE_DOCKER::create_volumes( $this->site_data['site_url'], $volume );
}
}
$site_docker_yml = $this->site_data['site_fs_path'] . '/docker-compose.yml';
$filter = [];
$filter[] = $this->site_data['app_sub_type'];
$filter[] = $this->site_data['cache_host'];
$filter[] = $this->site_data['db_host'];
$filter['is_ssl'] = $this->site_data['site_ssl'];
$filter['site_prefix'] = \EE_DOCKER::get_docker_style_prefix( $this->site_data['site_url'] );
$filter['php_version'] = ( string ) $this->site_data['php_version'];
$site_docker = new Site_WP_Docker();
foreach ( $additional_filters as $key => $addon_filter ) {
$filter[ $key ] = $addon_filter;
}
$docker_compose_content = $site_docker->generate_docker_compose_yml( $filter, $volumes );
$this->fs->dumpFile( $site_docker_yml, $docker_compose_content );
}
/**
* Function to generate main.conf from mustache templates.
*
* @param string $site_type Type of site (subdom, subdir etc..).
* @param boolean $cache_type Cache enabled or not.
* @param string $server_name Name of server to use in virtual_host.
*
* @return string Parsed mustache template string output.
*/
private function generate_default_conf( $site_type, $cache_type, $server_name ) {
$default_conf_data['site_type'] = $site_type;
$default_conf_data['site_url'] = $this->site_data['site_url'];
$default_conf_data['server_name'] = $server_name;
$default_conf_data['include_php_conf'] = ! $cache_type;
$default_conf_data['include_wpsubdir_conf'] = $site_type === 'subdir';
$default_conf_data['include_redis_conf'] = $cache_type;
$default_conf_data['cache_host'] = $this->site_data['cache_host'];
$default_conf_data['document_root'] = $this->site_data['site_container_fs_path'];
return \EE\Utils\mustache_render( SITE_WP_TEMPLATE_ROOT . '/config/nginx/main.conf.mustache', $default_conf_data );
}
/**
* Verify if the passed database credentials are working or not.
*
* @throws \Exception
*/
private function maybe_verify_remote_db_connection() {
if ( in_array( $this->site_data['db_host'], [ 'db', GLOBAL_DB ], true ) ) {
return;
}
$db_host = $this->site_data['db_host'];
$img_versions = \EE\Utils\get_image_versions();
$container_name = \EE\Utils\random_password();
$network = ( GLOBAL_DB === $this->site_data['db_host'] ) ? "--network='" . GLOBAL_FRONTEND_NETWORK . "'" : '';
$run_temp_container = sprintf(
'docker run --name %s %s -e MYSQL_ROOT_PASSWORD=%s -d --restart always easyengine/mariadb:%s',
$container_name,
$network,
\EE\Utils\random_password(),
$img_versions['easyengine/mariadb']
);
if ( ! \EE::exec( $run_temp_container ) ) {
\EE::exec( "docker rm -f $container_name" );
throw new \Exception( 'There was a problem creating container to test mysql connection. Please check the logs' );
}
// Docker needs special handling if we want to connect to host machine.
// The since we're inside the container and we want to access host machine,
// we would need to replace localhost with default gateway.
if ( '127.0.0.1' === $db_host || 'localhost' === $db_host ) {
$launch = \EE::launch( sprintf( "docker exec %s bash -c \"ip route show default | cut -d' ' -f3\"", $container_name ) );
if ( ! $launch->return_code ) {
$db_host = trim( $launch->stdout, "\n" );
} else {
\EE::exec( "docker rm -f $container_name" );
throw new \Exception( 'There was a problem in connecting to the database. Please check the logs' );
}
}
\EE::log( 'Verifying connection to remote database' );
$check_db_connection = sprintf(
"docker exec %s sh -c \"mysql --host='%s' --port='%s' --user='%s' --password='%s' --execute='EXIT'\"",
$container_name,
$db_host,
$this->site_data['db_port'],
$this->site_data['db_user'],
$this->site_data['db_password']
);
if ( ! \EE::exec( $check_db_connection ) ) {
\EE::exec( "docker rm -f $container_name" );
throw new \Exception( 'Unable to connect to remote db' );
}
\EE::success( 'Connection to remote db verified' );
$name = str_replace( '_', '\_', $this->site_data['db_name'] );
$check_db_exists = sprintf( "docker exec %s bash -c \"mysqlshow --user='%s' --password='%s' --host='%s' --port='%s' '%s'\"", $container_name, $this->site_data['db_user'], $this->site_data['db_password'], $db_host, $this->site_data['db_port'], $name );
if ( ! \EE::exec( $check_db_exists ) ) {
\EE::log( sprintf( 'Database `%s` does not exist. Attempting to create it.', $this->site_data['db_name'] ) );
$create_db_command = sprintf(
"docker exec %s bash -c \"mysql --host='%s' --port='%s' --user='%s' --password='%s' --execute='CREATE DATABASE %s;'\"",
$container_name,
$db_host,
$this->site_data['db_port'],
$this->site_data['db_user'],
$this->site_data['db_password'],
$this->site_data['db_name']
);
if ( ! \EE::exec( $create_db_command ) ) {
\EE::exec( "docker rm -f $container_name" );
throw new \Exception( sprintf(
'Could not create database `%s` on `%s:%s`. Please check if %s has rights to create database or manually create a database and pass with `--dbname` parameter.',
$this->site_data['db_name'],
$this->site_data['db_host'],
$this->site_data['db_port'],
$this->site_data['db_user']
) );
}
} else {
if ( $this->force ) {
\EE::exec(
sprintf(
"docker exec %s bash -c \"mysql --host='%s' --port='%s' --user='%s' --password='%s' --execute='DROP DATABASE %s;'\"",
$container_name,
$db_host,
$this->site_data['db_port'],
$this->site_data['db_user'],
$this->site_data['db_password'],
$this->site_data['db_name']
)
);
\EE::exec(
sprintf(
"docker exec %s bash -c \"mysql --host='%s' --port='%s' --user='%s' --password='%s' --execute='CREATE DATABASE %s;'\"",
$container_name,
$db_host,
$this->site_data['db_port'],
$this->site_data['db_user'],
$this->site_data['db_password'],
$this->site_data['db_name']
)
);
}
$check_tables = sprintf(
"docker exec %s bash -c \"mysql --host='%s' --port='%s' --user='%s' --password='%s' --execute='USE %s; show tables;'\"",
$container_name,
$db_host,
$this->site_data['db_port'],
$this->site_data['db_user'],
$this->site_data['db_password'],
$this->site_data['db_name']
);
$launch = \EE::launch( $check_tables );
if ( ! $launch->return_code ) {
$tables = trim( $launch->stdout, "\n" );
if ( ! empty( $tables ) ) {
\EE::exec( "docker rm -f $container_name" );
throw new \Exception( sprintf( 'Some database tables seem to exist in database %s. Please backup and reset the database or use `--force` in the site create command to reset it.', $this->site_data['db_name'] ) );
}
} else {
\EE::exec( "docker rm -f $container_name" );
throw new \Exception( 'There was a problem in connecting to the database. Please check the logs' );
}
}
\EE::exec( "docker rm -f $container_name" );
}
/**
* Function to create the site.
*/
private function create_site( $assoc_args ) {
$this->level = 1;
try {
if ( 'inherit' === $this->site_data['site_ssl'] ) {
$this->check_parent_site_certs( $this->site_data['site_url'] );
}
\EE\Site\Utils\create_site_root( $this->site_data['site_fs_path'], $this->site_data['site_url'] );
$this->level = 2;
$this->maybe_verify_remote_db_connection();
$this->configure_site_files();
$this->level = 3;
\EE\Site\Utils\configure_postfix( $this->site_data['site_url'], $this->site_data['site_fs_path'] );
$this->wp_download_and_config( $assoc_args );
if ( ! $this->skip_install ) {
if ( ! $this->site_data['site_ssl'] || 'self' === $this->site_data['site_ssl'] ) {
\EE\Site\Utils\create_etc_hosts_entry( $this->site_data['site_url'] );
}
if ( ! $this->skip_status_check ) {
$this->level = 4;
\EE\Site\Utils\site_status_check( $this->site_data['site_url'] );
}
$this->install_wp();
if ( $this->is_vip ) {
$this->setup_vip( $assoc_args );
}
}
if ( 'custom' === $this->site_data['site_ssl'] ) {
$this->custom_site_ssl();
}
$this->www_ssl_wrapper( [ 'nginx' ] );
} catch ( \Exception $e ) {
$this->catch_clean( $e );
}
if ( ! empty( $this->cache_type ) ) {
if ( 'all' === $this->cache_type ) {
$this->enable_object_cache();
$this->enable_page_cache();
} elseif ( 'object' === $this->cache_type ) {
$this->enable_object_cache();
} elseif ( 'page' === $this->cache_type ) {
$this->enable_page_cache();
}
if ( $this->is_vip ) {
EE::warning( 'Nginx-helper and wp-redis plugin is installed to enable cache. Please add it in your .gitignore to avoid it from git diff and commit' );
EE::log( 'Note: Redis cache is setup for this site so it will use wp-redis object cache but not the memcache which is mentioned in VIP development doc from mu-plugin drop-ins.' );
}
}
$wp_root_dir = $this->site_data['site_fs_path'] . '/app/htdocs';
if ( $this->is_vip && file_exists( $wp_root_dir . '/mu-plugins' ) ) {
// Enable VIP MU plugins.
$this->fs->rename( $wp_root_dir . '/mu-plugins', $wp_root_dir . '/wp-content/mu-plugins' );
}
// Reset wp-content permission which may have been changed during git clone from host machine.
EE::exec( "docker-compose exec --user=root php chown -R www-data: /var/www/htdocs/wp-content" );
$this->create_site_db_entry();
\EE::log( 'Site entry created.' );
\EE::log( 'Creating cron entry' );
$cron_interval = rand( 0, 9 );
\EE::runcommand( 'cron create ' . $this->site_data['site_url'] . " --user=www-data --command='wp cron event run --due-now' --schedule='$cron_interval/10 * * * *'" );
$this->info( [ $this->site_data['site_url'] ], [] );
}
/**
* Download and configure WordPress according to the user passed parameters.
*
* @param array $assoc_args Associative arguments passed during site creation.
*
* @throws \EE\ExitException
* @throws \Exception
*/
private function wp_download_and_config( $assoc_args ) {
$core_download_args = [
'version',
'skip-content',
];
$config_args = [
'dbprefix',
'dbcharset',
'dbcollate',
'skip-check',
];
$core_download_arguments = '';
if ( ! empty( $assoc_args ) ) {
foreach ( $assoc_args as $key => $value ) {
$core_download_arguments .= in_array( $key, $core_download_args, true ) ? ' --' . $key . '=' . $value : '';
}
}
$config_arguments = '';
if ( ! empty( $assoc_args ) ) {
foreach ( $assoc_args as $key => $value ) {
$config_arguments .= in_array( $key, $config_args, true ) ? ' --' . $key . '=' . $value : '';
}
}