44
55use EE \RevertableStepProcessor ;
66use EE ;
7+ use Symfony \Component \Filesystem \Filesystem ;
78
89/**
910 * Upgrade existing containers to new docker-image
@@ -35,30 +36,35 @@ public static function start_container_migration() {
3536 'easyengine/php7.4 ' ,
3637 'easyengine/php8.0 ' ,
3738 'easyengine/php8.1 ' ,
39+ 'easyengine/php8.2 ' ,
40+ 'easyengine/php8.3 ' ,
3841 'easyengine/newrelic-daemon ' ,
3942 ];
4043
4144 foreach ( $ img_versions as $ img => $ version ) {
4245
43- if ( in_array ( $ img , $ skip_download ) ) {
44- continue ;
45- }
4646 if ( array_key_exists ( $ img , $ current_versions ) ) {
4747 if ( $ current_versions [ $ img ] !== $ version ) {
4848 $ updated_images [] = $ img ;
49- self ::pull_or_error ( $ img , $ version );
49+ if ( ! in_array ( $ img , $ skip_download ) ) {
50+ self ::pull_or_error ( $ img , $ version );
51+ }
5052 }
5153 } else {
52- self ::pull_or_error ( $ img , $ version );
54+ if ( ! in_array ( $ img , $ skip_download ) ) {
55+ self ::pull_or_error ( $ img , $ version );
56+ }
5357 }
5458 }
5559
5660 if ( empty ( $ updated_images ) ) {
61+
5762 return ;
5863 }
5964
6065 self ::migrate_global_containers ( $ updated_images );
6166 self ::migrate_site_containers ( $ updated_images );
67+ self ::maybe_update_docker_compose ();
6268 self ::save_upgraded_image_versions ( $ current_versions , $ img_versions , $ updated_images );
6369
6470 if ( ! self ::$ rsp ->execute () ) {
@@ -68,6 +74,21 @@ public static function start_container_migration() {
6874 EE \Utils \delem_log ( 'Container migration completed ' );
6975 }
7076
77+ /**
78+ * Maybe update docker-compose at the end of migration.
79+ * Need to update to latest docker-compose version for new template changes.
80+ */
81+ public static function maybe_update_docker_compose () {
82+
83+ self ::$ rsp ->add_step (
84+ 'update-compose ' ,
85+ 'EE\Migration\Containers::update_docker_compose ' ,
86+ 'EE\Migration\Containers::revert_docker_compose ' ,
87+ null ,
88+ null
89+ );
90+ }
91+
7192 /**
7293 * Save updated image version in database.
7394 *
@@ -102,6 +123,41 @@ public static function image_cleanup() {
102123 EE ::exec ( 'docker image prune -af --filter=label=org.label-schema.vendor="EasyEngine" ' );
103124 }
104125
126+ /**
127+ * Update docker-compose to v2.27.0 if lower version is installed.
128+ */
129+ public static function update_docker_compose () {
130+
131+ $ docker_compose_version = trim ( EE ::launch ( 'docker-compose version --short ' )->stdout );
132+ $ docker_compose_path = trim ( EE ::launch ( 'command -v docker-compose ' )->stdout );
133+ $ docker_compose_backup_path = EE_BACKUP_DIR . '/docker-compose.backup ' ;
134+ $ docker_compose_new_path = EE_BACKUP_DIR . '/docker-compose ' ;
135+
136+ if ( version_compare ( '2.27.0 ' , $ docker_compose_version , '> ' ) ) {
137+
138+ $ fs = new Filesystem ();
139+ if ( ! $ fs ->exists ( EE_BACKUP_DIR ) ) {
140+ $ fs ->mkdir ( EE_BACKUP_DIR );
141+ }
142+ $ fs ->copy ( $ docker_compose_path , $ docker_compose_backup_path );
143+
144+ EE ::exec ( "curl -L https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-$(uname -s)-$(uname -m) -o $ docker_compose_new_path && chmod +x $ docker_compose_new_path " );
145+ EE ::exec ( "mv $ docker_compose_new_path $ docker_compose_path " );
146+ }
147+ }
148+
149+ /**
150+ * Revert docker-compose to previous version.
151+ */
152+ public static function revert_docker_compose () {
153+
154+ $ docker_compose_path = EE ::launch ( 'command -v docker-compose ' )->stdout ;
155+ $ docker_compose_path = trim ( $ docker_compose_path );
156+ $ docker_compose_backup_path = EE_BACKUP_DIR . '/docker-compose.backup ' ;
157+ $ fs = new Filesystem ();
158+ $ fs ->copy ( $ docker_compose_backup_path , $ docker_compose_path );
159+ }
160+
105161 /**
106162 * Update database entry of images
107163 *
@@ -151,16 +207,29 @@ public static function pull_or_error( $image, $version ) {
151207 * @throws \Exception
152208 */
153209 public static function get_current_docker_images_versions () {
154- $ images = EE ::db ()
155- ->table ( 'options ' )
156- ->where ( 'key ' , 'like ' , 'easyengine/% ' )
157- ->all ();
158210
159- $ images = array_map ( function ( $ image ) {
160- return [ $ image ['key ' ] => $ image ['value ' ] ];
161- }, $ images );
211+ $ dbImages = EE ::db ()
212+ ->table ( 'options ' )
213+ ->where ( 'key ' , 'like ' , 'easyengine/% ' )
214+ ->all ();
215+
216+ $ dbImages = array_column ( $ dbImages , 'value ' , 'key ' );
217+
218+ $ dockerImages = EE ::launch ( 'docker ps --format "{{.Image}}" | grep "^easyengine/" | sort -u ' )->stdout ;
219+ $ dockerImages = explode ( "\n" , trim ( $ dockerImages ) );
220+
221+ $ dockerImages = array_reduce ( $ dockerImages , function ( $ result , $ image ) {
222+
223+ [ $ imageName , $ tag ] = explode ( ': ' , $ image , 2 ) + [ 1 => null ];
224+ $ result [ $ imageName ] = $ tag ;
225+
226+ return $ result ;
227+ }, [] );
162228
163- return array_merge ( ...$ images );
229+ $ mergedImages = $ dockerImages + $ dbImages ;
230+ $ mergedImages = array_filter ( $ mergedImages );
231+
232+ return $ mergedImages ;
164233 }
165234
166235 /**
@@ -186,17 +255,6 @@ private static function migrate_global_containers( $updated_images ) {
186255 [ $ global_compose_file_backup_path , $ global_compose_file_path , $ updated_global_images ]
187256 );
188257
189- /**
190- * Create support containers.
191- */
192- self ::$ rsp ->add_step (
193- 'create-support-global-containers ' ,
194- 'EE\Migration\GlobalContainers::enable_support_containers ' ,
195- 'EE\Migration\GlobalContainers::disable_support_containers ' ,
196- null ,
197- null
198- );
199-
200258 self ::$ rsp ->add_step (
201259 'stop-global-containers ' ,
202260 'EE\Migration\GlobalContainers::down_global_containers ' ,
@@ -226,17 +284,6 @@ private static function migrate_global_containers( $updated_images ) {
226284 [ $ global_service_name ]
227285 );
228286 }
229-
230- /**
231- * Remove support containers.
232- */
233- self ::$ rsp ->add_step (
234- 'remove-support-global-containers ' ,
235- 'EE\Migration\GlobalContainers::disable_support_containers ' ,
236- 'EE\Migration\GlobalContainers::enable_support_containers ' ,
237- null ,
238- null
239- );
240287 }
241288
242289 /**
0 commit comments