改进 init

This commit is contained in:
iVampireSP.com 2023-01-03 21:13:53 +08:00
parent f6dadf7e75
commit e38d32692d
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
5 changed files with 38 additions and 14 deletions

View File

@ -107,3 +107,5 @@ DASHBOARD_BIRTHDAY_PATH=/birthdays
TRUSTED_PROXIES=
NODE_TYPE=master
NODE_ID=
NODE_IP=

View File

@ -34,7 +34,7 @@ public function handle(): int
{
$this->info('正在下载配置。');
$node_type = config('settings.node_type');
$node_type = config('settings.node.type');
if ($node_type === 'master') {
$confirm = $this->ask('主节点同步将会恢复上一次数据,确定吗?', 'yes');

View File

@ -31,7 +31,7 @@ class Upload extends Command
*/
public function handle(): int
{
$node_type = config('settings.node_type');
$node_type = config('settings.node.type');
if ($node_type === 'slave') {
$this->info('正在同步从节点配置文件。');

View File

@ -4,6 +4,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
use Symfony\Component\Console\Command\Command as CommandAlias;
class Init extends Command
@ -29,20 +30,37 @@ class Init extends Command
*/
public function handle(): int
{
$this->info('正在删除 bootstrap/cache 目录。');
$cache_path = base_path('bootstrap/cache');
$files = glob($cache_path . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
// 重写 .env 文件中的 NODE_ID
$this->info('正在重写 .env 文件中的 NODE_ID。');
$node_id = Str::random(8);
if (config('settings.node.type') === 'master') {
$node_id = 'master';
}
Artisan::call('route:clear');
Artisan::call('config:clear');
Artisan::call('view:clear');
$env = file_get_contents(base_path('.env'));
Artisan::call('optimize');
$env = preg_replace('/^NODE_ID=.*$/m', 'NODE_ID=' . $node_id, $env);
file_put_contents(base_path('.env'), $env);
if (!config('settings.node.ip')) {
$this->error('请先配置节点 IP。');
return CommandAlias::FAILURE;
}
// 刷新配置缓存
$this->info('正在刷新配置缓存。');
Artisan::call('config:cache');
// redis 创建一个 hash
$this->info('正在创建 Redis hash。');
$redis = app('redis')->connection();
$redis->hset('nodes', $node_id, [
'type' => config('settings.node.type'),
'id' => $node_id,
]);
return CommandAlias::SUCCESS;
}

View File

@ -15,5 +15,9 @@
'base_url' => env('DASHBOARD_BASE_URL', 'https://dash.laecloud.com'),
'birthday_path' => env('DASHBOARD_BIRTHDAY_PATH', '/birthdays'),
],
'node_type' => env('NODE_TYPE', 'master')
'node' => [
'type' => env('NODE_TYPE', 'master'),
'id' => env('NODE_ID'),
'ip' => env('NODE_IP'),
]
];