Created
May 19, 2020 14:31
-
-
Save imaman/2bbe4a4f8f553f11d78179be8e9e5c95 to your computer and use it in GitHub Desktop.
grcpy syubby grpcurl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
const child_process = require('child_process') | |
const JSON5 = require('json5') | |
const argv = process.argv.slice(2).map(x => x.trim()).filter(Boolean) | |
if (argv.length !== 3) { | |
console.log(`Usage: ${process.argv[1]} <groupId>:<artifactId> <GRPC-method> <payload>`) | |
process.exit(1) | |
} | |
const serviceId = argv[0] | |
const method = argv[1] | |
const payload = JSON.stringify(JSON5.parse(argv[2])) | |
https.get(`https://something.acme.com/api/v2/getBuildClusters/${serviceId}`, (resp) => { | |
let data = ''; | |
resp.on('data', (chunk) => { | |
data += chunk; | |
}); | |
resp.on('end', () => { | |
const parsed = JSON.parse(data) | |
const ips = parsed[0].clusterNodeList.map(x => x.ip) | |
run(ips) | |
}); | |
}).on("error", (err) => { | |
console.error("Error: " + err.message); | |
process.exit(1); | |
}); | |
function pickOne(ips) { | |
const index = Math.trunc(Math.random() * ips.length) | |
const ret = ips[index] | |
console.log(`picked IP (${ret}) from index ${index}`) | |
return ret | |
} | |
function run(ips) { | |
const ip = pickOne(ips) | |
const commandLine = ['-d', `'${payload}'`, '-H', 'wix-request-context-bin:""', '-plaintext', `${ip}:8081`, method] | |
const cmd = 'grpcurl ' + commandLine.join(' ') | |
const out = child_process.execSync(cmd) | |
console.log(out.toString()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment