|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Command;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Services\Config;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Initialize only the greenfield Go host-agent control-plane tables.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This command is intentionally independent from CloudSetup. It does not
|
|
|
|
|
|
* inspect, migrate, decrypt, export, or otherwise depend on any legacy
|
|
|
|
|
|
* Python-engine table or secret. The agent schema is a separate deployment
|
|
|
|
|
|
* boundary and must be initialized explicitly with `cloudAgentInit`.
|
|
|
|
|
|
*/
|
|
|
|
|
|
final class CloudAgentSetup
|
|
|
|
|
|
{
|
|
|
|
|
|
/** @var list<string> */
|
|
|
|
|
|
private const TABLES = [
|
|
|
|
|
|
'cloud_agents',
|
|
|
|
|
|
'cloud_agent_client_certificates',
|
|
|
|
|
|
'cloud_agent_operations',
|
|
|
|
|
|
'cloud_agent_instance_state',
|
|
|
|
|
|
'cloud_agent_artifacts',
|
|
|
|
|
|
'cloud_agent_schema_meta',
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
private const SCHEMA_NAME = 'virt-cloud-agent';
|
|
|
|
|
|
private const SCHEMA_VERSION = 4;
|
|
|
|
|
|
private const SCHEMA_FINGERPRINT = '87a45391989171906c485383fa1f57fabad828adcb23ff01fe67cae99e7ed6e4';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Create the v4 Agent schema on the configured default database. The
|
|
|
|
|
|
* installer filename remains cloud_agent_v1.sql because the HTTP protocol
|
|
|
|
|
|
* is still v1; the marker prevents accepting the earlier pre-CAS shape.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The command is deliberately conservative around partial installs:
|
|
|
|
|
|
* rerunning a complete install is a no-op, while a partially-created
|
|
|
|
|
|
* schema stops before executing more DDL so an operator can inspect and
|
|
|
|
|
|
* repair the fresh database explicitly. No legacy table is queried.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static function initialize(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((string) Config::get('db_prefix', '') !== '') {
|
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
|
'Go Agent 控制面使用固定表名,请将 db_prefix 留空后再执行 cloudAgentInit'
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Boot::bootDb() installs Eloquent's resolver for the CLI process.
|
|
|
|
|
|
// Resolving through the framework connection does not instantiate or
|
|
|
|
|
|
// query a legacy model/table.
|
|
|
|
|
|
$connection = Model::resolveConnection();
|
|
|
|
|
|
$schema = $connection->getSchemaBuilder();
|
|
|
|
|
|
$existing = [];
|
|
|
|
|
|
foreach (self::TABLES as $table) {
|
|
|
|
|
|
if ($schema->hasTable($table)) {
|
|
|
|
|
|
$existing[] = $table;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (count($existing) === count(self::TABLES)) {
|
|
|
|
|
|
self::assertSchemaMarker($connection);
|
|
|
|
|
|
echo "Go Agent v4 控制面表已存在,未执行重复建表。\n";
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($existing !== []) {
|
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
|
'Go Agent 控制面检测到不完整的新表集合,请先核对 cloud_agent_v1.sql:'
|
|
|
|
|
|
. implode(', ', $existing)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$sql = self::schema();
|
|
|
|
|
|
$connection->unprepared($sql);
|
|
|
|
|
|
|
|
|
|
|
|
$missing = [];
|
|
|
|
|
|
foreach (self::TABLES as $table) {
|
|
|
|
|
|
if (!$schema->hasTable($table)) {
|
|
|
|
|
|
$missing[] = $table;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($missing !== []) {
|
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
|
'Go Agent 控制面建表后仍缺少新表:' . implode(', ', $missing)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self::assertSchemaMarker($connection);
|
|
|
|
|
|
|
|
|
|
|
|
echo "Go Agent v4 控制面表已初始化;未调用旧 CloudSetup、未读取旧引擎密钥、未连接宿主机。\n";
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static function assertSchemaMarker($connection): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$marker = $connection->table('cloud_agent_schema_meta')
|
|
|
|
|
|
->where('schema_name', self::SCHEMA_NAME)
|
|
|
|
|
|
->first();
|
|
|
|
|
|
if ($marker === null
|
|
|
|
|
|
|| (int) ($marker->schema_version ?? 0) !== self::SCHEMA_VERSION
|
|
|
|
|
|
|| (string) ($marker->schema_fingerprint ?? '') !== self::SCHEMA_FINGERPRINT) {
|
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
|
'Go Agent 控制面 schema marker 缺失或版本/指纹不匹配,已停止启动;请使用全新的 cloud_agent_v1.sql'
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static function schema(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!defined('BASE_PATH')) {
|
|
|
|
|
|
throw new \RuntimeException('缺少面板 BASE_PATH,无法定位 Go Agent 建表脚本');
|
|
|
|
|
|
}
|
|
|
|
|
|
$path = BASE_PATH . '/sql/cloud_agent_v1.sql';
|
|
|
|
|
|
$sql = file_get_contents($path);
|
|
|
|
|
|
if ($sql === false || trim($sql) === '') {
|
|
|
|
|
|
throw new \RuntimeException('缺少 Go Agent v4 建表脚本:' . $path);
|
|
|
|
|
|
}
|
|
|
|
|
|
return $sql;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|