Skip to content

Commit 192e499

Browse files
committed
Merge branch 'fix-down-time' of https://github.com/sagarnasit/easyengine into sagarnasit-fix-down-time
2 parents 236f77f + 7359ddf commit 192e499

File tree

4 files changed

+158
-19
lines changed

4 files changed

+158
-19
lines changed

php/EE/Migration/Containers.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ private static function migrate_global_containers( $updated_images ) {
156156
[ $global_compose_file_backup_path, $global_compose_file_path, $updated_global_images ]
157157
);
158158

159+
/**
160+
* Create support containers.
161+
*/
162+
self::$rsp->add_step(
163+
'create-support-global-containers',
164+
'EE\Migration\GlobalContainers::enable_support_containers',
165+
'EE\Migration\GlobalContainers::disable_support_containers',
166+
null,
167+
null
168+
);
169+
159170
self::$rsp->add_step(
160171
'stop-global-containers',
161172
'EE\Migration\GlobalContainers::down_global_containers',
@@ -175,7 +186,8 @@ private static function migrate_global_containers( $updated_images ) {
175186
$all_global_images = GlobalContainers::get_all_global_images_with_service_name();
176187
foreach ( $updated_global_images as $image_name ) {
177188
$global_container_name = $all_global_images[ $image_name ];
178-
$global_service_name = ltrim( $global_container_name, 'ee-' );
189+
$global_service_name = ltrim( $global_container_name, 'services_' );
190+
$global_service_name = rtrim( $global_service_name, '_1' );
179191
self::$rsp->add_step(
180192
"upgrade-$global_container_name-container",
181193
"EE\Migration\GlobalContainers::global_service_up",
@@ -184,6 +196,17 @@ private static function migrate_global_containers( $updated_images ) {
184196
[ $global_service_name ]
185197
);
186198
}
199+
200+
/**
201+
* Remove support containers.
202+
*/
203+
self::$rsp->add_step(
204+
'remove-support-global-containers',
205+
'EE\Migration\GlobalContainers::disable_support_containers',
206+
'EE\Migration\GlobalContainers::enable_support_containers',
207+
null,
208+
null
209+
);
187210
}
188211

189212
/**
@@ -210,11 +233,23 @@ public static function migrate_site_containers( $updated_images ) {
210233
$ee_site_object = SiteContainers::get_site_object( $site['site_type'] );
211234

212235
if ( $site['site_enabled'] ) {
236+
237+
/**
238+
* Enable support containers.
239+
*/
240+
self::$rsp->add_step(
241+
sprintf( 'enable-support-containers-%s', $site['site_url'] ),
242+
'EE\Migration\SiteContainers::enable_support_containers',
243+
'EE\Migration\SiteContainers::disable_support_containers',
244+
[ $site['site_url'], $site['site_fs_path'] ],
245+
[ $site['site_url'], $site['site_fs_path'] ]
246+
);
247+
213248
self::$rsp->add_step(
214249
"disable-${site['site_url']}-containers",
215-
'EE\Migration\SiteContainers::disable_site',
216-
'EE\Migration\SiteContainers::enable_site',
217-
[ $site, $ee_site_object ],
250+
'EE\Migration\SiteContainers::disable_default_containers',
251+
'EE\Migration\SiteContainers::enable_default_containers',
252+
[ $site ],
218253
[ $site, $ee_site_object ]
219254
);
220255
}
@@ -238,11 +273,22 @@ public static function migrate_site_containers( $updated_images ) {
238273
if ( $site['site_enabled'] ) {
239274
self::$rsp->add_step(
240275
"upgrade-${site['site_url']}-containers",
241-
'EE\Migration\SiteContainers::enable_site',
242-
'EE\Migration\SiteContainers::enable_site',
276+
'EE\Migration\SiteContainers::enable_default_containers',
277+
'EE\Migration\SiteContainers::enable_default_containers',
243278
[ $site, $ee_site_object ],
244279
[ $site, $ee_site_object ]
245280
);
281+
282+
/**
283+
* Disable support containers.
284+
*/
285+
self::$rsp->add_step(
286+
sprintf( 'disable-support-containers-%s', $site['site_url'] ),
287+
'EE\Migration\SiteContainers::disable_support_containers',
288+
'EE\Migration\SiteContainers::enable_support_containers',
289+
[ $site['site_url'], $site['site_fs_path'] ],
290+
[ $site['site_url'], $site['site_fs_path'] ]
291+
);
246292
}
247293
}
248294
}

php/EE/Migration/GlobalContainers.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EE\Migration;
44

55
use EE;
6+
use Symfony\Component\Filesystem\Filesystem;
67

78
/**
89
* Upgrade existing global containers to new docker-image
@@ -43,7 +44,7 @@ public static function revert_global_containers( $source_path, $dest_path, $upda
4344
$all_global_images = self::get_all_global_images_with_service_name();
4445
foreach ( $updated_images as $image_name ) {
4546
$global_container_name = $all_global_images[ $image_name ];
46-
$services_to_regenerate .= str_replace( '-', '_', ltrim( $global_container_name, 'ee-' ) ) . ' ';
47+
$services_to_regenerate .= ltrim( rtrim( $global_container_name, '_1' ), 'services_' ) . ' ';
4748
}
4849
if ( empty( trim( $services_to_regenerate ) ) ) {
4950
return;
@@ -74,7 +75,8 @@ public static function down_global_containers( $updated_images ) {
7475

7576
foreach ( $updated_images as $image_name ) {
7677
$global_container_name = $all_global_images[ $image_name ];
77-
$global_service_name = ltrim( $global_container_name, 'ee-' );
78+
$global_service_name = ltrim( $global_container_name, 'services_' );
79+
$global_service_name = rtrim( $global_service_name, '_1' );
7880
EE::debug( "Removing $global_container_name" );
7981

8082
if ( false !== \EE_DOCKER::container_status( $global_container_name ) ) {
@@ -129,4 +131,32 @@ public static function get_all_global_images_with_service_name() {
129131
// 'easyengine/cron' => EE_CRON_SCHEDULER, //TODO: Add it to global docker-compose.
130132
];
131133
}
134+
135+
/**
136+
* Create support containers for global-db and global-redis service.
137+
*/
138+
public static function enable_support_containers() {
139+
if ( ! chdir( EE_SERVICE_DIR ) ) {
140+
throw new \Exception( sprintf( '%s path does not exist', EE_SERVICE_DIR ) );
141+
}
142+
143+
$command = 'docker-compose --project-name=ee up -d global-db global-redis';
144+
if ( ! EE::exec( $command ) ) {
145+
throw new \Exception( 'Unable to create support container.' );
146+
}
147+
}
148+
149+
/**
150+
* Remove support containers for global-db and global-redis service.
151+
*/
152+
public static function disable_support_containers() {
153+
if ( ! chdir( EE_SERVICE_DIR ) ) {
154+
throw new \Exception( sprintf( '%s path does not exist', EE_SERVICE_DIR ) );
155+
}
156+
157+
$command = 'docker-compose --project-name=ee down';
158+
if ( ! EE::exec( $command ) ) {
159+
throw new \Exception( 'Unable to remove support container.' );
160+
}
161+
}
132162
}

php/EE/Migration/SiteContainers.php

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,42 @@ public static function generate_site_docker_compose_file( $site_info, $site_obje
103103
/**
104104
* Enable site.
105105
*
106-
* @param array $site_info array of site information.
106+
* @param array $site_info array of site information.
107107
* @param object $site_object object of site-type( HTML, PHP, WordPress ).
108108
*
109109
* @throws \Exception
110110
*/
111-
public static function enable_site( $site_info, $site_object ) {
112-
EE::debug( "Start enabling ${site_info['site_url']}" );
111+
public static function enable_default_containers( $site_info, $site_object ) {
112+
EE::debug( "Start enabling default containers of ${site_info['site_url']}" );
113+
113114
try {
114115
$site_object->enable( [ $site_info['site_url'] ], [ 'force' => true ], false );
115116
} catch ( \Exception $e ) {
116117
throw new \Exception( $e->getMessage() );
117118
}
118-
EE::debug( "Complete enabling ${site_info['site_url']}" );
119+
120+
EE::debug( "Complete enabling default containers of ${site_info['site_url']}" );
119121
}
120122

121123
/**
122124
* Disable site.
123125
*
124-
* @param array $site_info array of site information.
125-
* @param object $site_object object of site-type( HTML, PHP, Wordpress ).
126+
* @param array $site_info array of site information.
127+
*
128+
* @throws \Exception
126129
*/
127-
public static function disable_site( $site_info, $site_object ) {
128-
EE::debug( "Start disabling ${site_info['site_url']}" );
129-
$site_object->disable( [ $site_info['site_url'] ], [] );
130-
EE::debug( "Complete disabling ${site_info['site_url']}" );
130+
public static function disable_default_containers( $site_info ) {
131+
EE::debug( "Start disabling default containers of ${site_info['site_url']}" );
132+
133+
if ( ! chdir( $site_info['site_fs_path'] ) ) {
134+
throw new \Exception( sprintf( '%s path does not exist', $site_info['site_fs_path'] ) );
135+
}
136+
137+
if ( ! EE::exec( 'docker-compose stop && docker-compose rm -f' ) ) {
138+
throw new \Exception( sprintf( 'Something went wrong on disable site %s', $site_info['site_url'] ) );
139+
}
140+
141+
EE::debug( "Complete disabling default containers of ${site_info['site_url']}" );
131142
}
132143

133144
/**
@@ -219,4 +230,56 @@ public static function docker_compose_pull( $site_fs_path ) {
219230
throw new \Exception( 'Could pull given images.' );
220231
}
221232
}
233+
234+
/**
235+
* Enable support containers for sites.
236+
*
237+
* @param $site_url string Site URL.
238+
* @param $site_fs_path string File system path of site.
239+
*
240+
* @throws \Exception
241+
*/
242+
public static function enable_support_containers( $site_url, $site_fs_path ) {
243+
EE::debug( sprintf( 'Start enabling containers for %s', $site_url ) );
244+
245+
$site_name = str_replace( '.', '', $site_url );
246+
$project_name = sprintf( 'update-ee-%s', $site_name );
247+
248+
if ( ! chdir( $site_fs_path ) ) {
249+
throw new \Exception( sprintf( '%s does not exist.', $site_fs_path ) );
250+
}
251+
252+
$command = sprintf( 'docker-compose --project-name=%s up -d nginx', $project_name );
253+
if ( ! EE::exec( $command ) ) {
254+
throw new \Exception( sprintf( 'Unable to create support container for %s', $site_url ) );
255+
}
256+
257+
EE::debug( sprintf( 'Complete enabling support containers for %s', $site_url ) );
258+
}
259+
260+
/**
261+
* Disable support containers for sites.
262+
*
263+
* @param $site_url string Site URL.
264+
* @param $site_fs_path string File system path of site.
265+
*
266+
* @throws \Exception
267+
*/
268+
public static function disable_support_containers( $site_url, $site_fs_path ) {
269+
EE::debug( sprintf( 'Start disabling support containers for %s', $site_url ) );
270+
271+
$site_name = str_replace( '.', '', $site_url );
272+
$project_name = sprintf( 'update-ee-%s', $site_name );
273+
274+
if ( ! chdir( $site_fs_path ) ) {
275+
throw new \Exception( sprintf( '%s does not exist.', $site_fs_path ) );
276+
}
277+
278+
$command = sprintf( 'docker-compose --project-name=%1$s stop && docker-compose --project-name=%1$s rm -f', $project_name );
279+
if ( ! EE::exec( $command ) ) {
280+
throw new \Exception( sprintf( 'Unable to remove support container for %s', $site_url ) );
281+
}
282+
283+
EE::debug( sprintf( 'Complete disabling support containers for %s', $site_url ) );
284+
}
222285
}

php/init-ee.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
define( 'EE_BACKUP_DIR', EE_ROOT_DIR . '/.backup' );
1616
define( 'EE_SERVICE_DIR', EE_ROOT_DIR . '/services' );
17-
define( 'EE_PROXY_TYPE', 'ee-global-nginx-proxy' );
17+
define( 'EE_PROXY_TYPE', 'services_global-nginx-proxy_1' );
1818

1919
if ( file_exists( EE_ROOT . '/vendor/autoload.php' ) ) {
2020
define( 'EE_VENDOR_DIR', EE_ROOT . '/vendor' );

0 commit comments

Comments
 (0)