Files
code-server/src/node/coder_cloud.ts
T

43 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-09-08 19:39:17 -04:00
import { logger } from "@coder/logger"
2020-09-09 00:06:28 -04:00
import { spawn } from "child_process"
import path from "path"
import split2 from "split2"
2020-09-09 00:03:01 -04:00
2022-02-01 22:15:19 +05:30
// https://github.com/coder/coder-cloud
2020-09-09 00:03:01 -04:00
const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent")
2020-09-08 19:39:17 -04:00
2020-10-07 15:54:41 -04:00
function runAgent(...args: string[]): Promise<void> {
logger.debug(`running agent with ${args}`)
2020-10-07 15:54:41 -04:00
const agent = spawn(coderCloudAgent, args, {
2020-09-08 19:39:17 -04:00
stdio: ["inherit", "inherit", "pipe"],
})
2020-09-09 00:06:28 -04:00
agent.stderr.pipe(split2()).on("data", (line) => {
2020-09-08 19:39:17 -04:00
line = line.replace(/^[0-9-]+ [0-9:]+ [^ ]+\t/, "")
logger.info(line)
})
return new Promise((res, rej) => {
agent.on("error", rej)
2020-09-09 00:06:28 -04:00
agent.on("close", (code) => {
2020-09-08 19:39:17 -04:00
if (code !== 0) {
rej({
2021-03-16 22:59:30 -04:00
message: `--link agent exited with ${code}`,
2020-09-08 19:39:17 -04:00
})
return
}
res()
})
})
}
2020-09-09 00:03:01 -04:00
export function coderCloudBind(address: URL | string, serverName = ""): Promise<void> {
if (typeof address === "string") {
throw new Error("Cannot link socket paths")
}
// Address needs to be in hostname:port format without the protocol.
return runAgent("bind", `--code-server-addr=${address.host}`, serverName)
2020-09-09 00:03:01 -04:00
}