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

232 lines
8.8 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudHost;
use App\Models\CloudHostVpcTransport;
/** Panel authority for one compute node's existing private L3 underlay. */
final class CloudHostVpcTransportService
{
public static function read(int $hostId): array
{
$host = self::host($hostId);
return self::dto($host, CloudHostVpcTransport::find($hostId));
}
public static function save(int $hostId, array $input): array
{
CloudInput::only($input, [
'transport_address', 'transport_prefix_length',
'transport_interface', 'transport_mode', 'mtu',
'expected_revision',
]);
$desired = self::normalize($input);
$expected = $input['expected_revision'] ?? null;
if (is_bool($expected) || !is_int($expected) || $expected < 0) {
throw new CloudError('VPC 传输配置版本无效,请刷新后重试', 409);
}
$host = self::host($hostId);
$connection = (new CloudHostVpcTransport())->getConnection();
$row = $connection->transaction(static function () use (
$hostId, $expected, $desired
): CloudHostVpcTransport {
$row = CloudHostVpcTransport::query()->where('host_id', $hostId)
->lockForUpdate()->first();
$actual = $row === null ? 0 : (int) $row->desired_revision;
if ($actual !== $expected) {
throw new CloudError(
'VPC 传输配置已被修改,请刷新后重试', 409
);
}
if ($row !== null && self::same($row, $desired)) {
return $row;
}
$now = time();
if ($row === null) {
$row = new CloudHostVpcTransport();
$row->host_id = $hostId;
$row->created_at = $now;
}
foreach ($desired as $field => $value) {
$row->{$field} = $value;
}
$row->desired_revision = $actual + 1;
$row->observed_revision = 0;
$row->sync_state = 'pending';
$row->last_operation_id = null;
$row->last_error_code = '';
$row->last_verified_at = 0;
$row->updated_at = $now;
$row->save();
return $row;
});
return self::dto($host, $row);
}
/** Return bounded host evidence without changing panel desired state. */
public static function discover(int $hostId, array $actor): array
{
self::host($hostId);
$payload = CloudAgentSettingsService::clientForConfiguration(
$hostId, 20.0
)->request('GET', '/v1/capabilities', $actor);
return CloudVpcTransportCapability::inspect($payload);
}
/** Verify that one saved revision is exactly present and executable. */
public static function test(int $hostId, array $actor): array
{
$host = self::host($hostId);
$row = CloudHostVpcTransport::find($hostId);
if ($row === null) {
throw new CloudError('请先保存宿主机 VPC 私网传输配置', 409);
}
$revision = (int) $row->desired_revision;
try {
$capability = self::discover($hostId, $actor);
CloudVpcTransportCapability::assertReady(
$capability, $row->toArray()
);
$fresh = self::recordResult(
$hostId, $revision, 'ready', '', time()
);
return self::dto($host, $fresh) + ['capability' => $capability];
} catch (CloudAgentError $error) {
self::recordResult(
$hostId, $revision, 'failed', $error->agentCode, 0
);
throw $error;
}
}
private static function recordResult(int $hostId, int $revision,
string $state, string $errorCode, int $verifiedAt): CloudHostVpcTransport
{
return (new CloudHostVpcTransport())->getConnection()->transaction(
static function () use (
$hostId, $revision, $state, $errorCode, $verifiedAt
): CloudHostVpcTransport {
$row = CloudHostVpcTransport::query()
->where('host_id', $hostId)->lockForUpdate()->first();
if ($row === null
|| (int) $row->desired_revision !== $revision) {
throw new CloudError(
'VPC 传输配置在验证期间已被修改,请刷新后重试', 409
);
}
$row->observed_revision = $revision;
$row->sync_state = $state;
$row->last_error_code = substr($errorCode, 0, 64);
$row->last_verified_at = $verifiedAt;
$row->updated_at = time();
$row->save();
return $row;
}
);
}
private static function normalize(array $input): array
{
$address = $input['transport_address'] ?? null;
$prefix = $input['transport_prefix_length'] ?? null;
$interface = $input['transport_interface'] ?? null;
$mode = $input['transport_mode'] ?? 'direct_l3';
$mtu = $input['mtu'] ?? null;
if (!is_string($address) || trim($address) !== $address
|| !self::isPrivateIpv4($address)) {
throw new CloudError('VPC 传输地址必须是有效的 RFC1918 IPv4 地址');
}
if (is_bool($prefix) || !is_int($prefix)
|| $prefix < 1 || $prefix > 32) {
throw new CloudError('VPC 传输地址前缀必须是 1 至 32 的整数');
}
if (!is_string($interface)
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,14}$/D',
$interface) !== 1) {
throw new CloudError('VPC 传输接口名称无效');
}
if ($mode !== 'direct_l3') {
throw new CloudError('当前仅支持 direct_l3 私网传输模式');
}
if (is_bool($mtu) || !is_int($mtu) || $mtu < 576 || $mtu > 9216) {
throw new CloudError('VPC 传输 MTU 必须是 576 至 9216 的整数');
}
return [
'transport_address' => $address,
'transport_prefix_length' => $prefix,
'transport_interface' => $interface,
'transport_mode' => $mode,
'mtu' => $mtu,
];
}
private static function same(CloudHostVpcTransport $row,
array $desired): bool
{
foreach ($desired as $field => $value) {
if ((string) $row->{$field} !== (string) $value) {
return false;
}
}
return true;
}
private static function isPrivateIpv4(string $address): bool
{
if (filter_var($address, FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4) === false) {
return false;
}
$parts = array_map('intval', explode('.', $address));
return $parts[0] === 10
|| ($parts[0] === 172 && $parts[1] >= 16 && $parts[1] <= 31)
|| ($parts[0] === 192 && $parts[1] === 168);
}
private static function host(int $hostId): CloudHost
{
$host = CloudHost::find($hostId);
if ($host === null) {
throw new CloudError('宿主机不存在', 404);
}
return $host;
}
private static function dto(CloudHost $host,
?CloudHostVpcTransport $row): array
{
$configured = $row !== null;
$desired = $configured ? (int) $row->desired_revision : 0;
$observed = $configured ? (int) $row->observed_revision : 0;
$state = $configured ? (string) $row->sync_state : 'never';
return [
'host' => [
'id' => (int) $host->id,
'name' => (string) $host->name,
'region' => (string) $host->region,
'enabled' => (bool) $host->enabled,
],
'configured' => $configured,
'transport_address' => $configured
? (string) $row->transport_address : '',
'transport_prefix_length' => $configured
? (int) $row->transport_prefix_length : null,
'transport_interface' => $configured
? (string) $row->transport_interface : '',
'transport_mode' => $configured
? (string) $row->transport_mode : 'direct_l3',
'mtu' => $configured ? (int) $row->mtu : 1500,
'desired_revision' => $desired,
'observed_revision' => $observed,
'sync_state' => $state,
'ready' => $state === 'ready' && $desired > 0
&& $observed === $desired,
'last_error_code' => $configured
? (string) $row->last_error_code : '',
'last_verified_at' => $configured
? (int) $row->last_verified_at : 0,
];
}
}