<commit_before><?php

namespace EE\Migration;

use EE;

/**
 * Upgrade existing global containers to new docker-image
 */
class GlobalContainers {

	/**
	 * Get global containers which has new image update.
	 *
	 * @param $updated_images array of updated docker-images
	 *
	 * @return array
	 */
	public static function get_updated_global_images( $updated_images ) {

		$global_images           = self::get_all_global_images_with_service_name();
		$running_global_services = [];
		foreach ( $global_images as $image => $container_name ) {
			if ( 'running' === \EE_DOCKER::container_status( $container_name ) ) {
				$running_global_services[] = $image;
			}
		}

		return array_intersect( $running_global_services, $updated_images );
	}

	/**
	 * * Restore  backed up docker-compose.yml file.
	 *
	 * @param $source_path string path of backup file.
	 * @param $dest_path   string path of global docker-compose.yml
	 *
	 * @throws \Exception
	 */
	public static function revert_global_containers( $source_path, $dest_path, $updated_images ) {

		$services_to_regenerate = '';
		$all_global_images      = self::get_all_global_images_with_service_name();
		foreach ( $updated_global_images as $image_name ) {
			$global_container_name  = $all_global_images[ $image_name ];
			$services_to_regenerate .= str_replace( '-', '_', ltrim( $global_container_name, 'ee-' ) ) . ' ';
		}
		if ( empty( trim( $services_to_regenerate ) ) ) {
			return;
		}
		EE::debug( 'Start restoring global docker-compose.yml file from backup' );
		$fs = new Filesystem();
		$fs->copy( $source_path, $dest_path, true );

		chdir( EE_ROOT_DIR . '/services' );

		if ( ! EE::exec( 'docker-compose up -d ' . $services_to_regenerate ) ) {
			throw new \Exception( 'Unable to downgrade global containers. Please check logs for more details.' );
		}
		EE::debug( 'Complete restoring global docker-compose.yml file from backup' );
	}

	/**
	 * Stop global container and remove them.
	 *
	 * @param $updated_images array of newly available images.
	 *
	 * @throws \Exception<commit_msg>Fix revert-global containers variable<commit_after><?php

namespace EE\Migration;

use EE;

/**
 * Upgrade existing global containers to new docker-image
 */
class GlobalContainers {

	/**
	 * Get global containers which has new image update.
	 *
	 * @param $updated_images array of updated docker-images
	 *
	 * @return array
	 */
	public static function get_updated_global_images( $updated_images ) {

		$global_images           = self::get_all_global_images_with_service_name();
		$running_global_services = [];
		foreach ( $global_images as $image => $container_name ) {
			if ( 'running' === \EE_DOCKER::container_status( $container_name ) ) {
				$running_global_services[] = $image;
			}
		}

		return array_intersect( $running_global_services, $updated_images );
	}

	/**
	 * * Restore  backed up docker-compose.yml file.
	 *
	 * @param $source_path string path of backup file.
	 * @param $dest_path   string path of global docker-compose.yml
	 *
	 * @throws \Exception
	 */
	public static function revert_global_containers( $source_path, $dest_path, $updated_images ) {

		$services_to_regenerate = '';
		$all_global_images      = self::get_all_global_images_with_service_name();
		foreach ( $updated_images as $image_name ) {
			$global_container_name  = $all_global_images[ $image_name ];
			$services_to_regenerate .= str_replace( '-', '_', ltrim( $global_container_name, 'ee-' ) ) . ' ';
		}
		if ( empty( trim( $services_to_regenerate ) ) ) {
			return;
		}
		EE::debug( 'Start restoring global docker-compose.yml file from backup' );
		$fs = new Filesystem();
		$fs->copy( $source_path, $dest_path, true );

		chdir( EE_ROOT_DIR . '/services' );

		if ( ! EE::exec( 'docker-compose up -d ' . $services_to_regenerate ) ) {
			throw new \Exception( 'Unable to downgrade global containers. Please check logs for more details.' );
		}
		EE::debug( 'Complete restoring global docker-compose.yml file from backup' );
	}

	/**
	 * Stop global container and remove them.
	 *
	 * @param $updated_images array of newly available images.
	 *
	 * @throws \Exception
{'fraction_non_alphanumeric': 0.12260257344015538, 'fraction_numerical': 0.0, 'mean_word_length': 3.8873072360616843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5155262', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1277, 'n_words': 326}