-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclass-ee-site.php
More file actions
1324 lines (1143 loc) · 40.9 KB
/
class-ee-site.php
File metadata and controls
1324 lines (1143 loc) · 40.9 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
namespace EE\Site\Type;
use EE;
use EE\Model\Site;
use EE\Model\Option;
use Symfony\Component\Filesystem\Filesystem;
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\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\Utils\remove_trailing_slash;
/**
* 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.
*/
protected $site_data;
/**
* @var array $site_meta Associative array containing essential site meta related information.
*/
protected $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 );
$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 );
}
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." );
}
$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.' );
}
}
/**
* 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.' );
$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";
$cert_files = [ $conf_certs, $conf_var, $crt_file, $key_file, $pem_file ];
try {
$this->fs->remove( $cert_files );
} catch ( \Exception $e ) {
\EE::warning( $e );
}
}
if ( Site::find( $site_url )->delete() ) {
\EE::log( 'Removed database entry.' );
} else {
\EE::error( 'Could not remove the database entry' );
}
}
\EE::log( "Site $site_url deleted." );
}
/**
* Supports updating and upgrading site.
*
* [<site-name>]
* : Name of the site.
*
* [--ssl=<ssl>]
* : Enable ssl on site
*
* [--wildcard]
* : Enable wildcard SSL on site.
*
* ## 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
*
*/
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 );
if ( $ssl ) {
$this->update_ssl( $assoc_args );
}
}
/**
* Funciton to update ssl of a site.
*/
protected function update_ssl( $assoc_args ) {
$ssl = get_flag_value( $assoc_args, 'ssl', false );
$wildcard = get_flag_value( $assoc_args, 'wildcard', false );
$show_error = $this->site_data->site_ssl ? true : false;
$wildcard_error = ( ! $this->site_data->site_ssl_wildcard && $wildcard ) ? true : false;
$error = $wildcard_error ? 'Update from normal ssl to wildcard is not supported yet.' : 'Site ' . $this->site_data->site_url . ' already contains SSL.';
if ( $show_error ) {
EE::error( $error );
}
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;
$this->www_ssl_wrapper( [ 'nginx' ] );
$site->site_ssl = $ssl;
} catch ( \Exception $e ) {
EE::error( $e->getMessage() );
}
$site->save();
EE::success( 'Enabled ssl for ' . $this->site_data['site_url'] );
delem_log( 'site ssl update end' );
}
/**
* 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.
*
* ## 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
*/
public function enable( $args, $assoc_args, $exit_on_error = true ) {
\EE\Utils\delem_log( 'site enable start' );
$force = \EE\Utils\get_flag_value( $assoc_args, 'force' );
$verify = \EE\Utils\get_flag_value( $assoc_args, 'verify' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, false, true, false );
if ( $this->site_data->site_enabled && ! $force ) {
\EE::error( sprintf( '%s is already enabled!', $this->site_data->site_url ) );
}
if ( $verify ) {
$this->verify_services();
}
\EE::log( sprintf( 'Enabling site %s.', $this->site_data->site_url ) );
$success = false;
$containers_to_start = [ 'nginx' ];
if ( \EE_DOCKER::docker_compose_up( $this->site_data->site_fs_path, $containers_to_start ) ) {
$this->site_data->site_enabled = 1;
$this->site_data->save();
$success = true;
}
if ( $success ) {
\EE::success( sprintf( 'Site %s enabled.', $this->site_data->site_url ) );
} else {
$err_msg = sprintf( 'There was error in enabling %s. Please check logs.', $this->site_data->site_url );
if ( $exit_on_error ) {
\EE::error( $err_msg );
}
throw new \Exception( $err_msg );
}
\EE::log( 'Running post enable configurations.' );
$postfix_exists = \EE_DOCKER::service_exists( 'postfix', $this->site_data->site_fs_path );
$containers_to_start = $postfix_exists ? [ 'nginx', 'postfix' ] : [ 'nginx' ];
\EE\Site\Utils\start_site_containers( $this->site_data->site_fs_path, $containers_to_start );
$site_data_array = (array) $this->site_data;
$this->site_data = reset( $site_data_array );
$this->www_ssl_wrapper( $containers_to_start, true );
if ( $postfix_exists ) {
\EE\Site\Utils\configure_postfix( $this->site_data['site_url'], $this->site_data['site_fs_path'] );
}
if ( true === (bool) $this->site_data['admin_tools'] ) {
$admin_tools = new \Admin_Tools_Command();
$admin_tools->enable( [ $this->site_data['site_url'] ], [ 'force' => true ] );
}
if ( true === (bool) $this->site_data['mailhog_enabled'] ) {
$mailhog = new \Mailhog_Command();
$mailhog->enable( [ $this->site_data['site_url'] ], [ 'force' => true ] );
}
\EE::success( 'Post enable configurations complete.' );
\EE\Utils\delem_log( 'site enable end' );
}
/**
* Disables a website. It will stop and remove the docker containers of the website if they are running.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to be disabled.
*
* ## EXAMPLES
*
* # Disable site
* $ ee site disable example.com
*
*/
public function disable( $args, $assoc_args ) {
\EE\Utils\delem_log( 'site disable start' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, false, true, false );
\EE::log( sprintf( 'Disabling site %s.', $this->site_data->site_url ) );
$fs = new Filesystem();
$redirect_config_file_path = EE_ROOT_DIR . '/services/nginx-proxy/conf.d/' . $args[0] . '-redirect.conf';
if ( $fs->exists( $redirect_config_file_path ) ) {
$fs->remove( $redirect_config_file_path );
\EE\Site\Utils\reload_global_nginx_proxy();
}
if ( \EE_DOCKER::docker_compose_down( $this->site_data->site_fs_path ) ) {
$this->site_data->site_enabled = 0;
$this->site_data->save();
\EE::success( sprintf( 'Site %s disabled.', $this->site_data->site_url ) );
} else {
\EE::error( sprintf( 'There was error in disabling %s. Please check logs.', $this->site_data->site_url ) );
}
\EE\Utils\delem_log( 'site disable end' );
}
/**
* Restarts containers associated with site.
* When no service(--nginx etc.) is specified, all site containers will be restarted.
*
* [<site-name>]
* : Name of the site.
*
* [--all]
* : Restart all containers of site.
*
* [--nginx]
* : Restart nginx container of site.
*
* ## EXAMPLES
*
* # Restart all containers of site
* $ ee site restart example.com
*
*/
public function restart( $args, $assoc_args, $whitelisted_containers = [] ) {
\EE\Utils\delem_log( 'site restart start' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$all = \EE\Utils\get_flag_value( $assoc_args, 'all' );
$no_service_specified = count( $assoc_args ) === 0;
$this->site_data = get_site_info( $args );
if ( $all || $no_service_specified ) {
$containers = $whitelisted_containers;
} else {
$containers = array_keys( $assoc_args );
}
if ( in_array( 'nginx', $containers ) ) {
$nginx_test_command = "sh -c 'nginx -t'";
if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], 'nginx', $nginx_test_command ) ) {
throw new \Exception( 'There was some error in docker-compose exec.' );
}
}
$all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers;
if ( ! \EE_DOCKER::docker_compose_restart( $this->site_data['site_fs_path'], $all_containers ) ) {
throw new \Exception( 'There was some error in docker-compose restart.' );
}
\EE\Utils\delem_log( 'site restart stop' );
}
/**
* Clears Object and Page cache for site.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to be enabled.
*
* [--page]
* : Clear page cache.
*
* [--object]
* : Clear object cache.
*
* ## EXAMPLES
*
* # Clear Both cache type for site.
* $ ee site clean example.com
*
* # Clear Object cache for site.
* $ ee site clean example.com --object
*
* # Clear Page cache for site.
* $ ee site clean example.com --page
*/
public function clean( $args, $assoc_args ) {
\EE\Utils\delem_log( 'site clean start' );
$object = \EE\Utils\get_flag_value( $assoc_args, 'object' );
$page = \EE\Utils\get_flag_value( $assoc_args, 'page' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, false, true, false );
$purge_key = '';
$error = [];
// No param passed.
if ( empty( $object ) && empty( $page ) ) {
$object = true;
$page = true;
}
// Object cache clean.
if ( ! empty( $object ) ) {
if ( 1 === intval( $this->site_data->cache_mysql_query ) ) {
$purge_key = $this->site_data->site_url . '_obj';
} else {
$error[] = 'Site object cache is not enabled.';
}
}
// Page cache clean.
if ( ! empty( $page ) ) {
if ( 1 === intval( $this->site_data->cache_nginx_fullpage ) ) {
$purge_key = $this->site_data->site_url . '_page';
} else {
$error[] = 'Site page cache is not enabled.';
}
}
// If Page and Object both passed.
if ( ! empty( $object ) && ! empty( $page ) ) {
$purge_key = $this->site_data->site_url;
}
if ( ! empty( $error ) ) {
\EE::error( implode( ' ', $error ) );
}
EE\Site\Utils\clean_site_cache( $purge_key );
if ( $page ) {
\EE::success( 'Page cache cleared for ' . $this->site_data->site_url );
}
if ( $object ) {
\EE::success( 'Object cache cleared for ' . $this->site_data->site_url );
}
\EE\Utils\delem_log( 'site clean end' );
}
/**
* Reload services in containers without restarting container(s) associated with site.
* When no service(--nginx etc.) is specified, all services will be reloaded.
*
* [<site-name>]
* : Name of the site.
*
* [--all]
* : Reload all services of site(which are supported).
*
* [--nginx]
* : Reload nginx service in container.
*
* ## EXAMPLES
*
* # Reload all containers of site
* $ ee site reload example.com
*
*/
public function reload( $args, $assoc_args, $whitelisted_containers = [], $reload_commands = [] ) {
\EE\Utils\delem_log( 'site reload start' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$all = \EE\Utils\get_flag_value( $assoc_args, 'all' );
$reload_commands['php'] = "bash -c 'kill -USR2 1'";
$reload_commands['nginx'] = "sh -c 'nginx -t && service openresty reload'";
$no_service_specified = count( $assoc_args ) === 0;
$this->site_data = get_site_info( $args );
if ( $all || $no_service_specified ) {
foreach ( $whitelisted_containers as $container ) {
if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], $container, $reload_commands[ $container ] ) ) {
throw new \Exception( 'There was some error in docker-compose exec.' );
}
}
} else {
foreach ( array_keys( $assoc_args ) as $container ) {
if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], $container, $reload_commands[ $container ] ) ) {
throw new \Exception( 'There was some error in docker-compose exec.' );
}
}
}
\EE\Utils\delem_log( 'site reload stop' );
}
/**
* Function to verify and check the global services dependent for given site.
* Enables the dependent service if it is down.
*/
private function verify_services() {
if ( 'running' !== \EE_DOCKER::container_status( EE_PROXY_TYPE ) ) {
EE\Service\Utils\nginx_proxy_check();
}
if ( 'global-db' === $this->site_data->db_host ) {
EE\Service\Utils\init_global_container( GLOBAL_DB );
}
if ( 'global-redis' === $this->site_data->cache_host ) {
EE\Service\Utils\init_global_container( GLOBAL_REDIS );
}
}
/**
* Function to add site redirects and initialise ssl process.
*
* @param array $containers_to_start Containers to start for that site. Default, empty will start all.
* @param bool $force Force ssl renewal.
* @param bool $renew True if function is being used for cert renewal.
*
* @throws EE\ExitException
* @throws \Exception
*/
protected function www_ssl_wrapper( $containers_to_start = [], $site_enable = false, $force = false, $renew = false ) {
/**
* This adds http www redirection which is needed for issuing cert for a site.
* i.e. when you create example.com site, certs are issued for example.com and www.example.com
*
* We're issuing certs for both domains as it is needed in order to perform redirection of
* https://www.example.com -> https://example.com
*
* We add redirection config two times in case of ssl as we need http redirection
* when certs are being requested and http+https redirection after we have certs.
*/
\EE\Site\Utils\add_site_redirects( $this->site_data['site_url'], false, 'inherit' === $this->site_data['site_ssl'] );
\EE\Site\Utils\reload_global_nginx_proxy();
// Need second reload sometimes for changes to reflect.
\EE\Site\Utils\reload_global_nginx_proxy();
$is_www_or_non_www_pointed = $this->check_www_or_non_www_domain( $this->site_data['site_url'], $this->site_data['site_fs_path'] ) || $this->site_data['site_ssl_wildcard'];
if ( ! $is_www_or_non_www_pointed ) {
$fs = new Filesystem();
$confd_path = EE_ROOT_DIR . '/services/nginx-proxy/conf.d/';
$config_file = $confd_path . $this->site_data['site_url'] . '-redirect.conf';
$fs->remove( $config_file );
\EE\Site\Utils\reload_global_nginx_proxy();
}
if ( $this->site_data['site_ssl'] ) {
if ( ! $site_enable ) {
if ( 'custom' !== $this->site_data['site_ssl'] ) {
$this->init_ssl( $this->site_data['site_url'], $this->site_data['site_fs_path'], $this->site_data['site_ssl'], $this->site_data['site_ssl_wildcard'], $is_www_or_non_www_pointed, $force );
}
if ( ! $renew ) {
$this->dump_docker_compose_yml( [ 'nohttps' => false ] );
}
\EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], $containers_to_start );
}
if ( $is_www_or_non_www_pointed ) {
\EE\Site\Utils\add_site_redirects( $this->site_data['site_url'], true, 'inherit' === $this->site_data['site_ssl'] );
}
\EE\Site\Utils\reload_global_nginx_proxy();
}
}
/**
* Runs the acme le registration and authorization.
*
* @param string $site_url Name of the site for ssl.
*
* @throws \Exception
*/
protected function check_parent_site_certs( $site_url ) {
$parent_site_name = implode( '.', array_slice( explode( '.', $site_url ), 1 ) );
$parent_site = Site::find( $parent_site_name, [ 'site_ssl', 'site_ssl_wildcard' ] );
if ( ! $parent_site ) {
throw new \Exception( 'Unable to find existing site: ' . $parent_site_name );
}
if ( ! $parent_site->site_ssl ) {
throw new \Exception( "Cannot inherit from $parent_site_name as site does not have SSL cert" . var_dump( $parent_site ) );
}
if ( ! $parent_site->site_ssl_wildcard ) {
throw new \Exception( "Cannot inherit from $parent_site_name as site does not have wildcard SSL cert" );
}
}
/**
* Runs SSL procedure.
*
* @param string $site_url Name of the site for ssl.
* @param string $site_fs_path Webroot of the site.
* @param string $ssl_type Type of ssl cert to issue.
* @param bool $wildcard SSL with wildcard or not.
* @param bool $www_or_non_www Allow LetsEncrypt on www or non-www subdomain.
* @param bool $force Force ssl renewal.
*
* @throws \EE\ExitException If --ssl flag has unrecognized value.
* @throws \Exception
*/
protected function init_ssl( $site_url, $site_fs_path, $ssl_type, $wildcard = false, $www_or_non_www = false, $force = false ) {
\EE::debug( 'Starting SSL procedure' );
if ( 'le' === $ssl_type ) {
\EE::debug( 'Initializing LE' );
$this->init_le( $site_url, $site_fs_path, $wildcard, $www_or_non_www, $force );
} elseif ( 'inherit' === $ssl_type ) {
if ( $wildcard ) {
throw new \Exception( 'Cannot use --wildcard with --ssl=inherit', false );
}
// We don't have to do anything now as nginx-proxy handles everything for us.
EE::success( 'Inherited certs from parent' );
} elseif ( 'self' === $ssl_type ) {
$client = new Site_Self_signed();
$client->create_certificate( $site_url );
// Update wildcard to 1 as self-signed certs are wildcard by default.
$this->site_data['site_ssl_wildcard'] = 1;
} else {
throw new \Exception( "Unrecognized value in --ssl flag: $ssl_type" );
}
}
/**
* Runs the acme le registration and authorization.
*
* @param string $site_url Name of the site for ssl.
* @param string $site_fs_path Webroot of the site.
* @param bool $wildcard SSL with wildcard or not.
* @param bool $www_or_non_www Allow LetsEncrypt on www or non-www subdomain.
* @param bool $force Force ssl renewal.
*/
protected function init_le( $site_url, $site_fs_path, $wildcard = false, $www_or_non_www, $force = false ) {
$preferred_challenge = get_config_value( 'preferred_ssl_challenge', '' );
$is_solver_dns = ( $wildcard || 'dns' === $preferred_challenge ) ? true : false;
\EE::debug( 'Wildcard in init_le: ' . ( bool ) $wildcard );
$this->site_data['site_fs_path'] = $site_fs_path;
$this->site_data['site_ssl_wildcard'] = $wildcard;
$client = new Site_Letsencrypt();
$this->le_mail = \EE::get_runner()->config['le-mail'] ?? \EE::input( 'Enter your mail id: ' );
\EE::get_runner()->ensure_present_in_config( 'le-mail', $this->le_mail );
if ( ! $client->register( $this->le_mail ) ) {
$this->site_data['site_ssl'] = null;
return;
}
$domains = $this->get_cert_domains( $site_url, $wildcard, $www_or_non_www );
if ( ! $client->authorize( $domains, $wildcard, $preferred_challenge ) ) {
return;
}
$api_key_absent = empty( get_config_value( 'cloudflare-api-key' ) );
if ( $is_solver_dns && $api_key_absent ) {
echo \cli\Colors::colorize( '%YIMPORTANT:%n Run `ee site ssl ' . $site_url . '` once the DNS changes have propagated to complete the certification generation and installation.', null );
} else {
if ( ! $api_key_absent && $is_solver_dns ) {
EE::log( 'Waiting for DNS entry propagation.' );
sleep( 10 );
}
$this->ssl( [], [ 'force' => $force ], $www_or_non_www );
}
}
/**
* Returns all domains required by cert
*
* @param string $site_url Name of site
* @param bool $wildcard Wildcard cert required?
* @param bool $www_or_non_www Allow LetsEncrypt on www subdomain.
*
* @return array
*/
private function get_cert_domains( string $site_url, $wildcard, $www_or_non_www = false ): array {
$preferred_challenge = get_config_value( 'preferred_ssl_challenge', '' );
$is_solver_dns = ( $wildcard || 'dns' === $preferred_challenge ) ? true : false;
$domains = [ $site_url ];
if ( $wildcard ) {
$domains[] = "*.{$site_url}";
} elseif ( $www_or_non_www || $is_solver_dns ) {
if ( 0 === strpos( $site_url, 'www.' ) ) {
$www_or_non_www_site_url = ltrim( $site_url, 'www.' );
} else {
$www_or_non_www_site_url = 'www.' . $site_url;
}
$domains[] = $www_or_non_www_site_url;
}
return $domains;
}
/**
* Check www or non-www is working with site domain.
*
* @param string Site url.
* @param string Absolute path of site.
*
* @return bool
*/
protected function check_www_or_non_www_domain( $site_url, $site_path ): bool {
$random_string = EE\Utils\random_password();
$successful = false;
$file_path = $site_path . '/app/htdocs/check.html';
file_put_contents( $file_path, $random_string );
if ( 0 === strpos( $site_url, 'www.' ) ) {
$site_url = ltrim( $site_url, 'www.' );
} else {
$site_url = 'www.' . $site_url;
}
$site_url .= '/check.html';
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $site_url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
$data = curl_exec( $curl );
curl_close( $curl );
if ( ! empty( $data ) && $random_string === $data ) {
$successful = true;
EE::debug( "pointed for $site_url" );
}
if ( file_exists( $file_path ) ) {
unlink( $file_path );
}
return $successful;
}
/**
* Verifies ssl challenge and also renews certificates(if expired).
*
* ## OPTIONS
*
* <site-name>
* : Name of website.
*
* [--force]
* : Force renewal.
*/
public function ssl( $args = [], $assoc_args = [], $www_or_non_www = false ) {
EE::log( 'Starting SSL verification.' );
// This checks if this method was called internally by ee or by user
$called_by_ee = ! empty( $this->site_data['site_url'] );
$api_key_absent = empty( get_config_value( 'cloudflare-api-key' ) );
if ( ! $called_by_ee ) {
$this->site_data = get_site_info( $args );
}
if ( ! isset( $this->le_mail ) ) {
$this->le_mail = \EE::get_config( 'le-mail' ) ?? \EE::input( 'Enter your mail id: ' );
}
$force = \EE\Utils\get_flag_value( $assoc_args, 'force' );
$domains = $this->get_cert_domains( $this->site_data['site_url'], $this->site_data['site_ssl_wildcard'], $www_or_non_www );
$client = new Site_Letsencrypt();
$preferred_challenge = get_config_value( 'preferred_ssl_challenge', '' );
try {
$client->check( $domains, $this->site_data['site_ssl_wildcard'], $preferred_challenge );
} catch ( \Exception $e ) {
if ( $called_by_ee && $api_key_absent ) {
throw $e;
}
$is_solver_dns = ( $this->site_data['site_ssl_wildcard'] || 'dns' === $preferred_challenge ) ? true : false;
$api_key_present = ! empty( get_config_value( 'cloudflare-api-key' ) );
$warning = ( $is_solver_dns && $api_key_present ) ? "The dns entries have not yet propogated. Manually check: \nhost -t TXT _acme-challenge." . $this->site_data['site_url'] . "\nBefore retrying `ee site ssl " . $this->site_data['site_url'] . "`" : 'Failed to verify SSL: ' . $e->getMessage();
EE::warning( $warning );
EE::warning( sprintf( 'Check logs and retry `ee site ssl %s` once the issue is resolved.', $this->site_data['site_url'] ) );
return;
}
$san = array_values( array_diff( $domains, [ $this->site_data['site_url'] ] ) );
$client->request( $this->site_data['site_url'], $san, $this->le_mail, $force );
if ( ! $this->site_data['site_ssl_wildcard'] ) {
$client->cleanup();
}
reload_global_nginx_proxy();
EE::success( 'SSL verification completed.' );
}
/**
* Renews letsencrypt ssl certificates.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website.
*
* [--force]
* : Force renewal.
*
* [--all]
* : Run renewal for all ssl sites. (Skips renewal for dns/wildcards sites if cloudflare api is not set).
*
* ## EXAMPLES
*
* # Renew ssl cert of a site
* $ ee site ssl-renew example.com
*
* # Renew all ssl certs
* $ ee site ssl-renew --all
*
* # Force renew ssl cert
* $ ee site ssl-renew example.com --force
*
* @subcommand ssl-renew
*/
public function ssl_renew( $args, $assoc_args ) {
EE::log( 'Starting SSL cert renewal' );
if ( ! isset( $this->le_mail ) ) {
$this->le_mail = EE::get_config( 'le-mail' ) ?? EE::input( 'Enter your mail id: ' );
}