Skip to content

Commit 27277e2

Browse files
committed
Fix service command
1 parent 0a8914f commit 27277e2

File tree

1 file changed

+63
-16
lines changed

1 file changed

+63
-16
lines changed

src/Service_Command.php

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

33
/**
4-
* Executes wp-cli command on a site.
4+
* Manages global containers of EasyEngine.
55
*
66
* ## EXAMPLES
77
*
8-
* # Create simple WordPress site
9-
* $ ee wp test.local plugin list
8+
* # Restarts global nginx proxy containers
9+
* $ ee service restart nginx-proxy
1010
*
1111
* @package ee-cli
1212
*/
@@ -15,33 +15,80 @@
1515

1616
class Service_Command extends EE_Command {
1717

18-
private $global_container_name = 'ee-nginx-proxy';
18+
private $whitelisted_containers = [
19+
'ee-nginx-proxy'
20+
];
21+
22+
/**
23+
* Starts global containers.
24+
*
25+
* ## OPTIONS
26+
*
27+
* <container-name>
28+
* : Name of container.
29+
*/
30+
public function start( $args, $assoc_args ) {
31+
$container = $this->filter_container( $args );
32+
\EE\Utils\default_launch( "docker start $container" );
33+
}
34+
1935
/**
20-
* Starts global reverse proxy container.
36+
* Stops global containers.
37+
*
38+
* ## OPTIONS
39+
*
40+
* <container-name>
41+
* : Name of container.
2142
*/
22-
public function start( $cmd, $descriptors = null ) {
23-
\EE\Utils\default_launch( "docker start $this->global_container_name" );
43+
public function stop( $args, $assoc_args ) {
44+
$container = $this->filter_container( $args );
45+
\EE\Utils\default_launch( "docker stop $container" );
2446
}
2547

2648
/**
27-
* Stops global reverse proxy container.
49+
* Restarts global containers.
50+
*
51+
* ## OPTIONS
52+
*
53+
* <container-name>
54+
* : Name of container.
2855
*/
29-
public function stop( $cmd, $descriptors = null ) {
30-
\EE\Utils\default_launch( "docker stop $this->global_container_name" );
56+
public function restart( $args, $assoc_args ) {
57+
$container = $this->filter_container( $args );
58+
\EE\Utils\default_launch( "docker restart $container" );
3159
}
3260

3361
/**
34-
* Restarts global reverse proxy container.
62+
* Reloads global service without restarting containers.
63+
*
64+
* ## OPTIONS
65+
*
66+
* <service-name>
67+
* : Name of container.
3568
*/
36-
public function restart( $cmd, $descriptors = null ) {
37-
\EE\Utils\default_launch( "docker restart $this->global_container_name" );
69+
public function reload( $args, $assoc_args ) {
70+
$container = $this->filter_container( $args );
71+
$command = $this->container_reload_command( $container );
72+
\EE\Utils\default_launch( "docker exec $container $command" );
3873
}
3974

4075
/**
41-
* Reloads global reverse proxy service without .
76+
* Returns valid container name from arguments.
4277
*/
43-
public function reload( $cmd, $descriptors = null ) {
44-
\EE\Utils\default_launch( "docker exec $this->global_container_name sh -c 'nginx -t && service nginx reload'" );
78+
private function filter_container( $args ) {
79+
$containers = array_intersect( $this->whitelisted_containers, $args );
80+
81+
if( empty( $containers ) ) {
82+
EE::error( "Unable to find global EasyEngine container $args[0]" );
83+
}
84+
85+
return $containers[0];
4586
}
4687

88+
private function container_reload_command( $container ) {
89+
$command_map = [
90+
'ee-nginx-proxy' => "sh -c 'nginx -t && service nginx reload'"
91+
];
92+
return $command_map[ $container ];
93+
}
4794
}

0 commit comments

Comments
 (0)