-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathclass-ee-docker.php
More file actions
375 lines (334 loc) · 11.1 KB
/
class-ee-docker.php
File metadata and controls
375 lines (334 loc) · 11.1 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
use Symfony\Component\Filesystem\Filesystem;
class EE_DOCKER {
/**
* Check and Start or create container if not running.
*
* @param String $container Name of the container.
* @param String $command Command to launch that container if needed.
*
* @return bool success.
*/
public static function boot_container( $container, $command = '' ) {
$status = self::container_status( $container );
if ( $status ) {
if ( 'exited' === $status ) {
return self::start_container( $container );
} else {
return true;
}
} else {
return EE::exec( $command );
}
}
public static function container_status( $container ) {
$exec_command = 'which docker';
exec( $exec_command, $out, $ret );
EE::debug( 'COMMAND: ' . $exec_command );
EE::debug( 'RETURN CODE: ' . $ret );
if ( $ret ) {
EE::error( 'Docker is not installed. Please install Docker to run EasyEngine.' );
}
$status = EE::launch( "docker inspect -f '{{.State.Running}}' $container" );
if ( ! $status->return_code ) {
if ( preg_match( '/true/', $status->stdout ) ) {
return 'running';
} else {
return 'exited';
}
}
return false;
}
/**
* Function to start the container if it exists but is not running.
*
* @param String $container Container to be started.
*
* @return bool success.
*/
public static function start_container( $container ) {
return EE::exec( "docker start $container" );
}
/**
* Function to stop a container
*
* @param String $container Container to be stopped.
*
* @return bool success.
*/
public static function stop_container( $container ) {
return EE::exec( "docker stop $container" );
}
/**
* Function to restart a container
*
* @param String $container Container to be restarted.
*
* @return bool success.
*/
public static function restart_container( $container ) {
return EE::exec( "docker restart $container" );
}
/**
* Create docker network.
*
* @param String $name Name of the network to be created.
*
* @return bool success.
*/
public static function create_network( $name ) {
return EE::exec( "docker network create $name --label=org.label-schema.vendor=\"EasyEngine\" " );
}
/**
* Connect to given docker network.
*
* @param String $name Name of the network that has to be connected.
* @param String $connect_to Name of the network to which connection has to be established.
*
* @return bool success.
*/
public static function connect_network( $name, $connect_to ) {
return EE::exec( "docker network connect $name $connect_to" );
}
/**
* Remove docker network.
*
* @param String $name Name of the network to be removed.
*
* @return bool success.
*/
public static function rm_network( $name ) {
return EE::exec( "docker network rm $name" );
}
/**
* Disconnect docker network.
*
* @param String $name Name of the network to be disconnected.
* @param String $connected_to Name of the network from which it has to be disconnected.
*
* @return bool success.
*/
public static function disconnect_network( $name, $connected_to ) {
return EE::exec( "docker network disconnect $name $connected_to" );
}
/**
* Function to connect site network to appropriate containers.
*/
public static function connect_site_network_to( $site_name, $to_container ) {
if ( self::connect_network( $site_name, $to_container ) ) {
EE::success( "Site connected to $to_container." );
} else {
throw new Exception( "There was some error connecting to $to_container." );
}
}
/**
* Function to disconnect site network from appropriate containers.
*/
public static function disconnect_site_network_from( $site_name, $from_container ) {
if ( self::disconnect_network( $site_name, $from_container ) ) {
EE::log( "[$site_name] Disconnected from Docker network of $from_container" );
} else {
EE::warning( "Error in disconnecting from Docker network of $from_container" );
}
}
/**
* Function to boot the containers.
*
* @param String $dir Path to docker-compose.yml.
* @param array $services Services to bring up.
*
* @return bool success.
*/
public static function docker_compose_up( $dir, $services = [] ) {
$chdir_return_code = chdir( $dir );
if ( $chdir_return_code ) {
if ( empty( $services ) ) {
return EE::exec( 'docker-compose up -d' );
} else {
$all_services = implode( ' ', $services );
return EE::exec( "docker-compose up -d $all_services" );
}
}
return false;
}
/**
* Function to check if a network exists
*
* @param string $network Name/ID of network to check
*
* @return bool Network exists or not
*/
public static function docker_network_exists( string $network ) {
return EE::exec( "docker network inspect $network" );
}
/**
* Function to destroy the containers.
*
* @param String $dir Path to docker-compose.yml.
*
* @return bool success.
*/
public static function docker_compose_down( $dir ) {
$chdir_return_code = chdir( $dir );
if ( $chdir_return_code ) {
return EE::exec( 'docker-compose down' );
}
return false;
}
/**
* Function to restart the containers.
*
* @param String $dir Path to docker-compose.yml.
*
* @return bool success.
*/
public static function docker_compose_restart( $dir, $containers ) {
if ( chdir( $dir ) ) {
return EE::exec( "docker-compose restart $containers", true, true );
}
throw new \Exception( "Could not resolve $dir" );
}
/**
* Function to stop the containers.
*
* @param String $dir Path to docker-compose.yml.
*
* @return bool success.
*/
public static function docker_compose_stop( $dir, $containers ) {
if ( chdir( $dir ) ) {
return EE::exec( "docker-compose stop $containers", true, true );
}
throw new \Exception( "Could not resolve $dir" );
}
/**
* Function to force remove the containers.
*
* @param String $dir Path to docker-compose.yml.
*
* @return bool success.
*/
public static function docker_compose_forcerm( $dir, $containers ) {
if ( chdir( $dir ) ) {
return EE::exec( "docker-compose rm -f $containers", true, true );
}
throw new \Exception( "Could not resolve $dir" );
}
/**
* Function to exec command in a running container.
*
* @param String $dir Path to docker-compose.yml.
*
* @return bool success.
*/
public static function docker_compose_exec( $dir, $container, $exec_command ) {
if ( chdir( $dir ) ) {
EE::log( "Executing: [$container]: $exec_command" );
return EE::exec( "docker-compose exec $container $exec_command", true, true );
}
throw new \Exception( "Could not resolve $dir" );
}
/**
* Check if a particular service exists in given docker-compose.yml.
*
* @param string $service Service whose availability needs to be checked.
* @param string $site_fs_path Path to the site root where docker-compose.yml file is present.
*
* @return bool Whether service is available or not.
*/
public static function service_exists( $service, $site_fs_path ) {
chdir( $site_fs_path );
$launch = EE::launch( 'docker-compose config --services' );
$services = explode( PHP_EOL, trim( $launch->stdout ) );
return in_array( $service, $services, true );
}
/**
* Gets a dockerized prefix created for site.
*
* @param string $site_url Name of the site.
*
* @return string prefix derived from the name.
*/
public static function get_docker_style_prefix( $site_url ) {
return str_replace( [ '.', '-' ], '', $site_url );
}
/**
* Function to create external docker volumes and related symlinks.
*
* @param string $prefix Prefix by volumes have to be created.
* @param array $volumes The volumes to be created.
* $volumes[$key]['name'] => specifies the name of volume to be created.
* $volumes[$key]['path_to_symlink'] => specifies the path to symlink the
* created volume.
* $volumes[$key]['skip_volume'] => if set to `true` will skip volume creation
* for that entry.
* @param bool $update_to_docker_prefix Update the prefix in dockerized style.
*/
public static function create_volumes( $prefix, $volumes, $update_to_docker_prefix = true ) {
$volume_prefix = $update_to_docker_prefix ? self::get_docker_style_prefix( $prefix ) : $prefix;
$fs = new Filesystem();
// This command will get the root directory for Docker, generally `/var/lib/docker`.
$launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" );
$docker_root_dir = trim( $launch->stdout );
foreach ( $volumes as $volume ) {
if ( ! empty( $volume['skip_volume'] ) && true === $volume['skip_volume'] ) {
continue;
}
$path_to_symlink_not_empty = ! empty( dirname( $volume['path_to_symlink'] ) );
if ( $path_to_symlink_not_empty ) {
$fs->mkdir( dirname( $volume['path_to_symlink'] ) );
}
EE::exec(
sprintf(
'docker volume create \
--label "org.label-schema.vendor=EasyEngine" \
--label "io.easyengine.site=%s" \
%s_%s',
$prefix,
$volume_prefix,
$volume['name']
)
);
if ( $path_to_symlink_not_empty ) {
$fs->symlink( sprintf( '%s/volumes/%s_%s/_data', $docker_root_dir, $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] );
}
}
}
/**
* Function to get all the volumes with a specific label.
*
* @param string $label The label to search for.
*
* @return array Found containers.
*/
public static function get_volumes_by_label( $label ) {
$launch = EE::launch( sprintf( 'docker volume ls --filter="label=org.label-schema.vendor=EasyEngine" --filter="label=io.easyengine.site=%s" -q', $label ) );
return array_filter( explode( PHP_EOL, trim( $launch->stdout ) ), 'trim' );
}
/**
* Function to return minimal docker-compose `host:container` volume mounting array.
*
* @param array $extended_vols :
* $extended_vols['name'] - Host path for docker-compose generation in linux
* $extended_vols['path_to_symlink'] - Host path for docker-compose generation in
* darwin.
* $extended_vols['container_path'] - Path inside container, common for linux and
* darwin.
* $extended_vols['skip_darwin'] - if set to true skips that volume for darwin.
* $extended_vols['skip_linux'] - if set to true skips that volume for linux.
*
* @return array having docker-compose `host:container` volume mounting.
*/
public static function get_mounting_volume_array( $extended_vols ) {
$volume_gen_key = IS_DARWIN ? 'path_to_symlink' : 'name';
$skip_key = IS_DARWIN ? 'skip_darwin' : 'skip_linux';
$final_mount_volumes = [];
foreach ( $extended_vols as $extended_vol ) {
if ( ! empty( $extended_vol[ $skip_key ] ) && true === $extended_vol[ $skip_key ] ) {
continue;
}
$final_mount_volumes[] = $extended_vol[ $volume_gen_key ] . ':' . $extended_vol['container_path'];
}
return $final_mount_volumes;
}
}