|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EE\Migration; |
| 4 | + |
| 5 | +use EE; |
| 6 | +use EE\Migration\Base; |
| 7 | +use EE\Migration\SiteContainers; |
| 8 | +use EE\RevertableStepProcessor; |
| 9 | +use EE\Model\Site; |
| 10 | +use Symfony\Component\Filesystem\Filesystem; |
| 11 | + |
| 12 | +class UpdateConfigWithEnv extends Base { |
| 13 | + |
| 14 | + private $sites; |
| 15 | + /** @var RevertableStepProcessor $rsp Keeps track of migration state. Reverts on error */ |
| 16 | + private static $rsp; |
| 17 | + |
| 18 | + public function __construct() { |
| 19 | + |
| 20 | + parent::__construct(); |
| 21 | + $this->sites = Site::all(); |
| 22 | + if ( $this->is_first_execution ) { |
| 23 | + $this->skip_this_migration = true; |
| 24 | + } |
| 25 | + if ( $this->fs->exists( EE_ROOT_DIR . '/admin-tools/index.php' ) ) { |
| 26 | + $this->skip_this_migration = false; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Execute php config updates. |
| 32 | + * |
| 33 | + * @throws EE\ExitException |
| 34 | + */ |
| 35 | + public function up() { |
| 36 | + |
| 37 | + if ( $this->skip_this_migration ) { |
| 38 | + EE::debug( 'Skipping update-index migration as it is not needed.' ); |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + self::$rsp = new RevertableStepProcessor(); |
| 43 | + |
| 44 | + |
| 45 | + EE::debug( 'Starting update-config-with-env' ); |
| 46 | + |
| 47 | + $pma_config = EE_ROOT_DIR . '/admin-tools/pma/config.inc.php'; |
| 48 | + $pra_config = EE_ROOT_DIR . '/admin-tools/pra/includes/config.inc.php'; |
| 49 | + $bak_pma_config = EE_BACKUP_DIR . '/admin-tools/pma/config.inc.php.bak'; |
| 50 | + $bak_pra_config = EE_BACKUP_DIR . '/admin-tools/pra/includes/config.inc.php.bak'; |
| 51 | + |
| 52 | + self::$rsp->add_step( |
| 53 | + 'take-pma-config-backup', |
| 54 | + 'EE\Migration\SiteContainers::backup_restore', |
| 55 | + 'EE\Migration\SiteContainers::backup_restore', |
| 56 | + [ $pma_config, $bak_pma_config ], |
| 57 | + [ $bak_pma_config, $pma_config ] |
| 58 | + ); |
| 59 | + |
| 60 | + self::$rsp->add_step( |
| 61 | + 'take-pra-config-backup', |
| 62 | + 'EE\Migration\SiteContainers::backup_restore', |
| 63 | + 'EE\Migration\SiteContainers::backup_restore', |
| 64 | + [ $pra_config, $bak_pra_config ], |
| 65 | + [ $bak_pra_config, $pra_config ] |
| 66 | + ); |
| 67 | + |
| 68 | + self::$rsp->add_step( |
| 69 | + 'generate-new-config-files', |
| 70 | + 'EE\Migration\UpdateConfigWithEnv::generate_new_config_files', |
| 71 | + null, |
| 72 | + null, |
| 73 | + null |
| 74 | + ); |
| 75 | + |
| 76 | + foreach ( $this->sites as $site ) { |
| 77 | + if ( $site->admin_tools ) { |
| 78 | + $array_data = ( array ) $site; |
| 79 | + $site_data = reset( $array_data ); |
| 80 | + self::$rsp->add_step( |
| 81 | + 're-enable-admin-tools', |
| 82 | + 'EE\Migration\UpdateConfigWithEnv::re_enable_admin_tools', |
| 83 | + null, |
| 84 | + [ $site_data ], |
| 85 | + null |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if ( ! self::$rsp->execute() ) { |
| 91 | + throw new \Exception( 'Unable run update-config-env migrations.' ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Not needed. |
| 97 | + * |
| 98 | + * @throws EE\ExitException |
| 99 | + */ |
| 100 | + public function down() { |
| 101 | + } |
| 102 | + |
| 103 | + public static function generate_new_config_files() { |
| 104 | + |
| 105 | + $new_pma_config = EE_ROOT_DIR . '/admin-tools/pma/config.inc.php'; |
| 106 | + $new_pra_config = EE_ROOT_DIR . '/admin-tools/pra/includes/config.inc.php'; |
| 107 | + |
| 108 | + $fs = new Filesystem(); |
| 109 | + $fs->dumpFile( $new_pma_config, file_get_contents( ADMIN_TEMPLATE_ROOT . '/pma.config.mustache' ) ); |
| 110 | + $fs->dumpFile( $new_pra_config, file_get_contents( ADMIN_TEMPLATE_ROOT . '/pra.config.mustache' ) ); |
| 111 | + |
| 112 | + $index_path_data = [ |
| 113 | + 'db_path' => DB, |
| 114 | + 'ee_admin_path' => '/var/www/htdocs/ee-admin', |
| 115 | + ]; |
| 116 | + $index_file = EE\Utils\mustache_render( ADMIN_TEMPLATE_ROOT . '/index.mustache', $index_path_data ); |
| 117 | + $fs->dumpFile( EE_ROOT_DIR . '/admin-tools/index.php', $index_file ); |
| 118 | + } |
| 119 | + |
| 120 | + public static function re_enable_admin_tools( $site ) { |
| 121 | + |
| 122 | + $fs = new Filesystem(); |
| 123 | + chdir( $site['site_fs_path'] ); |
| 124 | + |
| 125 | + $docker_compose_data = [ |
| 126 | + 'ee_root_dir' => EE_ROOT_DIR, |
| 127 | + 'db_path' => DB, |
| 128 | + 'ee_admin_path' => '/var/www/htdocs/ee-admin', |
| 129 | + 'redis_host' => $site['cache_host'], |
| 130 | + 'db_host' => $site['db_host'], |
| 131 | + ]; |
| 132 | + $docker_compose_admin = EE\Utils\mustache_render( ADMIN_TEMPLATE_ROOT . '/docker-compose-admin.mustache', $docker_compose_data ); |
| 133 | + $fs->dumpFile( $site['site_fs_path'] . '/docker-compose-admin.yml', $docker_compose_admin ); |
| 134 | + |
| 135 | + if ( EE::exec( 'docker-compose -f docker-compose.yml -f docker-compose-admin.yml up -d nginx' ) ) { |
| 136 | + EE::debug( sprintf( 'admin-tools re-enabled for %s site.', $site['site_url'] ) ); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments