This commit is contained in:
Lino Silva
2025-06-12 12:08:43 +01:00
parent b40044b5b2
commit a813db4ac1
4 changed files with 91 additions and 35 deletions
+30 -33
View File
@@ -1,6 +1,13 @@
import fs from "fs";
import path from "path";
import { niceBytes } from "./nice-bytes";
interface Disk {
size: string;
type: string;
format: string;
}
interface BuildNodeFileParams {
name: string;
cpuCores: number;
@@ -8,40 +15,30 @@ interface BuildNodeFileParams {
cpuName: string;
sockets: number;
memory: number;
disks: Disk[];
}
export const buildNodeFile = (
params: BuildNodeFileParams
): Promise<boolean> => {
const { name, cpuCores, cpuThreads, cpuName, sockets, memory } = params;
export const buildNodeFile = ({
name,
cpuCores,
cpuThreads,
cpuName,
sockets,
memory,
disks,
}: BuildNodeFileParams): string => `# Cluster Node - ${name}
const content = `# Node: ${name}
# CPU: ${cpuCores} cores, ${cpuThreads} threads - ${cpuName}
# Sockets: ${sockets}
# Memory: ${memory} MB
# Generated by Proxmox API Helper
## Hardware
##### CPU
${cpuCores} Cores, ${cpuThreads} Threads - ${cpuName} (${sockets} Sockets)
##### Memory
${niceBytes(memory)}
##### Disk
| Size | Type | Format |
| ---- | ---- | ------ |
${disks
.map((disk) => `| ${disk.size} | ${disk.type} | ${disk.format} |`)
.join("\n")}
#### Generated by Proxmox API Helper
`;
const filePath = path.join(
__dirname,
"../../",
"build",
`${name}.nodeinfo.md`
);
return new Promise((resolve, reject) => {
fs.writeFile(
filePath,
content,
{ flag: "wx", encoding: "utf8" },
(err: NodeJS.ErrnoException | null) => {
if (err) {
console.error(`Error writing file ${filePath}:`, err);
reject(err);
} else {
console.info(`File ${filePath} created successfully.`);
resolve(true);
}
}
);
});
};
+11
View File
@@ -0,0 +1,11 @@
const units = ["bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
export function niceBytes(n: number): string {
let l = 0;
while (n >= 1024 && ++l) {
n = n / 1024;
}
return n.toFixed(n < 10 && l > 0 ? 1 : 0) + " " + units[l];
}
+22
View File
@@ -0,0 +1,22 @@
import fs from "fs";
import path from "path";
export const writeFile = (content, fileName): Promise<boolean> => {
const filePath = path.join(__dirname, "../../", "build", fileName);
return new Promise((resolve, reject) => {
fs.writeFile(
filePath,
content,
{ flag: "w", encoding: "utf8" },
(err: NodeJS.ErrnoException | null) => {
if (err) {
console.error(`Error writing file ${filePath}:`, err);
reject(err);
} else {
console.info(`File ${filePath} created successfully.`);
resolve(true);
}
}
);
});
};
+28 -2
View File
@@ -1,5 +1,6 @@
import proxmoxApi from "proxmox-api";
import { buildNodeFile } from "./helpers/build-node-file";
import { writeFile } from "./helpers/write-file";
// authorize self signed cert if you do not use a valid SSL certificat
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
@@ -25,20 +26,45 @@ async function test() {
if (node.status === "online") {
const status = await theNode.status.$get();
console.log(`##### Status: ${JSON.stringify(status)}`);
const cpuName = status.cpuinfo.model;
const cpuCores = status.cpuinfo.cores;
const cpuThreads = status.cpuinfo.cpus;
const sockets = status.cpuinfo.sockets;
await buildNodeFile({
const diskTypes = await theNode.disks.$get();
const disks = diskTypes.map((diskType) => {
return theNode.disks[diskType.name]
.$get()
.then((diskList) => {
if (!diskList) return [];
return diskList.map((disk) => ({
size: disk.size,
type: disk.type,
format: disk.format,
}));
})
.catch((error) => {
console.error(`Error fetching disk type ${diskType.name}:`, error);
return [];
});
});
console.log(`##### Directory disks: ${JSON.stringify(disks)}`);
const nodeFile = buildNodeFile({
name: node.node,
cpuCores,
cpuThreads,
cpuName,
sockets,
memory: status.memory.total,
disks: disks.map((disk) => ({
size: disk.size,
type: disk.type,
format: disk.format,
})),
});
await writeFile(nodeFile, `${node.node}.nodeinfo.md`);
console.log(`CPU: ${cpuCores} cores, ${cpuThreads} threads - ${cpuName}`);