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

164 lines
5.2 KiB
PHP
Raw Normal View History

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;
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-18 10:12:28 +00:00
$this->warn('此节点为主节点,将同时上传两份版本(如果有 .env.slave 的话)。以及让边缘节点重载 SSL。');
2023-01-03 12:48:35 +00:00
// 上传 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-14 17:21:12 +00:00
ClusterSupport::publish('config.updated');
2023-01-04 15:57:36 +00:00
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 upload($node_type)
{
2023-01-10 13:42:27 +00:00
$this->warn("正在上传 $node_type 文件。");
2023-01-03 11:42:52 +00:00
$this->info('正在打包 config 目录。');
if ($node_type === 'master') {
// 相对路径
$cache = 'config';
2023-02-07 09:03:47 +00:00
$cacheZip = $cache . '.zip';
2023-01-03 11:42:52 +00:00
$zip = new ZipArchive();
$zip->open($cacheZip, ZipArchive::CREATE);
$this->addFileToZip($cache, $zip);
$zip->close();
$this->info('正在上传 config 目录。');
2023-01-05 14:12:47 +00:00
$cache_key = "{$node_type}_config_zip";
2023-01-14 17:21:12 +00:00
ClusterSupport::forever($cache_key, file_get_contents($cacheZip));
2023-01-03 11:42:52 +00:00
// md5
$this->info('正在报告 cache 目录的 MD5 值。');
2023-01-05 14:12:47 +00:00
$cache_md5_key = "{$node_type}_config_zip_md5";
2023-01-14 17:21:12 +00:00
ClusterSupport::forever($cache_md5_key, md5_file($cacheZip));
2023-01-03 11:42:52 +00:00
unlink($cacheZip);
2023-01-18 10:12:28 +00:00
// 上传 config/secrets/ssl_fullchain.pem 和 config/secrets/ssl_privkey.pem
2023-01-30 16:14:07 +00:00
$ssl_fullchain_key = 'config/secrets/ssl_fullchain.pem';
$ssl_privkey_key = 'config/secrets/ssl_privkey.pem';
2023-01-18 10:12:28 +00:00
if (file_exists(base_path($ssl_fullchain_key)) && file_exists(base_path($ssl_privkey_key))) {
$this->info('正在上传 SSL 证书。');
2023-01-30 16:14:07 +00:00
ClusterSupport::forever('ssl_fullchain', file_get_contents(base_path($ssl_fullchain_key)));
ClusterSupport::forever('ssl_privkey', file_get_contents(base_path($ssl_privkey_key)));
2023-01-18 10:12:28 +00:00
// 计算 md5
$ssl_fullchain_md5 = md5_file(base_path($ssl_fullchain_key));
$ssl_privkey_md5 = md5_file(base_path($ssl_privkey_key));
$this->info('正在报告 SSL 证书的 MD5 值。');
2023-01-30 16:14:07 +00:00
ClusterSupport::forever('ssl_fullchain_md5', $ssl_fullchain_md5);
ClusterSupport::forever('ssl_privkey_md5', $ssl_privkey_md5);
2023-01-18 10:12:28 +00:00
ClusterSupport::publish('config.ssl.updated');
} else {
$this->warn('SSL 证书不存在,跳过。');
}
2023-01-03 11:42:52 +00:00
}
// 上传 .env 文件
$this->info('正在上传 .env 文件。');
2023-01-05 14:12:47 +00:00
$env_key = "{$node_type}_env";
2023-01-14 17:21:12 +00:00
ClusterSupport::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-05 14:12:47 +00:00
$env_md5_key = "{$node_type}_env_md5";
2023-01-14 17:21:12 +00:00
ClusterSupport::forever($env_md5_key, md5_file(base_path('.env')));
2023-01-03 11:42:52 +00:00
$this->info('完成。');
}
2023-01-05 14:12:47 +00:00
public function addFileToZip(string $path, ZipArchive $zip): void
{
$handler = opendir($path);
while (($filename = readdir($handler)) !== false) {
if ($filename != '.' && $filename != '..') {
2023-02-07 09:03:47 +00:00
if (is_dir($path . '/' . $filename)) {
$this->addFileToZip($path . '/' . $filename, $zip);
2023-01-05 14:12:47 +00:00
} else {
2023-02-07 09:03:47 +00:00
$zip->addFile($path . '/' . $filename);
2023-01-05 14:12:47 +00:00
}
}
}
@closedir($handler);
}
2023-01-03 11:42:52 +00:00
}