-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathService_Command.php
159 lines (141 loc) · 3.34 KB
/
Service_Command.php
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
<?php
use Symfony\Component\Filesystem\Filesystem;
use EE\Service\Utils as Service_Utils;
/**
* Manages global services of EasyEngine.
*
* ## EXAMPLES
*
* # Restarts global nginx proxy service
* $ ee service restart nginx-proxy
*
* @package ee-cli
*/
class Service_Command extends EE_Command {
/**
* @var array Array of services defined in global docker-compose.yml
*/
private $whitelisted_services = [
'nginx-proxy',
'db',
'redis',
];
/**
* Service_Command constructor.
*
* Changes directory to EE_SERVICE_DIR since that's where all docker-compose commands will be executed
*/
public function __construct() {
if ( ! is_dir( EE_SERVICE_DIR ) ) {
mkdir( EE_SERVICE_DIR );
}
chdir( EE_SERVICE_DIR );
}
/**
* Starts global services.
*
* ## OPTIONS
*
* <service-name>
* : Name of service.
*
* ## EXAMPLES
*
* # Enable global service
* $ ee service enable nginx-proxy
*
*/
public function enable( $args, $assoc_args ) {
$service = $this->filter_service( $args );
$container = "ee-$service";
if ( EE_PROXY_TYPE === $container ) {
Service_Utils\nginx_proxy_check();
} else {
Service_Utils\init_global_container( $service );
}
}
/**
* Returns valid service name from arguments.
*/
private function filter_service( $args ) {
$services = array_intersect( $this->whitelisted_services, $args );
if ( empty( $services ) ) {
EE::error( "Unable to find global EasyEngine service $args[0]" );
}
$services = array_values( $services );
return 'global-' . $services[0];
}
/**
* Stops global services.
*
* ## OPTIONS
*
* <service-name>
* : Name of service.
*
* ## EXAMPLES
*
* # Disable global service
* $ ee service disable nginx-proxy
*
*/
public function disable( $args, $assoc_args ) {
$service = $this->filter_service( $args );
EE::exec( "docker-compose stop $service", true, true );
}
/**
* Restarts global services.
*
* ## OPTIONS
*
* <service-name>
* : Name of service.
*
* ## EXAMPLES
*
* # Restart global service
* $ ee service restart nginx-proxy
*
*/
public function restart( $args, $assoc_args ) {
$service = $this->filter_service( $args );
EE::exec( "docker-compose restart $service", true, true );
}
/**
* Reloads global service without restarting services.
*
* ## OPTIONS
*
* <service-name>
* : Name of service.
*
* ## EXAMPLES
*
* # Reload global service
* $ ee service reload nginx-proxy
*
*/
public function reload( $args, $assoc_args ) {
$service = $this->filter_service( $args );
$command = $this->service_reload_command( $service );
if ( $command ) {
EE::exec( "docker-compose exec $service $command", true, true );
} else {
EE::warning( "$service can not be reloaded." );
}
}
/**
* Returns reload command of a service.
* This is necessary since command to reload each service can be different.
*
* @param $service string name of service
*
* @return mixed
*/
private function service_reload_command( string $service ) {
$command_map = [
'global-nginx-proxy' => 'sh -c "/app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -t; /usr/sbin/nginx -s reload"',
];
return array_key_exists( $service, $command_map ) ? $command_map[ $service ] : false;
}
}