1
1
<?php
2
2
3
+ use EE \Utils ;
4
+ use EE \Model \Site ;
5
+ use function EE \Site \Utils \auto_site_name ;
6
+
3
7
/**
4
- * Executes wp-cli command on a site .
8
+ * Brings up a shell to run wp-cli, composer etc .
5
9
*
6
10
* ## EXAMPLES
7
11
*
8
- * # Create simple WordPress site
9
- * $ ee wp test.local plugin list
12
+ * # Open shell of example.com
13
+ * $ ee shell example.com
10
14
*
11
15
* @package ee-cli
12
16
*/
13
-
14
- use EE \Utils ;
15
-
16
17
class Shell_Command extends EE_Command {
17
18
18
19
/**
@@ -24,28 +25,38 @@ class Shell_Command extends EE_Command {
24
25
* : Name of website to run shell on.
25
26
*/
26
27
public function __invoke ( $ args ) {
28
+
27
29
EE \Utils \delem_log ( 'ee shell start ' );
28
- $ args = EE \ Utils \set_site_arg ( $ args , 'shell ' );
30
+ $ args = auto_site_name ( $ args , 'shell ' , ' ' );
29
31
$ site_name = EE \Utils \remove_trailing_slash ( $ args [0 ] );
30
- if ( EE :: db ():: site_in_db ( $ site_name ) ) {
31
- $ db_select = EE :: db ():: select ( [ ' site_path ' ], [ ' sitename ' => $ site_name] );
32
- $ site_root = $ db_select [ 0 ][ ' site_path ' ];
33
- } else {
34
- EE ::error ( "Site $ site_name does not exist. " );
32
+
33
+ $ site = Site:: find ( $ site_name );
34
+
35
+ if ( ! $ site || ! $ site -> site_enabled ) {
36
+ EE ::error ( "Site $ site_name does not exist or is not enabled . " );
35
37
}
36
- chdir ($ site_root );
38
+
39
+ chdir ( $ site ->site_fs_path );
40
+ $ this ->check_shell_available ( 'php ' , $ site );
37
41
$ this ->run ( "docker-compose exec --user='www-data' php bash " );
38
42
EE \Utils \delem_log ( 'ee shell end ' );
39
43
}
40
44
45
+ /**
46
+ * Run the command to open shell.
47
+ *
48
+ * @param string $cmd Command to be executed to open shell.
49
+ * @param null|array $descriptors File descriptors for proc.
50
+ */
41
51
private function run ( $ cmd , $ descriptors = null ) {
52
+
42
53
EE \Utils \check_proc_available ( 'ee_shell ' );
43
54
if ( ! $ descriptors ) {
44
55
$ descriptors = array ( STDIN , STDOUT , STDERR );
45
56
}
46
-
57
+
47
58
$ final_cmd = EE \Utils \force_env_on_nix_systems ( $ cmd );
48
- $ proc = EE \Utils \proc_open_compat ( $ final_cmd , $ descriptors , $ pipes );
59
+ $ proc = EE \Utils \proc_open_compat ( $ final_cmd , $ descriptors , $ pipes );
49
60
if ( ! $ proc ) {
50
61
exit ( 1 );
51
62
}
@@ -55,4 +66,21 @@ private function run( $cmd, $descriptors = null ) {
55
66
}
56
67
}
57
68
69
+ /**
70
+ * Function to check if container supporting shell is present in docker-compose.yml or not.
71
+ *
72
+ * @param string $shell_container Container to be checked.
73
+ * @param Object $site Contains relevant site info.
74
+ */
75
+ private function check_shell_available ( $ shell_container , $ site ) {
76
+
77
+ $ launch = EE ::launch ( 'docker-compose config --services ' );
78
+ $ services = explode ( PHP_EOL , trim ( $ launch ->stdout ) );
79
+ if ( in_array ( $ shell_container , $ services , true ) ) {
80
+ return ;
81
+ }
82
+ EE ::debug ( 'Site type: ' . $ site ->site_type );
83
+ EE ::debug ( 'Site command: ' . $ site ->app_sub_type );
84
+ EE ::error ( sprintf ( '%s site does not have support to launch %s shell. ' , $ site ->site_url , $ shell_container ) );
85
+ }
58
86
}
0 commit comments