Skip to content

Commit ddfefbc

Browse files
committed
Merge branch 'sagarnasit-fix-down-time' into develop
2 parents 5e934d8 + a0a6786 commit ddfefbc

File tree

5 files changed

+306
-15
lines changed

5 files changed

+306
-15
lines changed

migrations/container/20181203191120_service-command_rc1_db_fix.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public function up() {
4848
chdir( EE_SERVICE_DIR );
4949

5050
EE::exec( 'docker-compose rm --stop --force global-db' );
51-
EE::exec( 'docker-compose run -d --name=ee-global-db global-db --skip-grant-tables' );
51+
EE::exec( sprintf( 'docker-compose run -d --name=%s global-db --skip-grant-tables', GLOBAL_DB_CONTAINER ) );
5252
$health_script = 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"exit"';
5353
$db_script_path = \EE\Utils\get_temp_dir() . 'db_exec';
5454
file_put_contents( $db_script_path, $health_script );
5555
$mysql_unhealthy = true;
56-
EE::exec( sprintf( 'docker cp %s ee-global-db:/db_exec', $db_script_path ) );
56+
EE::exec( sprintf( 'docker cp %s %s:/db_exec', $db_script_path, GLOBAL_DB_CONTAINER ) );
5757
$count = 0;
5858
while ( $mysql_unhealthy ) {
59-
$mysql_unhealthy = ! EE::exec( 'docker exec ee-global-db sh db_exec' );
59+
$mysql_unhealthy = ! EE::exec( sprintf( 'docker exec %s sh db_exec', GLOBAL_DB_CONTAINER ) );
6060
if ( $count ++ > 60 ) {
6161
break;
6262
}
@@ -66,9 +66,9 @@ public function up() {
6666
$reset_script = "FLUSH PRIVILEGES;\nALTER USER 'root'@'localhost' IDENTIFIED BY '$password';";
6767
$db_script_path = \EE\Utils\get_temp_dir() . 'db_exec';
6868
file_put_contents( $db_script_path, $reset_script );
69-
EE::exec( sprintf( 'docker cp %s ee-global-db:/db_exec', $db_script_path ) );
70-
EE::exec( 'docker exec ee-global-db bash -c "mysql < db_exec"' );
71-
EE::exec( 'docker rm -f ee-global-db' );
69+
EE::exec( sprintf( 'docker cp %s %s:/db_exec', $db_script_path, GLOBAL_DB_CONTAINER ) );
70+
EE::exec( sprintf( 'docker exec %s bash -c "mysql < db_exec"', GLOBAL_DB_CONTAINER ) );
71+
EE::exec( sprintf( 'docker rm -f %s', GLOBAL_DB_CONTAINER ) );
7272
EE::exec( 'docker-compose up -d global-db' );
7373
}
7474

@@ -79,7 +79,7 @@ public function up() {
7979
*/
8080
public function down() {
8181
chdir( EE_SERVICE_DIR );
82-
EE::exec( 'docker rm -f ee-global-db' );
82+
EE::exec( sprintf( 'docker rm -f %s', GLOBAL_DB_CONTAINER ) );
8383
EE::exec( 'docker-compose up -d global-db' );
8484
}
8585
}
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
<?php
2+
3+
namespace EE\Migration;
4+
5+
use EE;
6+
use EE\Migration\Base;
7+
8+
class ChangeGlobalServiceContainerNames extends Base {
9+
10+
/** @var RevertableStepProcessor $rsp Keeps track of migration state. Reverts on error */
11+
private static $rsp;
12+
13+
public function __construct() {
14+
15+
parent::__construct();
16+
if ( $this->is_first_execution ) {
17+
$this->skip_this_migration = true;
18+
}
19+
}
20+
21+
/**
22+
* Execute global service container name update.
23+
*
24+
* @throws EE\ExitException
25+
*/
26+
public function up() {
27+
28+
if ( $this->skip_this_migration ) {
29+
EE::debug( 'Skipping change-global-service-container-name migration as it is not needed.' );
30+
31+
return;
32+
}
33+
34+
EE::debug( 'Starting change-global-service-container-name' );
35+
self::$rsp = new EE\RevertableStepProcessor();
36+
37+
/**
38+
* Sites wp-config changes for global-cache.
39+
*/
40+
$cache_sites = EE\Model\Site::where( 'cache_host', 'global-redis' );
41+
foreach ( $cache_sites as $site ) {
42+
43+
self::$rsp->add_step(
44+
sprintf( 'update-cache-host-%s', $site->site_url ),
45+
'EE\Migration\ChangeGlobalServiceContainerNames::update_cache_host',
46+
null,
47+
[ $site ],
48+
null
49+
);
50+
51+
}
52+
53+
$global_compose_file_path = EE_ROOT_DIR . '/services/docker-compose.yml';
54+
$global_compose_file_backup_path = EE_BACKUP_DIR . '/services/docker-compose.yml.backup';
55+
56+
$old_containers = [ 'ee-global-nginx-proxy', 'ee-global-redis', 'ee-global-db' ];
57+
58+
$running_containers = [];
59+
foreach ( $old_containers as $container ) {
60+
if ( 'running' === \EE_DOCKER::container_status( $container ) ) {
61+
$running_containers[] = $container;
62+
}
63+
}
64+
65+
/**
66+
* Backup old docker-compose file.
67+
*/
68+
self::$rsp->add_step(
69+
'backup-global-docker-compose-file',
70+
'EE\Migration\SiteContainers::backup_restore',
71+
'EE\Migration\ChangeGlobalServiceContainerNames::restore_yml_file',
72+
[ $global_compose_file_path, $global_compose_file_backup_path ],
73+
[ $global_compose_file_backup_path, $global_compose_file_path, $running_containers ]
74+
);
75+
76+
/**
77+
* Generate new docker-compose file.
78+
*/
79+
self::$rsp->add_step(
80+
'generate-global-docker-compose-file',
81+
'EE\Service\Utils\generate_global_docker_compose_yml',
82+
null,
83+
[ new \Symfony\Component\Filesystem\Filesystem() ],
84+
null
85+
);
86+
87+
/**
88+
* Start support containers.
89+
*/
90+
self::$rsp->add_step(
91+
'create-support-global-containers',
92+
'EE\Migration\GlobalContainers::enable_support_containers',
93+
'EE\Migration\GlobalContainers::disable_support_containers',
94+
null,
95+
null
96+
);
97+
98+
/**
99+
* Remove global service ee-container.
100+
*/
101+
self::$rsp->add_step(
102+
'remove-global-ee-containers',
103+
'EE\Migration\ChangeGlobalServiceContainerNames::remove_global_ee_containers',
104+
null,
105+
[ $running_containers ],
106+
null
107+
);
108+
109+
/**
110+
* Start global container.
111+
*/
112+
self::$rsp->add_step(
113+
'start-renamed-containers',
114+
'EE\Migration\ChangeGlobalServiceContainerNames::start_global_service_containers',
115+
'EE\Migration\ChangeGlobalServiceContainerNames::stop_default_containers',
116+
[ $running_containers ],
117+
null
118+
);
119+
120+
/**
121+
* Disable support containers.
122+
*/
123+
self::$rsp->add_step(
124+
'remove-support-containers',
125+
'EE\Migration\GlobalContainers::disable_support_containers',
126+
null,
127+
null,
128+
null
129+
);
130+
131+
/**
132+
* Update site's docker-compose.yml
133+
*/
134+
$db = new \EE_DB();
135+
$sites = ( $db->table( 'sites' )->all() );
136+
137+
foreach ( $sites as $site ) {
138+
$docker_yml = $site['site_fs_path'] . '/docker-compose.yml';
139+
$docker_yml_backup = EE_BACKUP_DIR . '/' . $site['site_url'] . '/docker-compose.yml.backup';
140+
$ee_site_object = SiteContainers::get_site_object( $site['site_type'] );
141+
142+
self::$rsp->add_step(
143+
"take-${site['site_url']}-docker-compose-backup",
144+
'EE\Migration\SiteContainers::backup_restore',
145+
'EE\Migration\SiteContainers::backup_restore',
146+
[ $docker_yml, $docker_yml_backup ],
147+
[ $docker_yml_backup, $docker_yml ]
148+
);
149+
150+
self::$rsp->add_step(
151+
"generate-${site['site_url']}-docker-compose",
152+
'EE\Migration\SiteContainers::generate_site_docker_compose_file',
153+
null,
154+
[ $site, $ee_site_object ],
155+
null
156+
);
157+
158+
if ( $site['site_enabled'] ) {
159+
160+
/**
161+
* Enable support containers.
162+
*/
163+
self::$rsp->add_step(
164+
sprintf( 'enable-support-containers-%s', $site['site_url'] ),
165+
'EE\Migration\SiteContainers::enable_support_containers',
166+
'EE\Migration\SiteContainers::disable_support_containers',
167+
[ $site['site_url'], $site['site_fs_path'] ],
168+
[ $site['site_url'], $site['site_fs_path'] ]
169+
);
170+
171+
self::$rsp->add_step(
172+
"upgrade-${site['site_url']}-containers",
173+
'EE\Migration\SiteContainers::enable_default_containers',
174+
null,
175+
[ $site, $ee_site_object ],
176+
null
177+
);
178+
179+
/**
180+
* Disable support containers.
181+
*/
182+
self::$rsp->add_step(
183+
sprintf( 'disable-support-containers-%s', $site['site_url'] ),
184+
'EE\Migration\SiteContainers::disable_support_containers',
185+
'EE\Migration\SiteContainers::enable_support_containers',
186+
[ $site['site_url'], $site['site_fs_path'] ],
187+
[ $site['site_url'], $site['site_fs_path'] ]
188+
);
189+
}
190+
}
191+
192+
if ( ! self::$rsp->execute() ) {
193+
throw new \Exception( 'Unable run change-global-service-container-name migrations.' );
194+
}
195+
196+
}
197+
198+
/**
199+
* No need for down.
200+
*
201+
* @throws EE\ExitException
202+
*/
203+
public function down() {
204+
}
205+
206+
/**
207+
* Restore docker-compose.yml and start old ee-containers.
208+
*
209+
* @param $source string path of source file.
210+
* @param $destination string path of destination.
211+
* @param $containers array of running containers.
212+
*
213+
* @throws \Exception
214+
*/
215+
public static function restore_yml_file( $source, $destination, $containers ) {
216+
EE\Migration\SiteContainers::backup_restore( $source, $destination );
217+
chdir( EE_SERVICE_DIR );
218+
219+
$running_containers = implode( ' ', $containers );
220+
if ( ! EE::exec( sprintf( 'docker-compose up -d %s', $running_containers ) ) ) {
221+
throw new \Exception( 'Unable to start ee-containers' );
222+
}
223+
}
224+
225+
/**
226+
* Remove running global ee-containers.
227+
*
228+
* @param $containers array of running global containers.
229+
*
230+
* @throws \Exception
231+
*/
232+
public static function remove_global_ee_containers( $containers ) {
233+
$removable_containers = implode( ' ', $containers );
234+
if ( ! EE::exec( "docker rm -f $removable_containers" ) ) {
235+
throw new \Exception( 'Unable to remove global service containers' );
236+
}
237+
}
238+
239+
/**
240+
* Stop default global containers.
241+
*
242+
* @throws \Exception
243+
*/
244+
public static function stop_default_containers() {
245+
246+
chdir( EE_SERVICE_DIR );
247+
248+
if ( ! EE::exec( 'docker-compose stop && docker-compose rm -f' ) ) {
249+
throw new \Exception( 'Unable to remove default global service containers' );
250+
}
251+
}
252+
253+
/**
254+
* Start global services with renamed containers names.
255+
*
256+
* @param $containers array of running global containers.
257+
*
258+
* @throws \Exception
259+
*/
260+
public static function start_global_service_containers( $containers ) {
261+
262+
foreach ( $containers as $container ) {
263+
$service = ltrim( $container, 'ee-' );
264+
GlobalContainers::global_service_up( $service );
265+
}
266+
267+
}
268+
269+
/**
270+
* Update redis cache host name.
271+
*
272+
* @param $site_info EE\Model\Site site information.
273+
*
274+
* @throws \Exception
275+
*/
276+
public static function update_cache_host( $site_info ) {
277+
$update_hostname_constant = "docker-compose exec --user='www-data' php wp config set RT_WP_NGINX_HELPER_REDIS_HOSTNAME global-redis --add=true --type=constant";
278+
$redis_plugin_constant = 'docker-compose exec --user=\'www-data\' php wp config set --type=variable redis_server "array(\'host\'=> \'global-redis\',\'port\'=> 6379,)" --raw';
279+
280+
if ( ! chdir( $site_info->site_fs_path ) ) {
281+
throw new \Exception( sprintf( '%s path not exists', $site_info->site_fs_path ) );
282+
}
283+
284+
if ( ! EE::exec( $update_hostname_constant ) ) {
285+
throw new \Exception( sprintf( 'Unable to update cache host of %s', $site_info->site_url ) );
286+
}
287+
288+
if ( ! EE::exec( $redis_plugin_constant ) ) {
289+
throw new \Exception( sprintf( 'Unable to update plugin constant %s', $site_info->site_url ) );
290+
}
291+
EE::log( sprintf( '%s Updated cache-host successfully', $site_info->site_url ) );
292+
}
293+
294+
}

service-command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010

1111
if ( ! defined( 'GLOBAL_DB_CONTAINER' ) ) {
12-
define( 'GLOBAL_DB_CONTAINER', 'ee-global-db' );
12+
define( 'GLOBAL_DB_CONTAINER', 'services_global-db_1' );
1313
}
1414

1515
if ( ! defined( 'GLOBAL_FRONTEND_NETWORK' ) ) {
@@ -25,7 +25,7 @@
2525
}
2626

2727
if ( ! defined( 'GLOBAL_REDIS_CONTAINER' ) ) {
28-
define( 'GLOBAL_REDIS_CONTAINER', 'ee-global-redis' );
28+
define( 'GLOBAL_REDIS_CONTAINER', 'services_global-redis_1' );
2929
}
3030

3131
if ( ! class_exists( 'EE' ) ) {

src/helper/service-utils.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function nginx_proxy_check() {
1818
$config_443_port = \EE\Utils\get_config_value( 'proxy_443_port', '443' );
1919

2020
if ( 'running' === \EE_DOCKER::container_status( $proxy_type ) ) {
21-
$launch_80_test = EE::launch( 'docker inspect --format \'{{ (index (index .NetworkSettings.Ports "80/tcp") 0).HostPort }}\' ee-global-nginx-proxy' );
22-
$launch_443_test = EE::launch( 'docker inspect --format \'{{ (index (index .NetworkSettings.Ports "443/tcp") 0).HostPort }}\' ee-global-nginx-proxy' );
21+
$launch_80_test = EE::launch( sprintf( 'docker inspect --format \'{{ (index (index .NetworkSettings.Ports "80/tcp") 0).HostPort }}\' %s', EE_PROXY_TYPE ) );
22+
$launch_443_test = EE::launch( sprintf( 'docker inspect --format \'{{ (index (index .NetworkSettings.Ports "443/tcp") 0).HostPort }}\' %s', EE_PROXY_TYPE ) );
2323

2424
if ( $config_80_port !== trim( $launch_80_test->stdout ) || $config_443_port !== trim( $launch_443_test->stdout ) ) {
2525
EE::error( "Ports of current running nginx-proxy and ports specified in EasyEngine config file don't match." );
@@ -63,7 +63,7 @@ function nginx_proxy_check() {
6363
function init_global_container( $service, $container = '' ) {
6464

6565
if ( empty( $container ) ) {
66-
$container = 'ee-' . $service;
66+
$container = 'services_' . $service . '_1';
6767
}
6868

6969
boot_global_networks();
@@ -259,7 +259,6 @@ function generate_global_docker_compose_yml( Filesystem $fs ) {
259259
],
260260
[
261261
'name' => GLOBAL_DB,
262-
'container_name' => GLOBAL_DB_CONTAINER,
263262
'image' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'],
264263
'restart' => 'always',
265264
'environment' => [
@@ -272,7 +271,6 @@ function generate_global_docker_compose_yml( Filesystem $fs ) {
272271
],
273272
[
274273
'name' => GLOBAL_REDIS,
275-
'container_name' => GLOBAL_REDIS_CONTAINER,
276274
'image' => 'easyengine/redis:' . $img_versions['easyengine/redis'],
277275
'restart' => 'always',
278276
'command' => '["redis-server", "/usr/local/etc/redis/redis.conf"]',

templates/global_docker_compose.yml.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ services:
44

55
{{#services}}
66
{{name}}:
7-
container_name: {{container_name}}
87
image: {{image}}
98
{{#ports.0}}
109
ports:

0 commit comments

Comments
 (0)