Archived
1
0

Add coder cloud expose command

This commit is contained in:
Anmol Sethi
2020-09-08 19:39:17 -04:00
parent 548a35c0ee
commit 579bb94a6c
6 changed files with 68 additions and 1 deletions

View File

@ -47,6 +47,8 @@ export interface Args extends VsArgs {
readonly _: string[]
readonly "reuse-window"?: boolean
readonly "new-window"?: boolean
readonly "expose"?: OptionalString
}
interface Option<T> {
@ -155,6 +157,9 @@ const options: Options<Required<Args>> = {
locale: { type: "string" },
log: { type: LogLevel },
verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." },
"expose": { type: OptionalString, description: "Expose 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. Authorization is done via GitHub." },
}
export const optionDescriptions = (): string[] => {

30
src/node/coder-cloud.ts Normal file
View File

@ -0,0 +1,30 @@
import { spawn } from "child_process"
import path from "path"
import { logger } from "@coder/logger"
import split2 from "split2"
export async function coderCloudExpose(serverName: string): Promise<void> {
const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent")
const agent = spawn(coderCloudAgent, ["link", serverName], {
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()
})
})
}

View File

@ -16,6 +16,7 @@ import { AuthType, HttpServer, HttpServerOptions } from "./http"
import { loadPlugins } from "./plugin"
import { generateCertificate, hash, humanPath, open } from "./util"
import { ipcMain, wrap } from "./wrapper"
import { coderCloudExpose } from "./coder-cloud"
process.on("uncaughtException", (error) => {
logger.error(`Uncaught exception: ${error.message}`)
@ -188,6 +189,20 @@ async function entry(): Promise<void> {
process.exit(1)
})
vscode.on("exit", (code) => process.exit(code || 0))
} else if (args["expose"]) {
logger.debug("exposing code-server via the coder-cloud agent")
if (!args["expose"].value) {
logger.error("You must pass a name to expose with coder cloud. See --help")
process.exit(1)
}
try {
await coderCloudExpose(args["expose"].value)
} catch (err) {
logger.error(err.message)
process.exit(1)
}
} else if (process.env.VSCODE_IPC_HOOK_CLI) {
const pipeArgs: OpenCommandPipeArgs = {
type: "open",