feat: place instances on verified network attachments

go
codex 3 weeks ago
parent 669ca96226
commit cdd5bbf833

@ -20,6 +20,8 @@ final class CloudDeploymentPageService
*
* 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.
* Sellability is delegated to the exact-pool placement advisory so this
* projection shares its current Agent/network/IP evidence contract.
*/
public static function saleCatalog(): array
{
@ -69,9 +71,10 @@ final class CloudDeploymentPageService
'disk_gib' => (int) $row->disk_gib,
];
}
$capacity = CloudPlacementService::poolCapacityAvailability(
$capacityRequests
);
$placementAvailability =
CloudPlacementService::poolCapacityAvailability(
$capacityRequests
);
$regions = [];
$lines = [];
$offers = [];
@ -120,7 +123,7 @@ final class CloudDeploymentPageService
'traffic_gib' => $trafficGiB,
'traffic_tib' => self::formatDecimalUnit($trafficGiB, 1024, 10),
'sellable' => (int) $row->offer_stock !== 0
&& ($capacity[$offerCapacityKey] ?? false),
&& ($placementAvailability[$offerCapacityKey] ?? false),
];
}

@ -0,0 +1,394 @@
<?php
namespace App\Services\Cloud;
use App\Models\CloudHost;
use App\Models\CloudHostAgent;
use App\Models\CloudHostNetworkAttachment;
use App\Models\CloudPublicIp;
use App\Models\CloudPublicIpPool;
/**
* Exact network admission for new-instance placement.
*
* The caller chooses an exact resource-pool membership and owns the host lock.
* This module resolves only that pool's line into current Agent-verified
* attachments, applied public pools and one concrete IP ledger row.
*/
final class CloudPlacementNetworkService
{
private const MAX_PUBLIC_IPS_PER_POOL = 254;
/**
* Non-locking catalog hint. Payment-time placement repeats the same
* evidence checks under host, attachment, pool and IP row locks.
*
* @return array<int,bool>
*/
public static function advisoryAvailableHosts($candidateRows): array
{
$readyByHostLine = self::advisoryAvailableHostLines($candidateRows);
$available = [];
foreach ($candidateRows as $candidate) {
$hostId = (int) ($candidate->id ?? 0);
$lineId = (int) ($candidate->resource_pool_line_id ?? 0);
if (isset($readyByHostLine[$hostId][$lineId])) {
$available[$hostId] = true;
}
}
return $available;
}
/** @return array<int,array<int,bool>> keyed by host ID then line ID. */
public static function advisoryAvailableHostLines($candidateRows): array
{
$hostLines = [];
foreach ($candidateRows as $candidate) {
$hostId = (int) ($candidate->id ?? 0);
$lineId = (int) ($candidate->resource_pool_line_id ?? 0);
if ($hostId > 0 && $lineId > 0) {
$hostLines[$hostId][$lineId] = true;
}
}
if ($hostLines === []) {
return [];
}
$rows = CloudPublicIp::query()->join(
'cloud_public_ip_pools as public_pool',
'public_pool.id', '=', 'cloud_public_ips.public_ip_pool_id'
)->join(
'cloud_host_network_attachments as network_attachment',
'network_attachment.id', '=', 'public_pool.network_attachment_id'
)->join(
'cloud_host_agents as host_agent',
'host_agent.host_id', '=', 'cloud_public_ips.host_id'
)->whereIn('cloud_public_ips.host_id', array_keys($hostLines))
->where('cloud_public_ips.state', 'available')
->whereNull('cloud_public_ips.reservation_id')
->whereNull('cloud_public_ips.instance_id')
->whereNull('cloud_public_ips.operation_id')
->whereColumn('cloud_public_ips.host_id', 'public_pool.host_id')
->whereColumn(
'cloud_public_ips.prefix_length',
'public_pool.guest_prefix_length'
)
->whereColumn(
'cloud_public_ips.host_id',
'network_attachment.host_id'
)
->where('host_agent.enabled', 1)
->where('host_agent.last_test_status', 'healthy')
->where('host_agent.tested_revision', '>', 0)
->whereColumn('host_agent.tested_revision', 'host_agent.revision')
->where('network_attachment.state', 'active')
->where('network_attachment.verification_state', 'verified')
->where('public_pool.state', 'active')
->where('public_pool.sync_state', 'applied')
->whereColumn(
'public_pool.observed_revision',
'>=',
'public_pool.desired_revision'
)
->get([
'cloud_public_ips.host_id',
'cloud_public_ips.address',
'network_attachment.line_id',
'network_attachment.network_selector',
'network_attachment.verified_agent_revision',
'network_attachment.last_verified_at',
'host_agent.host_id as agent_host_id',
'host_agent.enabled as agent_enabled',
'host_agent.revision as agent_revision',
'host_agent.tested_revision as agent_tested_revision',
'host_agent.last_test_status as agent_last_test_status',
'host_agent.network_selectors_json as agent_network_selectors_json',
'public_pool.pool_key',
'public_pool.cidr',
'public_pool.gateway',
'public_pool.dns_servers_json',
'public_pool.reserved_ips_json',
]);
$available = [];
foreach ($rows as $row) {
$hostId = (int) $row->host_id;
$lineId = (int) $row->line_id;
if (!isset($hostLines[$hostId][$lineId])
|| !self::agentEvidenceReady(
self::agentFromEvidenceRow($row),
(string) $row->network_selector,
(int) $row->verified_agent_revision,
(int) $row->last_verified_at
)
|| preg_match(
'/^[a-f0-9]{32}$/D',
(string) $row->pool_key
) !== 1
|| !self::ipv4BelongsToCidr(
(string) $row->address,
(string) $row->cidr
) || !self::addressAllowedByPool(
(string) $row->address,
$row,
null
)) {
continue;
}
$available[$hostId][$lineId] = true;
}
return $available;
}
/**
* Lock and return every network candidate for one exact host/line.
*
* The host and current Agent row must already be locked. Attachment and
* public-pool locks are always acquired in primary-key order.
*
* @return list<array{attachment:CloudHostNetworkAttachment,
* public_pool:CloudPublicIpPool}>
*/
public static function lockedCandidates(int $hostId, int $lineId,
CloudHostAgent $agent): array
{
if ($hostId < 1 || $lineId < 1 || !$agent->isReady()
|| (int) $agent->host_id !== $hostId) {
return [];
}
$attachments = CloudHostNetworkAttachment::where('host_id', $hostId)
->where('line_id', $lineId)
->where('state', 'active')
->where('verification_state', 'verified')
->orderBy('id')->lockForUpdate()->get();
$eligibleAttachments = [];
foreach ($attachments as $attachment) {
if (self::agentEvidenceReady(
$agent,
(string) $attachment->network_selector,
(int) $attachment->verified_agent_revision,
(int) $attachment->last_verified_at
)) {
$eligibleAttachments[(int) $attachment->id] = $attachment;
}
}
if ($eligibleAttachments === []) {
return [];
}
$pools = CloudPublicIpPool::where('host_id', $hostId)
->whereIn('network_attachment_id', array_keys($eligibleAttachments))
->where('state', 'active')
->where('sync_state', 'applied')
->whereColumn('observed_revision', '>=', 'desired_revision')
->orderBy('id')->lockForUpdate()->get();
$candidates = [];
foreach ($pools as $pool) {
$attachment = $eligibleAttachments[
(int) $pool->network_attachment_id
] ?? null;
if ($attachment === null
|| preg_match(
'/^[a-f0-9]{32}$/D',
(string) $pool->pool_key
) !== 1) {
continue;
}
$candidates[] = [
'attachment' => $attachment,
'public_pool' => $pool,
];
}
usort($candidates, static function (array $left, array $right): int {
return [
(int) $left['attachment']->priority,
(int) $left['public_pool']->priority,
(int) $left['attachment']->id,
(int) $left['public_pool']->id,
] <=> [
(int) $right['attachment']->priority,
(int) $right['public_pool']->priority,
(int) $right['attachment']->id,
(int) $right['public_pool']->id,
];
});
return $candidates;
}
/**
* Lock one available IP after the caller has locked the capacity ledger.
*
* @param list<array{attachment:CloudHostNetworkAttachment,
* public_pool:CloudPublicIpPool}> $candidates
* @return null|array{attachment:CloudHostNetworkAttachment,
* public_pool:CloudPublicIpPool,public_ip:CloudPublicIp}
*/
public static function lockAvailableSelection(int $hostId,
array $candidates): ?array
{
foreach ($candidates as $candidate) {
$attachment = $candidate['attachment'] ?? null;
$pool = $candidate['public_pool'] ?? null;
if (!$attachment instanceof CloudHostNetworkAttachment
|| !$pool instanceof CloudPublicIpPool
|| (int) $attachment->host_id !== $hostId
|| (int) $pool->host_id !== $hostId
|| (int) $pool->network_attachment_id
!== (int) $attachment->id) {
throw new CloudError('公网地址池与网络接入关联不一致', 409);
}
$ips = self::availablePublicIpQuery(
$hostId,
(int) $pool->id,
(int) $pool->guest_prefix_length
)->limit(self::MAX_PUBLIC_IPS_PER_POOL + 1)
->lockForUpdate()->get();
if ($ips->count() > self::MAX_PUBLIC_IPS_PER_POOL) {
continue;
}
foreach ($ips as $ip) {
if (self::publicIpBelongsToPool($ip, $pool)) {
return [
'attachment' => $attachment,
'public_pool' => $pool,
'public_ip' => $ip,
];
}
}
}
return null;
}
private static function agentEvidenceReady(?CloudHostAgent $agent,
string $selector, int $verifiedRevision, int $lastVerifiedAt): bool
{
return $agent !== null
&& $agent->isReady()
&& $verifiedRevision === (int) $agent->revision
&& $verifiedRevision === (int) $agent->tested_revision
&& $lastVerifiedAt > 0
&& in_array(
$selector,
CloudAgentSettingsService::testedNetworkSelectors($agent),
true
);
}
/** Hydrate joined evidence without issuing one Agent query per IP row. */
private static function agentFromEvidenceRow($row): CloudHostAgent
{
$agent = new CloudHostAgent();
$agent->setRawAttributes([
'host_id' => (int) $row->agent_host_id,
'enabled' => (bool) $row->agent_enabled,
'revision' => (int) $row->agent_revision,
'tested_revision' => (int) $row->agent_tested_revision,
'last_test_status' => (string) $row->agent_last_test_status,
'network_selectors_json' =>
(string) $row->agent_network_selectors_json,
], true);
return $agent;
}
private static function availablePublicIpQuery(int $hostId,
int $publicPoolId, int $prefixLength)
{
return CloudPublicIp::where('host_id', $hostId)
->where('public_ip_pool_id', $publicPoolId)
->where('prefix_length', $prefixLength)
->where('state', 'available')
->whereNull('reservation_id')
->whereNull('instance_id')
->whereNull('operation_id')
->orderBy('id');
}
private static function publicIpBelongsToPool(CloudPublicIp $ip,
CloudPublicIpPool $pool): bool
{
return (int) $ip->host_id === (int) $pool->host_id
&& (int) $ip->public_ip_pool_id === (int) $pool->id
&& (int) $ip->prefix_length === (int) $pool->guest_prefix_length
&& self::ipv4BelongsToCidr(
(string) $ip->address,
(string) $pool->cidr
) && self::addressAllowedByPool(
(string) $ip->address,
$pool,
CloudHost::find((int) $pool->host_id)
);
}
private static function addressAllowedByPool(string $address, $pool,
?CloudHost $host): bool
{
$blocked = [(string) $pool->gateway];
$managementAddress = CloudPanelUsageService::managementAddress($host);
if ($managementAddress !== null) {
$blocked[] = $managementAddress;
}
foreach (['dns_servers_json', 'reserved_ips_json'] as $field) {
try {
$values = json_decode(
(string) $pool->{$field},
true,
16,
JSON_THROW_ON_ERROR
);
} catch (\Throwable $error) {
return false;
}
if (!is_array($values) || !array_is_list($values)) {
return false;
}
foreach ($values as $value) {
if (!is_string($value)
|| filter_var(
$value,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4
) === false) {
return false;
}
$blocked[] = $value;
}
}
return !in_array($address, $blocked, true);
}
private static function ipv4BelongsToCidr(string $addressValue,
string $cidr): bool
{
$parts = explode('/', $cidr, 2);
if (count($parts) !== 2 || !ctype_digit($parts[1])) {
return false;
}
$prefix = (int) $parts[1];
$address = self::ipv4Number($addressValue);
$network = self::ipv4Number($parts[0]);
if ($address === null || $network === null
|| $prefix < 24 || $prefix > 30) {
return false;
}
$block = 2 ** (32 - $prefix);
$base = (int) (floor($network / $block) * $block);
return $address > $base && $address < $base + $block - 1;
}
private static function ipv4Number(string $value): ?int
{
if (filter_var(
$value,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4
) === false) {
return null;
}
$packed = inet_pton($value);
if ($packed === false || strlen($packed) !== 4) {
return null;
}
$parts = unpack('Nnumber', $packed);
return is_array($parts) && isset($parts['number'])
? (int) sprintf('%u', $parts['number']) : null;
}
}

@ -1139,7 +1139,6 @@ final class CloudPlacementService
$replica
);
if ($host === null || $membership === null || $policy === null
|| !CloudNetworkSelector::isValid($host->default_network)
|| $agent === null || !$agent->isReady()
|| $storage === null || !$storage->isApplied()
|| (string) $membership->scheduler_state !== 'active'
@ -1151,41 +1150,12 @@ final class CloudPlacementService
return null;
}
$attachments = CloudHostNetworkAttachment::where('host_id', $hostId)
->where('line_id', (int) $pool->line_id)
->where('network_selector', (string) $host->default_network)
->where('state', 'active')
->where('sync_state', 'applied')
->whereColumn('observed_revision', '>=', 'desired_revision')
->orderBy('id')->lockForUpdate()->get();
if ($attachments->isEmpty()) {
return null;
}
$attachmentById = $attachments->keyBy('id');
$publicPools = CloudPublicIpPool::where('host_id', $hostId)
->whereIn('network_attachment_id', $attachments->pluck('id')->all())
->where('state', 'active')
->where('sync_state', 'applied')
->whereColumn('observed_revision', '>=', 'desired_revision')
// Lock pool rows in primary-key order. Selection preference is
// applied in memory only after the complete row set is locked.
->orderBy('id')->lockForUpdate()->get();
$eligiblePools = [];
foreach ($publicPools as $publicPool) {
if (preg_match('/^[a-f0-9]{32}$/D',
(string) $publicPool->pool_key) === 1) {
$eligiblePools[] = $publicPool;
}
}
usort($eligiblePools, static function ($left, $right) use ($attachmentById): int {
$leftAttachment = $attachmentById->get((int) $left->network_attachment_id);
$rightAttachment = $attachmentById->get((int) $right->network_attachment_id);
return ((int) $leftAttachment->priority <=> (int) $rightAttachment->priority)
?: ((int) $left->priority <=> (int) $right->priority)
?: ((int) $leftAttachment->id <=> (int) $rightAttachment->id)
?: ((int) $left->id <=> (int) $right->id);
});
if ($eligiblePools === []) {
$networkCandidates = CloudPlacementNetworkService::lockedCandidates(
$hostId,
(int) $pool->line_id,
$agent
);
if ($networkCandidates === []) {
return null;
}
@ -1204,33 +1174,16 @@ final class CloudPlacementService
return null;
}
$ip = null;
$selectedPool = null;
foreach ($eligiblePools as $publicPool) {
$candidateIps = self::availablePublicIpQuery(
$hostId, (int) $publicPool->id,
(int) $publicPool->guest_prefix_length
)->limit(self::MAX_PUBLIC_IPS_PER_POOL + 1)->lockForUpdate()->get();
if ($candidateIps->count() > self::MAX_PUBLIC_IPS_PER_POOL) {
continue;
}
foreach ($candidateIps as $candidateIp) {
if (self::publicIpBelongsToPool($candidateIp, $publicPool)) {
$ip = $candidateIp;
$selectedPool = $publicPool;
break 2;
}
}
}
if ($ip === null || $selectedPool === null) {
return null;
}
$selectedAttachment = $attachmentById->get(
(int) $selectedPool->network_attachment_id
$network = CloudPlacementNetworkService::lockAvailableSelection(
$hostId,
$networkCandidates
);
if ($selectedAttachment === null) {
throw new CloudError('公网地址池与网络接入关联不一致', 409);
if ($network === null) {
return null;
}
$selectedAttachment = $network['attachment'];
$selectedPool = $network['public_pool'];
$ip = $network['public_ip'];
$operationId = bin2hex(random_bytes(16));
$reservation = new CloudResourceReservation();
$reservation->reservation_key = bin2hex(random_bytes(16));
@ -1436,7 +1389,6 @@ final class CloudPlacementService
->distinct()
->get([
'cloud_hosts.id',
'cloud_hosts.default_network',
'membership_pool.line_id as resource_pool_line_id',
'capacity_policy.configured_cpu_cores',
'capacity_policy.configured_memory_mib',
@ -1445,8 +1397,7 @@ final class CloudPlacementService
'capacity_policy.safety_memory_mib',
'capacity_policy.safety_disk_gib',
'capacity_policy.max_instances',
])->filter(static fn ($host): bool =>
CloudNetworkSelector::isValid($host->default_network));
]);
}
private static function pendingUsageByHost($candidateRows): array
@ -1470,62 +1421,9 @@ final class CloudPlacementService
*/
private static function availableIpHosts($candidateRows): array
{
$hostIds = $candidateRows->pluck('id')->map(
static fn ($id): int => (int) $id
)->unique()->values()->all();
if ($hostIds === []) {
return [];
}
$lineId = (int) ($candidateRows->first()->resource_pool_line_id ?? 0);
if ($lineId < 1) {
return [];
}
// Management, gateway, DNS and other operator-owned addresses are
// removed from the ledger when the pool's reserved_ips is materialized.
// cloud_hosts intentionally has no Agent/SSH host-address column.
$rows = CloudPublicIp::query()->join(
'cloud_public_ip_pools as public_pool',
'public_pool.id', '=', 'cloud_public_ips.public_ip_pool_id'
)->join(
'cloud_host_network_attachments as network_attachment',
'network_attachment.id', '=', 'public_pool.network_attachment_id'
)->join(
'cloud_hosts as eligible_host',
'eligible_host.id', '=', 'cloud_public_ips.host_id'
)->whereIn('cloud_public_ips.host_id', $hostIds)
->where('cloud_public_ips.state', 'available')
->whereNull('cloud_public_ips.reservation_id')
->whereNull('cloud_public_ips.instance_id')
->whereNull('cloud_public_ips.operation_id')
->whereColumn('cloud_public_ips.host_id', 'public_pool.host_id')
->whereColumn('cloud_public_ips.prefix_length',
'public_pool.guest_prefix_length')
->whereColumn('cloud_public_ips.host_id', 'network_attachment.host_id')
->whereColumn('network_attachment.network_selector',
'eligible_host.default_network')
->where('network_attachment.line_id', $lineId)
->where('network_attachment.state', 'active')
->where('network_attachment.sync_state', 'applied')
->whereColumn('network_attachment.observed_revision', '>=',
'network_attachment.desired_revision')
->where('public_pool.state', 'active')
->where('public_pool.sync_state', 'applied')
->whereColumn('public_pool.observed_revision', '>=',
'public_pool.desired_revision')
->distinct()->get([
'cloud_public_ips.host_id', 'cloud_public_ips.address',
'public_pool.pool_key', 'public_pool.cidr',
]);
$available = [];
foreach ($rows as $row) {
if (preg_match('/^[a-f0-9]{32}$/D', (string) $row->pool_key) === 1
&& self::ipv4BelongsToCidr(
(string) $row->address, (string) $row->cidr
)) {
$available[(int) $row->host_id] = true;
}
}
return $available;
return CloudPlacementNetworkService::advisoryAvailableHosts(
$candidateRows
);
}
private static function emptyUsage(): array

@ -111,9 +111,8 @@ final class CloudTemplateAvailabilityService
->where('agent.tested_revision', '>', 0)
->whereColumn('agent.tested_revision', 'agent.revision')
->where('storage.state', 'applied')
->first(['replica.*', 'host.default_network']);
->first(['replica.*']);
return $replica instanceof CloudTemplateReplica
&& CloudNetworkSelector::isValid($replica->default_network)
&& CloudTemplateReplicaCoordinator::isExactReplica(
$template,
$replica
@ -177,12 +176,17 @@ final class CloudTemplateAvailabilityService
->orderBy('pool.id')->orderBy('host.id')->get([
'replica.template_id',
'pool.id as pool_id',
'pool.line_id as resource_pool_line_id',
'region.id as region_id',
'host.default_network',
'host.id',
]);
$readyHostLines =
CloudPlacementNetworkService::advisoryAvailableHostLines($rows);
$result = [];
foreach ($rows as $row) {
if (!CloudNetworkSelector::isValid($row->default_network)) {
if (!($readyHostLines[(int) $row->id][
(int) $row->resource_pool_line_id
] ?? false)) {
continue;
}
$result[(string) $row->template_id][(int) $row->pool_id] =

@ -79,6 +79,12 @@ namespace App\Services\Cloud {
}
return self::$client;
}
public static function testedNetworkSelectors(
\App\Models\CloudHostAgent $setting
): array {
return $setting->testedNetworkSelectors();
}
}
}
namespace {
@ -450,23 +456,28 @@ namespace {
&& ($catalog['offers'][1]['sellable'] ?? null) === false
&& $catalogQueryCount <= 6,
'Catalog availability hid a sold-out offer, misclassified capacity, or queried per offer.');
foreach (['public', 'bridge:br0'] as $hostNetwork) {
$host->default_network = $hostNetwork;
$host->save();
$attachment->network_selector = 'public';
$attachment->save();
$legacyCatalog = CloudDeploymentPageService::saleCatalog();
$assert(($legacyCatalog['offers'][0]['sellable'] ?? null) === false,
'A legacy host network or mismatched attachment remained sellable.');
try {
CloudOrderService::purchase($input, $actor);
throw new RuntimeException('A legacy network created a purchase order.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Legacy network admission returned the wrong status.');
}
}
$host->default_network = 'public';
$host->save();
$catalogWithoutLegacyDefault = CloudDeploymentPageService::saleCatalog();
$assert(($catalogWithoutLegacyDefault['offers'][0]['sellable'] ?? null)
=== true,
'A retired host default network still controlled sale availability.');
$attachment->network_selector = 'public';
$attachment->save();
$unverifiedSelectorCatalog = CloudDeploymentPageService::saleCatalog();
$assert(($unverifiedSelectorCatalog['offers'][0]['sellable'] ?? null)
=== false,
'An attachment outside current Agent evidence remained sellable.');
$attachment->network_selector = 'bridge:br0';
$attachment->verified_agent_revision = 0;
$attachment->save();
$staleEvidenceCatalog = CloudDeploymentPageService::saleCatalog();
$assert(($staleEvidenceCatalog['offers'][0]['sellable'] ?? null) === false,
'A stale attachment verification revision remained sellable.');
$host->default_network = 'bridge:br0';
$host->save();
$attachment->network_selector = 'bridge:br0';
$attachment->verified_agent_revision = 1;
$attachment->save();
try {
CloudOrderService::purchase($input + ['network' => 'bridge:attacker'], $actor);
@ -589,6 +600,29 @@ namespace {
&& CloudJob::count() === 0,
'Failed opening-invoice payment changed invoice, order, or queue state.');
$user = User::find(1); $user->money = '100.00'; $user->save();
$attachment->verified_agent_revision = 0;
$attachment->save();
try {
CloudInvoiceService::payBalance(
$openingInvoice,
$actor,
'provision-stale-network-evidence-0001'
);
throw new RuntimeException(
'Payment admitted stale attachment verification evidence.'
);
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Stale attachment evidence returned the wrong payment status.');
}
$assert(User::find(1)->money === '100.00'
&& CloudInvoice::find($openingInvoice->id)->status === 'open'
&& CloudOrder::find($first->id)->status === 'awaiting_payment'
&& CloudResourceReservation::count() === 0
&& CloudJob::count() === 0,
'Failed network admission retained money, a reservation, or queued work.');
$attachment->verified_agent_revision = 1;
$attachment->save();
// Capacity must remain tied to the immutable order snapshot even if an
// administrator changes the base plan while the invoice is still open.
$changedPlan = Shop::find($plan->id);
@ -609,10 +643,14 @@ namespace {
'reason' => 'Verify queued creates retain deleted catalog rules.',
], $policyManager);
$testRuleKey = (string) $testCatalog['rules'][0]['id'];
$host->default_network = 'public';
$host->save();
$paidOpening = CloudInvoiceService::payBalance(
$openingInvoice, $actor, 'provision-balance-test-0001');
$paidOpeningAgain = CloudInvoiceService::payBalance(
$paidOpening->fresh(), $actor, 'provision-balance-test-0001');
$host->default_network = 'bridge:br0';
$host->save();
$openingOrder = CloudOrder::find($first->id);
$openingReservation = CloudResourceReservation::where('order_id', $first->id)->first();
$openingJob = CloudJob::where('order_id', $first->id)->first();

@ -5,6 +5,9 @@ $controller = file_get_contents($root . '/app/Controllers/CloudAdminInstanceCont
$options = file_get_contents($root . '/app/Services/Cloud/CloudNetworkChangeOptionsService.php');
$jobs = file_get_contents($root . '/app/Services/Cloud/CloudJobService.php');
$placement = file_get_contents($root . '/app/Services/Cloud/CloudPlacementService.php');
$placementNetwork = file_get_contents(
$root . '/app/Services/Cloud/CloudPlacementNetworkService.php'
);
$worker = file_get_contents($root . '/app/Services/Cloud/CloudWorkerService.php');
$input = file_get_contents($root . '/app/Services/Cloud/CloudInput.php');
$adminInstances = file_get_contents($root . '/public/cloud/admin-instances.js')
@ -93,6 +96,7 @@ $assert(is_string($worker)
&& str_contains($worker, '$effectMayHaveApplied'),
'Worker no longer confirms pool identity or settles network changes from one terminal receipt.');
$assert(is_string($placement)
&& is_string($placementNetwork)
&& str_contains($placement, 'cloud_host_resource_pool_memberships as pool_membership')
&& !str_contains($placement, 'eligible_host.host')
&& !str_contains($placement, "->where('cloud_hosts.resource_pool_id', \$poolId)")
@ -112,8 +116,14 @@ $assert(is_string($placement)
&& str_contains($placement, '(string) $reservation->state === \'committed\'')
&& str_contains($placement, '$ip->instance_id ?? 0'),
'Placement no longer uses membership/pool-scoped allocation or enforce atomic multi-pool ledger swaps.');
$assert(str_contains($placement, 'MAX_PUBLIC_IPS_PER_POOL + 1')
&& str_contains($placement, 'foreach ($candidateIps as $candidateIp)'),
$assert(str_contains($placement,
'CloudPlacementNetworkService::lockedCandidates(')
&& str_contains($placement,
'CloudPlacementNetworkService::lockAvailableSelection(')
&& str_contains($placementNetwork, 'MAX_PUBLIC_IPS_PER_POOL + 1')
&& str_contains($placementNetwork, 'foreach ($ips as $ip)')
&& str_contains($placementNetwork, 'addressAllowedByPool(')
&& str_contains($placementNetwork, "['dns_servers_json', 'reserved_ips_json']"),
'A malformed first address can still hide the rest of an otherwise usable pool.');
$assert($finishMethod !== ''
&& !str_contains($finishMethod, '$reservation->updated_at = $now'),

@ -0,0 +1,211 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudHostAgent;
use App\Services\Cloud\CloudPlacementNetworkService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('default_network');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->boolean('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->text('network_selectors_json');
});
$schema->create('cloud_host_network_attachments', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->integer('priority');
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
// Deliberately stale: this is controller intent, not Agent evidence.
$table->string('sync_state')->default('never');
});
$schema->create('cloud_public_ip_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->string('cidr');
$table->string('gateway');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$table->string('state');
$table->integer('priority');
$table->integer('desired_revision');
$table->integer('observed_revision');
$table->string('sync_state');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->string('address');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
});
Capsule::table('cloud_hosts')->insert([
'id' => 1,
'default_network' => 'bridge:retired',
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1,
'enabled' => 1,
'revision' => 4,
'tested_revision' => 4,
'last_test_status' => 'healthy',
'network_selectors_json' => '["bridge:line-a","bridge:line-b"]',
]);
Capsule::table('cloud_host_network_attachments')->insert([
[
'id' => 10, 'host_id' => 1, 'line_id' => 10,
'network_selector' => 'bridge:line-a', 'state' => 'active',
'priority' => 0, 'verification_state' => 'verified',
'verified_agent_revision' => 4, 'last_verified_at' => time(),
'sync_state' => 'never',
],
[
'id' => 20, 'host_id' => 1, 'line_id' => 20,
'network_selector' => 'bridge:line-b', 'state' => 'active',
'priority' => 0, 'verification_state' => 'verified',
'verified_agent_revision' => 4, 'last_verified_at' => time(),
'sync_state' => 'never',
],
]);
Capsule::table('cloud_public_ip_pools')->insert([
[
'id' => 100, 'pool_key' => str_repeat('a', 32), 'host_id' => 1,
'network_attachment_id' => 10, 'cidr' => '203.0.113.0/29',
'gateway' => '203.0.113.1', 'guest_prefix_length' => 29,
'dns_servers_json' => '["203.0.113.2"]',
'reserved_ips_json' => '["203.0.113.3"]', 'state' => 'active',
'priority' => 0, 'desired_revision' => 2, 'observed_revision' => 2,
'sync_state' => 'applied',
],
[
'id' => 200, 'pool_key' => str_repeat('b', 32), 'host_id' => 1,
'network_attachment_id' => 20, 'cidr' => '198.51.100.8/29',
'gateway' => '198.51.100.9', 'guest_prefix_length' => 29,
'dns_servers_json' => '[]', 'reserved_ips_json' => '[]',
'state' => 'active', 'priority' => 0, 'desired_revision' => 1,
'observed_revision' => 1, 'sync_state' => 'applied',
],
]);
Capsule::table('cloud_public_ips')->insert([
['id' => 1, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.1', 'prefix_length' => 29,
'state' => 'available'],
['id' => 2, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.2', 'prefix_length' => 29,
'state' => 'available'],
['id' => 3, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.3', 'prefix_length' => 29,
'state' => 'available'],
['id' => 4, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.4', 'prefix_length' => 29,
'state' => 'available'],
['id' => 5, 'host_id' => 1, 'public_ip_pool_id' => 200,
'address' => '198.51.100.10', 'prefix_length' => 29,
'state' => 'available'],
]);
$candidateRows = collect([
(object) ['id' => 1, 'resource_pool_line_id' => 10],
(object) ['id' => 1, 'resource_pool_line_id' => 20],
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(($ready[1][10] ?? false) && ($ready[1][20] ?? false),
'One physical host lost one of its exact host-line readiness results.');
$assert(CloudPlacementNetworkService::advisoryAvailableHosts(
collect([(object) ['id' => 1, 'resource_pool_line_id' => 20]])
) === [1 => true],
'The exact-pool compatibility view did not preserve host readiness.');
Capsule::table('cloud_host_network_attachments')->where('id', 20)->update([
'verified_agent_revision' => 3,
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(($ready[1][10] ?? false) && !isset($ready[1][20]),
'A stale line verification affected another line or remained ready.');
Capsule::table('cloud_host_network_attachments')->where('id', 20)->update([
'verified_agent_revision' => 4,
]);
Capsule::table('cloud_host_network_attachments')->where('id', 10)->update([
'last_verified_at' => 0,
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(!isset($ready[1][10]) && ($ready[1][20] ?? false),
'Missing verification time remained ready or poisoned another line.');
Capsule::table('cloud_host_network_attachments')->where('id', 10)->update([
'last_verified_at' => time(),
]);
$selection = Capsule::connection('default')->transaction(static function () {
$agent = CloudHostAgent::find(1);
$candidates = CloudPlacementNetworkService::lockedCandidates(
1,
10,
$agent
);
return CloudPlacementNetworkService::lockAvailableSelection(
1,
$candidates
);
});
$assert($selection !== null
&& (int) $selection['attachment']->id === 10
&& (int) $selection['public_pool']->id === 100
&& (string) $selection['public_ip']->address === '203.0.113.4',
'Payment-time selection used the retired default or admitted a gateway, DNS, or reserved address.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'network_selectors_json' => '["bridge:line-a"]',
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(($ready[1][10] ?? false) && !isset($ready[1][20]),
'A selector absent from current Agent test evidence remained ready.');
echo "Placement network exact host-line contract passed.\n";

@ -114,6 +114,7 @@ $capsule->schema('default')->create('cloud_host_agents',
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->text('network_selectors_json');
});
$capsule->schema('default')->create('cloud_host_storage_configurations',
static function (Blueprint $table): void {
@ -137,6 +138,45 @@ $capsule->schema('default')->create('cloud_host_capacity_policies',
$table->integer('configured_memory_mib');
$table->integer('configured_disk_gib');
});
$capsule->schema('default')->create('cloud_host_network_attachments',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
});
$capsule->schema('default')->create('cloud_public_ip_pools',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->string('cidr');
$table->string('gateway');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$table->string('state');
$table->string('sync_state');
$table->integer('desired_revision');
$table->integer('observed_revision');
});
$capsule->schema('default')->create('cloud_public_ips',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->string('address');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
});
$capsule->schema('default')->create('cloud_offers',
static function (Blueprint $table): void {
$table->increments('id');
@ -163,6 +203,7 @@ Capsule::table('cloud_hosts')->insert([
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1, 'enabled' => 1, 'revision' => 1,
'tested_revision' => 1, 'last_test_status' => 'healthy',
'network_selectors_json' => '["bridge:br-public"]',
]);
Capsule::table('cloud_host_storage_configurations')->insert([
'host_id' => 1, 'state' => 'applied',
@ -173,6 +214,7 @@ Capsule::table('cloud_hosts')->insert([
Capsule::table('cloud_host_agents')->insert([
'host_id' => 2, 'enabled' => 1, 'revision' => 1,
'tested_revision' => 0, 'last_test_status' => 'failed',
'network_selectors_json' => '[]',
]);
Capsule::table('cloud_host_storage_configurations')->insert([
'host_id' => 2, 'state' => 'applied',
@ -184,6 +226,27 @@ Capsule::table('cloud_host_capacity_policies')->insert([
'host_id' => 1, 'configured_cpu_cores' => 32,
'configured_memory_mib' => 65536, 'configured_disk_gib' => 1024,
]);
Capsule::table('cloud_host_network_attachments')->insert([
'id' => 1, 'host_id' => 1, 'line_id' => 1,
'network_selector' => 'bridge:br-public', 'state' => 'active',
'verification_state' => 'verified', 'verified_agent_revision' => 1,
'last_verified_at' => time(),
]);
Capsule::table('cloud_public_ip_pools')->insert([
'id' => 1, 'pool_key' => str_repeat('1', 32), 'host_id' => 1,
'network_attachment_id' => 1, 'cidr' => '203.0.113.0/29',
'gateway' => '203.0.113.1',
'guest_prefix_length' => 29, 'state' => 'active',
'dns_servers_json' => '[]', 'reserved_ips_json' => '[]',
'sync_state' => 'applied', 'desired_revision' => 1,
'observed_revision' => 1,
]);
Capsule::table('cloud_public_ips')->insert([
'host_id' => 1, 'public_ip_pool_id' => 1,
'address' => '203.0.113.2', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
]);
Capsule::table('cloud_offers')->insert([
'id' => 1, 'plan_id' => 1, 'resource_pool_id' => 1,
'status' => 1, 'currency' => 'USD',

@ -60,7 +60,6 @@ $schema->create('cloud_resource_pools', static function (Blueprint $table): void
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('enabled');
$table->string('default_network');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
@ -68,6 +67,7 @@ $schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->text('network_selectors_json');
});
$schema->create('cloud_host_storage_configurations', static function (
Blueprint $table
@ -92,6 +92,46 @@ $schema->create('cloud_host_capacity_policies', static function (
$table->integer('configured_memory_mib');
$table->integer('configured_disk_gib');
});
$schema->create('cloud_host_network_attachments', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
});
$schema->create('cloud_public_ip_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->string('cidr');
$table->string('gateway');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$table->string('state');
$table->string('sync_state');
$table->integer('desired_revision');
$table->integer('observed_revision');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->string('address');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
});
$schema->create('cloud_offers', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('plan_id');
@ -155,11 +195,13 @@ foreach ([1, 2] as $id) {
]);
Capsule::table('cloud_hosts')->insert([
'id' => $id, 'enabled' => 1,
'default_network' => 'bridge:br' . ($id - 1),
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => $id, 'enabled' => 1, 'revision' => 1,
'tested_revision' => 1, 'last_test_status' => 'healthy',
'network_selectors_json' => json_encode([
'bridge:public-' . $id,
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_host_storage_configurations')->insert([
'host_id' => $id, 'state' => 'applied',
@ -172,6 +214,30 @@ foreach ([1, 2] as $id) {
'host_id' => $id, 'configured_cpu_cores' => 16,
'configured_memory_mib' => 32768, 'configured_disk_gib' => 512,
]);
$attachmentId = 100 + $id;
Capsule::table('cloud_host_network_attachments')->insert([
'id' => $attachmentId, 'host_id' => $id, 'line_id' => $id,
'network_selector' => 'bridge:public-' . $id,
'state' => 'active', 'verification_state' => 'verified',
'verified_agent_revision' => 1, 'last_verified_at' => time(),
]);
$publicPoolId = 200 + $id;
Capsule::table('cloud_public_ip_pools')->insert([
'id' => $publicPoolId, 'pool_key' => str_repeat((string) $id, 32),
'host_id' => $id, 'network_attachment_id' => $attachmentId,
'cidr' => '203.0.11' . $id . '.0/29',
'gateway' => '203.0.11' . $id . '.1',
'guest_prefix_length' => 29, 'state' => 'active',
'dns_servers_json' => '[]', 'reserved_ips_json' => '[]',
'sync_state' => 'applied', 'desired_revision' => 1,
'observed_revision' => 1,
]);
Capsule::table('cloud_public_ips')->insert([
'host_id' => $id, 'public_ip_pool_id' => $publicPoolId,
'address' => '203.0.11' . $id . '.2', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
]);
Capsule::table('cloud_offers')->insert([
'id' => 10 + $id, 'plan_id' => $id, 'resource_pool_id' => $id,
'status' => 1, 'currency' => 'USD',
@ -255,6 +321,54 @@ $assert($public[0]['offer_ids'] === [11, 12]
=== 1,
'A newly exact replica did not expose its offer, region and reinstall host.');
Capsule::table('cloud_host_network_attachments')->where('host_id', 2)->update([
'verified_agent_revision' => 0,
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& $public[0]['region_ids'] === [1]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'Stale network evidence affected reinstall or remained deployable.');
Capsule::table('cloud_host_network_attachments')->where('host_id', 2)->update([
'verified_agent_revision' => 1,
]);
Capsule::table('cloud_public_ip_pools')->where('host_id', 2)->update([
'sync_state' => 'failed',
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'An unapplied public pool remained deployable or blocked reinstall.');
Capsule::table('cloud_public_ip_pools')->where('host_id', 2)->update([
'sync_state' => 'applied',
]);
Capsule::table('cloud_public_ips')->where('host_id', 2)->update([
'state' => 'reserved',
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'A host without an available IP remained deployable or blocked reinstall.');
Capsule::table('cloud_public_ips')->where('host_id', 2)->update([
'state' => 'available',
]);
Capsule::table('cloud_host_resource_pool_memberships')
->where('host_id', 2)->where('resource_pool_id', 2)->update([
'scheduler_state' => 'disabled',
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'An inactive exact membership remained deployable or blocked reinstall.');
Capsule::table('cloud_host_resource_pool_memberships')
->where('host_id', 2)->where('resource_pool_id', 2)->update([
'scheduler_state' => 'active',
]);
Capsule::table('cloud_host_agents')->where('host_id', 2)->update([
'last_test_status' => 'unhealthy',
]);

Loading…
Cancel
Save