Skip to content

Commit

Permalink
feat(mcl/host_info): Upload results to Coda
Browse files Browse the repository at this point in the history
  • Loading branch information
monyarm committed Jun 20, 2024
1 parent 5c544d6 commit 60b756f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
97 changes: 93 additions & 4 deletions packages/mcl/src/src/mcl/commands/host_info.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import std.stdio : writeln;
import std.conv : to;
import std.string : strip, indexOf, isNumeric;
import std.array : split, join, array, replace;
import std.algorithm : map, filter, startsWith, joiner, any, sum;
import std.algorithm : map, filter, startsWith, joiner, any, sum, find;
import std.file : exists, write, readText, readLink, dirEntries, SpanMode;
import std.path : baseName;
import std.json;
Expand All @@ -22,6 +22,7 @@ import mcl.utils.process : execute, isRoot;
import mcl.utils.number : humanReadableSize;
import mcl.utils.array : uniqIfSame;
import mcl.utils.nix : Literal;
import mcl.utils.coda;

// enum InfoFormat
// {
Expand All @@ -34,10 +35,12 @@ struct Params
{
// @optional()
// InfoFormat format = InfoFormat.JSON;
@optional() string codaApiToken;
void setup()
{
}
}
Params params;

string[string] cpuinfo;

Expand All @@ -64,7 +67,7 @@ string[string] getProcInfo(string fileOrData, bool file = true)

export void host_info()
{
const params = parseEnv!Params;
params = parseEnv!Params;

Info info = getInfo();

Expand All @@ -79,6 +82,8 @@ Info getInfo()
meminfo = getProcInfo("/proc/meminfo");

Info info;
info.softwareInfo.hostid = execute("hostid", false);
info.softwareInfo.hostname = execute("cat /etc/hostname", false);
info.softwareInfo.operatingSystemInfo = getOperatingSystemInfo();
info.softwareInfo.opensshInfo = getOpenSSHInfo();
info.softwareInfo.machineConfigInfo = getMachineConfigInfo();
Expand All @@ -90,9 +95,91 @@ Info getInfo()
info.hardwareInfo.displayInfo = getDisplayInfo();
info.hardwareInfo.graphicsProcessorInfo = getGraphicsProcessorInfo();

if (params.codaApiToken) {
auto docId = "0rz18jyJ1M";
auto hostTableId = "grid-b3MAjem325";
auto cpuTableId = "grid-mCI3x3nEIE";
auto memoryTableId = "grid-o7o2PeB4rz";
auto motherboardTableId = "grid-270PlzmA8K";
auto gpuTableId = "grid-ho6EPztvni";
auto storageTableId = "grid-JvXFbttMNz";
auto osTableId = "grid-ora7n98-ls";
auto coda = CodaApiClient(params.codaApiToken);

auto hostValues = RowValues([
CodaCell("Host Name", info.softwareInfo.hostname),
CodaCell("Host ID", info.softwareInfo.hostid),
CodaCell("OpenSSH Public Key", info.softwareInfo.opensshInfo.publicKey),
CodaCell("JSON", info.toJSON(true).toPrettyString(JSONOptions.doNotEscapeSlashes))
]);

coda.updateOrInsertRow(docId, hostTableId, hostValues);

auto cpuValues = RowValues([
CodaCell("Host Name", info.softwareInfo.hostname),
CodaCell("Vendor", info.hardwareInfo.processorInfo.vendor),
CodaCell("Model", info.hardwareInfo.processorInfo.model),
CodaCell("Architecture", info.hardwareInfo.processorInfo.architectureInfo.architecture),
CodaCell("Flags", info.hardwareInfo.processorInfo.architectureInfo.flags),
]);
coda.updateOrInsertRow(docId, cpuTableId, cpuValues);

auto memoryValues = RowValues([
CodaCell("Host Name", info.softwareInfo.hostname),
CodaCell("Vendor", info.hardwareInfo.memoryInfo.vendor),
CodaCell("Part Number", info.hardwareInfo.memoryInfo.partNumber),
CodaCell("Serial", info.hardwareInfo.memoryInfo.serial),
CodaCell("Generation", info.hardwareInfo.memoryInfo.type),
CodaCell("Slots", info.hardwareInfo.memoryInfo.slots == 0 ? "Soldered" : info.hardwareInfo.memoryInfo.count.to!string ~ "/" ~ info.hardwareInfo.memoryInfo.slots.to!string),
CodaCell("Total", info.hardwareInfo.memoryInfo.total),
CodaCell("Speed", info.hardwareInfo.memoryInfo.speed),
]);
coda.updateOrInsertRow(docId, memoryTableId, memoryValues);

auto motherboardValues = RowValues([
CodaCell("Host Name", info.softwareInfo.hostname),
CodaCell("Vendor", info.hardwareInfo.motherboardInfo.vendor),
CodaCell("Model", info.hardwareInfo.motherboardInfo.model),
CodaCell("Revision", info.hardwareInfo.motherboardInfo.version_),
CodaCell("Serial", info.hardwareInfo.motherboardInfo.serial),
CodaCell("BIOS Vendor", info.hardwareInfo.motherboardInfo.biosInfo.vendor),
CodaCell("BIOS Version", info.hardwareInfo.motherboardInfo.biosInfo.version_),
CodaCell("BIOS Release", info.hardwareInfo.motherboardInfo.biosInfo.release),
CodaCell("BIOS Date", info.hardwareInfo.motherboardInfo.biosInfo.date)
]);
coda.updateOrInsertRow(docId, motherboardTableId, motherboardValues);

auto gpuValues = RowValues([
CodaCell("Host Name", info.softwareInfo.hostname),
CodaCell("Vendor", info.hardwareInfo.graphicsProcessorInfo.vendor),
CodaCell("Model", info.hardwareInfo.graphicsProcessorInfo.model),
CodaCell("VRam", info.hardwareInfo.graphicsProcessorInfo.vram)
]);
coda.updateOrInsertRow(docId, gpuTableId, gpuValues);

auto osValues = RowValues([
CodaCell("Host Name", info.softwareInfo.hostname),
CodaCell("Distribution", info.softwareInfo.operatingSystemInfo.distribution),
CodaCell("Distribution Version", info.softwareInfo.operatingSystemInfo.distributionVersion),
CodaCell("Kernel", info.softwareInfo.operatingSystemInfo.kernel),
CodaCell("Kernel Version", info.softwareInfo.operatingSystemInfo.kernelVersion)
]);
coda.updateOrInsertRow(docId, osTableId, osValues);

auto storageValues = RowValues([
CodaCell("Host Name", info.softwareInfo.hostname),
CodaCell("Count", info.hardwareInfo.storageInfo.devices.length.to!string),
CodaCell("Total", info.hardwareInfo.storageInfo.total),
CodaCell("JSON", info.hardwareInfo.storageInfo.toJSON(true).toPrettyString(JSONOptions.doNotEscapeSlashes))
]);
coda.updateOrInsertRow(docId, storageTableId, storageValues);
}

return info;
}



struct Info
{
SoftwareInfo softwareInfo;
Expand All @@ -101,6 +188,8 @@ struct Info

struct SoftwareInfo
{
string hostname;
string hostid;
OperatingSystemInfo operatingSystemInfo;
OpenSSHInfo opensshInfo;
MachineConfigInfo machineConfigInfo;
Expand Down Expand Up @@ -308,7 +397,7 @@ string getDistribution()
if (exists("/etc/os-release"))
{
foreach (line; execute([
"awk", "-F", "=", "/^NAME=/ {print $2}", "/etc/os-release"
"awk", "-F", "=", "'/^NAME=/ {print $2}'", "/etc/os-release"
], false).split("\n"))
{
distribution = line;
Expand All @@ -331,7 +420,7 @@ string getDistributionVersion()
if (exists("/etc/os-release"))
{
foreach (line; execute([
"awk", "-F", "=", "/^VERSION=/ {print $2}", "/etc/os-release"
"awk", "-F", "=", "'/^VERSION=/ {print $2}'", "/etc/os-release"
], false).split("\n"))
{
distributionVersion = line.strip("\"");
Expand Down
12 changes: 11 additions & 1 deletion packages/mcl/src/src/mcl/utils/coda.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import std.traits : isArray;
import mcl.utils.json : toJSON, fromJSON;
import std.process : environment;
import std.stdio : writeln, writefln;
import std.algorithm : map, filter;
import std.algorithm : map, filter, find;
import std.exception : assertThrown;
import std.sumtype : SumType;
import core.thread;
Expand Down Expand Up @@ -527,6 +527,16 @@ struct CodaApiClient
coda.deleteRow("dEJJPwdxcw", tables[0].id, resp[0]);
}

void updateOrInsertRow(string docId, string tableId, RowValues values) {
auto table = listRows(docId, tableId);
auto rows = find!(row => row.name == values.cells[0].value)(table);
if (rows.length > 0) {
updateRow(docId, tableId, rows[0].id, values);
}
else {
insertRows(docId, tableId, [values]);
}
}
struct PushButtonResponse {
string requestId;
string rowId;
Expand Down
1 change: 1 addition & 0 deletions packages/mcl/src/src/mcl/utils/process.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ T execute(T = string)(string[] args, bool printCommand = true, bool returnErr =
import std.logger : logf, LogLevel;
import std.array : join;
import std.conv : to;
import std.stdio : writeln;

if (printCommand)
{
Expand Down

0 comments on commit 60b756f

Please sign in to comment.