coder-cloud: Use consolidated bind command

This commit is contained in:
Anmol Sethi 2020-10-07 15:54:41 -04:00
parent 7cc16ceb3a
commit df3089f3ad
No known key found for this signature in database
GPG Key ID: 8CEF1878FF10ADEB
3 changed files with 9 additions and 46 deletions

View File

@ -48,7 +48,7 @@ export interface Args extends VsArgs {
readonly "reuse-window"?: boolean readonly "reuse-window"?: boolean
readonly "new-window"?: boolean readonly "new-window"?: boolean
readonly "coder-bind"?: string readonly "coder-bind"?: OptionalString
} }
interface Option<T> { interface Option<T> {
@ -160,7 +160,7 @@ const options: Options<Required<Args>> = {
verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." }, verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." },
"coder-bind": { "coder-bind": {
type: "string", type: OptionalString,
description: ` description: `
Securely bind code-server via Coder Cloud with the passed name. You'll get a URL like Securely bind code-server via Coder Cloud with the passed name. You'll get a URL like
https://myname.coder-cloud.com at which you can easily access your code-server instance. https://myname.coder-cloud.com at which you can easily access your code-server instance.

View File

@ -5,8 +5,8 @@ import split2 from "split2"
const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent") const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent")
export async function coderCloudBind(serverName: string): Promise<void> { function runAgent(...args: string[]): Promise<void> {
const agent = spawn(coderCloudAgent, ["link", serverName], { const agent = spawn(coderCloudAgent, args, {
stdio: ["inherit", "inherit", "pipe"], stdio: ["inherit", "inherit", "pipe"],
}) })
@ -30,43 +30,9 @@ export async function coderCloudBind(serverName: string): Promise<void> {
}) })
} }
export function coderCloudProxy(addr: string) { export function coderCloudBind(csAddr: string, serverName = ""): Promise<void> {
// addr needs to be in host:port format. // addr needs to be in host:port format.
// So we trim the protocol. // So we trim the protocol.
addr = addr.replace(/^https?:\/\//, "") csAddr = csAddr.replace(/^https?:\/\//, "")
return runAgent("bind", `--code-server-addr=${csAddr}`, serverName)
const _proxy = async () => {
const agent = spawn(coderCloudAgent, ["proxy", "--code-server-addr", addr], {
stdio: ["inherit", "inherit", "pipe"],
})
agent.stderr.pipe(split2()).on("data", (line) => {
line = line.replace(/^[0-9-]+ [0-9:]+ [^ ]+\t/, "")
logger.info(line)
})
return new Promise((res, rej) => {
agent.on("error", rej)
agent.on("close", (code) => {
if (code !== 0) {
rej({
message: `coder cloud agent exited with ${code}`,
})
return
}
res()
})
})
}
const proxy = async () => {
try {
await _proxy()
} catch (err) {
logger.error(err.message)
}
setTimeout(proxy, 3000)
}
proxy()
} }

View File

@ -12,7 +12,7 @@ import { StaticHttpProvider } from "./app/static"
import { UpdateHttpProvider } from "./app/update" import { UpdateHttpProvider } from "./app/update"
import { VscodeHttpProvider } from "./app/vscode" import { VscodeHttpProvider } from "./app/vscode"
import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile, setDefaults } from "./cli" import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile, setDefaults } from "./cli"
import { coderCloudBind, coderCloudProxy } from "./coder-cloud" import { coderCloudBind } from "./coder-cloud"
import { AuthType, HttpServer, HttpServerOptions } from "./http" import { AuthType, HttpServer, HttpServerOptions } from "./http"
import { loadPlugins } from "./plugin" import { loadPlugins } from "./plugin"
import { generateCertificate, hash, humanPath, open } from "./util" import { generateCertificate, hash, humanPath, open } from "./util"
@ -143,11 +143,8 @@ const main = async (args: Args, cliArgs: Args, configArgs: Args): Promise<void>
} }
if (args["coder-bind"]) { if (args["coder-bind"]) {
try { try {
logger.info(`binding code-server to the cloud with name ${args["coder-bind"]}`) await coderCloudBind(serverAddress!, args["coder-bind"].value)
await coderCloudBind(args["coder-bind"])
coderCloudProxy(serverAddress!)
} catch (err) { } catch (err) {
logger.error(err.message) logger.error(err.message)
ipcMain().exit(1) ipcMain().exit(1)