|
| 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 | +} |
0 commit comments