改进 同步

This commit is contained in:
iVampireSP.com 2023-01-03 20:48:35 +08:00
parent d70d502377
commit 58dfff5b0d
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 57 additions and 17 deletions

View File

@ -3,6 +3,7 @@
namespace App\Console\Commands\Cluster; namespace App\Console\Commands\Cluster;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Symfony\Component\Console\Command\Command as CommandAlias; use Symfony\Component\Console\Command\Command as CommandAlias;
@ -35,12 +36,21 @@ public function handle(): int
$node_type = config('settings.node_type'); $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); $config = Cache::get($cache_key);
if ($config) { if ($config) {
$this->info('检查下载目录的 MD5 值。'); $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, ''); $config_md5 = Cache::get($config_md5_key, '');
$md5 = md5($config); $md5 = md5($config);
@ -51,9 +61,12 @@ public function handle(): int
// 将缓存写入文件 // 将缓存写入文件
$this->info('正在写入文件。'); $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 目录 // 删除 config 目录
$this->info('正在删除 config 目录。'); $this->info('正在删除 config 目录。');
@ -63,17 +76,43 @@ public function handle(): int
$cmd = "rm -rf ${cache_path}"; $cmd = "rm -rf ${cache_path}";
exec($cmd); exec($cmd);
$this->info('正在解压缩。');
$this->info('正在解压缩。');
$zip = new ZipArchive(); $zip = new ZipArchive();
$zip->open($path); $zip->open($path);
$zip->extractTo(base_path()); $zip->extractTo(base_path());
$zip->close(); $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('正在清理。'); $this->info('正在清理。');
// 删除目录 // 删除目录
Storage::disk('local')->deleteDirectory('cluster'); Storage::disk('local')->delete($dir);
// 刷新配置
$this->info('正在刷新配置。');
Artisan::call('optimize');
} else { } else {
$this->error('没有找到缓存。请尝试从其他节点重新同步。'); $this->error('没有找到缓存。请尝试从其他节点重新同步。');
return CommandAlias::FAILURE; return CommandAlias::FAILURE;

View File

@ -31,19 +31,23 @@ class Upload extends Command
*/ */
public function handle(): int public function handle(): int
{ {
$this->info('正在初始化节点。');
$node_type = config('settings.node_type'); $node_type = config('settings.node_type');
if ($node_type === 'master') { if ($node_type === 'slave') {
$this->info('正在同步从节点配置文件。');
$this->upload('slave');
$this->info('从节点配置文件同步完成。');
return CommandAlias::FAILURE;
}
$this->warn('此节点为主节点,将同时上传两份版本(如果有 .env.slave 的话)。'); $this->warn('此节点为主节点,将同时上传两份版本(如果有 .env.slave 的话)。');
// 上传 master // 上传 master
$this->upload('master'); $this->upload('master');
}
// 检测 .env.slave 是否存在 // 检测 .env.slave 是否存在
if (file_exists(base_path('.env.slave'))) { if (file_exists(base_path('.env.slave'))) {
// 备份当前的 .env 文件为 .env.temp // 备份当前的 .env 文件为 .env.temp
@ -62,11 +66,10 @@ public function handle(): int
// 删除 .env.temp // 删除 .env.temp
unlink(base_path('.env.temp')); unlink(base_path('.env.temp'));
$this->info('节点初始化完成。');
} }
// if is local $this->info('节点初始化完成。');
if (app()->environment() === 'local') { if (app()->environment() === 'local') {
$this->info('清理开发节点。'); $this->info('清理开发节点。');
@ -90,7 +93,6 @@ public function addFileToZip(string $path, ZipArchive $zip): void
} }
} }
@closedir($handler); @closedir($handler);
} }
public function upload($node_type) public function upload($node_type)
@ -119,7 +121,6 @@ public function upload($node_type)
Cache::forever($cache_md5_key, md5_file($cacheZip)); Cache::forever($cache_md5_key, md5_file($cacheZip));
unlink($cacheZip); unlink($cacheZip);
} }
// 上传 .env 文件 // 上传 .env 文件