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/Controllers/Admin/ObjectStorageController.php

368 lines
15 KiB
PHTML

<?php
namespace App\Controllers\Admin;
use App\Controllers\AdminController;
use App\Models\CloudInstanceBackup;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudBackupService;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudObjectStorageBackendRegistry;
use App\Services\Cloud\CloudStorageProtectionSummaryService;
use App\Services\Cloud\CloudStorageReplicaAttentionService;
final class ObjectStorageController extends AdminController
{
public function index($request, $response, $args)
{
$backends = [];
$summary = [
'backend_count' => 0,
'write_ready_count' => 0,
'healthy_count' => 0,
'protected_artifact_count' => 0,
'degraded_artifact_count' => 0,
'replication_backlog_count' => 0,
'oldest_replication_age_seconds' => 0,
];
$error = '';
$cleanupDebt = null;
$cleanupDebtError = '';
$replicaAttention = null;
$replicaAttentionError = '';
$status = 200;
try {
$backends = CloudObjectStorageBackendRegistry::listView();
$registrySummary = CloudObjectStorageBackendRegistry::summary();
$protectionSummary = CloudStorageProtectionSummaryService::summary();
$summary = array_replace($summary, $registrySummary,
$protectionSummary, [
'backend_count' => (int) ($registrySummary['configured_backends'] ?? 0),
'write_ready_count' => (int) ($registrySummary['writable_backends'] ?? 0),
'healthy_count' => (int) ($registrySummary['healthy_backends'] ?? 0),
]);
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Object storage settings read failed: ' . get_class($exception));
$error = '对象存储配置暂时无法读取,请检查数据库状态。';
$status = 503;
}
try {
$query = $request->getQueryParams();
$cleanupDebt = CloudBackupService::cleanupDebt(
CloudAccess::actor(false), [
'page' => $query['cleanup_page'] ?? 1,
'per_page' => 20,
]
);
} catch (CloudError $exception) {
$cleanupDebtError = $exception->getMessage();
} catch (\Throwable $exception) {
error_log('Object cleanup debt read failed: ' . get_class($exception));
$cleanupDebtError = '对象清理债务暂时无法读取,请检查数据库结构。';
}
try {
$query = $request->getQueryParams();
$replicaAttention = CloudStorageReplicaAttentionService::listView([
'page' => $query['attention_page'] ?? 1,
'finalization_page' =>
$query['finalization_attention_page'] ?? 1,
'per_page' => 20,
]);
} catch (CloudError $exception) {
$replicaAttentionError = $exception->getMessage();
} catch (\Throwable $exception) {
error_log('Object replica attention read failed: '
. get_class($exception));
$replicaAttentionError = '副本人工核对列表暂时无法读取。';
}
$smarty = $this->view()->assign([
'object_storage_backends' => $backends,
'object_storage_summary' => $summary,
'object_storage_error' => $error,
'cleanup_debt' => $cleanupDebt,
'cleanup_debt_error' => $cleanupDebtError,
'replica_attention' => $replicaAttention,
'replica_attention_error' => $replicaAttentionError,
]);
$response->getBody()->write($smarty->fetch('admin/object_storage.tpl'));
return $response->withStatus($status)
->withHeader('Cache-Control', 'private, no-store');
}
public function createBackend($request, $response, $args)
{
try {
$input = CloudInput::object($request);
CloudInput::only($input, self::configurationFields(false));
$actor = CloudAccess::actor(false);
$backend = CloudObjectStorageBackendRegistry::create(
$input, (int) $actor['user_id']
);
return $this->json($response, 1,
'对象存储后端已创建,请先验证连接,再单独启用写入。',
201, $backend);
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(), $exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage backend create failed: ' . get_class($exception));
return $this->json($response, 0, '对象存储后端创建失败,请稍后重试。', 500);
}
}
public function updateBackend($request, $response, $args)
{
try {
$input = CloudInput::object($request);
CloudInput::only($input, self::configurationFields(true));
$actor = CloudAccess::actor(false);
$backend = CloudObjectStorageBackendRegistry::update(
self::backendId($args),
$input,
self::expectedRevision($input),
(int) $actor['user_id']
);
return $this->json($response, 1,
'对象存储配置已保存;当前修订需要重新验证。', 200,
$backend);
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(), $exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage backend update failed: ' . get_class($exception));
return $this->json($response, 0, '对象存储配置保存失败,请稍后重试。', 500);
}
}
public function testBackend($request, $response, $args)
{
try {
[$backendId, $revision, $actorId] = $this->actionInput(
$request, $args
);
$backend = CloudObjectStorageBackendRegistry::test(
$backendId, $revision, $actorId
);
return $this->json($response, 1,
'连接验证成功;尚未改变该后端的写入状态。', 200, $backend);
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(), $exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage backend test failed: ' . get_class($exception));
return $this->json($response, 0, '连接验证失败,请稍后重试。', 500);
}
}
public function activateBackend($request, $response, $args)
{
return $this->transition($request, $response, $args, 'activate',
'对象存储后端已启用,可参与后续新对象写入。');
}
public function drainBackend($request, $response, $args)
{
return $this->transition($request, $response, $args, 'drain',
'对象存储后端已进入排空状态;历史读取与精确清理仍保留。');
}
public function resumeBackend($request, $response, $args)
{
return $this->transition($request, $response, $args, 'resume',
'对象存储后端已恢复,可重新参与后续新对象写入。');
}
public function disableBackend($request, $response, $args)
{
return $this->transition($request, $response, $args, 'disable',
'对象存储后端已停用;加密凭据和历史对象身份均已保留。');
}
public function cleanupDebt($request, $response, $args)
{
try {
$payload = CloudBackupService::cleanupDebt(
CloudAccess::actor(false), $request->getQueryParams()
);
return $response->withJson([
'ret' => 1,
'msg' => '对象清理债务已刷新。',
'cleanup_debt' => $payload,
])->withHeader('Cache-Control', 'private, no-store');
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(),
$exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object cleanup debt refresh failed: '
. get_class($exception));
return $this->json($response, 0,
'对象清理债务刷新失败,请稍后重试。', 500);
}
}
public function retryCleanup($request, $response, $args)
{
try {
$backupId = filter_var($args['backup_id'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($backupId === false) {
throw new CloudError('对象清理记录无效', 400);
}
$backup = CloudInstanceBackup::find((int) $backupId);
if ($backup === null) {
throw new CloudError('对象清理记录不存在', 404);
}
$result = CloudBackupService::retryCleanup(
$backup, CloudAccess::actor(false)
);
return $response->withJson([
'ret' => 1,
'msg' => ($result['deleted'] ?? false)
? '对象已确认不存在,清理债务已关闭。'
: '已恢复稳定性核对;第二次证据一致后才会创建新删除代次。',
'cleanup' => $result,
])->withHeader('Cache-Control', 'private, no-store');
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(),
$exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object cleanup retry failed: ' . get_class($exception));
return $this->json($response, 0,
'对象清理重试失败,请稍后重试。', 500);
}
}
public function replicaAttention($request, $response, $args)
{
try {
return $response->withJson([
'ret' => 1,
'msg' => '对象存储人工核对列表已刷新。',
'replica_attention' =>
CloudStorageReplicaAttentionService::listView(
$request->getQueryParams()
),
])->withHeader('Cache-Control', 'private, no-store');
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(),
$exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage attention refresh failed: '
. get_class($exception));
return $this->json($response, 0,
'对象存储人工核对列表刷新失败。', 500);
}
}
public function retryReplicaAttention($request, $response, $args)
{
try {
$operationId = (string) ($args['operation_id'] ?? '');
$input = CloudInput::object($request);
CloudInput::only($input, ['operation_kind']);
$operationKind = isset($input['operation_kind'])
? (string) $input['operation_kind'] : null;
$result = CloudStorageReplicaAttentionService::retry(
$operationId,
$operationKind
);
$finalization = ($result['operation_kind'] ?? '') === 'finalize';
return $response->withJson([
'ret' => 1,
'msg' => $finalization
? '已重新打开本地业务最终化;不会发起对象存储写入。'
: '已使用原操作恢复只读核对;不会新建上传或终止远端会话。',
'replica_attention' => $result,
])->withHeader('Cache-Control', 'private, no-store');
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(),
$exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage attention retry failed: '
. get_class($exception));
return $this->json($response, 0,
'对象存储核对恢复失败,请检查原操作状态。', 500);
}
}
private function transition($request, $response, array $args,
string $method, string $success)
{
try {
[$backendId, $revision, $actorId] = $this->actionInput(
$request, $args
);
$backend = CloudObjectStorageBackendRegistry::{$method}(
$backendId, $revision, $actorId
);
return $this->json($response, 1, $success, 200, $backend);
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(),
$exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage backend transition failed: '
. get_class($exception));
return $this->json($response, 0,
'对象存储状态修改失败,请稍后重试。', 500);
}
}
/** @return array{0:int,1:int,2:int} */
private function actionInput($request, array $args): array
{
$input = CloudInput::object($request);
CloudInput::only($input, ['expected_revision']);
$actor = CloudAccess::actor(false);
return [self::backendId($args), self::expectedRevision($input),
(int) $actor['user_id']];
}
private static function backendId(array $args): int
{
$value = filter_var($args['backend_id'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($value === false) {
throw new CloudError('对象存储后端无效', 400);
}
return (int) $value;
}
private static function expectedRevision(array $input): int
{
$value = filter_var($input['expected_revision'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($value === false) {
throw new CloudError('对象存储配置修订号无效', 400);
}
return (int) $value;
}
/** @return list<string> */
private static function configurationFields(bool $updating): array
{
$fields = [
'name', 'endpoint', 'region', 'bucket', 'prefix', 'path_style',
'failure_domain', 'write_priority', 'access_key_id',
'secret_access_key',
];
if ($updating) {
array_unshift($fields, 'expected_revision');
}
return $fields;
}
/** @param array<string, mixed>|null $backend */
private function json($response, int $ret, string $message, int $status,
?array $backend = null)
{
$payload = ['ret' => $ret, 'msg' => $message];
if ($backend !== null) {
$payload['backend'] = $backend;
}
return $response->withJson($payload, $status)
->withHeader('Cache-Control', 'private, no-store');
}
}