You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
435 lines
20 KiB
PHP
435 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Services {
|
|
final class Config
|
|
{
|
|
public static string $key = '';
|
|
|
|
public static function get($name, $default = null)
|
|
{
|
|
return $name === 'cloud_data_key' ? self::$key : $default;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudTemplateImportIntent;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudTemplateImportStateMachine;
|
|
use App\Services\Cloud\CloudTemplateImportReadModel;
|
|
use App\Services\Cloud\CloudTemplateMultipartAcquisition;
|
|
use App\Services\Config;
|
|
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);
|
|
}
|
|
};
|
|
|
|
Config::$key = base64_encode(str_repeat('i', 32));
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
$schema->create('cloud_template_import_intents',
|
|
static function (Blueprint $table): void {
|
|
$table->string('id')->primary();
|
|
$table->integer('user_id');
|
|
$table->string('request_idempotency_key');
|
|
$table->string('request_fingerprint');
|
|
$table->string('source_kind');
|
|
$table->string('name');
|
|
$table->string('os_family');
|
|
$table->string('os_version');
|
|
$table->string('source_filename')->default('');
|
|
$table->integer('source_size_bytes')->default(0);
|
|
$table->integer('source_last_modified')->default(0);
|
|
$table->string('source_sha256')->default('');
|
|
$table->text('sealed_source');
|
|
$table->string('template_id')->unique();
|
|
$table->string('catalog_key');
|
|
$table->string('version_label');
|
|
$table->string('generation_token')->unique();
|
|
$table->integer('storage_revision');
|
|
$table->string('object_key')->unique();
|
|
$table->string('multipart_upload_id')->default('');
|
|
$table->integer('multipart_part_size_bytes')->default(0);
|
|
$table->integer('multipart_part_count')->default(0);
|
|
$table->string('object_version_id')->default('');
|
|
$table->string('object_etag')->default('');
|
|
$table->string('state');
|
|
$table->integer('active_job_id')->nullable();
|
|
$table->string('error_code')->default('');
|
|
$table->integer('expires_at');
|
|
$table->integer('completed_at')->nullable();
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->unique(['user_id', 'request_idempotency_key']);
|
|
});
|
|
$schema->create('cloud_template_import_parts',
|
|
static function (Blueprint $table): void {
|
|
$table->string('intent_id');
|
|
$table->integer('part_number');
|
|
$table->integer('size_bytes');
|
|
$table->string('etag');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->primary(['intent_id', 'part_number']);
|
|
});
|
|
$schema->create('cloud_storage_backends',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('backend_key');
|
|
$table->string('identity_sha256');
|
|
$table->string('name');
|
|
$table->string('driver');
|
|
$table->string('endpoint');
|
|
$table->string('region');
|
|
$table->string('bucket');
|
|
$table->string('prefix');
|
|
$table->boolean('path_style');
|
|
$table->string('failure_domain');
|
|
$table->text('sealed_credentials');
|
|
$table->unsignedBigInteger('config_revision');
|
|
$table->unsignedBigInteger('tested_revision');
|
|
$table->string('admin_state');
|
|
$table->string('health_state');
|
|
$table->unsignedInteger('write_priority');
|
|
$table->unsignedInteger('consecutive_failures')->default(0);
|
|
$table->unsignedInteger('consecutive_successes')->default(0);
|
|
$table->string('last_error_code')->default('');
|
|
$table->unsignedBigInteger('last_tested_at')->default(0);
|
|
$table->unsignedBigInteger('last_healthy_at')->default(0);
|
|
$table->unsignedBigInteger('next_probe_at')->default(0);
|
|
$table->unsignedBigInteger('created_at');
|
|
$table->unsignedBigInteger('updated_at');
|
|
});
|
|
$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');
|
|
$table->string('sha256');
|
|
$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');
|
|
$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');
|
|
$table->string('lease_token')->nullable();
|
|
$table->unsignedBigInteger('lease_until');
|
|
$table->unsignedBigInteger('available_at');
|
|
$table->unsignedBigInteger('next_check_at');
|
|
$table->string('error_code');
|
|
$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');
|
|
});
|
|
|
|
foreach ([[1, 'provider-a'], [2, 'provider-b']] as [$id, $domain]) {
|
|
Capsule::table('cloud_storage_backends')->insert([
|
|
'id' => $id,
|
|
'backend_key' => str_repeat((string) $id, 32),
|
|
'identity_sha256' => str_repeat((string) $id, 64),
|
|
'name' => $domain,
|
|
'driver' => 's3',
|
|
'endpoint' => 'https://' . $domain . '.example.test',
|
|
'region' => 'us-east-1',
|
|
'bucket' => $domain,
|
|
'prefix' => $domain,
|
|
'path_style' => 1,
|
|
'failure_domain' => $domain,
|
|
'sealed_credentials' => 'sealed',
|
|
'config_revision' => 9,
|
|
'tested_revision' => 9,
|
|
'admin_state' => 'active',
|
|
'health_state' => 'healthy',
|
|
'write_priority' => $id * 10,
|
|
'consecutive_failures' => 0,
|
|
'consecutive_successes' => 1,
|
|
'last_error_code' => '',
|
|
'last_tested_at' => 2000000000,
|
|
'last_healthy_at' => 2000000000,
|
|
'next_probe_at' => 2000000300,
|
|
'created_at' => 2000000000,
|
|
'updated_at' => 2000000000,
|
|
]);
|
|
}
|
|
|
|
$uploads = new CloudTemplateMultipartAcquisition(
|
|
static fn (): int => 2000000000
|
|
);
|
|
$readModel = new CloudTemplateImportReadModel();
|
|
$actor = [
|
|
'user_id' => 7,
|
|
'is_admin' => true,
|
|
'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
'request_idempotency_key' => 'template-upload-intent-0001',
|
|
];
|
|
$input = [
|
|
'name' => 'Debian 13 本地版',
|
|
'os_family' => 'Debian',
|
|
'os_version' => '13',
|
|
'filename' => 'debian-13.qcow2',
|
|
'size_bytes' => 64 * 1024 * 1024,
|
|
'last_modified' => 1999999000000,
|
|
'sha256' => str_repeat('a', 64),
|
|
];
|
|
|
|
$first = $uploads->beginUpload($input, $actor);
|
|
$second = $uploads->beginUpload($input, $actor);
|
|
$assert($first['created'] === true && $second['created'] === false
|
|
&& $first['intent']['id'] === $second['intent']['id']
|
|
&& $first['intent']['state'] === 'prepared'
|
|
&& $first['intent']['can_resume'] === true
|
|
&& $first['intent']['source_size_bytes'] === 64 * 1024 * 1024,
|
|
'A same-key replay did not return the original template import intent.');
|
|
$stored = CloudTemplateImportIntent::findOrFail(
|
|
(string) $first['intent']['id']
|
|
);
|
|
$assert((int) $stored->storage_revision === 9
|
|
&& str_contains((string) $stored->object_key,
|
|
'/cloud-templates/')
|
|
&& Capsule::table('cloud_storage_artifacts')->count() === 1
|
|
&& Capsule::table('cloud_storage_replicas')->count() === 2
|
|
&& Capsule::table('cloud_storage_operations')->count() === 2
|
|
&& Capsule::table('cloud_storage_publications')
|
|
->where('template_import_intent_id', (string) $stored->id)
|
|
->exists()
|
|
&& !array_key_exists('sealed_source', $first['intent'])
|
|
&& !array_key_exists('multipart_upload_id', $first['intent']),
|
|
'The intent did not freeze its object identity or leaked secrets.');
|
|
|
|
try {
|
|
$uploads->beginUpload(
|
|
array_replace($input, ['name' => 'Different']),
|
|
$actor
|
|
);
|
|
throw new RuntimeException('A same-key different request was accepted.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
'A same-key fingerprint conflict returned the wrong status.');
|
|
}
|
|
|
|
$listed = $readModel->active($actor);
|
|
$one = $readModel->one((string) $stored->id, $actor);
|
|
$assert(count($listed['intents']) === 1
|
|
&& $one['intent']['id'] === (string) $stored->id,
|
|
'A refreshed administrator page cannot recover the active intent.');
|
|
$originalKey = Config::$key;
|
|
Config::$key = '';
|
|
$assert($readModel->one((string) $stored->id, ['is_admin' => true]) === $one,
|
|
'Reading an intent unexpectedly depends on storage credentials or execution runtime.');
|
|
Config::$key = $originalKey;
|
|
foreach ([['id' => (string) $stored->id, 'actor' => [], 'status' => 403],
|
|
['id' => 'invalid', 'actor' => $actor, 'status' => 422],
|
|
['id' => str_repeat('f', 32), 'actor' => $actor, 'status' => 404]] as $case) {
|
|
try {
|
|
$readModel->one($case['id'], $case['actor']);
|
|
throw new RuntimeException('An invalid import read was accepted.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === $case['status'], 'Import read error semantics changed.');
|
|
}
|
|
}
|
|
|
|
CloudTemplateImportStateMachine::assertIntegrity();
|
|
$definitions = CloudTemplateImportStateMachine::definitions();
|
|
foreach ($definitions as $stateName => $definition) {
|
|
foreach ($definition['sources'] as $source) {
|
|
$probe = clone $stored;
|
|
$probe->state = $stateName;
|
|
$probe->source_kind = $source;
|
|
$probe->multipart_upload_id = '';
|
|
foreach (['start_upload', 'reconcile_multipart_start',
|
|
'record_part', 'complete_upload', 'resume_remote',
|
|
'cancel'] as $action) {
|
|
$expected = in_array($action, $definition['actions'][$source], true);
|
|
try {
|
|
CloudTemplateImportStateMachine::assertAction($probe, $action);
|
|
$assert($expected, 'An unlisted import action was accepted.');
|
|
} catch (CloudError $error) {
|
|
$assert(!$expected && $error->httpStatus === 409,
|
|
'A listed import action was rejected.');
|
|
}
|
|
}
|
|
$missingOutlet = $definitions;
|
|
$missingOutlet[$stateName]['actions'][$source] = [];
|
|
try {
|
|
CloudTemplateImportStateMachine::assertDefinitions($missingOutlet);
|
|
throw new RuntimeException('An import state lost all executable outlets.');
|
|
} catch (LogicException) {
|
|
// A future state cannot silently lose its action policy.
|
|
}
|
|
}
|
|
}
|
|
$unknownUpload = clone $stored;
|
|
$unknownUpload->state = 'source_unknown';
|
|
$unknownUpload->source_kind = 'upload';
|
|
$unknownUpload->multipart_upload_id = 'pending-provider-upload';
|
|
$unknownUpload->error_code = 'multipart_complete_unknown';
|
|
$assert(!CloudTemplateImportStateMachine::capabilities($unknownUpload)['can_cancel'],
|
|
'An unknown upload completion was exposed as cancellable.');
|
|
try {
|
|
CloudTemplateImportStateMachine::assertAction(
|
|
$unknownUpload,
|
|
'reconcile_multipart_start'
|
|
);
|
|
throw new RuntimeException(
|
|
'An existing multipart identity was exposed as a new-session retry.'
|
|
);
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
'Multipart identity conflict returned the wrong status.');
|
|
}
|
|
$unknownCreate = clone $unknownUpload;
|
|
$unknownCreate->multipart_upload_id = '';
|
|
CloudTemplateImportStateMachine::assertAction(
|
|
$unknownCreate,
|
|
'reconcile_multipart_start'
|
|
);
|
|
try {
|
|
CloudTemplateImportStateMachine::assertAction($unknownUpload, 'cancel');
|
|
throw new RuntimeException('The cancellation endpoint disagrees with its projection.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409, 'Unknown cancellation returned the wrong status.');
|
|
}
|
|
$unknownUpload->error_code = 'multipart_abort_not_applied';
|
|
CloudTemplateImportStateMachine::assertAction($unknownUpload, 'cancel');
|
|
$mutated = $definitions;
|
|
$missingSuccessPath = $definitions;
|
|
$missingSuccessPath['source_verifying']['transitions'] = ['upload_failed', 'source_failed', 'source_unknown'];
|
|
$wrongStep = $definitions;
|
|
$wrongStep['source_ready']['resume_step'] = 'cancel';
|
|
$wrongAction = $definitions;
|
|
$wrongAction['source_ready']['actions']['upload'] = ['start_upload'];
|
|
foreach ([$missingSuccessPath, $wrongStep, $wrongAction] as $broken) {
|
|
try {
|
|
CloudTemplateImportStateMachine::assertDefinitions($broken);
|
|
throw new RuntimeException('An invalid import execution path passed completeness checks.');
|
|
} catch (LogicException) {
|
|
// A nonempty but wrong action/step or unreachable success must fail.
|
|
}
|
|
}
|
|
unset($mutated['prepared']['actions']['upload']);
|
|
try {
|
|
CloudTemplateImportStateMachine::assertDefinitions($mutated);
|
|
throw new RuntimeException(
|
|
'A source without an executable import action was accepted.'
|
|
);
|
|
} catch (LogicException) {
|
|
// Expected: every supported source/state pair needs an action policy.
|
|
}
|
|
$wrongSource = clone $stored;
|
|
$wrongSource->state = 'prepared';
|
|
$wrongSource->source_kind = 'upload';
|
|
try {
|
|
CloudTemplateImportStateMachine::move($wrongSource, 'fetching');
|
|
throw new RuntimeException(
|
|
'An upload intent entered the remote-fetch state.'
|
|
);
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
'A source/state mismatch returned the wrong status.');
|
|
}
|
|
$assert(in_array('source_unknown',
|
|
CloudTemplateImportStateMachine::cleanupStates('upload'), true)
|
|
&& !in_array('source_unknown',
|
|
CloudTemplateImportStateMachine::cleanupStates('remote'), true),
|
|
'Cleanup eligibility is not derived from the executable definitions.');
|
|
CloudTemplateImportStateMachine::move($stored, 'uploading');
|
|
CloudTemplateImportStateMachine::move($stored, 'source_verifying');
|
|
CloudTemplateImportStateMachine::move($stored, 'source_ready');
|
|
CloudTemplateImportStateMachine::move($stored, 'distributing');
|
|
CloudTemplateImportStateMachine::move($stored, 'ready');
|
|
$assert($stored->completed_at !== null
|
|
&& CloudTemplateImportStateMachine::terminal('ready'),
|
|
'The import state machine did not mark a terminal intent.');
|
|
try {
|
|
CloudTemplateImportStateMachine::move($stored, 'uploading');
|
|
throw new RuntimeException('A terminal import intent was reopened.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
'An invalid terminal transition returned the wrong status.');
|
|
}
|
|
|
|
echo "Cloud-template import intent and state-machine behavior passed.\n";
|
|
}
|