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...

373 lines
14 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'];
if (($snapshot['reconcile'] ?? false) === true) {
try {
$binding = CloudTemplateStoragePublicationService::binding(
$intent
);
/** @var \App\Models\CloudStorageOperation $operation */
$operation = $binding['operation'];
if ((string) $operation->phase === 'accepted') {
self::makeRemoteRetryable(
$id,
(int) ($this->clock)()
);
return $this->acquireRemote(
$id,
$actor,
$fetcher,
$client
);
}
$result = CloudTemplateStoragePublicationService::publishFile(
$intent,
[
'path' => '',
'size_bytes' => (int) $intent->source_size_bytes,
'sha256' => (string) $intent->source_sha256,
],
$client
);
} catch (\Throwable $error) {
self::finishRemoteFailure(
$id,
'source_unknown',
'remote_source_reconcile_unavailable',
(int) ($this->clock)()
);
throw new CloudError(
'无法确认官方云镜像是否已保存,请勿创建新的导入',
502
);
}
if (($result['primary_completed'] ?? false) === true) {
$identity = CloudTemplateStoragePublicationService::
primaryIdentity($intent);
$this->publication()->acceptCompletedIdentity($id, $identity);
return $this->publication()->finalizeReadyIntent($id, $actor, $client);
}
if (($result['requires_attention'] ?? false) === true) {
self::finishRemoteFailure(
$id,
'source_unknown',
(string) ($result['error_code']
?? 'remote_source_publish_attention'),
(int) ($this->clock)()
);
throw new CloudError(
'官方云镜像发布需要管理员核对,请勿创建新的导入',
409
);
}
throw new CloudError(
'官方云镜像发布结果仍在核对,请稍后重试原导入',
409
);
}
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();
CloudTemplateStoragePublicationService::reserve($intent);
$intent->refresh();
$publication = CloudTemplateStoragePublicationService::publishFile(
$intent,
[
'path' => (string) $file['path'],
'size_bytes' => (int) $size,
'sha256' => $digest,
],
$client
);
} 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']);
}
}
if (($publication['primary_completed'] ?? false) !== true) {
$attention = ($publication['requires_attention'] ?? false) === true;
$code = (string) ($publication['error_code']
?? ($attention
? 'remote_source_publish_attention'
: 'remote_source_publish_pending'));
self::finishRemoteFailure(
$id,
$attention ? 'source_unknown' : 'source_failed',
$code,
(int) ($this->clock)()
);
throw new CloudError(
$attention
? '官方云镜像发布需要管理员核对,请勿创建新的导入'
: '官方云镜像主副本尚未完成,可以从原导入记录重试',
$attention ? 409 : 502
);
}
$identity = CloudTemplateStoragePublicationService::primaryIdentity(
$intent
);
$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->clock);
}
private function publication(): CloudTemplateImportPublication
{
return new CloudTemplateImportPublication($this->clock);
}
}