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/CloudDeploymentPageService.php

480 lines
19 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudOffer;
use App\Models\CloudSSHKey;
use App\Models\CloudTemplateRetirement;
use App\Models\Configurations;
/**
* Database-backed first-paint data for instance deployment and reinstall.
*
* The template snapshot deliberately contains only public catalog labels and
* identifiers. Host paths, replica locations, credentials and checksums never
* cross this boundary or enter the configurations table.
*/
final class CloudDeploymentPageService
{
private const GIB = 1073741824;
private const TEMPLATE_SNAPSHOT_KEY = 'cloud_public_template_catalog_v1';
private const TEMPLATE_SNAPSHOT_VERSION = 1;
private const TEMPLATE_SNAPSHOT_REFRESH_SECONDS = 300;
/**
* Build the public Region -> Line -> Offer sale catalog.
*
* Hosts are not part of the sale catalog. Pools are read only to validate
* the active offer chain, and neither placement identity enters this DTO.
*/
public static function saleCatalog(): array
{
$linesEnabled = CloudNetworkLineFeature::enabled();
$query = CloudOffer::query()->from('cloud_offers as offer')
->join('shop as plan', 'plan.id', '=', 'offer.plan_id')
->join('cloud_resource_pools as pool', 'pool.id', '=', 'offer.resource_pool_id')
->join('cloud_network_lines as line', 'line.id', '=', 'pool.line_id')
->join('cloud_regions as region', 'region.id', '=', 'line.region_id')
->where('offer.status', 1)
->where('offer.currency', 'USD')
->where('plan.status', 1)
->where('pool.status', 'active')
->where('line.status', 'active')
->where('region.status', 'active');
if (!$linesEnabled) {
$query->where('line.is_default', 1);
}
$rows = $query->orderByDesc('region.sort')->orderBy('region.id')
->orderByDesc('line.sort')->orderBy('line.id')
->orderByDesc('offer.sort')->orderBy('offer.id')
->get([
'offer.id as offer_id', 'offer.name as offer_name',
'offer.price as offer_price', 'offer.stock as offer_stock',
'plan.cpu_cores', 'plan.memory_mib', 'plan.bandwidth_mbps',
'plan.disk_gib', 'plan.validity_days', 'plan.traffic_quota_bytes',
'pool.id as internal_pool_id',
'line.id as line_id', 'line.name as line_name', 'line.is_default',
'region.id as region_id', 'region.name as region_name',
'region.country_code',
]);
$capacityKey = static fn ($row): string => implode(':', [
(int) $row->internal_pool_id,
(int) $row->cpu_cores,
(int) $row->memory_mib,
(int) $row->disk_gib,
]);
$capacityRequests = [];
foreach ($rows as $row) {
$capacityRequests[$capacityKey($row)] = [
'pool_id' => (int) $row->internal_pool_id,
'cpu_cores' => (int) $row->cpu_cores,
'memory_mib' => (int) $row->memory_mib,
'disk_gib' => (int) $row->disk_gib,
];
}
$capacity = CloudPlacementService::poolCapacityAvailability(
$capacityRequests
);
$regions = [];
$lines = [];
$offers = [];
foreach ($rows as $row) {
$regionId = (int) $row->region_id;
$lineId = (int) $row->line_id;
if (!isset($regions[$regionId])) {
$countryCode = strtoupper(trim((string) $row->country_code));
$regions[$regionId] = [
'id' => $regionId,
'name' => trim((string) $row->region_name),
'country_code' => $countryCode,
'flag' => self::countryFlag($countryCode),
];
}
if (!isset($lines[$lineId])) {
$lines[$lineId] = [
'id' => $lineId,
'region_id' => $regionId,
'name' => trim((string) $row->line_name),
'is_default' => (bool) $row->is_default,
];
}
$trafficBytes = max(0, (int) $row->traffic_quota_bytes);
$trafficGiB = intdiv($trafficBytes, self::GIB);
$offerCapacityKey = $capacityKey($row);
$offers[] = [
'id' => (int) $row->offer_id,
'name' => trim((string) $row->offer_name),
'price' => number_format((float) $row->offer_price, 2, '.', ''),
'currency' => 'USD',
'region_id' => $regionId,
'line_id' => $lineId,
'cpu_cores' => (int) $row->cpu_cores,
'memory_mib' => (int) $row->memory_mib,
'bandwidth_mbps' => (int) $row->bandwidth_mbps,
'disk_gib' => (int) $row->disk_gib,
'validity_days' => (int) $row->validity_days,
'traffic_quota_bytes' => $trafficBytes,
'traffic_gib' => $trafficGiB,
'traffic_tib' => self::formatDecimalUnit($trafficGiB, 1024, 10),
'sellable' => (int) $row->offer_stock !== 0
&& ($capacity[$offerCapacityKey] ?? false),
];
}
return [
'lines_enabled' => $linesEnabled,
'regions' => array_values($regions),
'lines' => array_values($lines),
'offers' => $offers,
];
}
public static function sshKeys(int $userId): array
{
return CloudSSHKey::where('user_id', $userId)->orderBy('id')->get([
'id', 'name', 'fingerprint', 'created_at',
])->map(static fn ($key): array => [
'id' => (int) $key->id,
'name' => (string) $key->name,
'fingerprint' => (string) $key->fingerprint,
'created_at' => (int) $key->created_at,
])->all();
}
/**
* Return a complete public catalog for SSR. A durable last-known snapshot
* avoids an engine dependency on every page view. On a fresh installation
* the first request waits briefly for the loopback engine and still returns
* one complete HTML response instead of an empty browser shell.
*/
public static function templateCatalogForPage(array $actor): array
{
$snapshot = self::templateSnapshot();
if ($snapshot !== null
&& time() - (int) $snapshot['checked_at'] < self::TEMPLATE_SNAPSHOT_REFRESH_SECONDS) {
return self::pageCatalog($snapshot);
}
try {
return self::freshTemplateCatalog($actor, 2.0);
} catch (CloudError $error) {
error_log('Unable to refresh the public cloud template catalog: '
. get_class($error));
$snapshot ??= [
'version' => self::TEMPLATE_SNAPSHOT_VERSION,
'updated_at' => 0,
'templates' => [],
];
$snapshot['checked_at'] = time();
$snapshot['refresh_failed'] = true;
return self::pageCatalog($snapshot);
}
}
/** Fetch the authoritative engine-global catalog and refresh the snapshot. */
public static function freshTemplateCatalog(array $actor, float $timeoutSeconds = 20.0): array
{
$engine = new CloudEngineClient(null, $timeoutSeconds);
$payload = CloudTemplateDeletionService::fetchAuthoritativeCatalog(
static fn (): array => $engine->request(
'GET', '/internal/v1/vm-templates', $actor
),
static function (array $catalog): void {
self::rememberTemplateCatalog($catalog);
}
);
$payload['templates'] = self::availableTemplates(
self::sanitizeTemplates($payload['templates'] ?? [])
);
return $payload;
}
/** Persist only the public subset needed to draw an image picker. */
public static function rememberTemplateCatalog(array $payload): void
{
$templates = self::availableTemplates(
self::sanitizeTemplates($payload['templates'] ?? [])
);
$snapshot = [
'version' => self::TEMPLATE_SNAPSHOT_VERSION,
'updated_at' => time(),
'checked_at' => time(),
'refresh_failed' => false,
'templates' => array_map(static fn (array $template): array => [
'id' => $template['id'],
'name' => $template['name'],
'os_family' => $template['os_family'],
'os_version' => $template['os_version'],
], $templates),
];
self::saveTemplateSnapshot($snapshot);
}
private static function saveTemplateSnapshot(array $snapshot): void
{
$json = json_encode($snapshot, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
| JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR);
// configurations.value is TEXT. Refuse an accidental oversized write
// instead of truncating the JSON into a misleading partial catalog.
if (strlen($json) > 60000) {
error_log('Public cloud template catalog snapshot exceeds the storage limit');
return;
}
try {
$row = Configurations::where('name', self::TEMPLATE_SNAPSHOT_KEY)->first();
if ($row === null) {
$row = new Configurations();
$row->name = self::TEMPLATE_SNAPSHOT_KEY;
}
$row->value = $json;
$row->save();
} catch (\Throwable $error) {
// A public API read must not fail merely because its optional
// first-paint snapshot could not be refreshed.
error_log('Unable to save the public cloud template catalog snapshot: '
. get_class($error));
}
}
/** Update a cached rename/import result without ever fabricating a catalog. */
public static function rememberTemplate(array $template): void
{
$snapshot = self::templateSnapshot();
$records = self::sanitizeTemplates([$template]);
if ($snapshot === null || $records === []) {
return;
}
$replacement = $records[0];
$templates = [];
$found = false;
foreach ($snapshot['templates'] as $existing) {
if ((string) $existing['id'] === (string) $replacement['id']) {
$templates[] = $replacement;
$found = true;
} else {
$templates[] = $existing;
}
}
if (!$found) {
$templates[] = $replacement;
}
self::rememberTemplateCatalog(['templates' => $templates]);
}
/** Remove a deleted template from the first-paint snapshot immediately. */
public static function forgetTemplate(string $templateId): void
{
$snapshot = self::templateSnapshot();
if ($snapshot === null) {
return;
}
$templates = array_values(array_filter(
$snapshot['templates'],
static fn (array $template): bool =>
(string) ($template['id'] ?? '') !== $templateId
));
self::rememberTemplateCatalog(['templates' => $templates]);
}
/** Filter templates retired by PHP even if an engine retry is pending. */
private static function availableTemplates(array $templates): array
{
try {
$retired = array_fill_keys(
CloudTemplateRetirement::query()
->whereIn('state', ['deleting', 'deleted'])
->pluck('template_id')->map(
static fn ($id): string => (string) $id
)->all(),
true
);
} catch (\Throwable $error) {
// cloudInit creates the gate before the new code is started. Keep
// read-only first paint usable during tests or manual recovery.
$retired = [];
}
if ($retired === []) {
return array_values($templates);
}
return array_values(array_filter(
$templates,
static fn (array $template): bool =>
!isset($retired[(string) ($template['id'] ?? '')])
));
}
public static function templateGroups(array $templates): array
{
$groups = [];
foreach (self::sanitizeTemplates($templates) as $template) {
[$family, $version] = self::templateSystem($template);
if (!isset($groups[$family])) {
$groups[$family] = [
'family' => $family,
'icon_path' => self::templateIcon($family),
'templates' => [],
];
}
$template['display_version'] = $version;
$groups[$family]['templates'][] = $template;
}
foreach ($groups as &$group) {
usort($group['templates'], static function (array $left, array $right): int {
$version = version_compare(
(string) ($right['display_version'] ?? '0'),
(string) ($left['display_version'] ?? '0')
);
return $version !== 0 ? $version
: strnatcasecmp((string) $left['name'], (string) $right['name']);
});
}
unset($group);
$order = ['AlmaLinux' => 0, 'Debian' => 1, 'CentOS Stream' => 2, 'Ubuntu' => 3];
uasort($groups, static fn (array $left, array $right): int =>
($order[$left['family']] ?? 100) <=> ($order[$right['family']] ?? 100)
?: strcasecmp($left['family'], $right['family']));
return array_values($groups);
}
private static function templateSnapshot(): ?array
{
try {
$raw = Configurations::where('name', self::TEMPLATE_SNAPSHOT_KEY)->value('value');
} catch (\Throwable $error) {
return null;
}
if (!is_string($raw) || $raw === '' || strlen($raw) > 65535) {
return null;
}
$decoded = json_decode($raw, true);
if (!is_array($decoded) || (int) ($decoded['version'] ?? 0) !== self::TEMPLATE_SNAPSHOT_VERSION
|| !is_array($decoded['templates'] ?? null)) {
return null;
}
$templates = self::sanitizeTemplates($decoded['templates']);
// Do not accept a partially corrupted non-empty snapshot as an empty
// authoritative catalog; recover it from the engine instead.
if ($decoded['templates'] !== [] && $templates === []) {
return null;
}
return [
'version' => self::TEMPLATE_SNAPSHOT_VERSION,
'updated_at' => max(0, (int) ($decoded['updated_at'] ?? 0)),
'checked_at' => max(0, (int) ($decoded['checked_at']
?? $decoded['updated_at'] ?? 0)),
'refresh_failed' => ($decoded['refresh_failed'] ?? false) === true,
'templates' => $templates,
];
}
private static function pageCatalog(array $snapshot): array
{
$templates = is_array($snapshot['templates'] ?? null)
? $snapshot['templates'] : [];
$templates = self::availableTemplates($templates);
$failed = ($snapshot['refresh_failed'] ?? false) === true;
return [
'templates' => $templates,
'catalog_stale' => $failed && $templates !== [],
'catalog_unavailable' => $failed && $templates === [],
];
}
private static function sanitizeTemplates($values): array
{
if (!is_array($values)) {
return [];
}
$templates = [];
foreach (array_slice($values, 0, 256) as $value) {
if (!is_array($value)) {
continue;
}
$id = $value['id'] ?? null;
$name = $value['name'] ?? null;
$family = $value['os_family'] ?? '';
$version = $value['os_version'] ?? '';
if (!is_string($id)
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D', $id) !== 1
|| !self::plainLabel($name, 64)
|| !self::plainLabel($family, 40)
|| !is_string($version)
|| preg_match('/^[0-9]{1,4}(?:\.[0-9]{1,4}){0,2}$/D', $version) !== 1) {
continue;
}
$templates[$id] = [
'id' => $id,
'name' => trim($name),
'os_family' => trim($family),
'os_version' => $version,
];
}
return array_values($templates);
}
private static function plainLabel($value, int $maximum): bool
{
return is_string($value) && trim($value) !== '' && mb_strlen($value) <= $maximum
&& preg_match('//u', $value) === 1
&& preg_match('/[\x00-\x1F\x7F]/u', $value) !== 1;
}
private static function templateSystem(array $template): array
{
$familyKey = strtolower((string) preg_replace(
'/[\s_-]+/', '', (string) ($template['os_family'] ?? '')
));
$family = [
'alma' => 'AlmaLinux', 'almalinux' => 'AlmaLinux',
'debian' => 'Debian', 'centos' => 'CentOS Stream',
'centosstream' => 'CentOS Stream', 'ubuntu' => 'Ubuntu',
][$familyKey] ?? '';
$idVersion = '';
if (preg_match('/^(alma|debian|ubuntu|centosstream)([0-9]{1,4})-/i',
(string) $template['id'], $match) === 1) {
$family = $family ?: [
'alma' => 'AlmaLinux', 'debian' => 'Debian',
'ubuntu' => 'Ubuntu', 'centosstream' => 'CentOS Stream',
][strtolower($match[1])];
$idVersion = $match[2];
if (strtolower($match[1]) === 'ubuntu' && strlen($idVersion) === 4) {
$idVersion = substr($idVersion, 0, 2) . '.' . substr($idVersion, 2);
}
}
return [
$family ?: '其他系统',
(string) ($template['os_version'] ?: $idVersion),
];
}
private static function templateIcon(string $family): string
{
return [
'AlmaLinux' => '/cloud/almalinux-icon.svg',
'Debian' => '/cloud/debian-icon.svg',
'CentOS Stream' => '/cloud/centos-icon.svg',
'Ubuntu' => '/cloud/ubuntu-icon.svg',
][$family] ?? '';
}
private static function countryFlag(string $countryCode): string
{
if (preg_match('/^[A-Z]{2}$/D', $countryCode) !== 1) {
return '🌐';
}
return mb_chr(127397 + ord($countryCode[0]), 'UTF-8')
. mb_chr(127397 + ord($countryCode[1]), 'UTF-8');
}
private static function formatDecimalUnit(int $value, int $unit, int $precision): string
{
$formatted = number_format($value / $unit, $precision, '.', '');
$formatted = rtrim(rtrim($formatted, '0'), '.');
return $formatted === '' ? '0' : $formatted;
}
}