|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Executes wp-cli command on a site. |
| 4 | + * Manages global containers of EasyEngine. |
5 | 5 | * |
6 | 6 | * ## EXAMPLES |
7 | 7 | * |
8 | | - * # Create simple WordPress site |
9 | | - * $ ee wp test.local plugin list |
| 8 | + * # Restarts global nginx proxy containers |
| 9 | + * $ ee service restart nginx-proxy |
10 | 10 | * |
11 | 11 | * @package ee-cli |
12 | 12 | */ |
|
15 | 15 |
|
16 | 16 | class Service_Command extends EE_Command { |
17 | 17 |
|
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 | + |
19 | 35 | /** |
20 | | - * Starts global reverse proxy container. |
| 36 | + * Stops global containers. |
| 37 | + * |
| 38 | + * ## OPTIONS |
| 39 | + * |
| 40 | + * <container-name> |
| 41 | + * : Name of container. |
21 | 42 | */ |
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" ); |
24 | 46 | } |
25 | 47 |
|
26 | 48 | /** |
27 | | - * Stops global reverse proxy container. |
| 49 | + * Restarts global containers. |
| 50 | + * |
| 51 | + * ## OPTIONS |
| 52 | + * |
| 53 | + * <container-name> |
| 54 | + * : Name of container. |
28 | 55 | */ |
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" ); |
31 | 59 | } |
32 | 60 |
|
33 | 61 | /** |
34 | | - * Restarts global reverse proxy container. |
| 62 | + * Reloads global service without restarting containers. |
| 63 | + * |
| 64 | + * ## OPTIONS |
| 65 | + * |
| 66 | + * <service-name> |
| 67 | + * : Name of container. |
35 | 68 | */ |
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" ); |
38 | 73 | } |
39 | 74 |
|
40 | 75 | /** |
41 | | - * Reloads global reverse proxy service without . |
| 76 | + * Returns valid container name from arguments. |
42 | 77 | */ |
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]; |
45 | 86 | } |
46 | 87 |
|
| 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 | + } |
47 | 94 | } |
0 commit comments