From 58dfff5b0d1973ca5b4f7bc343eda911b82e9c58 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Tue, 3 Jan 2023 20:48:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Cluster/Sync.php | 51 ++++++++++++++++++++++--- app/Console/Commands/Cluster/Upload.php | 23 +++++------ 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/app/Console/Commands/Cluster/Sync.php b/app/Console/Commands/Cluster/Sync.php index 0c415b1..9406649 100644 --- a/app/Console/Commands/Cluster/Sync.php +++ b/app/Console/Commands/Cluster/Sync.php @@ -3,6 +3,7 @@ namespace App\Console\Commands\Cluster; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Storage; use Symfony\Component\Console\Command\Command as CommandAlias; @@ -35,12 +36,21 @@ public function handle(): int $node_type = config('settings.node_type'); - $cache_key = "cluster:${node_type}_config"; + if ($node_type === 'master') { + $confirm = $this->ask('主节点同步将会恢复上一次数据,确定吗?', 'yes'); + + if ($confirm !== 'yes') { + $this->warn('已取消。'); + return CommandAlias::SUCCESS; + } + } + + $cache_key = "cluster:master_config"; $config = Cache::get($cache_key); if ($config) { $this->info('检查下载目录的 MD5 值。'); - $config_md5_key = "cluster:${node_type}_config_md5"; + $config_md5_key = "cluster:master_config_md5"; $config_md5 = Cache::get($config_md5_key, ''); $md5 = md5($config); @@ -51,9 +61,12 @@ public function handle(): int // 将缓存写入文件 $this->info('正在写入文件。'); - Storage::disk('local')->put('cluster/config.zip', $config); - $path = Storage::disk('local')->path('cluster/config.zip'); + $dir = 'cluster/config.zip'; + + Storage::disk('local')->put($dir, $config); + + $path = Storage::disk('local')->path($dir); // 删除 config 目录 $this->info('正在删除 config 目录。'); @@ -63,17 +76,43 @@ public function handle(): int $cmd = "rm -rf ${cache_path}"; exec($cmd); - $this->info('正在解压缩。'); + $this->info('正在解压缩。'); $zip = new ZipArchive(); $zip->open($path); $zip->extractTo(base_path()); $zip->close(); + if ($node_type === 'slave') { + // 下载 .env 文件 + $this->info('正在下载 .env 文件。'); + $env_cache_key = "cluster:${node_type}_env"; + $env_md5_key = "cluster:${node_type}_env_md5"; + + $env = Cache::get($env_cache_key); + $env_md5 = Cache::get($env_md5_key); + + $this->info('检查 .env 文件的 MD5 值。'); + if (md5($env) !== $env_md5) { + $this->error('.env 文件 MD5 值被篡改。请尝试从其他节点重新同步。'); + return CommandAlias::FAILURE; + } else { + $this->info('正在写入 .env 文件。'); + + // 覆盖 .env 文件 + file_put_contents(base_path('.env'), $env); + } + + } + $this->info('正在清理。'); // 删除目录 - Storage::disk('local')->deleteDirectory('cluster'); + Storage::disk('local')->delete($dir); + + // 刷新配置 + $this->info('正在刷新配置。'); + Artisan::call('optimize'); } else { $this->error('没有找到缓存。请尝试从其他节点重新同步。'); return CommandAlias::FAILURE; diff --git a/app/Console/Commands/Cluster/Upload.php b/app/Console/Commands/Cluster/Upload.php index 3d68f8a..3c448d7 100644 --- a/app/Console/Commands/Cluster/Upload.php +++ b/app/Console/Commands/Cluster/Upload.php @@ -31,18 +31,22 @@ class Upload extends Command */ public function handle(): int { - $this->info('正在初始化节点。'); - $node_type = config('settings.node_type'); - if ($node_type === 'master') { - $this->warn('此节点为主节点,将同时上传两份版本(如果有 .env.slave 的话)。'); + if ($node_type === 'slave') { + $this->info('正在同步从节点配置文件。'); - // 上传 master - $this->upload('master'); + $this->upload('slave'); + $this->info('从节点配置文件同步完成。'); + + return CommandAlias::FAILURE; } + $this->warn('此节点为主节点,将同时上传两份版本(如果有 .env.slave 的话)。'); + + // 上传 master + $this->upload('master'); // 检测 .env.slave 是否存在 if (file_exists(base_path('.env.slave'))) { @@ -62,11 +66,10 @@ public function handle(): int // 删除 .env.temp unlink(base_path('.env.temp')); - - $this->info('节点初始化完成。'); } - // if is local + $this->info('节点初始化完成。'); + if (app()->environment() === 'local') { $this->info('清理开发节点。'); @@ -90,7 +93,6 @@ public function addFileToZip(string $path, ZipArchive $zip): void } } @closedir($handler); - } public function upload($node_type) @@ -119,7 +121,6 @@ public function upload($node_type) Cache::forever($cache_md5_key, md5_file($cacheZip)); unlink($cacheZip); - } // 上传 .env 文件