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,42 @@ 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 = EE ::launch ( 'docker-compose version --short ' )->stdout ;
132+ $ docker_compose_path = EE ::launch ( 'command -v docker-compose ' )->stdout ;
133+ $ docker_compose_path = trim ( $ docker_compose_path );
134+ $ docker_compose_backup_path = EE_BACKUP_DIR . '/docker-compose.backup ' ;
135+ $ docker_compose_new_path = EE_BACKUP_DIR . '/docker-compose ' ;
136+
137+ if ( version_compare ( '2.27.0 ' , $ docker_compose_version , '> ' ) ) {
138+
139+ $ fs = new Filesystem ();
140+ if ( ! $ fs ->exists ( EE_BACKUP_DIR ) ) {
141+ $ fs ->mkdir ( EE_BACKUP_DIR );
142+ }
143+ $ fs ->copy ( $ docker_compose_path , $ docker_compose_backup_path );
144+
145+ 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 " );
146+ EE ::exec ( "mv $ docker_compose_new_path $ docker_compose_path " );
147+ }
148+ }
149+
150+ /**
151+ * Revert docker-compose to previous version.
152+ */
153+ public static function revert_docker_compose () {
154+
155+ $ docker_compose_path = EE ::launch ( 'command -v docker-compose ' )->stdout ;
156+ $ docker_compose_path = trim ( $ docker_compose_path );
157+ $ docker_compose_backup_path = EE_BACKUP_DIR . '/docker-compose.backup ' ;
158+ $ fs = new Filesystem ();
159+ $ fs ->copy ( $ docker_compose_backup_path , $ docker_compose_path );
160+ }
161+
105162 /**
106163 * Update database entry of images
107164 *
@@ -151,16 +208,29 @@ public static function pull_or_error( $image, $version ) {
151208 * @throws \Exception
152209 */
153210 public static function get_current_docker_images_versions () {
154- $ images = EE ::db ()
155- ->table ( 'options ' )
156- ->where ( 'key ' , 'like ' , 'easyengine/% ' )
157- ->all ();
158211
159- $ images = array_map ( function ( $ image ) {
160- return [ $ image ['key ' ] => $ image ['value ' ] ];
161- }, $ images );
212+ $ dbImages = EE ::db ()
213+ ->table ( 'options ' )
214+ ->where ( 'key ' , 'like ' , 'easyengine/% ' )
215+ ->all ();
216+
217+ $ dbImages = array_column ( $ dbImages , 'value ' , 'key ' );
218+
219+ $ dockerImages = EE ::launch ( 'docker ps --format "{{.Image}}" | grep "^easyengine/" | sort -u ' )->stdout ;
220+ $ dockerImages = explode ( "\n" , trim ( $ dockerImages ) );
221+
222+ $ dockerImages = array_reduce ( $ dockerImages , function ( $ result , $ image ) {
223+
224+ [ $ imageName , $ tag ] = explode ( ': ' , $ image , 2 ) + [ 1 => null ];
225+ $ result [ $ imageName ] = $ tag ;
226+
227+ return $ result ;
228+ }, [] );
162229
163- return array_merge ( ...$ images );
230+ $ mergedImages = $ dockerImages + $ dbImages ;
231+ $ mergedImages = array_filter ( $ mergedImages );
232+
233+ return $ mergedImages ;
164234 }
165235
166236 /**
@@ -186,17 +256,6 @@ private static function migrate_global_containers( $updated_images ) {
186256 [ $ global_compose_file_backup_path , $ global_compose_file_path , $ updated_global_images ]
187257 );
188258
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-
200259 self ::$ rsp ->add_step (
201260 'stop-global-containers ' ,
202261 'EE\Migration\GlobalContainers::down_global_containers ' ,
@@ -226,17 +285,6 @@ private static function migrate_global_containers( $updated_images ) {
226285 [ $ global_service_name ]
227286 );
228287 }
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- );
240288 }
241289
242290 /**
0 commit comments