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.
479 lines
24 KiB
PHP
479 lines
24 KiB
PHP
<?php
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostNetworkAttachment;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudJob;
|
|
use App\Models\CloudPublicIp;
|
|
use App\Models\CloudPublicIpPool;
|
|
use App\Models\CloudResourcePool;
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
final class CloudJobService
|
|
{
|
|
public static function enqueue(CloudInstance $instance, array $actor, string $kind, string $method,
|
|
string $suffix, array $body = [], ?array $credential = null): CloudJob
|
|
{
|
|
CloudAccess::vmActor($instance, $actor);
|
|
return $instance->getConnection()->transaction(function () use ($instance, $actor, $kind, $method, $suffix, $body, $credential) {
|
|
if ($kind === 'reinstall') {
|
|
CloudTemplateDeletionService::assertAvailable(
|
|
CloudInput::template($body['template_id'] ?? '')
|
|
);
|
|
}
|
|
if ($kind === 'network_change') {
|
|
CloudInput::only($body,
|
|
['public_ip_cidr', 'public_pool_key', 'confirm_restart']);
|
|
if (($body['confirm_restart'] ?? false) !== true) {
|
|
throw new CloudError('更换公网 IP 前必须确认虚拟机会短暂重启');
|
|
}
|
|
$body['public_ip_cidr'] = CloudInput::publicIpCidr(
|
|
$body['public_ip_cidr'] ?? null
|
|
);
|
|
$body['public_pool_key'] = CloudInput::publicPoolKey(
|
|
$body['public_pool_key'] ?? null
|
|
);
|
|
}
|
|
if ($kind === 'instance_settings') {
|
|
CloudInput::only($body, ['hostname', 'root_password', 'ssh_public_key', 'confirm_restart']);
|
|
if (($body['confirm_restart'] ?? false) !== true) {
|
|
throw new CloudError('修改实例设置前必须确认虚拟机会短暂重启');
|
|
}
|
|
$changed = false;
|
|
if (array_key_exists('hostname', $body)) {
|
|
$body['hostname'] = CloudInput::hostname($body['hostname']);
|
|
$changed = true;
|
|
}
|
|
if (array_key_exists('root_password', $body)) {
|
|
if (!is_string($body['root_password']) || $body['root_password'] === '') {
|
|
throw new CloudError('实例密码不能为空');
|
|
}
|
|
$body['root_password'] = CloudInput::password($body['root_password']);
|
|
$changed = true;
|
|
}
|
|
if (array_key_exists('ssh_public_key', $body)) {
|
|
$parsed = CloudSSHKeys::parse((string) $body['ssh_public_key']);
|
|
$body['ssh_public_key'] = $parsed['public_key'];
|
|
$changed = true;
|
|
}
|
|
if (!$changed) {
|
|
throw new CloudError('请至少填写一项需要修改的实例设置');
|
|
}
|
|
}
|
|
$locked = CloudInstance::where('id', $instance->id)->lockForUpdate()->first();
|
|
if ($locked === null || (string) $locked->status === 'deleted') {
|
|
throw new CloudError('虚拟机不存在', 404);
|
|
}
|
|
if ((string) $locked->status !== 'active') {
|
|
throw new CloudError('实例正在执行到期销毁流程,不能提交其他操作', 409);
|
|
}
|
|
if ($kind === 'reinstall') {
|
|
CloudSnapshotService::assertReinstallAllowedLocked($locked);
|
|
}
|
|
$policyService = ['suspending_expired', 'resume_pending', 'resuming',
|
|
'suspend_failed', 'resume_failed', 'expired_grace',
|
|
'purge_stopping', 'purge_stop_failed', 'purge_deleting',
|
|
'purge_failed'];
|
|
$policyNetwork = ['blocking', 'blocking_expired', 'blocked_expired',
|
|
'restore_pending', 'restoring', 'block_failed', 'restore_failed'];
|
|
$superAdmin = ($actor['is_super_admin'] ?? false)
|
|
&& ($actor['can_manage'] ?? false);
|
|
if ($kind !== 'delete' && !$superAdmin
|
|
&& (in_array((string) $locked->service_status, $policyService, true)
|
|
|| in_array((string) $locked->network_status, $policyNetwork, true)
|
|
|| ((string) $locked->service_status !== 'exempt'
|
|
&& ($locked->expires_at === null || (int) $locked->expires_at <= time())))) {
|
|
throw new CloudError('实例正在执行到期或流量策略,请稍后重试', 409);
|
|
}
|
|
$activeJobs = CloudJob::where('instance_id', $locked->id)
|
|
->whereIn('status', ['queued', 'running', 'uncertain']);
|
|
if ($kind === 'delete') {
|
|
// Deletion is the dominant tenant mutation. A firewall result
|
|
// may remain uncertain indefinitely, but it owns neither VM
|
|
// disks nor the public-IP ledger; the engine's per-VM lock
|
|
// orders any executor still running and deletion then removes
|
|
// the deterministic filter as part of its cleanup checkpoint.
|
|
$activeJobs->where('kind', '<>', 'firewall_apply');
|
|
}
|
|
if ($activeJobs->exists()) {
|
|
throw new CloudError('虚拟机已有任务,请等待完成', 409);
|
|
}
|
|
$networkLedger = null;
|
|
if ($kind === 'network_change') {
|
|
$host = CloudHost::where('id', (int) $locked->host_id)
|
|
->lockForUpdate()->first();
|
|
// The host row is the sentinel for all of these references.
|
|
// Read their identifiers first, then lock attachment/pools,
|
|
// reservation and IP rows in the scheduler's fixed order.
|
|
$reservationRef = $locked->reservation_id === null ? null
|
|
: CloudResourceReservation::where('id', (int) $locked->reservation_id)
|
|
->first();
|
|
if ($host === null || $reservationRef === null
|
|
|| (int) $reservationRef->host_id !== (int) $host->id
|
|
|| (int) ($reservationRef->network_attachment_id ?? 0) < 1
|
|
|| (int) ($reservationRef->public_ip_pool_id ?? 0) < 1) {
|
|
throw new CloudError('虚拟机缺少有效的公网 IP 资源预留', 409);
|
|
}
|
|
$parts = explode('/', (string) $body['public_ip_cidr'], 2);
|
|
$targetPoolRef = CloudPublicIpPool::where('host_id', (int) $host->id)
|
|
->where('pool_key', (string) $body['public_pool_key'])->first();
|
|
if ($targetPoolRef === null) {
|
|
throw new CloudError('所选公网地址池已不可用,请刷新后重试', 409);
|
|
}
|
|
$targetRef = CloudPublicIp::where('host_id', (int) $host->id)
|
|
->where('public_ip_pool_id', (int) $targetPoolRef->id)
|
|
->where('address', $parts[0])
|
|
->where('prefix_length', (int) $parts[1])->first();
|
|
if ($targetRef === null) {
|
|
throw new CloudError('所选公网 IP 不在指定地址池的可用账本中', 409);
|
|
}
|
|
|
|
$networkAttachmentId = (int) $reservationRef->network_attachment_id;
|
|
$sourcePoolId = (int) $reservationRef->public_ip_pool_id;
|
|
$targetPoolId = (int) $targetPoolRef->id;
|
|
$attachment = CloudHostNetworkAttachment::where(
|
|
'id', $networkAttachmentId
|
|
)->lockForUpdate()->first();
|
|
$poolIds = array_values(array_unique([$sourcePoolId, $targetPoolId]));
|
|
sort($poolIds, SORT_NUMERIC);
|
|
$pools = CloudPublicIpPool::whereIn('id', $poolIds)->orderBy('id')
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
$sourcePool = $pools->get($sourcePoolId);
|
|
$targetPool = $pools->get($targetPoolId);
|
|
$reservation = CloudResourceReservation::where(
|
|
'id', (int) $reservationRef->id
|
|
)->lockForUpdate()->first();
|
|
$reservationPool = $reservation === null ? null
|
|
: CloudResourcePool::find((int) $reservation->resource_pool_id);
|
|
if ($reservation === null) {
|
|
throw new CloudError('虚拟机缺少有效的公网 IP 资源预留', 409);
|
|
}
|
|
$ipIds = array_values(array_unique([
|
|
(int) $reservation->public_ip_id, (int) $targetRef->id,
|
|
]));
|
|
sort($ipIds, SORT_NUMERIC);
|
|
$ips = CloudPublicIp::whereIn('id', $ipIds)->orderBy('id')
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
$source = $ips->get((int) $reservation->public_ip_id);
|
|
$target = $ips->get((int) $targetRef->id);
|
|
if ($attachment === null || $sourcePool === null
|
|
|| $targetPool === null || $source === null || $target === null
|
|
|| $reservationPool === null
|
|
|| (int) $attachment->host_id !== (int) $host->id
|
|
|| (int) $reservation->resource_pool_id
|
|
!== (int) ($locked->resource_pool_id ?? 0)
|
|
|| (int) $attachment->line_id
|
|
!== (int) $reservationPool->line_id
|
|
|| (string) $attachment->state !== 'active'
|
|
|| (string) $attachment->sync_state !== 'applied'
|
|
|| (int) $attachment->observed_revision
|
|
< (int) $attachment->desired_revision
|
|
|| (string) $attachment->network_selector
|
|
!== (string) $host->default_network
|
|
|| (int) $sourcePool->host_id !== (int) $host->id
|
|
|| (int) $targetPool->host_id !== (int) $host->id
|
|
|| (string) $sourcePool->state !== 'active'
|
|
|| (string) $sourcePool->sync_state !== 'applied'
|
|
|| (int) $sourcePool->observed_revision
|
|
< (int) $sourcePool->desired_revision
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) $sourcePool->pool_key) !== 1
|
|
|| (int) $sourcePool->network_attachment_id
|
|
!== $networkAttachmentId
|
|
|| (int) $targetPool->network_attachment_id
|
|
!== $networkAttachmentId
|
|
|| (string) $targetPool->pool_key
|
|
!== (string) $body['public_pool_key']
|
|
|| (string) $targetPool->state !== 'active'
|
|
|| (string) $targetPool->sync_state !== 'applied'
|
|
|| (int) $targetPool->observed_revision
|
|
< (int) $targetPool->desired_revision
|
|
|| (int) $reservation->host_id !== (int) $host->id
|
|
|| (int) ($reservation->instance_id ?? 0) !== (int) $locked->id
|
|
|| (string) $reservation->state !== 'committed'
|
|
|| (int) ($reservation->network_attachment_id ?? 0)
|
|
!== $networkAttachmentId
|
|
|| (int) ($reservation->public_ip_pool_id ?? 0) !== $sourcePoolId
|
|
|| (int) $source->host_id !== (int) $host->id
|
|
|| (int) ($source->public_ip_pool_id ?? 0) !== $sourcePoolId
|
|
|| (int) ($target->public_ip_pool_id ?? 0) !== $targetPoolId
|
|
|| (int) $target->prefix_length
|
|
!== (int) $targetPool->guest_prefix_length
|
|
|| !self::publicIpBelongsToPool($source, $sourcePool)
|
|
|| !self::publicIpBelongsToPool($target, $targetPool)
|
|
|| !self::instancePublicIpMatches($locked, $source)
|
|
|| (string) $source->state !== 'assigned'
|
|
|| (int) ($source->reservation_id ?? 0) !== (int) $reservation->id
|
|
|| (int) ($source->instance_id ?? 0) !== (int) $locked->id
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
(string) ($source->operation_id ?? ''))) {
|
|
throw new CloudError('虚拟机当前公网 IP 账本关联不一致', 409);
|
|
}
|
|
if ((int) $target->id === (int) $source->id
|
|
|| (string) $target->state !== 'available'
|
|
|| $target->reservation_id !== null
|
|
|| $target->instance_id !== null
|
|
|| $target->operation_id !== null) {
|
|
throw new CloudError('所选公网 IP 已不可分配,请刷新后重试', 409);
|
|
}
|
|
$networkLedger = [
|
|
'source' => $source,
|
|
'target' => $target,
|
|
'network_attachment_id' => $networkAttachmentId,
|
|
'source_public_ip_pool_id' => $sourcePoolId,
|
|
'target_public_ip_pool_id' => $targetPoolId,
|
|
];
|
|
}
|
|
$job = new CloudJob();
|
|
$job->operation_id = bin2hex(random_bytes(16));
|
|
$job->instance_id = $locked->id;
|
|
$job->user_id = $actor['user_id'];
|
|
$job->host_id = $locked->host_id;
|
|
$job->kind = $kind;
|
|
$job->status = 'queued';
|
|
$jobPayload = ['method' => $method,
|
|
'resource' => 'vms/' . rawurlencode($locked->uuid) . $suffix, 'body' => $body,
|
|
'credential' => $credential, 'owner_id' => (int) $locked->user_id,
|
|
'can_manage' => (bool) ($actor['can_manage'] ?? false)];
|
|
if ($networkLedger !== null) {
|
|
$source = $networkLedger['source'];
|
|
$target = $networkLedger['target'];
|
|
$jobPayload['source_public_ip_id'] = (int) $source->id;
|
|
$jobPayload['target_public_ip_id'] = (int) $target->id;
|
|
$jobPayload['network_attachment_id'] =
|
|
$networkLedger['network_attachment_id'];
|
|
$jobPayload['source_public_ip_pool_id'] =
|
|
$networkLedger['source_public_ip_pool_id'];
|
|
$jobPayload['target_public_ip_pool_id'] =
|
|
$networkLedger['target_public_ip_pool_id'];
|
|
// reservation_id is deliberately left NULL until the swap
|
|
// succeeds because cloud_public_ips enforces one current IP
|
|
// per reservation.
|
|
$target->state = 'changing';
|
|
$target->instance_id = (int) $locked->id;
|
|
$target->operation_id = $job->operation_id;
|
|
$target->updated_at = time();
|
|
$target->save();
|
|
}
|
|
$job->payload_cipher = CloudSecrets::encrypt($jobPayload);
|
|
$job->available_at = $job->created_at = $job->updated_at = time();
|
|
$job->save();
|
|
return $job;
|
|
}, 3);
|
|
}
|
|
|
|
/**
|
|
* Queue the irreversible second phase of expiry cleanup.
|
|
*
|
|
* Only the lifecycle state machine may reach this method. Unlike the
|
|
* four target-state policy jobs, expiry deletion is intentionally never
|
|
* replayed after dispatch because an interrupted volume deletion may be
|
|
* only partly complete.
|
|
*/
|
|
public static function enqueueExpiryDelete(CloudInstance $instance): CloudJob
|
|
{
|
|
return $instance->getConnection()->transaction(function () use ($instance) {
|
|
$locked = CloudInstance::where('id', $instance->id)->lockForUpdate()->first();
|
|
if ($locked === null || (string) $locked->status === 'deleted') {
|
|
throw new CloudError('虚拟机不存在', 404);
|
|
}
|
|
if ((string) $locked->status !== 'deleting'
|
|
|| (string) $locked->service_status !== 'purge_deleting') {
|
|
throw new CloudError('实例尚未完成销毁前停机,不能删除磁盘', 409);
|
|
}
|
|
$existing = CloudJob::where('instance_id', $locked->id)
|
|
->whereIn('status', ['queued', 'running', 'uncertain'])
|
|
// Firewall operations do not own disks, power state or the
|
|
// public-IP ledger. Expiry deletion must not be held forever
|
|
// by a firewall result that legitimately remains uncertain;
|
|
// the engine serializes both operations with the same per-VM
|
|
// target lock and never replays the destructive DELETE.
|
|
->where('kind', '<>', 'firewall_apply')
|
|
->orderByDesc('id')->first();
|
|
if ($existing !== null) {
|
|
if ((string) $existing->kind === 'expiry_delete') {
|
|
return $existing;
|
|
}
|
|
throw new CloudError('虚拟机已有任务,请等待完成', 409);
|
|
}
|
|
|
|
$job = new CloudJob();
|
|
$job->operation_id = bin2hex(random_bytes(16));
|
|
$job->instance_id = $locked->id;
|
|
$job->user_id = $locked->user_id;
|
|
$job->host_id = $locked->host_id;
|
|
$job->kind = 'expiry_delete';
|
|
$job->status = 'queued';
|
|
$job->payload_cipher = CloudSecrets::encrypt([
|
|
'method' => 'DELETE',
|
|
'resource' => 'vms/' . rawurlencode($locked->uuid)
|
|
. '?delete_disks=true',
|
|
'body' => [],
|
|
'owner_id' => (int) $locked->user_id,
|
|
'system_cleanup' => true,
|
|
]);
|
|
$job->available_at = $job->created_at = $job->updated_at = time();
|
|
$job->save();
|
|
return $job;
|
|
}, 3);
|
|
}
|
|
|
|
public static function display(CloudJob $job, bool $administrator = false): array
|
|
{
|
|
$result = $job->result_json === null ? null : json_decode($job->result_json, true);
|
|
// Historical rows may contain a status introduced by a newer engine.
|
|
// Keep that value from becoming another arbitrary browser text field.
|
|
$status = CloudOperationErrorCatalog::userStatus($job->status) ?? 'unknown';
|
|
$action = self::action($job);
|
|
$labels = CloudOperationCatalog::labels((string) $job->kind, $action);
|
|
$safeResult = self::displayResult(is_array($result) ? $result : null,
|
|
$administrator, (string) $job->kind, $status,
|
|
$job->order_id !== null, $action);
|
|
$error = $administrator
|
|
? CloudOperationErrorCatalog::adminSummary((string) $job->kind,
|
|
$status, $job->error_code, $job->order_id !== null, $action)
|
|
: CloudOperationErrorCatalog::userMessage((string) $job->kind,
|
|
$status, $job->error_code, $job->order_id !== null, $action);
|
|
$phase = $safeResult['phase'] ?? null;
|
|
$phase = $administrator
|
|
? CloudOperationErrorCatalog::adminSummary((string) $job->kind,
|
|
$status, $phase, $job->order_id !== null, $action)
|
|
: CloudOperationErrorCatalog::userPhase($phase, $status, (string) $job->kind);
|
|
return ['id' => (string) $job->id, 'operation_id' => $job->operation_id, 'kind' => $job->kind,
|
|
'status' => $status, 'error' => $error,
|
|
'error_code' => CloudOperationErrorCatalog::publicCode($job->error_code),
|
|
'attempts' => max(0, (int) $job->attempts),
|
|
'created_at' => max(0, (int) $job->created_at),
|
|
'updated_at' => max(0, (int) $job->updated_at),
|
|
'action' => $action,
|
|
'kind_label' => $labels['kind_label'],
|
|
'action_label' => $labels['action_label'],
|
|
'phase' => is_string($phase) ? mb_substr($phase, 0, 120) : null,
|
|
'result' => $safeResult];
|
|
}
|
|
|
|
/**
|
|
* Keep only the catalogued result DTO. Historical result JSON is not a
|
|
* trusted presentation format and must not bypass the error boundary.
|
|
*/
|
|
private static function displayResult(?array $result, bool $administrator,
|
|
?string $kind, ?string $status, bool $hasOrder, ?string $action): ?array
|
|
{
|
|
if ($result === null) {
|
|
return null;
|
|
}
|
|
return CloudOperationErrorCatalog::result($result, $administrator, $kind,
|
|
$status, $hasOrder, $action);
|
|
}
|
|
|
|
/**
|
|
* Return the non-sensitive power action from the encrypted task payload.
|
|
*
|
|
* The durable job payload is encrypted at rest and is never returned to
|
|
* the browser. Terminal jobs retain only CloudInput::safeResult($payload),
|
|
* so this lookup continues to work for historical rows while removing
|
|
* credentials and all other payload fields from the public response.
|
|
*/
|
|
public static function action(CloudJob $job): ?string
|
|
{
|
|
if ((string) $job->kind !== 'action' || trim((string) $job->payload_cipher) === '') {
|
|
return null;
|
|
}
|
|
try {
|
|
$payload = CloudSecrets::decrypt((string) $job->payload_cipher);
|
|
} catch (\Throwable $error) {
|
|
return null;
|
|
}
|
|
$body = $payload['body'] ?? null;
|
|
$action = is_array($body) && is_string($body['action'] ?? null)
|
|
? $body['action'] : '';
|
|
return CloudOperationCatalog::isPowerAction($action) ? $action : null;
|
|
}
|
|
|
|
/** Return the same safe labels used by every administrator DTO. */
|
|
public static function labels(CloudJob $job): array
|
|
{
|
|
return CloudOperationCatalog::labels((string) $job->kind, self::action($job));
|
|
}
|
|
|
|
/**
|
|
* Read the user's instance operation history directly from cloud_jobs.
|
|
* Power, reinstall and snapshot jobs are part of this user-facing audit view.
|
|
*/
|
|
public static function operationLogs(CloudInstance $instance, int $page = 1, int $perPage = 10): array
|
|
{
|
|
$perPage = max(1, min(50, $perPage));
|
|
$query = CloudJob::where('instance_id', $instance->id)
|
|
->whereIn('kind', ['action', 'reinstall', 'snapshot_create',
|
|
'snapshot_restore', 'snapshot_delete', 'firewall_apply'])
|
|
->orderByDesc('id');
|
|
$total = max(0, (int) (clone $query)->count('id'));
|
|
$pages = max(1, (int) ceil($total / $perPage));
|
|
$page = max(1, min($pages, $page));
|
|
$jobs = $query->offset(($page - 1) * $perPage)->limit($perPage)
|
|
->get(['id', 'kind', 'order_id', 'status', 'attempts', 'error_code', 'result_json',
|
|
'payload_cipher', 'created_at', 'updated_at']);
|
|
$items = [];
|
|
foreach ($jobs as $job) {
|
|
$entry = self::display($job);
|
|
unset($entry['operation_id'], $entry['result']);
|
|
$items[] = $entry;
|
|
}
|
|
$from = $total === 0 ? 0 : ($page - 1) * $perPage + 1;
|
|
return ['items' => $items, 'pagination' => [
|
|
'page' => $page, 'per_page' => $perPage, 'total' => $total,
|
|
'total_pages' => $pages, 'from' => $from,
|
|
'to' => $from === 0 ? 0 : $from + count($items) - 1,
|
|
]];
|
|
}
|
|
|
|
private static function publicIpBelongsToPool(CloudPublicIp $ip,
|
|
CloudPublicIpPool $pool): bool
|
|
{
|
|
$parts = explode('/', (string) $pool->cidr, 2);
|
|
if (count($parts) !== 2 || !ctype_digit($parts[1])) {
|
|
return false;
|
|
}
|
|
$prefix = (int) $parts[1];
|
|
if ($prefix < 24 || $prefix > 30
|
|
|| (int) $ip->prefix_length !== (int) $pool->guest_prefix_length) {
|
|
return false;
|
|
}
|
|
$address = self::ipv4Number((string) $ip->address);
|
|
$network = self::ipv4Number($parts[0]);
|
|
if ($address === null || $network === null) {
|
|
return false;
|
|
}
|
|
$block = 2 ** (32 - $prefix);
|
|
$base = (int) (floor($network / $block) * $block);
|
|
return $address > $base && $address < $base + $block - 1;
|
|
}
|
|
|
|
private static function instancePublicIpMatches(CloudInstance $instance,
|
|
CloudPublicIp $ip): bool
|
|
{
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
$recorded = is_array($spec) && !array_is_list($spec)
|
|
? ($spec['public_ip_cidr'] ?? null) : null;
|
|
return is_string($recorded) && hash_equals(
|
|
(string) $ip->address . '/' . (int) $ip->prefix_length,
|
|
trim($recorded)
|
|
);
|
|
}
|
|
|
|
private static function ipv4Number(string $value): ?int
|
|
{
|
|
if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
|
|
return null;
|
|
}
|
|
$packed = inet_pton($value);
|
|
if ($packed === false || strlen($packed) !== 4) {
|
|
return null;
|
|
}
|
|
$parts = unpack('Nnumber', $packed);
|
|
return is_array($parts) && isset($parts['number'])
|
|
? (int) sprintf('%u', $parts['number']) : null;
|
|
}
|
|
}
|