Lae/app/Console/Commands/Cluster/Upload.php

141 lines
4.0 KiB
PHP
Raw Normal View History

2023-01-03 11:42:52 +00:00
<?php
namespace App\Console\Commands\Cluster;
2023-01-04 12:18:22 +00:00
use App\Support\Cluster;
2023-01-03 11:42:52 +00:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Command\Command as CommandAlias;
use ZipArchive;
class Upload extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cluster:upload';
/**
* The console command description.
*
* @var string
*/
protected $description = '将此节点配置文件上传到集群中。';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
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 === 'slave') {
$this->info('正在同步从节点配置文件。');
2023-01-03 11:42:52 +00:00
2023-01-03 12:48:35 +00:00
$this->upload('slave');
2023-01-03 11:42:52 +00:00
2023-01-03 12:48:35 +00:00
$this->info('从节点配置文件同步完成。');
2023-01-03 12:49:46 +00:00
return CommandAlias::SUCCESS;
2023-01-03 11:42:52 +00:00
}
2023-01-03 12:48:35 +00:00
$this->warn('此节点为主节点,将同时上传两份版本(如果有 .env.slave 的话)。');
// 上传 master
$this->upload('master');
2023-01-03 11:42:52 +00:00
// 检测 .env.slave 是否存在
if (file_exists(base_path('.env.slave'))) {
// 备份当前的 .env 文件为 .env.temp
$this->info('正在备份 .env 文件。');
copy(base_path('.env'), base_path('.env.temp'));
// 复制 .env.slave 为 .env
$this->info('正在复制 .env.slave 文件。');
copy(base_path('.env.slave'), base_path('.env'));
$this->upload('slave');
// 恢复 .env 文件
$this->info('正在恢复 .env 文件。');
copy(base_path('.env.temp'), base_path('.env'));
// 删除 .env.temp
unlink(base_path('.env.temp'));
}
2023-01-04 15:57:36 +00:00
Cluster::publish('config.updated');
2023-01-03 12:48:35 +00:00
$this->info('节点初始化完成。');
2023-01-03 11:42:52 +00:00
if (app()->environment() === 'local') {
$this->info('清理开发节点。');
Artisan::call('route:clear');
Artisan::call('config:clear');
}
return CommandAlias::SUCCESS;
}
public function addFileToZip(string $path, ZipArchive $zip): void
{
$handler = opendir($path);
while (($filename = readdir($handler)) !== false) {
if ($filename != '.' && $filename != '..') {
if (is_dir($path . '/' . $filename)) {
$this->addFileToZip($path . '/' . $filename, $zip);
} else {
$zip->addFile($path . '/' . $filename);
}
}
}
@closedir($handler);
}
public function upload($node_type)
{
$this->warn("正在上传 ${node_type} 文件。");
$this->info('正在打包 config 目录。');
if ($node_type === 'master') {
// 相对路径
$cache = 'config';
$cacheZip = $cache . '.zip';
$zip = new ZipArchive();
$zip->open($cacheZip, ZipArchive::CREATE);
$this->addFileToZip($cache, $zip);
$zip->close();
$this->info('正在上传 config 目录。');
2023-01-04 15:57:36 +00:00
$cache_key = "${node_type}_config_zip";
2023-01-04 12:18:22 +00:00
Cluster::forever($cache_key, file_get_contents($cacheZip));
2023-01-03 11:42:52 +00:00
// md5
$this->info('正在报告 cache 目录的 MD5 值。');
2023-01-04 15:57:36 +00:00
$cache_md5_key = "${node_type}_config_zip_md5";
2023-01-04 12:18:22 +00:00
Cluster::forever($cache_md5_key, md5_file($cacheZip));
2023-01-03 11:42:52 +00:00
unlink($cacheZip);
}
// 上传 .env 文件
$this->info('正在上传 .env 文件。');
2023-01-04 12:18:22 +00:00
$env_key = "${node_type}_env";
Cluster::forever($env_key, file_get_contents(base_path('.env')));
2023-01-03 11:42:52 +00:00
// 上传 .env 文件的 MD5
$this->info('正在报告 .env 文件的 MD5 值。');
2023-01-04 12:18:22 +00:00
$env_md5_key = "${node_type}_env_md5";
Cluster::forever($env_md5_key, md5_file(base_path('.env')));
2023-01-03 11:42:52 +00:00
$this->info('完成。');
}
}