2023-01-03 11:42:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands\Cluster;
|
|
|
|
|
2023-01-14 17:21:12 +00:00
|
|
|
use App\Support\ClusterSupport;
|
2023-01-03 11:42:52 +00:00
|
|
|
use Illuminate\Console\Command;
|
2023-01-03 12:48:35 +00:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2023-01-03 11:42:52 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Symfony\Component\Console\Command\Command as CommandAlias;
|
|
|
|
use ZipArchive;
|
|
|
|
|
|
|
|
class Sync extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-01-04 15:57:36 +00:00
|
|
|
protected $signature = 'cluster:sync {--force=false}';
|
2023-01-03 11:42:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = '下载配置。';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
|
|
|
$this->info('正在下载配置。');
|
|
|
|
|
2023-01-03 13:13:53 +00:00
|
|
|
$node_type = config('settings.node.type');
|
2023-01-03 11:42:52 +00:00
|
|
|
|
2023-01-03 12:48:35 +00:00
|
|
|
if ($node_type === 'master') {
|
2023-01-04 15:57:36 +00:00
|
|
|
// if force is true, delete the old file
|
|
|
|
if ($this->option('force') === 'true') {
|
|
|
|
$confirm = 'yes';
|
|
|
|
} else {
|
|
|
|
$confirm = $this->ask('主节点同步将会恢复上一次数据,确定吗?', true);
|
|
|
|
}
|
|
|
|
|
2023-02-07 09:04:11 +00:00
|
|
|
if (! $confirm) {
|
2023-01-03 12:48:35 +00:00
|
|
|
$this->warn('已取消。');
|
2023-01-30 16:14:07 +00:00
|
|
|
|
2023-01-03 12:48:35 +00:00
|
|
|
return CommandAlias::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 16:14:07 +00:00
|
|
|
$cache_key = 'master_config_zip';
|
2023-01-14 17:21:12 +00:00
|
|
|
$config = ClusterSupport::get($cache_key);
|
2023-01-03 11:42:52 +00:00
|
|
|
|
|
|
|
if ($config) {
|
|
|
|
$this->info('检查下载目录的 MD5 值。');
|
2023-01-30 16:14:07 +00:00
|
|
|
$config_md5_key = 'master_config_zip_md5';
|
2023-01-14 17:21:12 +00:00
|
|
|
$config_md5 = ClusterSupport::get($config_md5_key, '');
|
2023-01-03 11:42:52 +00:00
|
|
|
|
|
|
|
$md5 = md5($config);
|
|
|
|
if ($md5 !== $config_md5) {
|
|
|
|
$this->error('下载目录 MD5 值被篡改。请尝试从其他节点重新同步。');
|
2023-01-30 16:14:07 +00:00
|
|
|
|
2023-01-03 11:42:52 +00:00
|
|
|
return CommandAlias::FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将缓存写入文件
|
|
|
|
$this->info('正在写入文件。');
|
|
|
|
|
2023-01-03 12:48:35 +00:00
|
|
|
$dir = 'cluster/config.zip';
|
|
|
|
|
|
|
|
Storage::disk('local')->put($dir, $config);
|
|
|
|
|
|
|
|
$path = Storage::disk('local')->path($dir);
|
2023-01-03 11:42:52 +00:00
|
|
|
|
|
|
|
// 删除 config 目录
|
|
|
|
$this->info('正在删除 config 目录。');
|
|
|
|
$cache_path = base_path('config');
|
|
|
|
|
|
|
|
// exec
|
2023-01-10 13:42:27 +00:00
|
|
|
$cmd = "rm -rf $cache_path";
|
2023-01-03 11:42:52 +00:00
|
|
|
exec($cmd);
|
|
|
|
|
2023-01-03 12:48:35 +00:00
|
|
|
$this->info('正在解压缩。');
|
2023-01-03 11:42:52 +00:00
|
|
|
$zip = new ZipArchive();
|
|
|
|
$zip->open($path);
|
|
|
|
$zip->extractTo(base_path());
|
|
|
|
$zip->close();
|
|
|
|
|
2023-01-03 12:48:35 +00:00
|
|
|
if ($node_type === 'slave') {
|
|
|
|
// 下载 .env 文件
|
|
|
|
$this->info('正在下载 .env 文件。');
|
2023-01-08 09:03:01 +00:00
|
|
|
$env_cache_key = "{$node_type}_env";
|
|
|
|
$env_md5_key = "{$node_type}_env_md5";
|
2023-01-03 12:48:35 +00:00
|
|
|
|
2023-01-14 17:21:12 +00:00
|
|
|
$env = ClusterSupport::get($env_cache_key);
|
|
|
|
$env_md5 = ClusterSupport::get($env_md5_key);
|
2023-01-03 12:48:35 +00:00
|
|
|
|
|
|
|
$this->info('检查 .env 文件的 MD5 值。');
|
|
|
|
if (md5($env) !== $env_md5) {
|
|
|
|
$this->error('.env 文件 MD5 值被篡改。请尝试从其他节点重新同步。');
|
2023-01-30 16:14:07 +00:00
|
|
|
|
2023-01-03 12:48:35 +00:00
|
|
|
return CommandAlias::FAILURE;
|
|
|
|
} else {
|
|
|
|
$this->info('正在写入 .env 文件。');
|
|
|
|
|
2023-01-04 18:16:18 +00:00
|
|
|
// $node_ip
|
|
|
|
$node_ip = config('settings.node.ip');
|
|
|
|
|
2023-01-03 12:48:35 +00:00
|
|
|
// 覆盖 .env 文件
|
|
|
|
file_put_contents(base_path('.env'), $env);
|
2023-01-04 18:16:18 +00:00
|
|
|
|
|
|
|
// 还原 $node_ip 到 .env
|
|
|
|
$this->info('正在还原 .env 文件中的 NODE_IP。');
|
|
|
|
$env = file_get_contents(base_path('.env'));
|
|
|
|
|
|
|
|
// REPLACE NODE_IP 这一行
|
2023-01-10 13:42:27 +00:00
|
|
|
$env = preg_replace('/^NODE_IP=.*$/m', "NODE_IP=$node_ip", $env);
|
2023-01-04 18:16:18 +00:00
|
|
|
|
|
|
|
file_put_contents(base_path('.env'), $env);
|
2023-01-03 12:48:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 11:42:52 +00:00
|
|
|
$this->info('正在清理。');
|
|
|
|
|
|
|
|
// 删除目录
|
2023-01-03 12:48:35 +00:00
|
|
|
Storage::disk('local')->delete($dir);
|
|
|
|
|
|
|
|
// 刷新配置
|
|
|
|
$this->info('正在刷新配置。');
|
|
|
|
Artisan::call('optimize');
|
2023-01-04 15:57:36 +00:00
|
|
|
|
|
|
|
// 上报消息
|
2023-01-14 17:21:12 +00:00
|
|
|
ClusterSupport::publish('synced');
|
2023-01-03 11:42:52 +00:00
|
|
|
} else {
|
|
|
|
$this->error('没有找到缓存。请尝试从其他节点重新同步。');
|
2023-01-30 16:14:07 +00:00
|
|
|
|
2023-01-03 11:42:52 +00:00
|
|
|
return CommandAlias::FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandAlias::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|