|
| 1 | +#!/usr/bin/env node |
| 2 | +const https = require("https"); |
| 3 | + |
| 4 | +// Function that request Github API to get self-hosted runners status |
| 5 | +async function getSelfHostedRunnersStatus() { |
| 6 | + const options = { |
| 7 | + hostname: "ghrunners-lambda.berty.io", |
| 8 | + port: 443, |
| 9 | + method: "GET", |
| 10 | + timeout: 30000, |
| 11 | + }; |
| 12 | + |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + const req = https.request(options, (res) => { |
| 15 | + const body = []; |
| 16 | + res.on("data", (chunk) => body.push(chunk)); |
| 17 | + res.on("end", () => { |
| 18 | + const resString = Buffer.concat(body).toString(); |
| 19 | + |
| 20 | + if (res.statusCode < 200 || res.statusCode > 299) { |
| 21 | + return reject( |
| 22 | + new Error(`HTTP status code ${res.statusCode}: ${resString}`) |
| 23 | + ); |
| 24 | + } |
| 25 | + resolve(resString); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + req.on("error", (err) => { |
| 30 | + reject(err); |
| 31 | + }); |
| 32 | + |
| 33 | + req.on("timeout", () => { |
| 34 | + req.destroy(); |
| 35 | + reject(new Error("Request timed out")); |
| 36 | + }); |
| 37 | + |
| 38 | + req.end(); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +// Return hom many self-hosted runners are online |
| 43 | +async function getSelfHostedRunnersOnlineCount() { |
| 44 | + return new Promise((resolve, reject) => { |
| 45 | + getSelfHostedRunnersStatus() |
| 46 | + .then((json) => { |
| 47 | + const stat = JSON.parse(json); |
| 48 | + const total = stat.runners.length; |
| 49 | + var count = 0; |
| 50 | + var desc = "None"; |
| 51 | + |
| 52 | + for (const runner of stat.runners) { |
| 53 | + if (runner.status === "online") count++; |
| 54 | + } |
| 55 | + |
| 56 | + if (count === total && total > 0) desc = "All"; |
| 57 | + else if (count > 0) desc = "Partially"; |
| 58 | + |
| 59 | + resolve(`${desc} (${count}/${total})`); |
| 60 | + }) |
| 61 | + .catch((err) => { |
| 62 | + reject(err); |
| 63 | + }); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +// Returns true if at least one self-hosted runner is available |
| 68 | +async function isSelfHostedRunnerAvailable() { |
| 69 | + try { |
| 70 | + const json = await getSelfHostedRunnersStatus(); |
| 71 | + const stat = JSON.parse(json); |
| 72 | + |
| 73 | + for (const runner of stat.runners) { |
| 74 | + if (runner.status === "online" && runner.busy === false) return true; |
| 75 | + } |
| 76 | + } catch (err) { |
| 77 | + console.error("Github API request self-hosted status error:", err); |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | +} |
| 82 | + |
| 83 | +// If executed as a script |
| 84 | +if (require.main === module) { |
| 85 | + const onlineCheck = process.argv[2]; |
| 86 | + |
| 87 | + if (process.argv.length > 3 || (onlineCheck && onlineCheck !== "online")) { |
| 88 | + console.error(`Usage: ${process.argv[1]} [online]`); |
| 89 | + process.exit(1); |
| 90 | + } |
| 91 | + |
| 92 | + if (onlineCheck) { |
| 93 | + getSelfHostedRunnersOnlineCount() |
| 94 | + .then((count) => console.log(count)) |
| 95 | + .catch((err) => { |
| 96 | + console.error(err); |
| 97 | + process.exit(1); |
| 98 | + }); |
| 99 | + } else { |
| 100 | + getSelfHostedRunnersStatus() |
| 101 | + .then((json) => { |
| 102 | + console.log(JSON.stringify(JSON.parse(json), undefined, 2)); |
| 103 | + }) |
| 104 | + .catch((err) => { |
| 105 | + console.error(err); |
| 106 | + process.exit(1); |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Export module functions |
| 112 | +module.exports = { |
| 113 | + getSelfHostedRunnersStatus: getSelfHostedRunnersStatus, |
| 114 | + isSelfHostedRunnerAvailable: isSelfHostedRunnerAvailable, |
| 115 | +}; |
0 commit comments