Last active
September 18, 2023 01:03
-
-
Save stakemepro/de685868629b5f223d855be8c284513c to your computer and use it in GitHub Desktop.
Auto set free port in cosmos node
This file contains 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 shell = require("shelljs"); | |
const TOKEN = process.argv[2]; | |
const CONFIG = process.argv[3]; | |
function openPort(port) { | |
return shell.exec(`sudo lsof -Pi :${port} -sTCP:LISTEN`, {shell: '/bin/bash', silent: true}).code !== 0; | |
} | |
for (let i = 0; i < 60; i++) { | |
let port = `${i + 26}657`; | |
if (openPort(port)) { | |
const proxyAppPort = `${i + 26}658`; | |
const firstLaddrPort = `${i + 26}657`; | |
const pprofLaddrPort = `${i + 6}060`; | |
const secondLaddrPort = `${i + 26}656`; | |
const externalAddress = `${i + 26}656`; | |
const prometheusListenAddr = `${i + 26}660`; | |
const firstAddress = `${i + 1}317`; | |
const secondAddress = `${i + 8}080`; | |
const thirdAddress = `${i + 9}090`; | |
const fourthAddress = `${i + 9}091`; | |
if ( | |
openPort(proxyAppPort) && openPort(firstLaddrPort) && openPort(pprofLaddrPort) && | |
openPort(secondLaddrPort) && openPort(externalAddress) && openPort(prometheusListenAddr) && | |
openPort(firstAddress) && openPort(secondAddress) && openPort(thirdAddress) && | |
openPort(fourthAddress) | |
) { | |
console.log('Set port:', port); | |
const firstCommandChange = `sudo sed -i.bak -e "\\ | |
s%^proxy_app = \\"tcp://127.0.0.1:26658\\"%proxy_app = \\"tcp://127.0.0.1:${i+26}658\\"%; \\ | |
s%^laddr = \\"tcp://127.0.0.1:26657\\"%laddr = \\"tcp://127.0.0.1:${i+26}657\\"%; \\ | |
s%^pprof_laddr = \\"localhost:6060\\"%pprof_laddr = \\"localhost:${i+6}060\\"%; \\ | |
s%^laddr = \\"tcp://0.0.0.0:26656\\"%laddr = \\"tcp://127.0.0.1:${i+26}656\\"%; \\ | |
s%^external_address = \\"\\"%external_address = \\"\`echo 127.0.0.1:${i+26}656\`\\"%; \\ | |
s%^prometheus_listen_addr = \\":26660\\"%prometheus_listen_addr = \\":${i+26}660\\"%" $HOME/${CONFIG}/config/config.toml`; | |
const secondCommandChange = `sudo sed -i.bak -e "\\ | |
s%^address = \\"tcp://0.0.0.0:1317\\"%address = \\"tcp://127.0.0.1:${i+1}317\\"%; \\ | |
s%^address = \\":8080\\"%address = \\":${i+8}080\\"%; \\ | |
s%^address = \\"0.0.0.0:9090\\"%address = \\"127.0.0.1:${i+9}090\\"%; \\ | |
s%^address = \\"0.0.0.0:9091\\"%address = \\"127.0.0.1:${i+9}091\\"%" $HOME/${CONFIG}/config/app.toml` | |
const thirdCommandChange = `${TOKEN} config node http://localhost:${i+26}657` | |
console.log(firstCommandChange); | |
console.log(secondCommandChange); | |
console.log(thirdCommandChange); | |
shell.exec(firstCommandChange, {silent: true, shell: '/bin/bash'}); | |
shell.exec(secondCommandChange, {silent: true, shell: '/bin/bash'}); | |
shell.exec(thirdCommandChange, {silent: true, shell: '/bin/bash'}); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment