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.
virt/web/app/Services/Cloud/CloudTemplateRemoteAcquisit...

355 lines
13 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudTemplateImportIntent;
/** Acquires and verifies one official HTTPS cloud image. */
final class CloudTemplateRemoteAcquisition extends CloudTemplateImportModule
{
public function importRemote(
array $input,
array $actor,
?CloudTemplateRemoteFetcher $fetcher = null,
?CloudObjectStorageS3Client $client = null
): array {
$reserved = $this->reservation()->beginRemote($input, $actor);
return $this->acquireRemote(
(string) $reserved['intent']['id'],
$actor,
$fetcher ?? new CloudTemplateRemoteFetcher(),
$client ?? new CloudObjectStorageS3Client()
);
}
public function resumeRemote(
string $id,
array $actor,
?CloudTemplateRemoteFetcher $fetcher = null,
?CloudObjectStorageS3Client $client = null
): array {
CloudAccess::manage($actor);
return $this->acquireRemote(
CloudTemplateImportId::parse($id),
$actor,
$fetcher ?? new CloudTemplateRemoteFetcher(),
$client ?? new CloudObjectStorageS3Client()
);
}
private function acquireRemote(
string $id,
array $actor,
CloudTemplateRemoteFetcher $fetcher,
CloudObjectStorageS3Client $client
): array {
$snapshot = $this->remoteSnapshot($id);
if (($snapshot['finalize'] ?? false) === true) {
return $this->publication()->finalizeReadyIntent($id, $actor, $client);
}
/** @var CloudTemplateImportIntent $intent */
$intent = $snapshot['intent'];
try {
$runtime = self::runtime($intent);
} catch (CloudError $error) {
self::finishRemoteFailure(
$id,
'source_failed',
'remote_storage_runtime_invalid',
(int) ($this->clock)()
);
throw $error;
}
$transport = new CloudImmutableArtifactTransport($client);
if (($snapshot['reconcile'] ?? false) === true) {
try {
$identity = $transport->inspect(
$runtime,
(string) $intent->object_key
);
} catch (\Throwable $error) {
self::finishRemoteFailure(
$id,
'source_unknown',
'remote_source_reconcile_unavailable',
(int) ($this->clock)()
);
throw new CloudError(
'无法确认官方云镜像是否已保存,请勿创建新的导入',
502
);
}
if ($identity !== null) {
$this->publication()->acceptCompletedIdentity($id, $identity);
return $this->publication()->finalizeReadyIntent($id, $actor, $client);
}
self::makeRemoteRetryable($id, (int) ($this->clock)());
return $this->acquireRemote(
$id,
$actor,
$fetcher,
$client
);
}
try {
$source = self::remoteSource($intent);
} catch (CloudError $error) {
self::finishRemoteFailure(
$id,
'source_failed',
'remote_source_ledger_invalid',
(int) ($this->clock)()
);
throw $error;
}
$file = null;
try {
$file = $fetcher->fetch(
$source['url'],
$source['expected_sha256']
);
$size = filter_var(
$file['size_bytes'] ?? null,
FILTER_VALIDATE_INT,
['options' => [
'min_range' => self::MIN_UPLOAD_BYTES,
'max_range' => self::MAX_UPLOAD_BYTES,
]]
);
$digest = strtolower((string) ($file['sha256'] ?? ''));
if ($size === false
|| preg_match('/^[a-f0-9]{64}$/D', $digest) !== 1
|| !is_string($file['path'] ?? null)
|| !is_file((string) $file['path'])) {
throw new CloudError('官方云镜像校验结果无效', 503);
}
$this->freezeRemoteFileIdentity($id, (int) $size, $digest);
$intent->refresh();
$publication = $transport->publish(
$runtime,
(string) $intent->object_key,
[
'path' => (string) $file['path'],
'size_bytes' => (int) $size,
'sha256' => $digest,
]
);
} catch (CloudError $error) {
self::finishRemoteFailure(
$id,
'source_failed',
'remote_fetch_failed',
(int) ($this->clock)()
);
throw $error;
} catch (\Throwable $error) {
self::finishRemoteFailure(
$id,
'source_failed',
'remote_acquire_failed',
(int) ($this->clock)()
);
throw new CloudError(
'处理官方云镜像时发生内部错误,可以从原导入记录重试',
503
);
} finally {
if (is_array($file) && is_string($file['path'] ?? null)
&& is_file((string) $file['path'])) {
@unlink((string) $file['path']);
}
}
$resolution = (string) ($publication['resolution'] ?? 'unknown');
$identity = $publication['identity'] ?? null;
if ($resolution !== 'applied' || !is_array($identity)) {
$code = (string) ($publication['error_code']
?? 'remote_source_publish_failed');
$next = $resolution === 'not_applied'
? 'source_failed' : 'source_unknown';
self::finishRemoteFailure(
$id,
$next,
$code,
(int) ($this->clock)()
);
throw new CloudError(
$resolution === 'conflict'
? '官方云镜像对象身份冲突,记录已锁定'
: ($resolution === 'not_applied'
? '对象存储已确认官方云镜像尚未保存,可以重试'
: '无法确认官方云镜像是否已保存,请勿创建新的导入'),
$resolution === 'conflict' ? 409 : 502
);
}
$this->publication()->acceptCompletedIdentity($id, $identity);
return $this->publication()->finalizeReadyIntent($id, $actor, $client);
}
private function remoteSnapshot(string $id): array
{
$now = ($this->clock)();
$connection = (new CloudTemplateImportIntent())->getConnection();
return $connection->transaction(static function () use (
$id,
$now
): array {
$intent = self::lockIntent($id);
if ((string) $intent->source_kind !== 'remote') {
throw new CloudError('该导入记录不是远程云镜像', 409);
}
CloudTemplateImportStateMachine::assertAction(
$intent, CloudTemplateImportStateMachine::ACTION_RESUME_REMOTE
);
$state = (string) $intent->state;
$step = CloudTemplateImportStateMachine::resumeStep($intent);
if ($step === 'publish' || $step === 'published') {
return ['intent' => $intent, 'finalize' => true];
}
if ($step === 'verify') {
if ((int) $intent->source_size_bytes
< self::MIN_UPLOAD_BYTES
|| preg_match('/^[a-f0-9]{64}$/D',
(string) $intent->source_sha256) !== 1) {
throw new CloudError(
'远程云镜像核对账本不完整',
409
);
}
return ['intent' => $intent, 'reconcile' => true];
}
if ($state === 'fetching'
&& (int) $intent->updated_at > $now - 3600) {
throw new CloudError(
'官方云镜像正在下载,请在当前导入记录中等待',
409
);
}
if ($state !== 'fetching') {
CloudTemplateImportStateMachine::move($intent, 'fetching');
}
$intent->error_code = '';
$intent->expires_at = $now + self::INTENT_TTL_SECONDS;
$intent->updated_at = $now;
$intent->save();
return ['intent' => $intent];
}, 3);
}
private function freezeRemoteFileIdentity(
string $id,
int $size,
string $sha256
): void {
$now = ($this->clock)();
$connection = (new CloudTemplateImportIntent())->getConnection();
$connection->transaction(static function () use (
$id,
$size,
$sha256,
$now
): void {
$intent = self::lockIntent($id);
if ((string) $intent->source_kind !== 'remote'
|| (string) $intent->state !== 'fetching') {
throw new CloudError('远程云镜像下载状态已变化', 409);
}
$expected = (string) $intent->source_sha256;
if ($expected !== '' && !hash_equals($expected, $sha256)) {
CloudTemplateImportStateMachine::move(
$intent,
'source_failed'
);
$intent->error_code = 'remote_sha256_mismatch';
$intent->updated_at = $now;
$intent->save();
throw new CloudError('云镜像 SHA-256 校验不一致', 409);
}
$intent->source_size_bytes = $size;
$intent->source_sha256 = $sha256;
CloudTemplateImportStateMachine::move(
$intent,
'source_verifying'
);
$intent->error_code = '';
$intent->updated_at = $now;
$intent->save();
}, 3);
}
private static function finishRemoteFailure(
string $id,
string $next,
string $code,
int $now
): void {
$connection = (new CloudTemplateImportIntent())->getConnection();
$connection->transaction(static function () use (
$id,
$next,
$code,
$now
): void {
$intent = self::lockIntent($id);
if ((string) $intent->source_kind !== 'remote'
|| !in_array((string) $intent->state, [
'fetching', 'source_verifying', 'source_unknown',
'source_failed',
], true)) {
return;
}
$preserveCode = (string) $intent->state === $next
&& trim((string) $intent->error_code) !== '';
if ((string) $intent->state !== $next) {
CloudTemplateImportStateMachine::move($intent, $next);
}
if (!$preserveCode) {
$intent->error_code = substr($code, 0, 128);
}
$intent->updated_at = $now;
$intent->save();
}, 3);
}
private static function makeRemoteRetryable(string $id, int $now): void
{
$connection = (new CloudTemplateImportIntent())->getConnection();
$connection->transaction(static function () use ($id, $now): void {
$intent = self::lockIntent($id);
if ((string) $intent->state === 'source_unknown') {
CloudTemplateImportStateMachine::move(
$intent,
'source_verifying'
);
}
if ((string) $intent->state !== 'source_verifying') {
throw new CloudError('远程云镜像核对状态已变化', 409);
}
CloudTemplateImportStateMachine::move(
$intent,
'source_failed'
);
$intent->error_code = 'remote_source_not_applied';
$intent->updated_at = $now;
$intent->save();
}, 3);
}
private function reservation(): CloudTemplateImportReservation
{
return new CloudTemplateImportReservation(
$this->runtimeProvider,
$this->clock
);
}
private function publication(): CloudTemplateImportPublication
{
return new CloudTemplateImportPublication(
$this->runtimeProvider,
$this->clock
);
}
}