|
|
|
|
@ -42,6 +42,8 @@ final class CloudVpcPortService
|
|
|
|
|
$blocking = CloudJobService::blockingQueryForInstance(
|
|
|
|
|
(int) $instance->id
|
|
|
|
|
)->orderBy('id')->first();
|
|
|
|
|
$activeVpcJob = $port === null ? null
|
|
|
|
|
: self::activePortJob((int) $instance->id, (int) $port->id);
|
|
|
|
|
return [
|
|
|
|
|
'port' => $port === null ? null : self::portDto($port),
|
|
|
|
|
'vpcs' => $vpcs->map(static fn (CloudVpc $vpc): array => [
|
|
|
|
|
@ -59,6 +61,11 @@ final class CloudVpcPortService
|
|
|
|
|
&& (string) $port->state === 'attached'
|
|
|
|
|
&& (string) $instance->status === 'active'
|
|
|
|
|
&& $instance->serviceAvailable(),
|
|
|
|
|
'active_task' => $activeVpcJob === null ? null : [
|
|
|
|
|
'id' => (string) $activeVpcJob->id,
|
|
|
|
|
'status_url' => '/api/v1/tasks/' . (int) $activeVpcJob->id,
|
|
|
|
|
'kind' => (string) $activeVpcJob->kind,
|
|
|
|
|
],
|
|
|
|
|
'requires_cold_restart' => true,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
@ -84,13 +91,10 @@ final class CloudVpcPortService
|
|
|
|
|
$locked = CloudInstance::query()->where('id', (int) $instance->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
self::assertOwnerAndInstance($locked, $actor, true);
|
|
|
|
|
self::assertNoInstanceJobLocked($locked);
|
|
|
|
|
$vpc = CloudVpc::query()->where('vpc_key', $vpcKey)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($vpc === null || (int) $vpc->user_id !== (int) $locked->user_id
|
|
|
|
|
|| $vpc->active_claim === null
|
|
|
|
|
|| !in_array((string) $vpc->state,
|
|
|
|
|
['active', 'degraded'], true)) {
|
|
|
|
|
|| $vpc->active_claim === null) {
|
|
|
|
|
throw new CloudError('VPC 不存在或当前不可用', 404);
|
|
|
|
|
}
|
|
|
|
|
if ((int) $vpc->region_id !== self::instanceRegionId($locked)) {
|
|
|
|
|
@ -109,20 +113,38 @@ final class CloudVpcPortService
|
|
|
|
|
return ['port' => self::portDto($port), 'job' => null,
|
|
|
|
|
'unchanged' => true];
|
|
|
|
|
}
|
|
|
|
|
if ((string) $port->state !== 'preparing') {
|
|
|
|
|
$expectedPortKey = self::portKey(
|
|
|
|
|
(int) $locked->user_id, (int) $locked->id,
|
|
|
|
|
$vpcKey, $requestKey
|
|
|
|
|
);
|
|
|
|
|
if ((string) $port->state !== 'preparing'
|
|
|
|
|
|| !hash_equals((string) $port->port_key,
|
|
|
|
|
$expectedPortKey)) {
|
|
|
|
|
throw new CloudError('VPC 端口正在执行其他操作', 409);
|
|
|
|
|
}
|
|
|
|
|
$active = self::activePortJobLocked($locked, $port);
|
|
|
|
|
if ($active !== null) {
|
|
|
|
|
return ['port' => self::portDto($port), 'job' => $active,
|
|
|
|
|
'unchanged' => false];
|
|
|
|
|
}
|
|
|
|
|
self::assertNoInstanceJobLocked($locked);
|
|
|
|
|
$job = self::retryPreparingLocked($locked, $vpc, $port);
|
|
|
|
|
return ['port' => self::portDto($port->fresh()),
|
|
|
|
|
'job' => $job, 'unchanged' => false];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!in_array((string) $vpc->state,
|
|
|
|
|
['active', 'degraded'], true)) {
|
|
|
|
|
throw new CloudError('VPC 当前正在执行其他操作', 409);
|
|
|
|
|
}
|
|
|
|
|
self::assertNoInstanceJobLocked($locked);
|
|
|
|
|
self::assertVpcSettledLocked($vpc);
|
|
|
|
|
$now = time();
|
|
|
|
|
$port = new CloudVpcPort();
|
|
|
|
|
$port->port_key = substr(hash('sha256', "vpc-port\0"
|
|
|
|
|
. (int) $locked->user_id . "\0" . (int) $locked->id
|
|
|
|
|
. "\0" . $vpcKey . "\0" . $requestKey), 0, 32);
|
|
|
|
|
$port->port_key = self::portKey(
|
|
|
|
|
(int) $locked->user_id, (int) $locked->id,
|
|
|
|
|
$vpcKey, $requestKey
|
|
|
|
|
);
|
|
|
|
|
$existing = CloudVpcPort::query()
|
|
|
|
|
->where('port_key', (string) $port->port_key)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
@ -177,7 +199,6 @@ final class CloudVpcPortService
|
|
|
|
|
$locked = CloudInstance::query()->where('id', (int) $instance->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
self::assertOwnerAndInstance($locked, $actor, true);
|
|
|
|
|
self::assertNoInstanceJobLocked($locked);
|
|
|
|
|
$port = CloudVpcPort::query()
|
|
|
|
|
->where('instance_id', (int) $locked->id)
|
|
|
|
|
->where('active_claim', 1)->lockForUpdate()->first();
|
|
|
|
|
@ -189,7 +210,21 @@ final class CloudVpcPortService
|
|
|
|
|
if ($vpc === null || (int) $vpc->user_id !== (int) $locked->user_id) {
|
|
|
|
|
throw new CloudError('VPC 端口账本与所属人不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if ((string) $port->state === 'detaching') {
|
|
|
|
|
$active = self::activePortJobLocked($locked, $port);
|
|
|
|
|
if ($active === null) {
|
|
|
|
|
throw new CloudError('VPC 断开任务账本不完整', 409);
|
|
|
|
|
}
|
|
|
|
|
return ['port' => self::portDto($port), 'job' => $active,
|
|
|
|
|
'unchanged' => false];
|
|
|
|
|
}
|
|
|
|
|
if ((string) $port->state === 'release_pending') {
|
|
|
|
|
$active = self::activePortJobLocked($locked, $port);
|
|
|
|
|
if ($active !== null) {
|
|
|
|
|
return ['port' => self::portDto($port), 'job' => $active,
|
|
|
|
|
'unchanged' => false];
|
|
|
|
|
}
|
|
|
|
|
self::assertNoInstanceJobLocked($locked);
|
|
|
|
|
$job = self::retryReleaseLocked($locked, $vpc, $port);
|
|
|
|
|
return ['port' => $port->active_claim === null
|
|
|
|
|
? null : self::portDto($port->fresh()),
|
|
|
|
|
@ -198,6 +233,7 @@ final class CloudVpcPortService
|
|
|
|
|
if ((string) $port->state !== 'attached') {
|
|
|
|
|
throw new CloudError('VPC 端口正在执行其他操作', 409);
|
|
|
|
|
}
|
|
|
|
|
self::assertNoInstanceJobLocked($locked);
|
|
|
|
|
self::assertVpcSettledLocked($vpc);
|
|
|
|
|
$hostState = CloudVpcHostState::query()
|
|
|
|
|
->where('vpc_id', (int) $vpc->id)
|
|
|
|
|
@ -961,6 +997,24 @@ final class CloudVpcPortService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function activePortJob(int $instanceId,
|
|
|
|
|
int $portId): ?CloudJob
|
|
|
|
|
{
|
|
|
|
|
return CloudJobService::blockingQueryForInstance($instanceId)
|
|
|
|
|
->where('vpc_port_id', $portId)
|
|
|
|
|
->whereIn('kind', self::JOB_KINDS)
|
|
|
|
|
->orderByDesc('id')->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function activePortJobLocked(CloudInstance $instance,
|
|
|
|
|
CloudVpcPort $port): ?CloudJob
|
|
|
|
|
{
|
|
|
|
|
return CloudJobService::blockingQueryForInstance((int) $instance->id)
|
|
|
|
|
->where('vpc_port_id', (int) $port->id)
|
|
|
|
|
->whereIn('kind', self::JOB_KINDS)
|
|
|
|
|
->orderByDesc('id')->lockForUpdate()->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertOwnerAndInstance(?CloudInstance $instance,
|
|
|
|
|
array $actor, bool $mutation): void
|
|
|
|
|
{
|
|
|
|
|
@ -1078,6 +1132,13 @@ final class CloudVpcPortService
|
|
|
|
|
return $key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function portKey(int $userId, int $instanceId,
|
|
|
|
|
string $vpcKey, string $requestKey): string
|
|
|
|
|
{
|
|
|
|
|
return substr(hash('sha256', "vpc-port\0" . $userId . "\0"
|
|
|
|
|
. $instanceId . "\0" . $vpcKey . "\0" . $requestKey), 0, 32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function hostReady(?CloudVpcHostState $state): bool
|
|
|
|
|
{
|
|
|
|
|
return $state !== null && (string) $state->sync_state === 'ready'
|
|
|
|
|
|