perf: render deployment workflows on first response
parent
dad9fa6ad2
commit
b82abf5547
@ -0,0 +1,382 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudHost;
|
||||
use App\Models\CloudSSHKey;
|
||||
use App\Models\Configurations;
|
||||
use App\Models\Shop;
|
||||
|
||||
/**
|
||||
* 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 TEMPLATE_SNAPSHOT_KEY = 'cloud_public_template_catalog_v1';
|
||||
private const TEMPLATE_SNAPSHOT_VERSION = 1;
|
||||
private const TEMPLATE_SNAPSHOT_REFRESH_SECONDS = 300;
|
||||
|
||||
public static function plans(): array
|
||||
{
|
||||
$plans = [];
|
||||
$available = Shop::where('status', 1)->orderBy('sort')->orderBy('id')->get();
|
||||
$hostIds = $available->pluck('host_id')->map(static fn ($id): int => (int) $id)
|
||||
->filter(static fn (int $id): bool => $id > 0)->unique()->values()->all();
|
||||
$hosts = $hostIds === [] ? collect() : CloudHost::whereIn('id', $hostIds)
|
||||
->where('enabled', 1)->get(['id', 'name', 'country_code', 'region'])->keyBy('id');
|
||||
|
||||
foreach ($available as $plan) {
|
||||
if ((int) $plan->stock === 0) {
|
||||
continue;
|
||||
}
|
||||
$host = $hosts->get((int) $plan->host_id);
|
||||
if ($host === null) {
|
||||
continue;
|
||||
}
|
||||
$plans[] = [
|
||||
'id' => (int) $plan->id,
|
||||
'name' => (string) $plan->name,
|
||||
'price' => (string) $plan->price,
|
||||
'cpu_cores' => (int) $plan->cpu_cores,
|
||||
'memory_mib' => (int) $plan->memory_mib,
|
||||
'bandwidth_mbps' => (int) $plan->bandwidth_mbps,
|
||||
'disk_gib' => (int) $plan->disk_gib,
|
||||
'validity_days' => (int) $plan->validity_days,
|
||||
'traffic_quota_bytes' => (int) $plan->traffic_quota_bytes,
|
||||
'traffic_gib' => $plan->trafficGiB(),
|
||||
'stock' => (int) $plan->stock,
|
||||
'host_id' => (int) $host->id,
|
||||
'host_name' => (string) $host->name,
|
||||
'country_code' => strtoupper(trim((string) ($host->country_code ?? ''))),
|
||||
'region' => trim((string) $host->region),
|
||||
];
|
||||
}
|
||||
|
||||
return $plans;
|
||||
}
|
||||
|
||||
/** Plans embedded in user HTML omit the administrator-facing host label. */
|
||||
public static function publicPagePlans(): array
|
||||
{
|
||||
return array_map(static function (array $plan): array {
|
||||
unset($plan['host_name']);
|
||||
return $plan;
|
||||
}, self::plans());
|
||||
}
|
||||
|
||||
public static function regions(array $plans): array
|
||||
{
|
||||
$regions = [];
|
||||
foreach ($plans as $plan) {
|
||||
$hostId = max(0, (int) ($plan['host_id'] ?? 0));
|
||||
if ($hostId === 0) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($regions[$hostId])) {
|
||||
$countryCode = strtoupper(trim((string) ($plan['country_code'] ?? '')));
|
||||
$regions[$hostId] = [
|
||||
'host_id' => $hostId,
|
||||
'country_code' => $countryCode,
|
||||
'flag' => self::countryFlag($countryCode),
|
||||
'region' => trim((string) ($plan['region'] ?? '')) ?: '地区未设置',
|
||||
'plans' => [],
|
||||
];
|
||||
}
|
||||
$regions[$hostId]['plans'][] = $plan;
|
||||
}
|
||||
|
||||
return array_values($regions);
|
||||
}
|
||||
|
||||
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;
|
||||
self::saveTemplateSnapshot($snapshot);
|
||||
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
|
||||
{
|
||||
$payload = (new CloudEngineClient(null, $timeoutSeconds))->request(
|
||||
'GET', '/internal/v1/vm-templates', $actor
|
||||
);
|
||||
$payload['templates'] = self::sanitizeTemplates($payload['templates'] ?? []);
|
||||
self::rememberTemplateCatalog($payload);
|
||||
return $payload;
|
||||
}
|
||||
|
||||
/** Persist only the public subset needed to draw an image picker. */
|
||||
public static function rememberTemplateCatalog(array $payload): void
|
||||
{
|
||||
$templates = 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]);
|
||||
}
|
||||
|
||||
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'] : [];
|
||||
$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');
|
||||
}
|
||||
}
|
||||
@ -1 +1 @@
|
||||
{assign var=venix_asset_version value='2026092810' scope='global'}
|
||||
{assign var=venix_asset_version value='2026092811' scope='global'}
|
||||
|
||||
@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
// Static regression contract for deployment/reinstall first paint. The HTML
|
||||
// response owns all data needed to draw the form; JavaScript only hydrates
|
||||
// interactions and must not briefly replace the catalog with loading states.
|
||||
$root = dirname(__DIR__);
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$section = static function (string $source, string $start, string $end): string {
|
||||
$offset = strpos($source, $start);
|
||||
if ($offset === false) {
|
||||
return '';
|
||||
}
|
||||
$limit = strpos($source, $end, $offset + strlen($start));
|
||||
return $limit === false ? substr($source, $offset)
|
||||
: substr($source, $offset, $limit - $offset);
|
||||
};
|
||||
|
||||
$controller = file_get_contents($root . '/app/Controllers/CloudPageController.php');
|
||||
$service = file_get_contents($root . '/app/Services/Cloud/CloudDeploymentPageService.php');
|
||||
$deployTemplate = file_get_contents($root . '/resources/views/venix/cloud/pages/deploy.tpl');
|
||||
$reinstallTemplate = file_get_contents($root . '/resources/views/venix/cloud/pages/reinstall.tpl');
|
||||
$deployModule = file_get_contents($root . '/public/cloud/deploy.js');
|
||||
$reinstallModule = file_get_contents($root . '/public/cloud/reinstall.js');
|
||||
$runtime = file_get_contents($root . '/public/cloud/runtime.js');
|
||||
$cloudShell = file_get_contents($root . '/resources/views/venix/cloud/page.tpl');
|
||||
$layoutAssets = file_get_contents($root . '/resources/views/venix/layout_assets.tpl');
|
||||
|
||||
$deployController = $section($controller, 'public function deploy(',
|
||||
'public function isoImages(');
|
||||
$reinstallController = $section($controller, 'public function reinstall(',
|
||||
'public function rescue(');
|
||||
$openCreate = $section($runtime, 'async function openCreate(',
|
||||
'async function loadGlobalTemplates(');
|
||||
$openReinstall = $section($runtime, 'async function openReinstall(',
|
||||
'async function pollJob(');
|
||||
$selectRegion = $section($runtime, 'async function selectRegion(',
|
||||
'async function selectPlan(');
|
||||
$selectPlan = $section($runtime, 'async function selectPlan(',
|
||||
'async function openCreate(');
|
||||
$sshKeyOptions = $section($controller, 'private function sshKeyOptions(',
|
||||
'private function reinstallVm(');
|
||||
|
||||
$assert(str_contains($deployController, 'CloudAccess::actor(false)')
|
||||
&& str_contains($deployController, 'CloudDeploymentPageService::publicPagePlans()')
|
||||
&& str_contains($deployController, 'CloudDeploymentPageService::regions($plans)')
|
||||
&& str_contains($deployController, 'CloudDeploymentPageService::templateCatalogForPage($actor)')
|
||||
&& str_contains($deployController, '$this->sshKeyOptions((int) $actor[\'user_id\'])')
|
||||
&& str_contains($deployController,
|
||||
'$defaultHostId = $regions === [] ? 0 : (int) $regions[0][\'host_id\']')
|
||||
&& str_contains($deployController, "'catalog_unavailable' => \$catalogUnavailable")
|
||||
&& str_contains($deployController,
|
||||
"'cloud_deploy_catalog_unavailable' => \$catalogUnavailable")
|
||||
&& str_contains($deployController, "'cloud_deploy_bootstrap_json' => \$this->safeJson(\$payload)"),
|
||||
'Deployment controller must build one complete, actor-scoped first-paint payload.');
|
||||
$assert(str_contains($sshKeyOptions, "'id' => \$key['id']")
|
||||
&& str_contains($sshKeyOptions, "'name' => \$key['name']")
|
||||
&& str_contains($sshKeyOptions, 'CloudDeploymentPageService::sshKeys($userId)')
|
||||
&& !str_contains($sshKeyOptions, 'fingerprint')
|
||||
&& !str_contains($sshKeyOptions, 'created_at'),
|
||||
'Deployment bootstraps must expose only the saved key identifier and display name.');
|
||||
$assert(str_contains($service, "->orderBy('sort')->orderBy('id')")
|
||||
&& str_contains($service, "->where('enabled', 1)")
|
||||
&& str_contains($service, 'if ((int) $plan->stock === 0)')
|
||||
&& str_contains($service, 'return array_values($regions);')
|
||||
&& str_contains($service, "unset(\$plan['host_name'])"),
|
||||
'Available plans and regions must retain administrator sale order and omit private host labels.');
|
||||
$assert(str_contains($service, "private const TEMPLATE_SNAPSHOT_KEY")
|
||||
&& str_contains($service, 'TEMPLATE_SNAPSHOT_REFRESH_SECONDS')
|
||||
&& str_contains($service, "'checked_at' => time()")
|
||||
&& str_contains($service, "'catalog_unavailable' =>")
|
||||
&& str_contains($service, 'Configurations::where(')
|
||||
&& str_contains($service, 'self::sanitizeTemplates(')
|
||||
&& str_contains($service, "'id' => \$template['id']")
|
||||
&& str_contains($service, "'os_family' => \$template['os_family']")
|
||||
&& str_contains($service, "'os_version' => \$template['os_version']"),
|
||||
'The first-paint image catalog must use a durable, sanitized public snapshot.');
|
||||
|
||||
$assert(str_contains($deployTemplate, 'id="deploy-bootstrap" type="application/json"')
|
||||
&& str_contains($deployTemplate, '{$cloud_deploy_bootstrap_json nofilter}')
|
||||
&& str_contains($deployTemplate, '{foreach $cloud_deploy_regions as $region}')
|
||||
&& str_contains($deployTemplate, '$region.host_id == $cloud_deploy_default_host_id')
|
||||
&& str_contains($deployTemplate, '{foreach $region.plans as $plan}')
|
||||
&& str_contains($deployTemplate, '{foreach $cloud_deploy_template_groups as $group}')
|
||||
&& str_contains($deployTemplate, '{foreach $cloud_deploy_ssh_keys as $key}')
|
||||
&& str_contains($deployTemplate, '$cloud_deploy_catalog_unavailable')
|
||||
&& str_contains($deployTemplate, '系统镜像目录暂时无法读取')
|
||||
&& !str_contains($deployTemplate, '请先选择地区')
|
||||
&& !str_contains($deployTemplate, '正在加载'),
|
||||
'Deployment HTML must immediately show regions, first-region plans, images and saved keys.');
|
||||
$assert(str_contains($deployModule, "readBootstrap('deploy-bootstrap')")
|
||||
&& str_contains($deployModule, "initializeSession('create', bootstrap?.user)")
|
||||
&& str_contains($deployModule, 'openCreate(bootstrap)')
|
||||
&& !str_contains($deployModule, '/api/v1/session')
|
||||
&& !str_contains($deployModule, '/api/v1/plans')
|
||||
&& !str_contains($deployModule, '/api/v1/resources')
|
||||
&& !str_contains($deployModule, '/api/v1/ssh-keys'),
|
||||
'Deployment page initialization must hydrate embedded data without a second data request.');
|
||||
$deployHydrationEnd = strpos($openCreate, ' return;');
|
||||
$deployFallback = strpos($openCreate, 'Promise.allSettled([');
|
||||
$assert($openCreate !== ''
|
||||
&& str_contains($openCreate, 'Array.isArray(bootstrap.plans)')
|
||||
&& str_contains($openCreate, 'Array.isArray(bootstrap.templates)')
|
||||
&& str_contains($openCreate, "bootstrapItems(bootstrap, 'regions')")
|
||||
&& str_contains($openCreate, 'bootstrap.catalog_unavailable === true')
|
||||
&& str_contains($openCreate, 'bootstrap.default_host_id')
|
||||
&& str_contains($openCreate, "(regions[0]?.hostId || '')")
|
||||
&& str_contains($openCreate, "renderTemplateCatalog('vm-template')")
|
||||
&& $deployHydrationEnd !== false && $deployFallback !== false
|
||||
&& $deployHydrationEnd < $deployFallback
|
||||
&& !str_contains(substr($openCreate, 0, $deployHydrationEnd), "api('/api/v1/"),
|
||||
'Deployment hydration must select the requested/first region and paint all data before any fallback.');
|
||||
$assert($selectRegion !== '' && $selectPlan !== ''
|
||||
&& !str_contains($selectRegion, 'resetInstallChoices()')
|
||||
&& !str_contains($selectRegion, 'renderTemplateCatalog(')
|
||||
&& !str_contains($selectRegion, 'loadGlobalTemplates(')
|
||||
&& !str_contains($selectRegion, "byId('vm-template').value")
|
||||
&& !str_contains($selectPlan, 'resetInstallChoices()')
|
||||
&& !str_contains($selectPlan, 'renderTemplateCatalog(')
|
||||
&& !str_contains($selectPlan, 'loadGlobalTemplates(')
|
||||
&& !str_contains($selectPlan, "byId('vm-template').value"),
|
||||
'Changing a region or plan must never clear or reload the global image selection.');
|
||||
|
||||
$assert(str_contains($reinstallController, 'CloudAccess::actor(false)')
|
||||
&& str_contains($reinstallController, 'CloudInstancePageService::detailForActor($actor, $vmId)')
|
||||
&& str_contains($reinstallController, '$vm = $this->reinstallVm($vm)')
|
||||
&& str_contains($reinstallController, 'CloudDeploymentPageService::templateCatalogForPage($actor)')
|
||||
&& str_contains($reinstallController, '$this->sshKeyOptions((int) $actor[\'user_id\'])')
|
||||
&& str_contains($reinstallController, "'catalog_unavailable' => \$catalogUnavailable")
|
||||
&& str_contains($reinstallController,
|
||||
"'cloud_reinstall_catalog_unavailable' => \$catalogUnavailable")
|
||||
&& str_contains($reinstallController, "'cloud_reinstall_bootstrap_json' => \$this->safeJson(\$payload)"),
|
||||
'Reinstall controller must embed an owner-scoped VM plus its image and key choices.');
|
||||
$assert(str_contains($reinstallTemplate, 'id="reinstall-bootstrap" type="application/json"')
|
||||
&& str_contains($reinstallTemplate, '{$cloud_reinstall_bootstrap_json nofilter}')
|
||||
&& str_contains($reinstallTemplate, '$cloud_reinstall_vm.name')
|
||||
&& str_contains($reinstallTemplate, '$cloud_reinstall_vm.public_ip')
|
||||
&& str_contains($reinstallTemplate, '{foreach $cloud_reinstall_template_groups as $group}')
|
||||
&& str_contains($reinstallTemplate, '{foreach $cloud_reinstall_ssh_keys as $key}')
|
||||
&& str_contains($reinstallTemplate, '$cloud_reinstall_catalog_unavailable')
|
||||
&& str_contains($reinstallTemplate, '系统镜像目录暂时无法读取')
|
||||
&& !str_contains($reinstallTemplate, '正在加载'),
|
||||
'Reinstall HTML must immediately show the VM, network, image catalog and saved keys.');
|
||||
$assert(str_contains($reinstallTemplate, '$cloud_reinstall_vm.can_operate')
|
||||
&& str_contains($reinstallTemplate, 'inert aria-disabled="true"')
|
||||
&& str_contains($reinstallTemplate, '当前不能重装系统')
|
||||
&& str_contains($openReinstall, "byId('reinstall-submit').disabled = true"),
|
||||
'A policy-blocked instance must expose reinstall information without an actionable destructive form.');
|
||||
$assert(str_contains($reinstallModule, "readBootstrap('reinstall-bootstrap')")
|
||||
&& str_contains($reinstallModule, "initializeSession('reinstall', bootstrap?.user)")
|
||||
&& str_contains($reinstallModule,
|
||||
'selectVmByApiId(document.body.dataset.vmId || \'\', bootstrap?.vm)')
|
||||
&& str_contains($reinstallModule, 'openReinstall(vm, bootstrap)')
|
||||
&& !str_contains($reinstallModule, '/api/v1/session')
|
||||
&& !str_contains($reinstallModule, '/api/v1/vms')
|
||||
&& !str_contains($reinstallModule, '/api/v1/resources')
|
||||
&& !str_contains($reinstallModule, '/api/v1/ssh-keys'),
|
||||
'Reinstall initialization must use embedded VM/catalog data without initial data APIs.');
|
||||
$reinstallBootstrapStart = strpos($openReinstall,
|
||||
'if (bootstrap && Array.isArray(bootstrap.templates))');
|
||||
$reinstallHydrationEnd = $reinstallBootstrapStart === false ? false
|
||||
: strpos($openReinstall, ' return;', $reinstallBootstrapStart);
|
||||
$reinstallFallback = strpos($openReinstall, 'api(`/api/v1/resources?vm_id=');
|
||||
$assert($openReinstall !== ''
|
||||
&& str_contains($openReinstall, 'Array.isArray(bootstrap.templates)')
|
||||
&& str_contains($openReinstall, 'bootstrap.catalog_unavailable === true')
|
||||
&& str_contains($openReinstall, "bootstrapItems(bootstrap, 'ssh_keys', 'keys')")
|
||||
&& str_contains($openReinstall, "renderTemplateCatalog('reinstall-template')")
|
||||
&& str_contains($openReinstall, "renderKeyCards('reinstall')")
|
||||
&& $reinstallHydrationEnd !== false && $reinstallFallback !== false
|
||||
&& $reinstallHydrationEnd < $reinstallFallback
|
||||
&& !str_contains(substr($openReinstall, 0, $reinstallHydrationEnd), '/api/v1/resources'),
|
||||
'Reinstall hydration must paint embedded choices before its compatibility fallback.');
|
||||
|
||||
$assert(str_contains($controller, 'JSON_HEX_TAG')
|
||||
&& str_contains($controller, "'csrf_token' => AdminCsrf::token()")
|
||||
&& str_contains($controller, "withHeader('Cache-Control', 'private, no-store')"),
|
||||
'Embedded deployment data must remain script-safe, authenticated and non-cacheable.');
|
||||
$assert(preg_match("/venix_asset_version value='[0-9]+'/", $layoutAssets) === 1
|
||||
&& str_contains($cloudShell, "{include file='layout_assets.tpl'}")
|
||||
&& str_contains($cloudShell, '?v={$venix_asset_version}')
|
||||
&& str_contains($cloudShell, 'data-asset-version="{$venix_asset_version|escape:"html"}"')
|
||||
&& str_contains($deployModule,
|
||||
"const assetVersion = new URL(import.meta.url).searchParams.get('v');")
|
||||
&& str_contains($deployModule, "runtimeUrl.searchParams.set('v', assetVersion)")
|
||||
&& str_contains($reinstallModule,
|
||||
"const assetVersion = new URL(import.meta.url).searchParams.get('v');")
|
||||
&& str_contains($reinstallModule, "runtimeUrl.searchParams.set('v', assetVersion)"),
|
||||
'Cloud page and dynamically imported runtime assets must share one cache-busting version.');
|
||||
|
||||
echo "Deployment and reinstall server-rendered first-paint contract passed.\n";
|
||||
Loading…
Reference in New Issue