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.
239 lines
9.2 KiB
PHP
239 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
|
/** Resolves and fingerprints one immutable maintenance-notice audience. */
|
|
final class CloudMaintenanceNoticeAudience
|
|
{
|
|
private const SCOPE_HOST = 'host';
|
|
private const SCOPE_PUBLIC_IP_POOL = 'public_ip_pool';
|
|
|
|
public function __construct(
|
|
private readonly CloudAffectedInstanceResolver $affectedInstances =
|
|
new CloudAffectedInstanceResolver()
|
|
) {
|
|
}
|
|
|
|
public function preview(string $scopeType, int $scopeId): array
|
|
{
|
|
$scopeType = trim($scopeType);
|
|
if (!in_array($scopeType, [
|
|
self::SCOPE_HOST, self::SCOPE_PUBLIC_IP_POOL,
|
|
], true) || $scopeId < 1) {
|
|
throw new CloudError('维护通知范围无效', 422);
|
|
}
|
|
|
|
$scope = $this->scope($scopeType, $scopeId);
|
|
$candidates = $this->affectedInstances->resolve($scopeType, $scopeId);
|
|
$userIds = [];
|
|
foreach ($candidates as $candidate) {
|
|
$userIds[(int) $candidate->user_id] = true;
|
|
}
|
|
$users = $userIds === [] ? [] : DB::table('user')
|
|
->whereIn('id', array_keys($userIds))->get()
|
|
->keyBy('id')->all();
|
|
|
|
$instances = [];
|
|
$recipients = [];
|
|
foreach ($candidates as $candidate) {
|
|
$instanceId = (int) $candidate->id;
|
|
$userId = (int) $candidate->user_id;
|
|
$network = $this->networkEvidence(
|
|
$candidate,
|
|
$scopeType === self::SCOPE_PUBLIC_IP_POOL
|
|
);
|
|
$instance = [
|
|
'id' => $instanceId,
|
|
'user_id' => $userId,
|
|
'host_id' => (int) $candidate->host_id,
|
|
'reservation_id' => (int) ($candidate->reservation_id ?? 0),
|
|
'name' => (string) $candidate->name,
|
|
'ipv4' => (string) ($network['ipv4'] ?? ''),
|
|
'ipv6' => (string) ($network['ipv6'] ?? ''),
|
|
'public_ip_pool_ids' => $network['public_ip_pool_ids'] ?? [],
|
|
'evidence' => $network['evidence'],
|
|
];
|
|
$instances[] = $instance;
|
|
|
|
if (!isset($recipients[$userId])) {
|
|
$user = $users[$userId] ?? null;
|
|
$email = strtolower(trim((string) ($user->email ?? '')));
|
|
[$deliverable, $reason] = $this->emailDisposition(
|
|
$user,
|
|
$email
|
|
);
|
|
$recipients[$userId] = [
|
|
'user_id' => $userId,
|
|
'email' => $email,
|
|
'deliverable' => $deliverable,
|
|
'skip_reason' => $reason,
|
|
'instance_ids' => [],
|
|
];
|
|
}
|
|
$recipients[$userId]['instance_ids'][] = $instanceId;
|
|
}
|
|
|
|
usort($instances, static fn (array $left, array $right): int =>
|
|
$left['id'] <=> $right['id']);
|
|
ksort($recipients, SORT_NUMERIC);
|
|
foreach ($recipients as &$recipient) {
|
|
sort($recipient['instance_ids'], SORT_NUMERIC);
|
|
}
|
|
unset($recipient);
|
|
$recipients = array_values($recipients);
|
|
$deliverable = count(array_filter(
|
|
$recipients,
|
|
static fn (array $recipient): bool => $recipient['deliverable']
|
|
));
|
|
|
|
$canonical = [
|
|
'scope' => $scope,
|
|
'instances' => $instances,
|
|
'recipients' => $recipients,
|
|
];
|
|
return $canonical + [
|
|
'instance_count' => count($instances),
|
|
'recipient_count' => $deliverable,
|
|
'skipped_count' => count($recipients) - $deliverable,
|
|
'audience_digest' => hash('sha256', self::json($canonical)),
|
|
];
|
|
}
|
|
|
|
private function scope(string $scopeType, int $scopeId): array
|
|
{
|
|
if ($scopeType === self::SCOPE_HOST) {
|
|
$host = DB::table('cloud_hosts')->where('id', $scopeId)->first();
|
|
if ($host === null) {
|
|
throw new CloudError('计算节点不存在', 404);
|
|
}
|
|
return [
|
|
'type' => $scopeType,
|
|
'id' => $scopeId,
|
|
'label' => (string) $host->name,
|
|
'host_id' => $scopeId,
|
|
'region' => (string) ($host->region ?? ''),
|
|
];
|
|
}
|
|
|
|
$pool = DB::table('cloud_public_ip_pools')
|
|
->where('id', $scopeId)->first();
|
|
if ($pool === null) {
|
|
throw new CloudError('公网地址池不存在', 404);
|
|
}
|
|
return [
|
|
'type' => $scopeType,
|
|
'id' => $scopeId,
|
|
'label' => (string) $pool->name,
|
|
'pool_key' => (string) $pool->pool_key,
|
|
'host_id' => (int) $pool->host_id,
|
|
'address_family' => (int) $pool->address_family,
|
|
'cidr' => (string) $pool->cidr,
|
|
];
|
|
}
|
|
|
|
private function networkEvidence(object $instance, bool $strict): array
|
|
{
|
|
$reservationId = (int) ($instance->reservation_id ?? 0);
|
|
$reservation = $reservationId < 1 ? null
|
|
: DB::table('cloud_resource_reservations')
|
|
->where('id', $reservationId)->first();
|
|
$reservationValid = $reservation !== null
|
|
&& (string) $reservation->state === 'committed'
|
|
&& (int) $reservation->instance_id === (int) $instance->id
|
|
&& (int) $reservation->host_id === (int) $instance->host_id
|
|
&& (int) $reservation->user_id === (int) $instance->user_id;
|
|
$rows = !$reservationValid ? []
|
|
: DB::table('cloud_resource_reservation_ips as claim')
|
|
->join('cloud_public_ips as ip', static function ($join): void {
|
|
$join->on('ip.id', '=', 'claim.public_ip_id')
|
|
->on('ip.reservation_ip_claim_id', '=', 'claim.id')
|
|
->on('ip.reservation_id', '=', 'claim.reservation_id')
|
|
->on('ip.public_ip_pool_id', '=',
|
|
'claim.public_ip_pool_id')
|
|
->on('ip.address_family', '=', 'claim.address_family')
|
|
->on('ip.operation_id', '=', 'claim.operation_id');
|
|
})
|
|
->where('claim.reservation_id', $reservationId)
|
|
->where('claim.active_claim', 1)
|
|
->where('claim.state', 'committed')
|
|
->where('ip.instance_id', (int) $instance->id)
|
|
->where('ip.host_id', (int) $instance->host_id)
|
|
->where('ip.state', 'assigned')
|
|
->orderBy('claim.address_family')
|
|
->get([
|
|
'claim.id as claim_id', 'claim.address_family',
|
|
'claim.claim_generation', 'claim.public_ip_pool_id',
|
|
'claim.public_ip_id', 'claim.operation_id',
|
|
'ip.address', 'ip.prefix_length',
|
|
])->all();
|
|
|
|
$claims = [];
|
|
foreach ($rows as $row) {
|
|
$family = (int) $row->address_family;
|
|
if (!in_array($family, [4, 6], true) || isset($claims[$family])) {
|
|
$claims = [];
|
|
break;
|
|
}
|
|
$claims[$family] = [
|
|
'family' => $family,
|
|
'claim_id' => (int) $row->claim_id,
|
|
'generation' => (int) $row->claim_generation,
|
|
'public_ip_pool_id' => (int) $row->public_ip_pool_id,
|
|
'public_ip_id' => (int) $row->public_ip_id,
|
|
'operation_id' => (string) $row->operation_id,
|
|
'address' => (string) $row->address,
|
|
'prefix_length' => (int) $row->prefix_length,
|
|
];
|
|
}
|
|
if (isset($claims[4], $claims[6]) && count($claims) === 2) {
|
|
ksort($claims, SORT_NUMERIC);
|
|
return [
|
|
'ipv4' => $claims[4]['address'],
|
|
'ipv6' => $claims[6]['address'],
|
|
'public_ip_pool_ids' => [
|
|
4 => $claims[4]['public_ip_pool_id'],
|
|
6 => $claims[6]['public_ip_pool_id'],
|
|
],
|
|
'evidence' => [
|
|
'kind' => 'active_committed_claims',
|
|
'reservation_id' => $reservationId,
|
|
'claims' => array_values($claims),
|
|
],
|
|
];
|
|
}
|
|
if ($strict) {
|
|
throw new CloudError('公网地址池受众账本不完整,请先核对资源', 409);
|
|
}
|
|
return [
|
|
'ipv4' => '',
|
|
'ipv6' => '',
|
|
'public_ip_pool_ids' => [],
|
|
'evidence' => [
|
|
'kind' => 'host_placement_only',
|
|
'reservation_id' => $reservationId,
|
|
],
|
|
];
|
|
}
|
|
|
|
private function emailDisposition(?object $user, string $email): array
|
|
{
|
|
if ($user === null) {
|
|
return [false, 'user_missing'];
|
|
}
|
|
if ($email === '' || strlen($email) > 254
|
|
|| preg_match('/^[\x21-\x7e]+$/D', $email) !== 1
|
|
|| filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
|
|
return [false, 'email_invalid'];
|
|
}
|
|
return [true, ''];
|
|
}
|
|
|
|
private static function json(array $value): string
|
|
{
|
|
return json_encode($value, JSON_THROW_ON_ERROR
|
|
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
}
|
|
}
|