Skip to content

Commit f8109b4

Browse files
committed
feat(mcl/host_info): Upload results to Coda
1 parent 20ad2bd commit f8109b4

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

packages/mcl/src/src/mcl/commands/host_info.d

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import std.stdio : writeln;
99
import std.conv : to;
1010
import std.string : strip, indexOf, isNumeric;
1111
import std.array : split, join, array, replace;
12-
import std.algorithm : map, filter, startsWith, joiner, any, sum;
12+
import std.algorithm : map, filter, startsWith, joiner, any, sum, find;
1313
import std.file : exists, write, readText, readLink, dirEntries, SpanMode;
1414
import std.path : baseName;
1515
import std.json;
@@ -22,6 +22,7 @@ import mcl.utils.process : execute, isRoot;
2222
import mcl.utils.number : humanReadableSize;
2323
import mcl.utils.array : uniqIfSame;
2424
import mcl.utils.nix : Literal;
25+
import mcl.utils.coda;
2526

2627
// enum InfoFormat
2728
// {
@@ -34,10 +35,12 @@ struct Params
3435
{
3536
// @optional()
3637
// InfoFormat format = InfoFormat.JSON;
38+
@optional() string codaApiToken;
3739
void setup()
3840
{
3941
}
4042
}
43+
Params params;
4144

4245
string[string] cpuinfo;
4346

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

6568
export void host_info()
6669
{
67-
const params = parseEnv!Params;
70+
params = parseEnv!Params;
6871

6972
Info info = getInfo();
7073

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

8184
Info info;
85+
info.softwareInfo.hostid = execute("hostid", false);
86+
info.softwareInfo.hostname = execute("cat /etc/hostname", false);
8287
info.softwareInfo.operatingSystemInfo = getOperatingSystemInfo();
8388
info.softwareInfo.opensshInfo = getOpenSSHInfo();
8489
info.softwareInfo.machineConfigInfo = getMachineConfigInfo();
@@ -90,9 +95,91 @@ Info getInfo()
9095
info.hardwareInfo.displayInfo = getDisplayInfo();
9196
info.hardwareInfo.graphicsProcessorInfo = getGraphicsProcessorInfo();
9297

98+
if (params.codaApiToken) {
99+
auto docId = "0rz18jyJ1M";
100+
auto hostTableId = "grid-b3MAjem325";
101+
auto cpuTableId = "grid-mCI3x3nEIE";
102+
auto memoryTableId = "grid-o7o2PeB4rz";
103+
auto motherboardTableId = "grid-270PlzmA8K";
104+
auto gpuTableId = "grid-ho6EPztvni";
105+
auto storageTableId = "grid-JvXFbttMNz";
106+
auto osTableId = "grid-ora7n98-ls";
107+
auto coda = CodaApiClient(params.codaApiToken);
108+
109+
auto hostValues = RowValues([
110+
CodaCell("Host Name", info.softwareInfo.hostname),
111+
CodaCell("Host ID", info.softwareInfo.hostid),
112+
CodaCell("OpenSSH Public Key", info.softwareInfo.opensshInfo.publicKey),
113+
CodaCell("JSON", info.toJSON(true).toPrettyString(JSONOptions.doNotEscapeSlashes))
114+
]);
115+
116+
coda.updateOrInsertRow(docId, hostTableId, hostValues);
117+
118+
auto cpuValues = RowValues([
119+
CodaCell("Host Name", info.softwareInfo.hostname),
120+
CodaCell("Vendor", info.hardwareInfo.processorInfo.vendor),
121+
CodaCell("Model", info.hardwareInfo.processorInfo.model),
122+
CodaCell("Architecture", info.hardwareInfo.processorInfo.architectureInfo.architecture),
123+
CodaCell("Flags", info.hardwareInfo.processorInfo.architectureInfo.flags),
124+
]);
125+
coda.updateOrInsertRow(docId, cpuTableId, cpuValues);
126+
127+
auto memoryValues = RowValues([
128+
CodaCell("Host Name", info.softwareInfo.hostname),
129+
CodaCell("Vendor", info.hardwareInfo.memoryInfo.vendor),
130+
CodaCell("Part Number", info.hardwareInfo.memoryInfo.partNumber),
131+
CodaCell("Serial", info.hardwareInfo.memoryInfo.serial),
132+
CodaCell("Generation", info.hardwareInfo.memoryInfo.type),
133+
CodaCell("Slots", info.hardwareInfo.memoryInfo.slots == 0 ? "Soldered" : info.hardwareInfo.memoryInfo.count.to!string ~ "/" ~ info.hardwareInfo.memoryInfo.slots.to!string),
134+
CodaCell("Total", info.hardwareInfo.memoryInfo.total),
135+
CodaCell("Speed", info.hardwareInfo.memoryInfo.speed),
136+
]);
137+
coda.updateOrInsertRow(docId, memoryTableId, memoryValues);
138+
139+
auto motherboardValues = RowValues([
140+
CodaCell("Host Name", info.softwareInfo.hostname),
141+
CodaCell("Vendor", info.hardwareInfo.motherboardInfo.vendor),
142+
CodaCell("Model", info.hardwareInfo.motherboardInfo.model),
143+
CodaCell("Revision", info.hardwareInfo.motherboardInfo.version_),
144+
CodaCell("Serial", info.hardwareInfo.motherboardInfo.serial),
145+
CodaCell("BIOS Vendor", info.hardwareInfo.motherboardInfo.biosInfo.vendor),
146+
CodaCell("BIOS Version", info.hardwareInfo.motherboardInfo.biosInfo.version_),
147+
CodaCell("BIOS Release", info.hardwareInfo.motherboardInfo.biosInfo.release),
148+
CodaCell("BIOS Date", info.hardwareInfo.motherboardInfo.biosInfo.date)
149+
]);
150+
coda.updateOrInsertRow(docId, motherboardTableId, motherboardValues);
151+
152+
auto gpuValues = RowValues([
153+
CodaCell("Host Name", info.softwareInfo.hostname),
154+
CodaCell("Vendor", info.hardwareInfo.graphicsProcessorInfo.vendor),
155+
CodaCell("Model", info.hardwareInfo.graphicsProcessorInfo.model),
156+
CodaCell("VRam", info.hardwareInfo.graphicsProcessorInfo.vram)
157+
]);
158+
coda.updateOrInsertRow(docId, gpuTableId, gpuValues);
159+
160+
auto osValues = RowValues([
161+
CodaCell("Host Name", info.softwareInfo.hostname),
162+
CodaCell("Distribution", info.softwareInfo.operatingSystemInfo.distribution),
163+
CodaCell("Distribution Version", info.softwareInfo.operatingSystemInfo.distributionVersion),
164+
CodaCell("Kernel", info.softwareInfo.operatingSystemInfo.kernel),
165+
CodaCell("Kernel Version", info.softwareInfo.operatingSystemInfo.kernelVersion)
166+
]);
167+
coda.updateOrInsertRow(docId, osTableId, osValues);
168+
169+
auto storageValues = RowValues([
170+
CodaCell("Host Name", info.softwareInfo.hostname),
171+
CodaCell("Count", info.hardwareInfo.storageInfo.devices.length.to!string),
172+
CodaCell("Total", info.hardwareInfo.storageInfo.total),
173+
CodaCell("JSON", info.hardwareInfo.storageInfo.toJSON(true).toPrettyString(JSONOptions.doNotEscapeSlashes))
174+
]);
175+
coda.updateOrInsertRow(docId, storageTableId, storageValues);
176+
}
177+
93178
return info;
94179
}
95180

181+
182+
96183
struct Info
97184
{
98185
SoftwareInfo softwareInfo;
@@ -101,6 +188,8 @@ struct Info
101188

102189
struct SoftwareInfo
103190
{
191+
string hostname;
192+
string hostid;
104193
OperatingSystemInfo operatingSystemInfo;
105194
OpenSSHInfo opensshInfo;
106195
MachineConfigInfo machineConfigInfo;
@@ -308,7 +397,7 @@ string getDistribution()
308397
if (exists("/etc/os-release"))
309398
{
310399
foreach (line; execute([
311-
"awk", "-F", "=", "/^NAME=/ {print $2}", "/etc/os-release"
400+
"awk", "-F", "=", "'/^NAME=/ {print $2}'", "/etc/os-release"
312401
], false).split("\n"))
313402
{
314403
distribution = line;
@@ -331,7 +420,7 @@ string getDistributionVersion()
331420
if (exists("/etc/os-release"))
332421
{
333422
foreach (line; execute([
334-
"awk", "-F", "=", "/^VERSION=/ {print $2}", "/etc/os-release"
423+
"awk", "-F", "=", "'/^VERSION=/ {print $2}'", "/etc/os-release"
335424
], false).split("\n"))
336425
{
337426
distributionVersion = line.strip("\"");

packages/mcl/src/src/mcl/utils/coda.d

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import std.traits : isArray;
3131
import mcl.utils.json : toJSON, fromJSON;
3232
import std.process : environment;
3333
import std.stdio : writeln, writefln;
34-
import std.algorithm : map, filter;
34+
import std.algorithm : map, filter, find;
3535
import std.exception : assertThrown;
3636
import std.sumtype : SumType;
3737
import core.thread;
@@ -527,6 +527,16 @@ struct CodaApiClient
527527
coda.deleteRow("dEJJPwdxcw", tables[0].id, resp[0]);
528528
}
529529

530+
void updateOrInsertRow(string docId, string tableId, RowValues values) {
531+
auto table = listRows(docId, tableId);
532+
auto rows = find!(row => row.name == values.cells[0].value)(table);
533+
if (rows.length > 0) {
534+
updateRow(docId, tableId, rows[0].id, values);
535+
}
536+
else {
537+
insertRows(docId, tableId, [values]);
538+
}
539+
}
530540
struct PushButtonResponse {
531541
string requestId;
532542
string rowId;

packages/mcl/src/src/mcl/utils/process.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ T execute(T = string)(string[] args, bool printCommand = true, bool returnErr =
2424
import std.array : join;
2525
import std.algorithm : map;
2626
import std.conv : to;
27+
import std.stdio : writeln;
2728

2829
auto cmd = args.map!escapeShellCommand.join(" ");
2930

0 commit comments

Comments
 (0)