|
| 1 | +<?php |
| 2 | + |
| 3 | +use EE\Utils; |
| 4 | + |
| 5 | +/** |
| 6 | + * Manages global services of EasyEngine. |
| 7 | + * |
| 8 | + * ## EXAMPLES |
| 9 | + * |
| 10 | + * # Restarts global nginx proxy service |
| 11 | + * $ ee service restart nginx-proxy |
| 12 | + * |
| 13 | + * @package ee-cli |
| 14 | + */ |
| 15 | +class Service_Command extends EE_Command { |
| 16 | + |
| 17 | + /** |
| 18 | + * @var array Array of services defined in global docker-compose.yml |
| 19 | + */ |
| 20 | + private $whitelisted_services = [ |
| 21 | + 'nginx-proxy', |
| 22 | + ]; |
| 23 | + |
| 24 | + /** |
| 25 | + * Service_Command constructor. |
| 26 | + * |
| 27 | + * Changes directory to EE_CONF_ROOT since that's where all docker-compose commands will be executed |
| 28 | + */ |
| 29 | + public function __construct() { |
| 30 | + chdir( EE_CONF_ROOT ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Starts global services. |
| 35 | + * |
| 36 | + * ## OPTIONS |
| 37 | + * |
| 38 | + * <service-name> |
| 39 | + * : Name of service. |
| 40 | + */ |
| 41 | + public function start( $args, $assoc_args ) { |
| 42 | + $service = $this->filter_service( $args ); |
| 43 | + |
| 44 | + EE::exec( "docker-compose start $service", true, true ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns valid service name from arguments. |
| 49 | + */ |
| 50 | + private function filter_service( $args ) { |
| 51 | + $services = array_intersect( $this->whitelisted_services, $args ); |
| 52 | + |
| 53 | + if ( empty( $services ) ) { |
| 54 | + EE::error( "Unable to find global EasyEngine service $args[0]" ); |
| 55 | + } |
| 56 | + |
| 57 | + return $services[0]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Stops global services. |
| 62 | + * |
| 63 | + * ## OPTIONS |
| 64 | + * |
| 65 | + * <service-name> |
| 66 | + * : Name of service. |
| 67 | + */ |
| 68 | + public function stop( $args, $assoc_args ) { |
| 69 | + $service = $this->filter_service( $args ); |
| 70 | + EE::exec( "docker-compose stop $service", true, true ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Restarts global services. |
| 75 | + * |
| 76 | + * ## OPTIONS |
| 77 | + * |
| 78 | + * <service-name> |
| 79 | + * : Name of service. |
| 80 | + */ |
| 81 | + public function restart( $args, $assoc_args ) { |
| 82 | + $service = $this->filter_service( $args ); |
| 83 | + EE::exec( "docker-compose restart $service", true, true ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Reloads global service without restarting services. |
| 88 | + * |
| 89 | + * ## OPTIONS |
| 90 | + * |
| 91 | + * <service-name> |
| 92 | + * : Name of service. |
| 93 | + */ |
| 94 | + public function reload( $args, $assoc_args ) { |
| 95 | + $service = $this->filter_service( $args ); |
| 96 | + $command = $this->service_reload_command( $service ); |
| 97 | + EE::exec( "docker-compose exec $service $command", true, true ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns reload command of a service. |
| 102 | + * This is necessary since command to reload each service can be different. |
| 103 | + * |
| 104 | + * @param $service string name of service |
| 105 | + * |
| 106 | + * @return mixed |
| 107 | + */ |
| 108 | + private function service_reload_command( string $service ) { |
| 109 | + $command_map = [ |
| 110 | + 'nginx-proxy' => "sh -c 'nginx -t && service nginx reload'", |
| 111 | + ]; |
| 112 | + |
| 113 | + return $command_map[ $service ]; |
| 114 | + } |
| 115 | +} |
0 commit comments