-
Notifications
You must be signed in to change notification settings - Fork 27
/
class-ee-site.php
2273 lines (1929 loc) · 72.4 KB
/
class-ee-site.php
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
namespace EE\Site\Type;
use AcmePhp\Ssl\Certificate;
use AcmePhp\Ssl\Parser\CertificateParser;
use EE;
use EE\Model\Cron;
use EE\Model\Site;
use EE\Model\Option;
use EE\Model\Auth;
use EE\Model\Whitelist;
use Symfony\Component\Filesystem\Filesystem;
use function EE\Site\Cloner\Utils\check_site_access;
use function EE\Site\Cloner\Utils\copy_site_db;
use function EE\Site\Cloner\Utils\copy_site_files;
use function EE\Site\Cloner\Utils\get_transfer_details;
use function EE\Site\Utils\get_domains_of_site;
use function EE\Site\Utils\get_preferred_ssl_challenge;
use function EE\Site\Utils\update_site_db_entry;
use function EE\Utils\download;
use function EE\Utils\extract_zip;
use function EE\Utils\get_flag_value;
use function EE\Utils\get_config_value;
use function EE\Utils\delem_log;
use function EE\Utils\remove_trailing_slash;
use function EE\Site\Utils\auto_site_name;
use function EE\Site\Utils\get_site_info;
use function EE\Site\Utils\reload_global_nginx_proxy;
use function EE\Site\Utils\get_parent_of_alias;
/**
* Base class for Site command
*
* @package ee
*/
abstract class EE_Site_Command {
/**
* @var Filesystem $fs Symfony Filesystem object.
*/
protected $fs;
/**
* @var bool $wildcard Whether the site is letsencrypt type is wildcard or not.
*/
private $wildcard;
/**
* @var bool $ssl Whether the site has SSL or not.
*/
private $ssl;
/**
* @var string $le_mail Mail id to be used for letsencrypt registration and certificate generation.
*/
private $le_mail;
/**
* @var array $site_data Associative array containing essential site related information.
*/
public $site_data;
/**
* @var array $site_meta Associative array containing essential site meta related information.
*/
public $site_meta;
public function __construct() {
$this->fs = new Filesystem();
pcntl_signal( SIGTERM, [ $this, 'rollback' ] );
pcntl_signal( SIGHUP, [ $this, 'rollback' ] );
pcntl_signal( SIGUSR1, [ $this, 'rollback' ] );
pcntl_signal( SIGINT, [ $this, 'rollback' ] );
$shutdown_handler = new Shutdown_Handler();
register_shutdown_function( [ $shutdown_handler, 'cleanup' ], [ &$this ] );
}
/**
* Lists the created websites.
* abstract list
*
* [--enabled]
* : List only enabled sites.
*
* [--disabled]
* : List only disabled sites.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - yaml
* - json
* - count
* - text
* ---
*
* ## EXAMPLES
*
* # List all sites
* $ ee site list
*
* # List enabled sites
* $ ee site list --enabled
*
* # List disabled sites
* $ ee site list --disabled
*
* # List all sites in JSON
* $ ee site list --format=json
*
* # Count all sites
* $ ee site list --format=count
*
* @subcommand list
*/
public function _list( $args, $assoc_args ) {
\EE\Utils\delem_log( 'site list start' );
$format = \EE\Utils\get_flag_value( $assoc_args, 'format' );
$enabled = \EE\Utils\get_flag_value( $assoc_args, 'enabled' );
$disabled = \EE\Utils\get_flag_value( $assoc_args, 'disabled' );
$sites = Site::all();
if ( $enabled && ! $disabled ) {
$sites = Site::where( 'site_enabled', true );
} elseif ( $disabled && ! $enabled ) {
$sites = Site::where( 'site_enabled', false );
}
if ( empty( $sites ) ) {
\EE::error( 'No sites found!' );
}
if ( 'text' === $format ) {
foreach ( $sites as $site ) {
\EE::log( $site->site_url );
}
} else {
$result = array_map(
function ( $site ) {
$site->site = $site->site_url;
$site->status = $site->site_enabled ? 'enabled' : 'disabled';
return $site;
}, $sites
);
$formatter = new \EE\Formatter( $assoc_args, [ 'site', 'status' ] );
$formatter->display_items( $result );
}
\EE\Utils\delem_log( 'site list end' );
}
/**
* Deletes a website.
*
* ## OPTIONS
*
* <site-name>
* : Name of website to be deleted.
*
* [--yes]
* : Do not prompt for confirmation.
*
* ## EXAMPLES
*
* # Delete site
* $ ee site delete example.com
*
*/
public function delete( $args, $assoc_args ) {
\EE\Utils\delem_log( 'site delete start' );
$this->site_data = get_site_info( $args, false );
$db_data = ( empty( $this->site_data['db_host'] ) || 'db' === $this->site_data['db_host'] ) ? [] : [
'db_host' => $this->site_data['db_host'],
'db_user' => $this->site_data['db_user'],
'db_name' => $this->site_data['db_name'],
];
\EE::confirm( sprintf( 'Are you sure you want to delete %s?', $this->site_data['site_url'] ), $assoc_args );
if ( $this->site_data['site_ssl'] ) {
$all_domains = array_unique( array_merge(
explode( ',', $this->site_data['alias_domains'] ),
[ $this->site_data['site_url'] ]
)
);
EE::log( 'Revoking certificate.' );
try {
$client = new Site_Letsencrypt();
$client->revokeAuthorizationChallenges( $all_domains );
$client->revokeCertificates( $all_domains );
} catch ( \Exception $e ) {
EE::warning( $e->getMessage() );
}
$client->removeDomain( $all_domains );
}
$this->delete_site( 5, $this->site_data['site_url'], $this->site_data['site_fs_path'], $db_data );
\EE\Utils\delem_log( 'site delete end' );
}
/**
* Function to delete the given site.
*
* @param int $level Level of deletion.
* Level - 0: No need of clean-up.
* Level - 1: Clean-up only the site-root.
* Level - 2: Try to remove network. The network may or may not have been created.
* Level - 3: Disconnect & remove network and try to remove containers. The containers
* may not have been created. Level - 4: Remove containers. Level - 5: Remove db entry.
* @param string $site_url Name of the site to be deleted.
* @param string $site_fs_path Webroot of the site.
* @param array $db_data Database host, user and password to cleanup db.
*
* @throws \EE\ExitException
*/
protected function delete_site( $level, $site_url, $site_fs_path, $db_data = [] ) {
$this->fs = new Filesystem();
if ( $level >= 3 ) {
if ( \EE_DOCKER::docker_compose_down( $site_fs_path ) ) {
\EE::log( "[$site_url] Docker Containers removed." );
} else {
\EE::exec( "docker rm -f $(docker ps -q -f=label=created_by=EasyEngine -f=label=site_name=$site_url)" );
if ( $level > 3 ) {
\EE::warning( 'Error in removing docker containers.' );
}
}
}
$volumes = \EE_DOCKER::get_volumes_by_label( $site_url );
foreach ( $volumes as $volume ) {
\EE::exec( 'docker volume rm ' . $volume );
}
$db_script_path = \EE\Utils\get_temp_dir() . 'db_exec';
if ( $this->fs->exists( $db_script_path ) ) {
try {
$this->fs->remove( $db_script_path );
} catch ( \Exception $e ) {
\EE::debug( $e );
}
}
if ( ! empty( $db_data['db_host'] ) ) {
\EE\Site\Utils\cleanup_db( $db_data['db_host'], $db_data['db_name'] );
\EE\Site\Utils\cleanup_db_user( $db_data['db_host'], $db_data['db_user'] );
}
if ( $this->fs->exists( $site_fs_path ) ) {
try {
$this->fs->remove( $site_fs_path );
} catch ( \Exception $e ) {
\EE::debug( $e );
\EE::error( 'Could not remove site root. Please check if you have sufficient rights.' );
}
\EE::log( "[$site_url] site root removed." );
}
\EE\Site\Utils\remove_etc_hosts_entry( $site_url );
$config_file_path = EE_ROOT_DIR . '/services/nginx-proxy/conf.d/' . $site_url . '-redirect.conf';
if ( $this->fs->exists( $config_file_path ) ) {
try {
$this->fs->remove( $config_file_path );
} catch ( \Exception $e ) {
\EE::debug( $e );
\EE::error( 'Could not remove site redirection file. Please check if you have sufficient rights.' );
}
}
$proxy_vhost_location = EE_ROOT_DIR . '/services/nginx-proxy/vhost.d/' . $site_url . '_location';
$proxy_conf_location = EE_ROOT_DIR . '/services/nginx-proxy/conf.d/' . $site_url . '.conf';
$proxy_vhost_location_subdom = EE_ROOT_DIR . '/services/nginx-proxy/vhost.d/*.' . $this->site_data['site_url'] . '_location';
$alias_domains = [];
if ( ! empty( $this->site_data['alias_domains'] ) ) {
$alias_domains = array_diff( explode( ',', $this->site_data['alias_domains'] ), [
$this->site_data['site_url'],
'*.' . $this->site_data['site_url'],
] );
}
$conf_locations = [ $proxy_conf_location, $proxy_vhost_location, $proxy_vhost_location_subdom ];
$reload = false;
foreach ( $conf_locations as $cl ) {
if ( $this->fs->exists( $cl ) ) {
$this->fs->remove( $cl );
$reload = true;
}
}
foreach ( $alias_domains as $ad ) {
$proxy_vhost_location = EE_ROOT_DIR . '/services/nginx-proxy/vhost.d/' . $ad . '_location';
if ( $this->fs->exists( $proxy_vhost_location ) ) {
$this->fs->remove( $proxy_vhost_location );
$reload = true;
}
}
if ( $reload ) {
\EE\Site\Utils\reload_global_nginx_proxy();
EE::exec( 'docker exec ' . EE_PROXY_TYPE . " bash -c 'rm -rf /var/cache/nginx/$site_url'" );
}
try {
$crons = Cron::where( [ 'site_url' => $site_url ] );
if ( ! empty( $crons ) ) {
\EE::log( 'Deleting cron entry' );
foreach ( $crons as $cron ) {
$cron->delete();
}
\EE\Cron\Utils\update_cron_config();
}
} catch ( \Exception $e ) {
\EE::debug( $e->getMessage() );
}
/**
* Execute before site db data cleanup and after site services cleanup.
* Note: This can be use to cleanup site data added by any package command.
*
* @param string $site_url Url of site which data is cleanup.
*/
\EE::do_hook( 'site_cleanup', $site_url );
if ( $level > 4 ) {
if ( $this->site_data['site_ssl'] ) {
\EE::log( 'Removing ssl certs and other config files.' );
$crt_file = EE_ROOT_DIR . "/services/nginx-proxy/certs/$site_url.crt";
$key_file = EE_ROOT_DIR . "/services/nginx-proxy/certs/$site_url.key";
$pem_file = EE_ROOT_DIR . "/services/nginx-proxy/certs/$site_url.chain.pem";
$conf_certs = EE_ROOT_DIR . "/services/nginx-proxy/acme-conf/certs/$site_url";
$conf_var = EE_ROOT_DIR . "/services/nginx-proxy/acme-conf/var/$site_url";
$htpasswd_file = EE_ROOT_DIR . "/services/nginx-proxy/htpasswd/$site_url";
$delete_files = [ $conf_certs, $conf_var, $crt_file, $key_file, $pem_file, $htpasswd_file ];
try {
$this->fs->remove( $delete_files );
} catch ( \Exception $e ) {
\EE::warning( $e );
}
}
$site_auth_file = EE_ROOT_DIR . '/services/nginx-proxy/htpasswd/' . $site_url;
if ( $this->fs->exists( $site_auth_file ) ) {
try {
$this->fs->remove( $site_auth_file );
} catch ( \Exception $e ) {
\EE::warning( $e );
}
reload_global_nginx_proxy();
}
$whitelists = Whitelist::where( [
'site_url' => $site_url,
] );
foreach ( $whitelists as $whitelist ) {
$whitelist->delete();
}
$auths = Auth::where( [
'site_url' => $site_url,
] );
foreach ( $auths as $auth ) {
$auth->delete();
}
if ( Site::find( $site_url )->delete() ) {
\EE::log( 'Removed database entry.' );
} else {
\EE::error( 'Could not remove the database entry' );
}
}
\EE::success( "Site $site_url deleted." );
}
/**
* Supports updating and upgrading site.
*
* [<site-name>]
* : Name of the site.
*
* [--ssl[=<ssl>]]
* : Update ssl on site
* ---
* options:
* - le
* - self
* - inherit
* - custom
* - "off"
* ---
*
* [--wildcard]
* : Enable wildcard SSL on site.
*
* [--php=<php-version>]
* : PHP version for site. Currently only supports PHP 5.6, 7.0, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3 and 8.4.
* ---
* options:
* - 5.6
* - 7.0
* - 7.2
* - 7.3
* - 7.4
* - 8.0
* - 8.1
* - 8.2
* - 8.3
* - 8.4
* ---
*
* [--proxy-cache=<on-or-off>]
* : Enable or disable proxy cache on site.
* ---
* options:
* - on
* - off
* ---
*
* [--proxy-cache-max-size=<size-in-m-or-g>]
* : Max size for proxy-cache.
*
* [--proxy-cache-max-time=<time-in-s-or-m>]
* : Max time for proxy cache to last.
*
* [--proxy-cache-key-zone-size=<size-in-m>]
* : Size of proxy cache key zone.
*
* [--add-alias-domains=<comma-seprated-domains-to-add>]
* : Comma seprated list of domains to add to site's alias domains.
*
* [--delete-alias-domains=<comma-seprated-domains-to-delete>]
* : Comma seprated list of domains to delete from site's alias domains.
*
* ## EXAMPLES
*
* # Add SSL to non-ssl site
* $ ee site update example.com --ssl=le
*
* # Add SSL to non-ssl site
* $ ee site update example.com --ssl=le --wildcard
*
* # Add self-signed SSL to non-ssl site
* $ ee site update example.com --ssl=self
*
* # Update PHP version of site.
* $ ee site update example.com --php=8.0
*
* # Update proxy cache of site.
* $ ee site update example.com --proxy-cache=on
*
* # Update proxy cache of site.
* $ ee site update example.com --proxy-cache=on --proxy-cache-max-size=1g --proxy-cache-max-time=30s
*
* # Add alias domains to a WordPress subdom site.
* $ ee site update example.com --add-alias-domains='a.com,*.a.com,b.com,c.com'
*
* # Delete alias domains from a WordPress subdom site.
* $ ee site update example.com --delete-alias-domains='a.com,*.a.com,b.com'
*/
public function update( $args, $assoc_args ) {
delem_log( 'site update start' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, true, true, false );
$ssl = get_flag_value( $assoc_args, 'ssl', false );
$php = get_flag_value( $assoc_args, 'php', false );
$proxy_cache = get_flag_value( $assoc_args, 'proxy-cache', false );
$add_domains = get_flag_value( $assoc_args, 'add-alias-domains', false );
$delete_domains = get_flag_value( $assoc_args, 'delete-alias-domains', false );
if ( $ssl ) {
$this->update_ssl( $assoc_args );
}
if ( $php ) {
$this->update_php( $args, $assoc_args );
}
if ( $proxy_cache ) {
$this->update_proxy_cache( $args, $assoc_args );
}
if ( $add_domains || $delete_domains ) {
$this->update_alias_domains( $args, $assoc_args );
}
}
/**
* Function to update alias domains of a site.
*/
protected function update_alias_domains( $args, $assoc_args ) {
$add_domains = get_flag_value( $assoc_args, 'add-alias-domains', false );
$delete_domains = get_flag_value( $assoc_args, 'delete-alias-domains', false );
try {
$site = $this->site_data;
$array_data = (array) $this->site_data;
$this->site_data = reset( $array_data );
// Validate data.
$existing_alias_domains = [];
$domains_to_add = [];
$domains_to_delete = [];
if ( ! empty( $this->site_data['alias_domains'] ) ) {
$existing_alias_domains = explode( ',', $this->site_data['alias_domains'] );
}
if ( ! empty( $add_domains ) ) {
$domains_to_add = explode( ',', $add_domains );
}
if ( ! empty( $delete_domains ) ) {
$domains_to_delete = explode( ',', $delete_domains );
}
$already_added_domains = array_intersect( $existing_alias_domains, $domains_to_add );
$domains_to_add = array_diff( $domains_to_add, $existing_alias_domains );
if ( empty( $domains_to_add ) && $add_domains ) {
$already_added_domains = implode( ',', $already_added_domains );
EE::error( "Alias domains: $already_added_domains is/are already present on the site." );
}
if ( ! empty ( $already_added_domains ) ) {
$already_added_domains = implode( ',', $already_added_domains );
EE::warning( "Following domains: $already_added_domains is/are already present on site, skipping addition of those." );
}
if ( ! empty( $domains_to_delete ) ) {
// Handle primary site in deletion.
$diff_delete_domains = array_diff( $domains_to_delete, $existing_alias_domains );
if ( in_array( $this->site_data['site_url'], $domains_to_delete ) ) {
EE::error( 'Primary site domain: `' . $this->site_data['site_url'] . '` can not be deleted.' );
}
if ( ! empty( $diff_delete_domains ) ) {
EE::error( "Domains to delete is/are not a subset of already existing alias domains." );
}
}
foreach ( $domains_to_add as $ad ) {
if ( Site::find( $ad ) ) {
\EE::error( sprintf( "%1\$s already exists as a site. If you want to add it to alias domain of this site, then please delete the existing site using:\n`ee site delete %1\$s`", $ad ) );
}
$parent_site = get_parent_of_alias( $ad );
if ( ! empty( $parent_site ) ) {
\EE::error( sprintf( "Site %1\$s already exists as an alias domain for site: %2\$s. Please delete it from alias domains of %2\$s if you want to add it as an alias domain for this site.", $ad, $parent_site ) );
}
}
$final_alias_domains = array_merge( $existing_alias_domains, $domains_to_add );
$final_alias_domains = array_diff( $final_alias_domains, $domains_to_delete );
$this->site_data['alias_domains'] = implode( ',', $final_alias_domains );
$is_ssl = $this->site_data['site_ssl'] ? true : false;
$preferred_ssl_challenge = get_preferred_ssl_challenge( get_domains_of_site( $this->site_data['site_url'] ) );
$nohttps = $is_ssl && 'dns' !== $preferred_ssl_challenge;
$this->dump_docker_compose_yml( [ 'nohttps' => $nohttps ] );
\EE_DOCKER::docker_compose_up( $this->site_data['site_fs_path'], [ 'nginx' ] );
} catch ( \Exception $e ) {
EE::error( $e->getMessage() );
}
$this->site_data['alias_domains'] = implode( ',', $final_alias_domains );
$all_domains = $final_alias_domains;
array_push( $all_domains, $this->site_data['site_url'] );
$all_domains = array_unique( $all_domains );
foreach ( $all_domains as $domain ) {
if ( '*.' . $this->site_data['site_url'] === $domain ) {
$this->site_data['site_ssl_wildcard'] = "1";
}
}
$client = new Site_Letsencrypt();
$old_certs = $client->loadDomainCertificates( $all_domains );
if ( $is_ssl ) {
// Update SSL.
EE::log( 'Updating and force renewing SSL certificate to accomodated alias domain changes.' );
try {
$this->ssl_renew( [ $this->site_data['site_url'] ], [ 'force' => true ] );
} catch ( \Exception $e ) {
EE::warning( 'Certificate could not be issued. Reverting back to original state.' );
$this->enable( [ $this->site_data['site_url'] ], [ 'refresh' => 'true' ] );
EE::error( $e->getMessage() );
}
}
// Revoke old certificate which will not be used
$client->revokeCertificates( $old_certs );
chdir( $this->site_data['site_fs_path'] );
// Required as env variables have changed.
\EE_DOCKER::docker_compose_up( $this->site_data['site_fs_path'], [ 'nginx' ] );
EE::success( 'Alias domains updated on site ' . $this->site_data['site_url'] . '.' );
try {
update_site_db_entry( $this->site_data['site_url'], $this->site_data );
} catch ( \Exception $e ) {
EE::error( $e->getMessage() );
}
if ( ! empty( $this->site_data['proxy_cache'] ) && 'on' === $this->site_data['proxy_cache'] ) {
EE::log( 'As proxy cache is enabled on this site, updating config to enable it in newly added alias domains.' );
$this->site_data = get_site_info( $args, true, true, false );
$assoc_args = [
'proxy-cache' => 'on',
'force' => true,
];
$this->update_proxy_cache( $args, $assoc_args );
}
delem_log( 'site alias domains update end' );
}
/**
* Function to enable/disable proxy cache of a site.
*/
protected function update_proxy_cache( $args, $assoc_args, $call_on_create = false ) {
$proxy_cache = get_flag_value( $assoc_args, 'proxy-cache', 'on' );
$force = get_flag_value( $assoc_args, 'force', false );
if ( ! $call_on_create ) {
if ( $proxy_cache === $this->site_data->proxy_cache && ! $force ) {
EE::error( 'Site ' . $this->site_data->site_url . ' already has proxy cache: ' . $proxy_cache );
}
}
$log_message = ( $proxy_cache === 'on' ) ? 'Enabling' : 'Disabling';
try {
if ( ! $call_on_create ) {
$site = $this->site_data;
$array_data = (array) $this->site_data;
$this->site_data = reset( $array_data );
}
$proxy_conf_location = EE_ROOT_DIR . '/services/nginx-proxy/conf.d/' . $this->site_data['site_url'] . '.conf';
$proxy_vhost_location = EE_ROOT_DIR . '/services/nginx-proxy/vhost.d/' . $this->site_data['site_url'] . '_location';
$proxy_vhost_location_subdom = EE_ROOT_DIR . '/services/nginx-proxy/vhost.d/*.' . $this->site_data['site_url'] . '_location';
$proxy_cache_time = strtolower( get_flag_value( $assoc_args, 'proxy-cache-max-time', '1s' ) );
$proxy_cache_size = strtolower( get_flag_value( $assoc_args, 'proxy-cache-max-size', '256m' ) );
$proxy_cache_key_zone_size = strtolower( get_flag_value( $assoc_args, 'proxy-cache-key-zone-size', '10m' ) );
$wrong_time = false;
$wrong_size = false;
$wrong_key_size = false;
in_array( substr( $proxy_cache_time, - 1 ), [ 's', 'm' ] ) ?: $wrong_time = true;
in_array( substr( $proxy_cache_size, - 1 ), [ 'm', 'g' ] ) ?: $wrong_size = true;
in_array( substr( $proxy_cache_key_zone_size, - 1 ), [ 'm' ] ) ?: $wrong_key_size = true;
is_numeric( substr( $proxy_cache_time, 0, - 1 ) ) ?: $wrong_time = true;
is_numeric( substr( $proxy_cache_size, 0, - 1 ) ) ?: $wrong_size = true;
is_numeric( substr( $proxy_cache_key_zone_size, 0, - 1 ) ) ?: $wrong_key_size = true;
if ( $wrong_time ) {
EE::warning( "Wrong value `$proxy_cache_time` supplied to param: `proxy-cache-max-time`, replacing it with default:1s" );
$proxy_cache_time = '1s';
}
if ( $wrong_size ) {
EE::warning( "Wrong value `$proxy_cache_size` supplied to param: `proxy-cache-max-size`, replacing it with default:256m" );
$proxy_cache_size = '256m';
}
if ( $wrong_key_size ) {
EE::warning( "Wrong value `$proxy_cache_key_zone_size` supplied to param: `proxy-cache-key-zone-size`, replacing it with default:10m" );
$proxy_cache_key_zone_size = '10m';
}
if ( $force ) {
EE::log( $log_message . ' proxy cache for alias domains of site: ' . $this->site_data['site_url'] );
} else {
EE::log( $log_message . ' proxy cache for: ' . $this->site_data['site_url'] );
}
$alias_domains = [];
if ( ! empty( $this->site_data['alias_domains'] ) ) {
$alias_domains = array_diff( explode( ',', $this->site_data['alias_domains'] ), [
$this->site_data['site_url'],
'*.' . $this->site_data['site_url'],
] );
}
if ( 'on' === $proxy_cache ) {
$sanitized_site_url = str_replace( '.', '-', $this->site_data['site_url'] );
$data = [
'site_url' => $this->site_data['site_url'],
'sanitized_site_url' => $sanitized_site_url,
'proxy_cache_size' => $proxy_cache_size,
'proxy_cache_key_zone_size' => $proxy_cache_key_zone_size,
'proxy_cache_time' => $proxy_cache_time,
'easyengine_version' => 'v' . EE_VERSION,
];
$proxy_conf_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx-proxy/proxy.conf.mustache', $data );
if ( ! $force ) {
$this->fs->dumpFile( $proxy_conf_location, $proxy_conf_content );
}
$proxy_vhost_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx-proxy/vhost_location.conf.mustache', $data );
$this->fs->dumpFile( $proxy_vhost_location, $proxy_vhost_content );
if ( 'subdom' === $this->site_data['app_sub_type'] ) {
$this->fs->dumpFile( $proxy_vhost_location_subdom, $proxy_vhost_content );
}
foreach ( $alias_domains as $ad ) {
$proxy_vhost_location = EE_ROOT_DIR . '/services/nginx-proxy/vhost.d/' . $ad . '_location';
$proxy_vhost_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx-proxy/vhost_location.conf.mustache', $data );
$this->fs->dumpFile( $proxy_vhost_location, $proxy_vhost_content );
}
} else {
$reload = false;
$conf_locations = [ $proxy_conf_location, $proxy_vhost_location, $proxy_vhost_location_subdom ];
$reload = false;
foreach ( $conf_locations as $cl ) {
if ( $this->fs->exists( $cl ) ) {
$this->fs->remove( $cl );
$reload = true;
}
}
foreach ( $alias_domains as $ad ) {
$proxy_vhost_location = EE_ROOT_DIR . '/services/nginx-proxy/vhost.d/' . $ad . '_location';
if ( $this->fs->exists( $proxy_vhost_location ) ) {
$this->fs->remove( $proxy_vhost_location );
$reload = true;
}
}
if ( $reload ) {
\EE\Site\Utils\reload_global_nginx_proxy();
EE::exec( 'docker exec ' . EE_PROXY_TYPE . " bash -c 'rm -rf /var/cache/nginx/" . $this->site_data['site_url'] . "'" );
}
}
if ( EE::exec( 'docker exec ' . EE_PROXY_TYPE . " bash -c 'nginx -t'" ) ) {
\EE\Site\Utils\reload_global_nginx_proxy();
EE::exec( 'docker restart ' . EE_PROXY_TYPE );
} else {
$this->fs->remove( $proxy_conf_location );
$this->fs->remove( $proxy_vhost_location );
$this->fs->remove( $proxy_vhost_location_subdom );
\EE\Site\Utils\reload_global_nginx_proxy();
}
} catch ( \Exception $e ) {
EE::error( $e->getMessage() );
}
if ( ! $call_on_create ) {
$site->proxy_cache = $proxy_cache;
$site->save();
}
EE::success( 'Proxy cache update on site ' . $this->site_data['site_url'] . ' completed.' );
delem_log( 'site proxy cache update end' );
}
/**
* Function to update php version of a site.
*/
protected function update_php( $args, $assoc_args ) {
$php_version = get_flag_value( $assoc_args, 'php', false );
if ( $php_version === $this->site_data->php_version ) {
EE::error( 'Site ' . $this->site_data->site_url . ' is already at PHP version: ' . $php_version );
}
EE::log( 'Starting php version update for: ' . $this->site_data->site_url );
try {
$old_php_version = $this->site_data->php_version;
$this->site_data->php_version = $php_version;
$no_https = $this->site_data->site_ssl ? false : true;
$site = $this->site_data;
$array_data = ( array ) $this->site_data;
$this->site_data = reset( $array_data );
EE::log( 'Taking backup of old php config.' );
$site_backup_dir = $this->site_data['site_fs_path'] . '/.backup';
$php_conf_backup_dir = $site_backup_dir . '/config/php-' . $old_php_version;
$php_conf_dir = $this->site_data['site_fs_path'] . '/config/php';
$php_confd_dir = $this->site_data['site_fs_path'] . '/config/php/php/conf.d';
$this->fs->mkdir( $php_conf_backup_dir );
$this->fs->mirror( $php_conf_dir, $php_conf_backup_dir );
$this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] );
EE::log( 'Starting site with new PHP version: ' . $php_version . '. This may take sometime.' );
$this->enable( $args, [ 'force' => true ] );
EE::log( 'Updating php config.' );
$temp_dir = EE\Utils\get_temp_dir();
$zip_path = $temp_dir . "phpconf-$php_version.zip";
$unzip_folder = $temp_dir . "php-$php_version";
$scanned_files = scandir( $php_conf_dir );
$diff = [ '.', '..' ];
$removal_files = array_diff( $scanned_files, $diff );
$this->fs->copy( SITE_TEMPLATE_ROOT . '/config/php-fpm/php' . str_replace( '.', '', $php_version ) . '.zip', $zip_path );
extract_zip( $zip_path, $unzip_folder );
chdir( $php_conf_dir );
$this->fs->remove( $removal_files );
$this->fs->mirror( $unzip_folder, $php_conf_dir );
$this->fs->remove( [ $zip_path, $unzip_folder ] );
$this->fs->chown( $php_confd_dir, 'www-data', true );
// Recover previous custom configs.
EE::log( 'Re-applying previous custom.ini and easyengine.conf changes.' );
$this->fs->copy( $php_conf_backup_dir . '/php/conf.d/custom.ini', $php_conf_dir . '/php/conf.d/custom.ini', true );
$this->fs->copy( $php_conf_backup_dir . '/php-fpm.d/easyengine.conf', $php_conf_dir . '/php-fpm.d/easyengine.conf', true );
if ( '5.6' == $old_php_version ) {
$this->sendmail_path_update( true );
} elseif ( '5.6' == $php_version ) {
$this->sendmail_path_update( false );
}
EE::exec( "chown -R www-data: $php_conf_dir" );
} catch ( \Exception $e ) {
EE::error( $e->getMessage() );
}
$site->save();
$this->restart( $args, [ 'php' => true ] );
EE::success( 'Updated site ' . $this->site_data['site_url'] . ' to PHP version: ' . $php_version );
delem_log( 'site php version update end' );
}
protected function sendmail_path_update( $msmtp ) {
$custom_ini_path = $this->site_data['site_fs_path'] . '/config/php/php/conf.d/custom.ini';
$custom_ini_data = file( $custom_ini_path );
if ( $msmtp ) {
$custom_ini_data = array_map( function ( $custom_ini_data ) {
$sendmail_path = 'sendmail_path = /usr/bin/msmtp -t';
return stristr( $custom_ini_data, 'sendmail_path' ) ? "$sendmail_path\n" : $custom_ini_data;
}, $custom_ini_data );
} else {
$custom_ini_data = array_map( function ( $custom_ini_data ) {
$sendmail_path = 'sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]';
return stristr( $custom_ini_data, 'sendmail_path' ) ? "$sendmail_path\n" : $custom_ini_data;
}, $custom_ini_data );
}
file_put_contents( $custom_ini_path, implode( '', $custom_ini_data ) );
}
/**
* Function to update ssl of a site.
*/
protected function update_ssl( $assoc_args ) {
$ssl = EE\Utils\get_value_if_flag_isset( $assoc_args, 'ssl', 'le' );
$wildcard = get_flag_value( $assoc_args, 'wildcard', false );
if ( $ssl === 'off' ) {
$ssl = false;
}
if ( ! $this->site_data->site_ssl_wildcard && $wildcard ) {
EE::error( 'Update from normal SSL to wildcard SSL is not supported yet.' );
}
if ( $this->site_data->site_ssl_wildcard && ! $wildcard ) {
EE::error( 'Update from wildcard SSL to normal SSL is not supported yet.' );
}
if ( $this->site_data->site_ssl && $ssl ) {
EE::error( 'SSL is already enabled on ' . $this->site_data->site_url );
}
if ( ! $this->site_data->site_ssl && ! $ssl ) {
EE::error( 'SSL is already disabled on ' . $this->site_data->site_url );
}
if ( ! $ssl && $wildcard ) {
EE::error( 'You cannot use --wildcard flag with --ssl=off' );
}
EE::log( 'Starting SSL update for: ' . $this->site_data->site_url );
try {
$this->site_data->site_ssl = $ssl;
$this->site_data->site_ssl_wildcard = $wildcard ? 1 : 0;
$site = $this->site_data;
$array_data = ( array ) $this->site_data;
$this->site_data = reset( $array_data );
$this->site_data['site_ssl'] = $ssl;
if ( $ssl ) {
$this->www_ssl_wrapper( [ 'nginx' ] );
} else {
$this->disable_ssl();
}
$site->site_ssl = $ssl;
} catch ( \Exception $e ) {
EE::error( $e->getMessage() );
}
$site->save();
if ( $ssl ) {
EE::success( 'Enabled SSL for ' . $this->site_data['site_url'] );
} else {
EE::success( 'Disabled SSL for ' . $this->site_data['site_url'] );
}
delem_log( 'site ssl update end' );
}
/**
* Disables SSL on a site.
*
* @throws \Exception
*/
private function disable_ssl() {
$this->dump_docker_compose_yml( [ 'nohttps' => true ] );
\EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx' ] );
\EE\Site\Utils\reload_global_nginx_proxy();
}
/**
* Enables a website. It will start the docker containers of the website if they are stopped.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to be enabled.
*
* [--force]
* : Force execution of site enable.
*
* [--verify]
* : Verify if required global services are working.
*
* [--refresh]
* : Force enable after regenerating docker-compose.yml of a site.
*
* ## EXAMPLES
*
* # Enable site
* $ ee site enable example.com
*
* # Enable site with verification of dependent global services. (Note: This takes longer time to enable the
* site.)
* $ ee site enable example.com --verify
*
* # Force enable a site.
* $ ee site enable example.com --force