2020-09-08 23:39:17 +00:00
|
|
|
import { logger } from "@coder/logger"
|
2020-09-09 04:06:28 +00:00
|
|
|
import { spawn } from "child_process"
|
|
|
|
import path from "path"
|
|
|
|
import split2 from "split2"
|
2020-09-09 04:03:01 +00:00
|
|
|
|
|
|
|
const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent")
|
2020-09-08 23:39:17 +00:00
|
|
|
|
2020-10-07 19:54:41 +00:00
|
|
|
function runAgent(...args: string[]): Promise<void> {
|
2020-10-07 21:16:20 +00:00
|
|
|
logger.debug(`running agent with ${args}`)
|
|
|
|
|
2020-10-07 19:54:41 +00:00
|
|
|
const agent = spawn(coderCloudAgent, args, {
|
2020-09-08 23:39:17 +00:00
|
|
|
stdio: ["inherit", "inherit", "pipe"],
|
|
|
|
})
|
|
|
|
|
2020-09-09 04:06:28 +00:00
|
|
|
agent.stderr.pipe(split2()).on("data", (line) => {
|
2020-09-08 23:39:17 +00:00
|
|
|
line = line.replace(/^[0-9-]+ [0-9:]+ [^ ]+\t/, "")
|
|
|
|
logger.info(line)
|
|
|
|
})
|
|
|
|
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
agent.on("error", rej)
|
|
|
|
|
2020-09-09 04:06:28 +00:00
|
|
|
agent.on("close", (code) => {
|
2020-09-08 23:39:17 +00:00
|
|
|
if (code !== 0) {
|
|
|
|
rej({
|
|
|
|
message: `coder cloud agent exited with ${code}`,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-09-09 04:03:01 +00:00
|
|
|
|
2020-10-07 19:54:41 +00:00
|
|
|
export function coderCloudBind(csAddr: string, serverName = ""): Promise<void> {
|
2020-10-09 11:45:20 +00:00
|
|
|
logger.info("Remember --coder-bind is a beta feature and requires being accepted for testing")
|
|
|
|
logger.info("See https://github.com/cdr/code-server/discussions/2137")
|
2020-09-09 04:03:01 +00:00
|
|
|
// addr needs to be in host:port format.
|
|
|
|
// So we trim the protocol.
|
2020-10-07 19:54:41 +00:00
|
|
|
csAddr = csAddr.replace(/^https?:\/\//, "")
|
|
|
|
return runAgent("bind", `--code-server-addr=${csAddr}`, serverName)
|
2020-09-09 04:03:01 +00:00
|
|
|
}
|