updates
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
|
import { niceBytes } from "./nice-bytes";
|
||||||
|
|
||||||
|
interface Disk {
|
||||||
|
size: string;
|
||||||
|
type: string;
|
||||||
|
format: string;
|
||||||
|
}
|
||||||
interface BuildNodeFileParams {
|
interface BuildNodeFileParams {
|
||||||
name: string;
|
name: string;
|
||||||
cpuCores: number;
|
cpuCores: number;
|
||||||
@@ -8,40 +15,30 @@ interface BuildNodeFileParams {
|
|||||||
cpuName: string;
|
cpuName: string;
|
||||||
sockets: number;
|
sockets: number;
|
||||||
memory: number;
|
memory: number;
|
||||||
|
disks: Disk[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const buildNodeFile = (
|
export const buildNodeFile = ({
|
||||||
params: BuildNodeFileParams
|
name,
|
||||||
): Promise<boolean> => {
|
cpuCores,
|
||||||
const { name, cpuCores, cpuThreads, cpuName, sockets, memory } = params;
|
cpuThreads,
|
||||||
|
cpuName,
|
||||||
|
sockets,
|
||||||
|
memory,
|
||||||
|
disks,
|
||||||
|
}: BuildNodeFileParams): string => `# Cluster Node - ${name}
|
||||||
|
|
||||||
const content = `# Node: ${name}
|
## Hardware
|
||||||
# CPU: ${cpuCores} cores, ${cpuThreads} threads - ${cpuName}
|
##### CPU
|
||||||
# Sockets: ${sockets}
|
${cpuCores} Cores, ${cpuThreads} Threads - ${cpuName} (${sockets} Sockets)
|
||||||
# Memory: ${memory} MB
|
##### Memory
|
||||||
# Generated by Proxmox API Helper
|
${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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -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];
|
||||||
|
}
|
||||||
@@ -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
@@ -1,5 +1,6 @@
|
|||||||
import proxmoxApi from "proxmox-api";
|
import proxmoxApi from "proxmox-api";
|
||||||
import { buildNodeFile } from "./helpers/build-node-file";
|
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
|
// authorize self signed cert if you do not use a valid SSL certificat
|
||||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
||||||
@@ -25,20 +26,45 @@ async function test() {
|
|||||||
|
|
||||||
if (node.status === "online") {
|
if (node.status === "online") {
|
||||||
const status = await theNode.status.$get();
|
const status = await theNode.status.$get();
|
||||||
console.log(`##### Status: ${JSON.stringify(status)}`);
|
|
||||||
const cpuName = status.cpuinfo.model;
|
const cpuName = status.cpuinfo.model;
|
||||||
const cpuCores = status.cpuinfo.cores;
|
const cpuCores = status.cpuinfo.cores;
|
||||||
const cpuThreads = status.cpuinfo.cpus;
|
const cpuThreads = status.cpuinfo.cpus;
|
||||||
const sockets = status.cpuinfo.sockets;
|
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,
|
name: node.node,
|
||||||
cpuCores,
|
cpuCores,
|
||||||
cpuThreads,
|
cpuThreads,
|
||||||
cpuName,
|
cpuName,
|
||||||
sockets,
|
sockets,
|
||||||
memory: status.memory.total,
|
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}`);
|
console.log(`CPU: ${cpuCores} cores, ${cpuThreads} threads - ${cpuName}`);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user