fix: harden M3 simulator identities and wiring

This commit is contained in:
Code Queue Review
2026-05-23 04:30:01 +00:00
parent 82b1f9491e
commit c095d4d943
14 changed files with 416 additions and 18 deletions
+13 -3
View File
@@ -8,12 +8,22 @@ import {
import { createHealth, createBoxState, selectInstanceValue } from "../../internal/sim/model.mjs";
import { createJsonServer, listen, parsePort, readJson } from "../../internal/sim/http.mjs";
const boxId = selectInstanceValue(process.env.HWLAB_BOX_ID, "boxsim_alpha");
const boxId = selectInstanceValue(process.env.HWLAB_BOX_ID, "boxsim_alpha", {
requireIndexForMultiple: true,
label: "HWLAB_BOX_ID"
});
const gatewaySessionId = selectInstanceValue(
process.env.HWLAB_GATEWAY_SESSION_ID,
"gws_mvp_simu"
"gws_mvp_simu",
{
requireIndexForMultiple: true,
label: "HWLAB_GATEWAY_SESSION_ID"
}
);
const resourceId = selectInstanceValue(process.env.HWLAB_BOX_RESOURCE_ID, "");
const resourceId = selectInstanceValue(process.env.HWLAB_BOX_RESOURCE_ID, "", {
requireIndexForMultiple: true,
label: "HWLAB_BOX_RESOURCE_ID"
});
const port = parsePort(process.env.PORT, 7201);
const state = createBoxState({ boxId, gatewaySessionId });
if (resourceId) {
+12 -6
View File
@@ -9,7 +9,10 @@ import {
import { fetchJson, createJsonServer, listen, parsePort, readJson } from "../../internal/sim/http.mjs";
import { createRpcErrorBody, ERROR_CODES } from "../../internal/protocol/index.mjs";
const gatewayId = selectInstanceValue(process.env.HWLAB_GATEWAY_ID, "gateway-a");
const gatewayId = selectInstanceValue(process.env.HWLAB_GATEWAY_ID, "gateway-a", {
requireIndexForMultiple: true,
label: "HWLAB_GATEWAY_ID"
});
const port = parsePort(process.env.PORT, 7101);
const endpoint = `http://127.0.0.1:${port}`;
const boxUrls = parseBoxUrls(process.env.HWLAB_BOX_URLS);
@@ -117,9 +120,9 @@ function parseList(value) {
}
function parseBoxes({ resources, ids, urls, instanceScoped = false }) {
const explicitResources = instanceScoped ? selectScopedList(resources) : parseList(resources);
const explicitIds = instanceScoped ? selectScopedList(ids) : parseList(ids);
const explicitUrls = instanceScoped ? selectScopedList(urls.join(",")) : urls;
const explicitResources = instanceScoped ? selectScopedList(resources, "HWLAB_BOX_RESOURCES") : parseList(resources);
const explicitIds = instanceScoped ? selectScopedList(ids, "HWLAB_BOX_IDS") : parseList(ids);
const explicitUrls = instanceScoped ? selectScopedList(urls.join(","), "HWLAB_BOX_URLS") : urls;
const count = Math.max(explicitResources.length, explicitIds.length, explicitUrls.length, 1);
const parsed = [];
@@ -140,12 +143,15 @@ function parseBoxes({ resources, ids, urls, instanceScoped = false }) {
return parsed;
}
function selectScopedList(value) {
function selectScopedList(value, label = "scoped list") {
const items = parseList(value);
if (items.length <= 1) {
return items;
}
const selected = selectInstanceValue(items.join(","), "");
const selected = selectInstanceValue(items.join(","), "", {
requireIndexForMultiple: true,
label
});
return selected ? [selected] : [];
}
+12 -2
View File
@@ -13,12 +13,13 @@ const topology = createM3Topology();
const endpointMap = parseEndpointMap(process.env.HWLAB_PATCH_PANEL_ENDPOINT_MAP) ?? {
res_boxsimu_2: "http://hwlab-box-simu-2.hwlab-dev.svc.cluster.local:7201"
};
const manifestWiringConfig = parseWiringConfig(process.env.HWLAB_PATCH_PANEL_WIRING_CONFIG);
const runtime = wiringConfigPath
? await createPatchPanelRuntimeFromFile({ path: wiringConfigPath })
: createPatchPanelRuntime({
wiringConfig: topology.wiringConfig,
wiringConfig: manifestWiringConfig ?? topology.wiringConfig,
endpointMap,
configSource: "internal:m3-topology"
configSource: manifestWiringConfig ? "env:HWLAB_PATCH_PANEL_WIRING_CONFIG" : "internal:m3-topology"
});
const routes = new Map();
@@ -51,3 +52,12 @@ function parseEndpointMap(value) {
return JSON.parse(source);
}
function parseWiringConfig(value) {
const source = String(value ?? "").trim();
if (!source) {
return null;
}
return JSON.parse(source);
}