Repository navigation
Expand file tree
/
Copy pathServerEnvironmentMachine.ts
More file actions
157 lines (145 loc) · 4.87 KB
/
Copy pathServerEnvironmentMachine.ts
File metadata and controls
157 lines (145 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type { EnvironmentMachineKind } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as ProcessRunner from "../processRunner.ts";
const DMI_ROOT = "/sys/class/dmi/id";
const KERNEL_RELEASE_PATH = "/proc/sys/kernel/osrelease";
// SMBIOS 3.x System Enclosure types (table 17).
const DMI_CHASSIS_KINDS: Readonly<Record<string, EnvironmentMachineKind>> = {
"3": "desktop",
"4": "desktop",
"5": "desktop",
"6": "desktop",
"7": "desktop",
"8": "laptop",
"9": "laptop",
"10": "laptop",
"13": "desktop",
"14": "laptop",
"15": "desktop",
"16": "desktop",
"17": "server",
"18": "server",
"19": "server",
"20": "server",
"21": "server",
"22": "server",
"23": "server",
"24": "server",
"28": "server",
"31": "laptop",
"32": "laptop",
"35": "desktop",
};
// VMs read as cloud regardless of chassis. Match Hyper-V's product, since Surface shares its vendor.
const VIRTUALIZATION_MARKERS = [
"qemu",
"kvm",
"bochs",
"vmware",
"virtualbox",
"innotek",
"xen",
"parallels",
"amazon ec2",
"google compute engine",
"digitalocean",
"hetzner",
"linode",
"vultr",
"scaleway",
"openstack",
"cloud",
"virtual machine",
];
function normalize(value: string | null | undefined): string | null {
const trimmed = value?.trim();
return trimmed && trimmed.length > 0 ? trimmed : null;
}
function machineKindFromAppleProductName(name: string): EnvironmentMachineKind | null {
const normalized = name.trim().toLowerCase().replaceAll(/\s+/g, "");
if (normalized.startsWith("macmini")) return "mac-mini";
if (normalized.startsWith("macstudio")) return "mac-studio";
if (normalized.startsWith("macbook")) return "laptop";
if (normalized.startsWith("imac") || normalized.startsWith("macpro")) return "desktop";
return null;
}
function machineKindFromDmi(input: {
readonly chassisType: string | null;
readonly sysVendor: string | null;
readonly productName: string | null;
}): EnvironmentMachineKind | null {
const productName = input.productName ?? "";
const vendorAndProduct = `${input.sysVendor ?? ""} ${productName}`.toLowerCase();
if (VIRTUALIZATION_MARKERS.some((marker) => vendorAndProduct.includes(marker))) {
return "cloud";
}
const appleKind = machineKindFromAppleProductName(productName);
if (appleKind !== null) {
return appleKind;
}
return input.chassisType === null ? null : (DMI_CHASSIS_KINDS[input.chassisType] ?? null);
}
const readOptionalFile = Effect.fn("readOptionalFile")(function* (path: string) {
const fileSystem = yield* FileSystem.FileSystem;
return yield* fileSystem.readFileString(path).pipe(
Effect.map(normalize),
Effect.orElseSucceed(() => null),
);
});
const runProbe = Effect.fn("runMachineProbe")(function* (input: {
readonly command: string;
readonly args: ReadonlyArray<string>;
}) {
const processRunner = yield* ProcessRunner.ProcessRunner;
return yield* processRunner
.run({
command: input.command,
args: input.args,
timeout: "5 seconds",
timeoutBehavior: "timedOutResult",
})
.pipe(
Effect.map((result) => (result.code === 0 ? normalize(result.stdout) : null)),
Effect.orElseSucceed(() => null),
);
});
// Apple silicon exposes IOKit's marketing name; Intel needs the hw.model fallback.
const detectDarwinMachineKind = Effect.fn("detectDarwinMachineKind")(function* () {
const ioreg = yield* runProbe({ command: "ioreg", args: ["-rd1", "-n", "product"] });
const productName = ioreg?.match(/"product-name"\s*=\s*<"([^"]+)">/)?.[1] ?? null;
const fromProductName =
productName === null ? null : machineKindFromAppleProductName(productName);
if (fromProductName !== null) {
return fromProductName;
}
const model = yield* runProbe({ command: "sysctl", args: ["-n", "hw.model"] });
return model === null ? null : machineKindFromAppleProductName(model);
});
const detectLinuxMachineKind = Effect.fn("detectLinuxMachineKind")(function* () {
const [kernelRelease, chassisType, sysVendor, productName] = yield* Effect.all([
readOptionalFile(KERNEL_RELEASE_PATH),
readOptionalFile(`${DMI_ROOT}/chassis_type`),
readOptionalFile(`${DMI_ROOT}/sys_vendor`),
readOptionalFile(`${DMI_ROOT}/product_name`),
]);
// Check WSL first; WSL 2 presents as a Hyper-V VM in DMI.
if (kernelRelease?.toLowerCase().includes("microsoft")) {
return "linux";
}
return machineKindFromDmi({ chassisType, sysVendor, productName });
});
export const detectServerEnvironmentMachineKind = Effect.fn("detectServerEnvironmentMachineKind")(
function* () {
const platform = yield* HostProcess.Platform;
switch (platform) {
case "darwin":
return yield* detectDarwinMachineKind();
case "linux":
return yield* detectLinuxMachineKind();
default:
return null;
}
},
);