From 531cc5eb5b1f9f6976def887bfcb161472f7f175 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 9 Apr 2025 15:25:49 +0200 Subject: [PATCH] wip process control --- src/handlers/processControl.js | 89 ++++++++++++++++++++++++++++++++++ src/main.js | 7 +++ 2 files changed, 96 insertions(+) create mode 100644 src/handlers/processControl.js diff --git a/src/handlers/processControl.js b/src/handlers/processControl.js new file mode 100644 index 0000000..1ba6570 --- /dev/null +++ b/src/handlers/processControl.js @@ -0,0 +1,89 @@ +export class ProcessControl { + constructor(configService, shellService, osService, fsService) { + this.config = configService.get(); + this.shell = shellService; + this.os = osService; + this.fs = fsService; + } + + getPublicIp = async () => { + if (this.os.isWindows()) { + const result = await this.shell.run( + "for /f \"delims=\" %a in ('curl -s --ssl-reqd ip.codex.storage') do @echo %a", + ); + return result.trim(); + } else { + return await this.shell.run("curl -s https://ip.codex.storage"); + } + } + + getLogFile = () =>{ + // function getCurrentLogFile(config) { + // const timestamp = new Date() + // .toISOString() + // .replaceAll(":", "-") + // .replaceAll(".", "-"); + // return path.join(config.logsDir, `codex_${timestamp}.log`); + // } + // todo, maybe use timestamp + + return this.fs.pathJoin([this.config.logsDir, "codex.log"]); + } + + doThing = async () => { + if (this.config.dataDir.length < 1) throw new Error("Missing config: dataDir"); + if (this.config.logsDir.length < 1) throw new Error("Missing config: logsDir"); + + console.log("start a codex detached"); + + console.log("nat: " + await this.getPublicIp()); + console.log("logs dir: " + this.getLogFile()); + + try { + + + + console.log( + showInfoMessage( + `Data location: ${config.dataDir}\n` + + `Logs: ${logFilePath}\n` + + `API port: ${config.ports.apiPort}`, + ), + ); + + const executable = config.codexExe; + const args = [ + `--data-dir="${config.dataDir}"`, + `--log-level=DEBUG`, + `--log-file="${logFilePath}"`, + `--storage-quota="${config.storageQuota}"`, + `--disc-port=${config.ports.discPort}`, + `--listen-addrs=/ip4/0.0.0.0/tcp/${config.ports.listenPort}`, + `--api-port=${config.ports.apiPort}`, + `--nat=${nat}`, + `--api-cors-origin="*"`, + `--bootstrap-node=spr:CiUIAhIhAiJvIcA_ZwPZ9ugVKDbmqwhJZaig5zKyLiuaicRcCGqLEgIDARo8CicAJQgCEiECIm8hwD9nA9n26BUoNuarCEllqKDnMrIuK5qJxFwIaosQ3d6esAYaCwoJBJ_f8zKRAnU6KkYwRAIgM0MvWNJL296kJ9gWvfatfmVvT-A7O2s8Mxp8l9c8EW0CIC-h-H-jBVSgFjg3Eny2u33qF7BDnWFzo7fGfZ7_qc9P`, + ]; + + const command = `"${executable}" ${args.join(" ")}`; + + console.log( + showInfoMessage( + "🚀 Codex node is running...\n\n" + + "If your firewall ask, be sure to allow Codex to receive connections. \n" + + "Please keep this terminal open. Start a new terminal to interact with the node.\n\n" + + "Press CTRL+C to stop the node", + ), + ); + + const nodeProcess = exec(command); + + await new Promise((resolve) => setTimeout(resolve, 5000)); + + } + catch + { + + } + } +} diff --git a/src/main.js b/src/main.js index b37b231..ed4c841 100644 --- a/src/main.js +++ b/src/main.js @@ -34,6 +34,7 @@ import { DataDirMover } from "./utils/dataDirMover.js"; import { Installer } from "./handlers/installer.js"; import { ShellService } from "./services/shellService.js"; import { OsService } from "./services/osService.js"; +import { ProcessControl } from "./handlers/processControl.js"; async function showNavigationMenu() { console.log("\n"); @@ -132,9 +133,15 @@ export async function main() { new DataDirMover(fsService, uiService), ); + const processControl = new ProcessControl(configService, shellService, osService, fsService); + await processControl.doThing(); + return; + await mainMenu.show(); return; + + try { while (true) { console.log("\n" + chalk.cyanBright(ASCII_ART));