2021-01-29 22:17:43 +00:00
|
|
|
const { getYesterdayISOString } = require("./utils");
|
|
|
|
|
2021-01-26 16:00:13 +00:00
|
|
|
require("yargs/yargs")(process.argv.slice(2)).command(
|
|
|
|
"$0 <type>",
|
|
|
|
"Skynet portal health checks",
|
|
|
|
(yargs) => {
|
|
|
|
yargs
|
|
|
|
.positional("type", {
|
|
|
|
describe: "Type of checks to run",
|
|
|
|
type: "string",
|
2021-04-29 11:43:40 +00:00
|
|
|
choices: ["critical", "extended"],
|
2021-01-26 16:00:13 +00:00
|
|
|
})
|
|
|
|
.option("portal-url", {
|
2021-01-26 11:18:40 +00:00
|
|
|
describe: "Skynet portal url",
|
2021-05-05 12:18:10 +00:00
|
|
|
default: process.env.SKYNET_PORTAL_API || "https://siasky.net",
|
2021-01-26 11:18:40 +00:00
|
|
|
type: "string",
|
2021-01-26 16:00:13 +00:00
|
|
|
})
|
|
|
|
.option("state-dir", {
|
2021-01-26 11:18:40 +00:00
|
|
|
describe: "State directory",
|
|
|
|
default: process.env.STATE_DIR || "state",
|
|
|
|
type: "string",
|
2021-01-26 16:00:13 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
async ({ type, portalUrl, stateDir }) => {
|
2021-05-05 12:18:10 +00:00
|
|
|
process.env.SKYNET_PORTAL_API = portalUrl;
|
2021-01-26 11:18:40 +00:00
|
|
|
process.env.STATE_DIR = stateDir;
|
|
|
|
|
|
|
|
const db = require("../src/db");
|
|
|
|
const checks = require(`../src/checks/${type}`);
|
|
|
|
|
|
|
|
const entry = {
|
2021-01-26 16:00:13 +00:00
|
|
|
date: new Date().toISOString(),
|
|
|
|
checks: await Promise.all(checks.map((check) => new Promise(check))),
|
2021-01-26 11:18:40 +00:00
|
|
|
};
|
|
|
|
|
2021-01-29 22:17:43 +00:00
|
|
|
db.read() // read before writing to make sure no external changes are overwritten
|
|
|
|
.get(type) // get the list of records of given type
|
|
|
|
.push(entry) // insert new record
|
|
|
|
.remove(({ date }) => date < getYesterdayISOString()) // drop old records
|
|
|
|
.write();
|
2021-06-16 09:32:52 +00:00
|
|
|
|
|
|
|
// exit with code 1 if any of the checks report failure
|
|
|
|
if (checks.some(({ up }) => !up)) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-01-26 16:00:13 +00:00
|
|
|
}
|
|
|
|
).argv;
|