|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudInstanceBackup;
|
|
|
|
|
use App\Models\CloudJob;
|
|
|
|
|
use App\Services\Cloud\CloudBackupService;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
use App\Services\Cloud\CloudPortableRestoreCompatibility;
|
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
|
|
$assert = static function (bool $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_instance_backups', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('backup_key')->unique();
|
|
|
|
|
$table->integer('storage_artifact_id')->nullable();
|
|
|
|
|
$table->integer('source_instance_id');
|
|
|
|
|
$table->integer('source_host_id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->string('source_uuid');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->string('consistency');
|
|
|
|
|
$table->string('object_key');
|
|
|
|
|
$table->integer('object_size_bytes')->default(0);
|
|
|
|
|
$table->integer('virtual_size_bytes')->default(0);
|
|
|
|
|
$table->string('sha256')->default('');
|
|
|
|
|
$table->string('object_etag')->default('');
|
|
|
|
|
$table->string('object_version_id')->default('');
|
|
|
|
|
$table->text('source_portable_restore_contract_json')->default('{}');
|
|
|
|
|
$table->string('source_portable_restore_contract_sha256', 64)->default('');
|
|
|
|
|
$table->text('spec_json');
|
|
|
|
|
$table->text('sealed_credential')->nullable();
|
|
|
|
|
$table->integer('active_job_id')->nullable();
|
|
|
|
|
$table->string('last_operation_id')->nullable();
|
|
|
|
|
$table->string('error_code')->default('');
|
|
|
|
|
$table->integer('ready_at')->nullable();
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('kind');
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
$table->integer('instance_id')->nullable();
|
|
|
|
|
$table->integer('backup_id')->nullable();
|
|
|
|
|
$table->integer('user_id')->nullable();
|
|
|
|
|
$table->string('operation_id')->unique();
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_storage_artifacts', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('artifact_key')->unique();
|
|
|
|
|
$table->string('kind');
|
|
|
|
|
$table->unsignedBigInteger('maximum_size_bytes');
|
|
|
|
|
$table->unsignedBigInteger('size_bytes')->default(0);
|
|
|
|
|
$table->string('sha256')->default('');
|
|
|
|
|
$table->string('durability_policy');
|
|
|
|
|
$table->unsignedTinyInteger('desired_copies');
|
|
|
|
|
$table->unsignedTinyInteger('required_copies');
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->string('last_operation_id')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('verified_at')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('created_at');
|
|
|
|
|
$table->unsignedBigInteger('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_storage_replicas', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('replica_key')->unique();
|
|
|
|
|
$table->unsignedBigInteger('artifact_id');
|
|
|
|
|
$table->unsignedBigInteger('backend_id');
|
|
|
|
|
$table->string('role');
|
|
|
|
|
$table->string('object_key');
|
|
|
|
|
$table->string('object_version_id')->default('');
|
|
|
|
|
$table->string('object_etag')->default('');
|
|
|
|
|
$table->unsignedBigInteger('size_bytes')->default(0);
|
|
|
|
|
$table->string('sha256')->default('');
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->string('publish_operation_id')->nullable();
|
|
|
|
|
$table->string('delete_operation_id')->nullable();
|
|
|
|
|
$table->unsignedInteger('delete_generation')->default(0);
|
|
|
|
|
$table->unsignedInteger('delete_cycle_attempts')->default(0);
|
|
|
|
|
$table->unsignedInteger('delete_presence_confirmations')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('delete_last_checked_at')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('delete_next_check_at')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('delete_attention_at')->nullable();
|
|
|
|
|
$table->string('lease_token')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('lease_until')->default(0);
|
|
|
|
|
$table->string('error_code')->default('');
|
|
|
|
|
$table->unsignedBigInteger('verified_at')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('created_at');
|
|
|
|
|
$table->unsignedBigInteger('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_storage_operations', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('operation_id')->unique();
|
|
|
|
|
$table->string('request_idempotency_key')->unique();
|
|
|
|
|
$table->string('request_fingerprint');
|
|
|
|
|
$table->unsignedBigInteger('artifact_id');
|
|
|
|
|
$table->unsignedBigInteger('replica_id')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('source_backend_id')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('target_backend_id')->nullable();
|
|
|
|
|
$table->string('kind');
|
|
|
|
|
$table->string('phase');
|
|
|
|
|
$table->string('outcome')->nullable();
|
|
|
|
|
$table->string('effect_resolution');
|
|
|
|
|
$table->boolean('effect_may_have_applied')->nullable();
|
|
|
|
|
$table->boolean('safe_to_resubmit')->nullable();
|
|
|
|
|
$table->unsignedInteger('attempts')->default(0);
|
|
|
|
|
$table->string('lease_token')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('lease_until')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('available_at')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('next_check_at')->default(0);
|
|
|
|
|
$table->string('error_code')->default('');
|
|
|
|
|
$table->text('result_json')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('terminal_at')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('created_at');
|
|
|
|
|
$table->unsignedBigInteger('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_storage_publications', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('publication_key')->unique();
|
|
|
|
|
$table->unsignedBigInteger('artifact_id')->unique();
|
|
|
|
|
$table->unsignedBigInteger('backup_id')->nullable()->unique();
|
|
|
|
|
$table->string('template_import_intent_id')->nullable()->unique();
|
|
|
|
|
$table->string('rescue_template_id')->nullable()->unique();
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->unsignedInteger('attempts')->default(0);
|
|
|
|
|
$table->unsignedBigInteger('available_at')->default(0);
|
|
|
|
|
$table->string('lease_token')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('lease_until')->default(0);
|
|
|
|
|
$table->string('last_error_code')->default('');
|
|
|
|
|
$table->unsignedBigInteger('coverage_ready_at')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('finalized_at')->nullable();
|
|
|
|
|
$table->unsignedBigInteger('created_at');
|
|
|
|
|
$table->unsignedBigInteger('updated_at');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$payload = [
|
|
|
|
|
'contract' => 1, 'architecture' => 'x86_64', 'machine' => 'q35',
|
|
|
|
|
'firmware' => 'uefi', 'secure_boot' => false, 'disk_format' => 'qcow2',
|
|
|
|
|
'system_disk_only' => true, 'public_network_contract' => 1,
|
|
|
|
|
'vpc_inherited' => false,
|
|
|
|
|
];
|
|
|
|
|
$contract = CloudPortableRestoreCompatibility::fromPayload($payload);
|
|
|
|
|
$now = time();
|
|
|
|
|
$create = static function (int $id, string $key, string $operation) use (
|
|
|
|
|
$capsule, $now
|
|
|
|
|
): CloudJob {
|
|
|
|
|
$storageOperation = substr(hash('sha256', 'source-contract-' . $id), 0, 32);
|
|
|
|
|
$objectKey = 'vm-backups/' . $key . '/system.qcow2';
|
|
|
|
|
$capsule->table('cloud_instance_backups')->insert([
|
|
|
|
|
'id' => $id, 'backup_key' => $key,
|
|
|
|
|
'storage_artifact_id' => $id,
|
|
|
|
|
'source_instance_id' => 100 + $id, 'source_host_id' => 3,
|
|
|
|
|
'user_id' => 9,
|
|
|
|
|
'source_uuid' => sprintf('00000000-0000-4000-8000-%012d', $id),
|
|
|
|
|
'name' => 'portable-' . $id, 'state' => 'creating',
|
|
|
|
|
'consistency' => 'crash',
|
|
|
|
|
'object_key' => $objectKey,
|
|
|
|
|
'spec_json' => '{}',
|
|
|
|
|
'active_job_id' => $id, 'last_operation_id' => $operation,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
$capsule->table('cloud_storage_artifacts')->insert([
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'artifact_key' => substr(hash('sha256', 'artifact-' . $id), 0, 32),
|
|
|
|
|
'kind' => 'backup',
|
|
|
|
|
'maximum_size_bytes' => 21 * 1073741824,
|
|
|
|
|
'size_bytes' => 0, 'sha256' => '',
|
|
|
|
|
'durability_policy' => 'backup_primary_async_replica',
|
|
|
|
|
'desired_copies' => 2, 'required_copies' => 1,
|
|
|
|
|
'state' => 'publishing',
|
|
|
|
|
'last_operation_id' => $storageOperation,
|
|
|
|
|
'verified_at' => 0, 'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
$capsule->table('cloud_storage_replicas')->insert([
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'replica_key' => substr(hash('sha256', 'replica-' . $id), 0, 32),
|
|
|
|
|
'artifact_id' => $id, 'backend_id' => 1, 'role' => 'primary',
|
|
|
|
|
'object_key' => $objectKey, 'state' => 'publishing',
|
|
|
|
|
'publish_operation_id' => $storageOperation,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
$capsule->table('cloud_storage_operations')->insert([
|
|
|
|
|
'id' => $id, 'operation_id' => $storageOperation,
|
|
|
|
|
'request_idempotency_key' => 'source-contract-' . $id,
|
|
|
|
|
'request_fingerprint' => hash('sha256', 'source-contract-' . $id),
|
|
|
|
|
'artifact_id' => $id, 'replica_id' => $id,
|
|
|
|
|
'target_backend_id' => 1, 'kind' => 'publish',
|
|
|
|
|
'phase' => 'awaiting_receipt', 'effect_resolution' => 'unknown',
|
|
|
|
|
'attempts' => 1, 'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
$capsule->table('cloud_storage_publications')->insert([
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'publication_key' => substr(
|
|
|
|
|
hash('sha256', 'publication-' . $id),
|
|
|
|
|
0,
|
|
|
|
|
32
|
|
|
|
|
),
|
|
|
|
|
'artifact_id' => $id, 'backup_id' => $id,
|
|
|
|
|
'state' => 'waiting_primary',
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
$capsule->table('cloud_jobs')->insert([
|
|
|
|
|
'id' => $id, 'kind' => 'backup_create', 'host_id' => 3,
|
|
|
|
|
'instance_id' => 100 + $id, 'backup_id' => $id,
|
|
|
|
|
'operation_id' => $operation,
|
|
|
|
|
]);
|
|
|
|
|
return CloudJob::findOrFail($id);
|
|
|
|
|
};
|
|
|
|
|
$result = static function (string $key) use ($payload): array {
|
|
|
|
|
return [
|
|
|
|
|
'backup_key' => $key, 'size_bytes' => 1024,
|
|
|
|
|
'virtual_size_bytes' => 20 * 1073741824,
|
|
|
|
|
'sha256' => str_repeat('a', 64), 'object_etag' => 'etag',
|
|
|
|
|
'object_version_id' => '', 'consistency' => 'crash',
|
|
|
|
|
'cleanup_pending' => false, 'source_contract' => $payload,
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
$finalizerPayload = static function (int $id, string $key) use ($contract): array {
|
|
|
|
|
return [
|
|
|
|
|
'owner_id' => 9, 'backup_key' => $key,
|
|
|
|
|
'maximum_size_bytes' => 21 * 1073741824,
|
|
|
|
|
'storage_artifact_id' => $id,
|
|
|
|
|
'storage_publication_operation_id' => substr(
|
|
|
|
|
hash('sha256', 'source-contract-' . $id),
|
|
|
|
|
0,
|
|
|
|
|
32
|
|
|
|
|
),
|
|
|
|
|
'source_portable_restore_contract_sha256' => $contract->sha256(),
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$key = str_repeat('1', 32);
|
|
|
|
|
CloudBackupService::finishJobLocked(
|
|
|
|
|
$create(1, $key, str_repeat('2', 32)),
|
|
|
|
|
'succeeded', $result($key), $finalizerPayload(1, $key)
|
|
|
|
|
);
|
|
|
|
|
$ready = CloudInstanceBackup::findOrFail(1);
|
|
|
|
|
$assert((string) $ready->state === 'ready'
|
|
|
|
|
&& (string) $ready->source_portable_restore_contract_json
|
|
|
|
|
=== $contract->json()
|
|
|
|
|
&& (string) $ready->source_portable_restore_contract_sha256
|
|
|
|
|
=== $contract->sha256()
|
|
|
|
|
&& $ready->sourcePortableRestoreCompatibility() !== null,
|
|
|
|
|
'Valid Agent source contract was not frozen with the ready backup.');
|
|
|
|
|
|
|
|
|
|
$missingKey = str_repeat('3', 32);
|
|
|
|
|
$missing = $result($missingKey);
|
|
|
|
|
unset($missing['source_contract']);
|
|
|
|
|
try {
|
|
|
|
|
CloudBackupService::finishJobLocked(
|
|
|
|
|
$create(2, $missingKey, str_repeat('4', 32)),
|
|
|
|
|
'succeeded', $missing, $finalizerPayload(2, $missingKey)
|
|
|
|
|
);
|
|
|
|
|
throw new RuntimeException('Missing source contract was accepted.');
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert($error->httpStatus === 502,
|
|
|
|
|
'Missing source contract returned the wrong status.');
|
|
|
|
|
}
|
|
|
|
|
$assert((string) CloudInstanceBackup::findOrFail(2)->state === 'creating',
|
|
|
|
|
'Missing source contract promoted a backup to ready.');
|
|
|
|
|
|
|
|
|
|
echo "Portable backup source contract finalizer passed.\n";
|