-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathservice-utils.php
More file actions
321 lines (283 loc) · 10.2 KB
/
service-utils.php
File metadata and controls
321 lines (283 loc) · 10.2 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace EE\Service\Utils;
use EE;
use Symfony\Component\Filesystem\Filesystem;
use EE\Utils as EE_Utils;
/**
* Boots up the container if it is stopped or not running.
* @throws EE\ExitException
*/
function nginx_proxy_check() {
$proxy_type = EE_PROXY_TYPE;
$config_80_port = EE_Utils\get_config_value( 'proxy_80_port', '80' );
$config_443_port = EE_Utils\get_config_value( 'proxy_443_port', '443' );
if ( 'running' === \EE_DOCKER::container_status( $proxy_type ) ) {
$launch_80_test = EE::launch( 'docker inspect --format \'{{ (index (index .NetworkSettings.Ports "80/tcp") 0).HostPort }}\' ee-global-nginx-proxy' );
$launch_443_test = EE::launch( 'docker inspect --format \'{{ (index (index .NetworkSettings.Ports "443/tcp") 0).HostPort }}\' ee-global-nginx-proxy' );
if ( $config_80_port !== trim( $launch_80_test->stdout ) || $config_443_port !== trim( $launch_443_test->stdout ) ) {
EE::error( "Ports of current running nginx-proxy and ports specified in EasyEngine config file don't match." );
}
return;
}
/**
* Checking ports.
*/
$port_80_status = EE_Utils\get_curl_info( 'localhost', $config_80_port, true );
$port_443_status = EE_Utils\get_curl_info( 'localhost', $config_443_port, true );
// if any/both the port/s is/are occupied.
if ( ! ( $port_80_status && $port_443_status ) ) {
EE::error( "Cannot create/start proxy container. Please make sure port $config_80_port and $config_443_port are free." );
} else {
$fs = new Filesystem();
if ( ! $fs->exists( EE_SERVICE_DIR . '/docker-compose.yml' ) ) {
generate_global_docker_compose_yml( $fs );
}
boot_global_networks();
if ( ! \EE_DOCKER::docker_compose_up( EE_SERVICE_DIR . '', [ 'global-nginx-proxy' ] ) ) {
EE::error( "There was some error in starting $proxy_type container. Please check logs." );
}
set_nginx_proxy_version_conf();
}
}
/**
* Function to start global conainer if it is not running.
*
* @param string $container Global container to be brought up.
*/
function init_global_container( $service, $container = '' ) {
if ( empty( $container ) ) {
$container = 'ee-' . $service;
}
boot_global_networks();
$fs = new Filesystem();
if ( ! $fs->exists( EE_SERVICE_DIR . '/docker-compose.yml' ) ) {
generate_global_docker_compose_yml( $fs );
}
if ( 'running' !== \EE_DOCKER::container_status( $container ) ) {
chdir( EE_SERVICE_DIR . '' );
$db_conf_file = EE_SERVICE_DIR . '/mariadb/conf/my.cnf';
if ( IS_DARWIN && GLOBAL_DB === $service && ! $fs->exists( $db_conf_file ) ) {
$fs->copy( SERVICE_TEMPLATE_ROOT . '/my.cnf.mustache', $db_conf_file );
}
\EE_DOCKER::boot_container( $container, 'docker-compose up -d ' . $service );
} else {
EE::log( "$service: Service already running" );
return;
}
EE::success( "$container container is up" );
}
/**
* Start required global networks if they don't exist.
*/
function boot_global_networks() {
if ( ! \EE_DOCKER::docker_network_exists( GLOBAL_BACKEND_NETWORK ) &&
! \EE_DOCKER::create_network( GLOBAL_BACKEND_NETWORK ) ) {
EE::error( 'Unable to create network ' . GLOBAL_BACKEND_NETWORK );
}
if ( ! \EE_DOCKER::docker_network_exists( GLOBAL_FRONTEND_NETWORK ) &&
! \EE_DOCKER::create_network( GLOBAL_FRONTEND_NETWORK ) ) {
EE::error( 'Unable to create network ' . GLOBAL_FRONTEND_NETWORK );
}
}
/**
* Generates global docker-compose.yml at EE_ROOT_DIR
*
* @param Filesystem $fs Filesystem object to write file.
*/
function generate_global_docker_compose_yml( Filesystem $fs ) {
$img_versions = EE_Utils\get_image_versions();
$config_80_port = EE_Utils\get_config_value( 'proxy_80_port', 80 );
$config_443_port = EE_Utils\get_config_value( 'proxy_443_port', 443 );
$fs = new Filesystem();
$backup_yml = EE_BACKUP_DIR . '/services/docker-compose.yml.backup';
$password = '';
if ( $fs->exists( $backup_yml ) ) {
$launch = EE::launch( sprintf( 'cat %s | grep MYSQL_ROOT_PASSWORD | cut -d"=" -f2', $backup_yml ) );
$password = trim( $launch->stdout );
}
$password = empty( $password ) ? EE_Utils\random_password() : $password;
$volumes_nginx_proxy = [
[
'name' => 'certs',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/certs',
'container_path' => '/etc/nginx/certs',
],
[
'name' => 'dhparam',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/dhparam',
'container_path' => '/etc/nginx/dhparam',
],
[
'name' => 'confd',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/conf.d',
'container_path' => '/etc/nginx/conf.d',
],
[
'name' => 'htpasswd',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/htpasswd',
'container_path' => '/etc/nginx/htpasswd',
],
[
'name' => 'vhostd',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/vhost.d',
'container_path' => '/etc/nginx/vhost.d',
],
[
'name' => 'html',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/html',
'container_path' => '/usr/share/nginx/html',
],
[
'name' => 'nginx_proxy_logs',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/logs',
'container_path' => '/var/log/nginx',
],
[
'name' => 'nginx_proxy_logs',
'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/logs',
'container_path' => '/var/log/nginx',
],
[
'name' => '/var/run/docker.sock',
'path_to_symlink' => '/var/run/docker.sock',
'container_path' => '/tmp/docker.sock:ro',
'skip_volume' => true,
],
];
$volumes_db = [
[
'name' => 'db_data',
'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/data',
'container_path' => '/var/lib/mysql',
],
[
'name' => 'db_conf',
'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/conf',
'container_path' => '/etc/mysql',
'skip_darwin' => true,
],
[
'name' => 'db_conf',
'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/conf/my.cnf',
'container_path' => '/etc/mysql/my.cnf',
'skip_linux' => true,
'skip_volume' => true,
],
[
'name' => 'db_logs',
'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/logs',
'container_path' => '/var/log/mysql',
],
];
$volumes_redis = [
[
'name' => 'redis_data',
'path_to_symlink' => EE_SERVICE_DIR . '/redis/data',
'container_path' => '/data',
'skip_darwin' => true,
],
[
'name' => 'redis_conf',
'path_to_symlink' => EE_SERVICE_DIR . '/redis/conf',
'container_path' => '/usr/local/etc/redis',
'skip_darwin' => true,
],
[
'name' => 'redis_logs',
'path_to_symlink' => EE_SERVICE_DIR . '/redis/logs',
'container_path' => '/var/log/redis',
],
];
if ( ! IS_DARWIN ) {
$data['created_volumes'] = [
'external_vols' => [
[ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'certs' ],
[ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'dhparam' ],
[ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'confd' ],
[ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'htpasswd' ],
[ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'vhostd' ],
[ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'html' ],
[ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'nginx_proxy_logs' ],
[ 'prefix' => GLOBAL_DB, 'ext_vol_name' => 'db_data' ],
[ 'prefix' => GLOBAL_DB, 'ext_vol_name' => 'db_conf' ],
[ 'prefix' => GLOBAL_DB, 'ext_vol_name' => 'db_logs' ],
[ 'prefix' => GLOBAL_REDIS, 'ext_vol_name' => 'redis_data' ],
[ 'prefix' => GLOBAL_REDIS, 'ext_vol_name' => 'redis_conf' ],
[ 'prefix' => GLOBAL_REDIS, 'ext_vol_name' => 'redis_logs' ],
],
];
if ( empty( \EE_DOCKER::get_volumes_by_label( 'global-nginx-proxy' ) ) ) {
\EE_DOCKER::create_volumes( 'global-nginx-proxy', $volumes_nginx_proxy, false );
}
if ( empty( \EE_DOCKER::get_volumes_by_label( GLOBAL_DB ) ) ) {
\EE_DOCKER::create_volumes( GLOBAL_DB, $volumes_db, false );
}
if ( empty( \EE_DOCKER::get_volumes_by_label( GLOBAL_REDIS ) ) ) {
\EE_DOCKER::create_volumes( GLOBAL_REDIS, $volumes_redis, false );
}
}
$data['services'] = [
[
'name' => 'global-nginx-proxy',
'container_name' => EE_PROXY_TYPE,
'image' => 'easyengine/nginx-proxy:' . $img_versions['easyengine/nginx-proxy'],
'restart' => 'always',
'ports' => [
"$config_80_port:80",
"$config_443_port:443",
],
'environment' => [
'LOCAL_USER_ID=' . posix_geteuid(),
'LOCAL_GROUP_ID=' . posix_getegid(),
],
'volumes' => \EE_DOCKER::get_mounting_volume_array( $volumes_nginx_proxy ),
'networks' => [
'global-frontend-network',
],
],
[
'name' => GLOBAL_DB,
'container_name' => GLOBAL_DB_CONTAINER,
'image' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'],
'restart' => 'always',
'environment' => [
'MYSQL_ROOT_PASSWORD=' . $password,
],
'volumes' => \EE_DOCKER::get_mounting_volume_array( $volumes_db ),
'networks' => [
'global-backend-network',
],
],
[
'name' => GLOBAL_REDIS,
'container_name' => GLOBAL_REDIS_CONTAINER,
'image' => 'easyengine/redis:' . $img_versions['easyengine/redis'],
'restart' => 'always',
'command' => '["redis-server", "/usr/local/etc/redis/redis.conf"]',
'volumes' => \EE_DOCKER::get_mounting_volume_array( $volumes_redis ),
'networks' => [
'global-backend-network',
],
],
];
$contents = EE_Utils\mustache_render( SERVICE_TEMPLATE_ROOT . '/global_docker_compose.yml.mustache', $data );
$fs->dumpFile( EE_SERVICE_DIR . '/docker-compose.yml', $contents );
}
/**
* Function to set nginx-proxy version.conf file.
*/
function set_nginx_proxy_version_conf() {
if ( 'running' !== \EE_DOCKER::container_status( EE_PROXY_TYPE ) ) {
return;
}
chdir( EE_SERVICE_DIR );
$version_line = sprintf( 'add_header X-Powered-By \"EasyEngine v%s\";', EE_VERSION );
$version_file = '/version.conf';
$version_success = EE::exec( sprintf( 'docker-compose exec global-nginx-proxy bash -c \'echo "%s" > %s\'', $version_line, $version_file ), false, false, [
$version_file,
$version_line,
] );
if ( $version_success ) {
EE::exec( 'docker-compose exec global-nginx-proxy bash -c "nginx -t && nginx -s reload"' );
}
}