fix: preserve cloud instance template metadata
parent
b9dcab4ef3
commit
a25aa0fe55
@ -0,0 +1,47 @@
|
||||
package hypervisor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVMMetadataWireContractExcludesPanelTemplateFields(t *testing.T) {
|
||||
encoded, err := json.Marshal(VMMetadata{
|
||||
UUID: "11111111-1111-4111-8111-111111111111",
|
||||
Name: "vm-one",
|
||||
VCPUs: 2,
|
||||
MemoryMiB: 2048,
|
||||
DiskGiB: 20,
|
||||
PublicIPCIDR: "203.0.113.18/28",
|
||||
TemplateID: "debian-13-template",
|
||||
Disks: []Disk{{
|
||||
Path: "/home/kvm/private-root.qcow2",
|
||||
Device: "disk",
|
||||
Format: "qcow2",
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal VMMetadata: %v", err)
|
||||
}
|
||||
|
||||
var wire map[string]any
|
||||
if err := json.Unmarshal(encoded, &wire); err != nil {
|
||||
t.Fatalf("decode VMMetadata wire payload: %v", err)
|
||||
}
|
||||
for _, key := range []string{"managed", "os_source", "os_name", "login_name"} {
|
||||
if _, exists := wire[key]; exists {
|
||||
t.Fatalf("panel-owned field %q escaped into VMMetadata", key)
|
||||
}
|
||||
}
|
||||
disks, ok := wire["disks"].([]any)
|
||||
if !ok || len(disks) != 1 {
|
||||
t.Fatalf("unexpected disk wire payload: %#v", wire["disks"])
|
||||
}
|
||||
disk, ok := disks[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected disk DTO: %#v", disks[0])
|
||||
}
|
||||
if _, exists := disk["path"]; exists {
|
||||
t.Fatalf("host disk path escaped into VMMetadata: %#v", disk)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Services\Cloud\CloudInstanceMetadataRepairService;
|
||||
|
||||
final class CloudRepairInstanceMetadata
|
||||
{
|
||||
public static function run(array $argv): int
|
||||
{
|
||||
$apply = in_array('--apply', $argv, true);
|
||||
$limit = 10000;
|
||||
$afterId = 0;
|
||||
foreach ($argv as $argument) {
|
||||
if (preg_match('/^--limit=([0-9]{1,5})$/D',
|
||||
(string) $argument, $match) === 1) {
|
||||
$limit = (int) $match[1];
|
||||
}
|
||||
if (preg_match('/^--after-id=([0-9]{1,18})$/D',
|
||||
(string) $argument, $match) === 1) {
|
||||
$afterId = (int) $match[1];
|
||||
}
|
||||
}
|
||||
$result = CloudInstanceMetadataRepairService::run(
|
||||
$apply, $limit, $afterId
|
||||
);
|
||||
echo json_encode($result, JSON_THROW_ON_ERROR
|
||||
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,220 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudInstance;
|
||||
use App\Models\CloudOrder;
|
||||
use App\Models\CloudTemplate;
|
||||
|
||||
/** Repair only the Go-migration metadata omission on proven panel VMs. */
|
||||
final class CloudInstanceMetadataRepairService
|
||||
{
|
||||
public static function run(bool $apply = false, int $limit = 10000,
|
||||
int $afterId = 0): array
|
||||
{
|
||||
$limit = max(1, min(10000, $limit));
|
||||
$afterId = max(0, $afterId);
|
||||
$stats = [
|
||||
'mode' => $apply ? 'apply' : 'dry-run',
|
||||
'after_id' => $afterId,
|
||||
'next_after_id' => null,
|
||||
'has_more' => false,
|
||||
'scanned' => 0,
|
||||
'would_repair' => 0,
|
||||
'repaired' => 0,
|
||||
'unchanged' => 0,
|
||||
'skipped' => 0,
|
||||
];
|
||||
$references = CloudInstance::query()
|
||||
->where('status', '<>', 'deleted')
|
||||
->whereNotNull('order_id')
|
||||
->where('id', '>', $afterId)
|
||||
->orderBy('id')
|
||||
->limit($limit + 1)
|
||||
->get(['id', 'order_id']);
|
||||
if ($references->count() > $limit) {
|
||||
$stats['has_more'] = true;
|
||||
$references = $references->take($limit);
|
||||
}
|
||||
|
||||
foreach ($references as $reference) {
|
||||
$stats['scanned']++;
|
||||
if ($stats['has_more']) {
|
||||
$stats['next_after_id'] = (int) $reference->id;
|
||||
}
|
||||
$outcome = $apply
|
||||
? self::applyOne((int) $reference->id,
|
||||
(int) $reference->order_id)
|
||||
: self::inspectOne((int) $reference->id,
|
||||
(int) $reference->order_id);
|
||||
if ($outcome === 'repair') {
|
||||
$stats[$apply ? 'repaired' : 'would_repair']++;
|
||||
} elseif ($outcome === 'unchanged') {
|
||||
$stats['unchanged']++;
|
||||
} else {
|
||||
$stats['skipped']++;
|
||||
}
|
||||
}
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private static function inspectOne(int $instanceId, int $orderId): string
|
||||
{
|
||||
$order = CloudOrder::query()->where('id', $orderId)->first(
|
||||
self::orderColumns()
|
||||
);
|
||||
$instance = CloudInstance::query()->where('id', $instanceId)->first(
|
||||
self::instanceColumns()
|
||||
);
|
||||
return self::evaluate($instance, $order)['outcome'];
|
||||
}
|
||||
|
||||
private static function applyOne(int $instanceId, int $orderId): string
|
||||
{
|
||||
$connection = (new CloudOrder())->getConnection();
|
||||
return $connection->transaction(static function () use (
|
||||
$instanceId, $orderId
|
||||
): string {
|
||||
// Match the worker's durable lock order: order before instance.
|
||||
$order = CloudOrder::query()->where('id', $orderId)
|
||||
->lockForUpdate()->first(self::orderColumns());
|
||||
$instance = CloudInstance::query()->where('id', $instanceId)
|
||||
->lockForUpdate()->first(self::instanceColumns());
|
||||
$evaluation = self::evaluate($instance, $order);
|
||||
if ($evaluation['outcome'] !== 'repair') {
|
||||
return $evaluation['outcome'];
|
||||
}
|
||||
$instance->spec_json = json_encode($evaluation['spec'],
|
||||
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
||||
| JSON_UNESCAPED_UNICODE);
|
||||
$instance->updated_at = time();
|
||||
$instance->save();
|
||||
return 'repair';
|
||||
}, 3);
|
||||
}
|
||||
|
||||
private static function evaluate(?CloudInstance $instance,
|
||||
?CloudOrder $order): array
|
||||
{
|
||||
if ($instance === null || $order === null
|
||||
|| (string) $instance->status === 'deleted'
|
||||
|| (int) $instance->order_id !== (int) $order->id
|
||||
|| (int) $order->instance_id !== (int) $instance->id
|
||||
|| (int) $order->user_id !== (int) $instance->user_id
|
||||
|| (int) $order->host_id !== (int) $instance->host_id
|
||||
|| (string) $order->status !== 'succeeded') {
|
||||
return ['outcome' => 'skip'];
|
||||
}
|
||||
$instanceSpec = self::spec($instance->spec_json);
|
||||
$orderSpec = self::spec($order->spec_json);
|
||||
if ($instanceSpec === null || $orderSpec === null
|
||||
|| ($instanceSpec['source'] ?? null) !== 'cloud'
|
||||
|| ($orderSpec['source'] ?? null) !== 'cloud'
|
||||
|| !is_string($instanceSpec['template_id'] ?? null)
|
||||
|| !is_string($orderSpec['template_id'] ?? null)
|
||||
|| !hash_equals($orderSpec['template_id'],
|
||||
$instanceSpec['template_id'])
|
||||
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D',
|
||||
$instanceSpec['template_id']) !== 1) {
|
||||
return ['outcome' => 'skip'];
|
||||
}
|
||||
$expected = self::expectedMetadata($orderSpec,
|
||||
$instanceSpec['template_id']);
|
||||
if ($expected === null) {
|
||||
return ['outcome' => 'skip'];
|
||||
}
|
||||
$missing = false;
|
||||
foreach ($expected as $key => $value) {
|
||||
if (!array_key_exists($key, $instanceSpec)
|
||||
|| $instanceSpec[$key] === null
|
||||
|| $instanceSpec[$key] === '') {
|
||||
$missing = true;
|
||||
continue;
|
||||
}
|
||||
if ($instanceSpec[$key] !== $value) {
|
||||
// Never reinterpret ISO/discovered instances or overwrite an
|
||||
// operator-visible value when historical evidence conflicts.
|
||||
return ['outcome' => 'skip'];
|
||||
}
|
||||
}
|
||||
if (!$missing) {
|
||||
return ['outcome' => 'unchanged'];
|
||||
}
|
||||
return [
|
||||
'outcome' => 'repair',
|
||||
'spec' => array_replace($instanceSpec, $expected),
|
||||
];
|
||||
}
|
||||
|
||||
/** Prefer the immutable paid-order snapshot; use the catalog only for
|
||||
* orders created before presentation metadata was frozen. */
|
||||
private static function expectedMetadata(array $orderSpec,
|
||||
string $templateId): ?array
|
||||
{
|
||||
$snapshotKeys = ['os_source', 'os_name', 'login_name'];
|
||||
$present = array_filter($snapshotKeys,
|
||||
static fn (string $key): bool => array_key_exists($key,
|
||||
$orderSpec));
|
||||
if ($present !== []) {
|
||||
if (count($present) !== count($snapshotKeys)
|
||||
|| ($orderSpec['os_source'] ?? null) !== 'cloud_template'
|
||||
|| ($orderSpec['login_name'] ?? null) !== 'root'
|
||||
|| !self::validOsName($orderSpec['os_name'] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
$osName = trim($orderSpec['os_name']);
|
||||
} else {
|
||||
$template = CloudTemplate::query()->where('id', $templateId)
|
||||
->where('state', 'active')->first(['id', 'name']);
|
||||
$osName = $template === null
|
||||
? '' : trim((string) $template->name);
|
||||
if (!self::validOsName($osName)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return [
|
||||
'os_source' => 'cloud_template',
|
||||
'os_name' => $osName,
|
||||
'login_name' => 'root',
|
||||
'managed' => true,
|
||||
];
|
||||
}
|
||||
|
||||
private static function validOsName($value): bool
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
$value = trim($value);
|
||||
return $value !== '' && mb_strlen($value) <= 64
|
||||
&& preg_match('//u', $value) === 1
|
||||
&& preg_match('/[\x00-\x1F\x7F]/u', $value) !== 1;
|
||||
}
|
||||
|
||||
private static function spec($value): ?array
|
||||
{
|
||||
try {
|
||||
$decoded = json_decode((string) $value, true, 32,
|
||||
JSON_THROW_ON_ERROR);
|
||||
} catch (\JsonException $error) {
|
||||
return null;
|
||||
}
|
||||
return is_array($decoded) && !array_is_list($decoded)
|
||||
? $decoded : null;
|
||||
}
|
||||
|
||||
private static function orderColumns(): array
|
||||
{
|
||||
return [
|
||||
'id', 'user_id', 'host_id', 'instance_id', 'status', 'spec_json',
|
||||
];
|
||||
}
|
||||
|
||||
private static function instanceColumns(): array
|
||||
{
|
||||
return [
|
||||
'id', 'host_id', 'user_id', 'order_id', 'status', 'spec_json',
|
||||
'updated_at',
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudInstanceMetadataRepairService;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite', 'database' => ':memory:',
|
||||
], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$schema = $capsule->schema('default');
|
||||
$schema->create('cloud_templates', static function (Blueprint $table): void {
|
||||
$table->string('id')->primary();
|
||||
$table->string('name');
|
||||
$table->string('state');
|
||||
});
|
||||
$schema->create('cloud_orders', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('host_id')->nullable();
|
||||
$table->integer('instance_id')->nullable();
|
||||
$table->string('status');
|
||||
$table->text('spec_json');
|
||||
});
|
||||
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('order_id')->nullable();
|
||||
$table->string('status');
|
||||
$table->text('spec_json');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
|
||||
Capsule::table('cloud_templates')->insert([
|
||||
'id' => 'debian-13-template',
|
||||
'name' => 'Debian 13',
|
||||
'state' => 'active',
|
||||
]);
|
||||
$baseSpec = [
|
||||
'source' => 'cloud',
|
||||
'template_id' => 'debian-13-template',
|
||||
'public_ip_cidr' => '203.0.113.18/28',
|
||||
'agent_metadata_version' => 1,
|
||||
];
|
||||
foreach ([
|
||||
1 => $baseSpec,
|
||||
2 => array_replace($baseSpec, ['os_source' => 'iso']),
|
||||
3 => $baseSpec,
|
||||
4 => $baseSpec,
|
||||
] as $id => $spec) {
|
||||
$orderSpec = $id === 4 ? array_replace($baseSpec, [
|
||||
'os_source' => 'cloud_template',
|
||||
'os_name' => 'Debian 12 Snapshot',
|
||||
'login_name' => 'root',
|
||||
]) : $baseSpec;
|
||||
Capsule::table('cloud_orders')->insert([
|
||||
'id' => $id,
|
||||
'user_id' => 10,
|
||||
'host_id' => 7,
|
||||
'instance_id' => $id,
|
||||
'status' => 'succeeded',
|
||||
'spec_json' => json_encode($orderSpec, JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
Capsule::table('cloud_instances')->insert([
|
||||
'id' => $id,
|
||||
'host_id' => 7,
|
||||
'user_id' => $id === 3 ? 99 : 10,
|
||||
'order_id' => $id,
|
||||
'status' => 'active',
|
||||
'spec_json' => json_encode($spec, JSON_THROW_ON_ERROR),
|
||||
'updated_at' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
$firstPage = CloudInstanceMetadataRepairService::run(false, 1);
|
||||
$assert($firstPage['scanned'] === 1 && $firstPage['would_repair'] === 1
|
||||
&& $firstPage['has_more'] === true
|
||||
&& $firstPage['next_after_id'] === 1,
|
||||
'Repair scan did not expose a stable next-page cursor.');
|
||||
$lastPage = CloudInstanceMetadataRepairService::run(false, 1, 3);
|
||||
$assert($lastPage['after_id'] === 3 && $lastPage['scanned'] === 1
|
||||
&& $lastPage['would_repair'] === 1
|
||||
&& $lastPage['has_more'] === false
|
||||
&& $lastPage['next_after_id'] === null,
|
||||
'Repair scan did not honor the after-id cursor.');
|
||||
|
||||
$before = (string) Capsule::table('cloud_instances')->where('id', 1)
|
||||
->value('spec_json');
|
||||
$dryRun = CloudInstanceMetadataRepairService::run(false, 100);
|
||||
$assert($dryRun === [
|
||||
'mode' => 'dry-run',
|
||||
'after_id' => 0,
|
||||
'next_after_id' => null,
|
||||
'has_more' => false,
|
||||
'scanned' => 4,
|
||||
'would_repair' => 2,
|
||||
'repaired' => 0,
|
||||
'unchanged' => 0,
|
||||
'skipped' => 2,
|
||||
] && Capsule::table('cloud_instances')->where('id', 1)
|
||||
->value('spec_json') === $before,
|
||||
'Dry-run changed data or did not report the one proven repair.');
|
||||
|
||||
$applied = CloudInstanceMetadataRepairService::run(true, 100);
|
||||
$repaired = json_decode((string) Capsule::table('cloud_instances')
|
||||
->where('id', 1)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
$conflict = json_decode((string) Capsule::table('cloud_instances')
|
||||
->where('id', 2)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
$snapshot = json_decode((string) Capsule::table('cloud_instances')
|
||||
->where('id', 4)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
$assert($applied['repaired'] === 2 && $applied['skipped'] === 2
|
||||
&& ($repaired['os_source'] ?? null) === 'cloud_template'
|
||||
&& ($repaired['os_name'] ?? null) === 'Debian 13'
|
||||
&& ($repaired['login_name'] ?? null) === 'root'
|
||||
&& ($repaired['managed'] ?? null) === true
|
||||
&& ($repaired['public_ip_cidr'] ?? null) === '203.0.113.18/28'
|
||||
&& ($repaired['agent_metadata_version'] ?? null) === 1
|
||||
&& ($conflict['os_source'] ?? null) === 'iso',
|
||||
'Apply did not repair only the proven panel instance.');
|
||||
$assert(($snapshot['os_name'] ?? null) === 'Debian 12 Snapshot'
|
||||
&& ($snapshot['managed'] ?? null) === true,
|
||||
'Repair did not prefer the immutable paid-order metadata snapshot.');
|
||||
|
||||
$repeat = CloudInstanceMetadataRepairService::run(true, 100);
|
||||
$assert($repeat['repaired'] === 0 && $repeat['unchanged'] === 2
|
||||
&& $repeat['skipped'] === 2,
|
||||
'Metadata repair was not idempotent.');
|
||||
|
||||
echo "Cloud instance metadata repair tests passed.\n";
|
||||
Loading…
Reference in New Issue