|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Command;
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Services\Config;
|
|
|
|
|
|
|
|
|
|
/** Initializes only the clean-break PHP business and Agent log schemas. */
|
|
|
|
|
final class CloudSetup
|
|
|
|
|
{
|
|
|
|
|
private const SCHEMA_VERSION = 'go-agent-wss-v1';
|
|
|
|
|
private const MAIN_TABLES = [
|
|
|
|
|
'cloud_regions', 'cloud_network_lines', 'cloud_resource_pools',
|
|
|
|
|
'cloud_hosts', 'cloud_host_agents', 'cloud_templates',
|
|
|
|
|
'cloud_template_replicas', 'cloud_user_isos',
|
|
|
|
|
'cloud_user_iso_replicas', 'cloud_rescue_template_deletions',
|
|
|
|
|
'cloud_rescue_template_imports', 'cloud_upload_staging',
|
|
|
|
|
'cloud_host_resource_pool_memberships',
|
|
|
|
|
'cloud_host_network_attachments', 'cloud_public_ip_pools',
|
|
|
|
|
'cloud_host_capacity_policies', 'cloud_public_ips', 'cloud_offers',
|
|
|
|
|
'cloud_instances', 'cloud_instance_firewalls',
|
|
|
|
|
'cloud_instance_firewall_versions', 'cloud_instance_firewall_rules',
|
|
|
|
|
'cloud_platform_test_policy_state',
|
|
|
|
|
'cloud_platform_test_policy_rules', 'cloud_instance_snapshots',
|
|
|
|
|
'cloud_instance_runtime', 'cloud_orders',
|
|
|
|
|
'cloud_resource_reservations', 'cloud_service_periods',
|
|
|
|
|
'cloud_renewals', 'cloud_invoices', 'cloud_invoice_payments',
|
|
|
|
|
'cloud_mail_outbox', 'cloud_jobs', 'cloud_ssh_keys',
|
|
|
|
|
'cloud_sessions', 'cloud_console_tickets',
|
|
|
|
|
];
|
|
|
|
|
private const LOG_TABLES = [
|
|
|
|
|
'cloud_vm_metric_samples', 'cloud_vm_traffic_checkpoints',
|
|
|
|
|
'cloud_vm_traffic_totals',
|
|
|
|
|
];
|
|
|
|
|
/** Refusal-only denylist: never read, import, rename or migrate these rows. */
|
|
|
|
|
private const RETIRED_LOG_TABLES = [
|
|
|
|
|
'engine_vm_metric_samples',
|
|
|
|
|
'engine_vm_traffic_checkpoints', 'engine_vm_traffic_totals',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public static function initialize(): bool
|
|
|
|
|
{
|
|
|
|
|
if (Config::get('enable_log', 'false') !== 'true') {
|
|
|
|
|
throw new \RuntimeException('请启用并配置日志数据库,虚拟机指标必须写入日志库');
|
|
|
|
|
}
|
|
|
|
|
if ((string) Config::get('db_prefix', '') !== '') {
|
|
|
|
|
throw new \RuntimeException('虚拟化集成使用独立表名,请将 db_prefix 留空');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$connection = (new User())->getConnection();
|
|
|
|
|
$schema = $connection->getSchemaBuilder();
|
|
|
|
|
if (!$schema->hasTable('user') || !$schema->hasTable('shop')) {
|
|
|
|
|
throw new \RuntimeException('请先在全新业务数据库导入 web/sql/ss.sql');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$logConnection = User::getConnectionResolver()->connection('log');
|
|
|
|
|
self::assertFreshSchemaBoundary($connection, $logConnection);
|
|
|
|
|
$connection->unprepared(self::schema('cloud.sql'));
|
|
|
|
|
self::ensureMultiHostDefaults($connection);
|
|
|
|
|
$logConnection->unprepared(self::schema('cloud_log.sql'));
|
|
|
|
|
|
|
|
|
|
echo "云业务表与 Agent 指标日志表已准备。未连接或修改任何宿主机。\n";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Seed only immutable/default control rows in an otherwise fresh schema. */
|
|
|
|
|
private static function ensureMultiHostDefaults($connection): void
|
|
|
|
|
{
|
|
|
|
|
$connection->table('configurations')->insertOrIgnore([
|
|
|
|
|
'name' => 'cloud_network_lines_enabled',
|
|
|
|
|
'value' => 'false',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Refuse to mix the Go/Agent schema with any unmarked cloud database. */
|
|
|
|
|
private static function assertFreshSchemaBoundary($connection,
|
|
|
|
|
$logConnection): void
|
|
|
|
|
{
|
|
|
|
|
$marker = $connection->table('configurations')
|
|
|
|
|
->where('name', 'cloud_schema_version')->first(['value']);
|
|
|
|
|
if ($marker !== null) {
|
|
|
|
|
if (!hash_equals(self::SCHEMA_VERSION, (string) $marker->value)) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'虚拟化数据库版本不兼容,请使用全新数据库'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
foreach (self::RETIRED_LOG_TABLES as $retiredTable) {
|
|
|
|
|
if ($logConnection->getSchemaBuilder()->hasTable($retiredTable)) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'日志库包含已废弃的 Python Engine 表,请使用全新日志库'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (self::MAIN_TABLES as $table) {
|
|
|
|
|
if ($connection->getSchemaBuilder()->hasTable($table)) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'检测到未标记的旧虚拟化数据库,不支持原地升级'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach (array_merge(self::LOG_TABLES, self::RETIRED_LOG_TABLES)
|
|
|
|
|
as $table) {
|
|
|
|
|
if ($logConnection->getSchemaBuilder()->hasTable($table)) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'检测到未标记的虚拟化日志库,不支持原地升级'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$connection->table('configurations')->insert([
|
|
|
|
|
'name' => 'cloud_schema_version',
|
|
|
|
|
'value' => self::SCHEMA_VERSION,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function schema(string $name): string
|
|
|
|
|
{
|
|
|
|
|
$sql = file_get_contents(BASE_PATH . '/sql/' . $name);
|
|
|
|
|
if ($sql === false || trim($sql) === '') {
|
|
|
|
|
throw new \RuntimeException('缺少虚拟化建表脚本:' . $name);
|
|
|
|
|
}
|
|
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
}
|