Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start workspaces by shelling out to CLI #400

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aaronlehmann
Copy link

Replace the REST-API-based start flow with one that shells out to the coder CLI.

Replace the REST-API-based start flow with one that shells out to the
coder CLI.

Signed-off-by: Aaron Lehmann <alehmann@netflix.com>
Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for putting this together! Just to test, I hard-coded --global-config and it worked great, so that is the only thing we really need to figure out.

// Before we start a workspace, we make an initial request to check it's not already started
const updatedWorkspace = await restClient.getWorkspace(workspace.id)

if (!["stopped", "failed"].includes(updatedWorkspace.latest_build.status)) {
return updatedWorkspace
}

const latestBuild = await restClient.startWorkspace(updatedWorkspace.id, versionID)
return new Promise((resolve, reject) => {
const startProcess: ChildProcessWithoutNullStreams = spawn(binPath, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there an issue with the types? I would expect this to have the same effect:

Suggested change
const startProcess: ChildProcessWithoutNullStreams = spawn(binPath, [
const startProcess = spawn(binPath, [

const startProcess: ChildProcessWithoutNullStreams = spawn(binPath, [
"start",
"--yes",
workspace.owner_name + "/" + workspace.name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be the slightly tricky part I think. For most folks this will also need --global-config so the cli can pick up the url and token.

--global-config expects url and session files in the directory to which it points, so it may be slightly tricky because the vscodessh command that the plugin has used up to this point takes separate url and file config flags (not sure why), and I guess as a consequence the token file (perhaps unintentionally) ended up with the slightly different name session_token (again not sure why the difference).

So all that to say, I think we will have to also make sure the session file exists, either by migrating from session_token to session or writing both, and then add the --global-config flag.

if (code === 0) {
resolve(restClient.getWorkspace(workspace.id))
} else {
reject(new Error(`"coder start" process exited with code ${code}`))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud, I was hitting this (because of the --global-config stuff) and wondering if we can make the error clearer. My thinking:

  1. Could possibly put the full command we are running, so I can copy-paste to try it myself.
  2. If we capture stderr separately perhaps we could add it to this error. Or, if we avoid disposing the terminal until later (or maybe we could just not dispose it at all, could be nice to pull up the build log whenever you want instead of having it immediately vanish) so it can still be viewed while the popup is up, that could work!

.toString()
.split(/\r*\n/)
.forEach((line: string) => {
if (line !== "") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there a lot of blank lines we have to strip? Does the API strip these out for us maybe so this is for parity?

Not a big deal, my only thought is that maybe start uses blank lines to delineate sections for readability or something (no idea though).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did see blank lines here, I believe due to the CLI's use of \r to overwrite existing lines (which doesn't work if passed through directly to VS Code's output).

Copy link
Member

@code-asher code-asher Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aahhh right, I completely forgot it does that. That makes perfect sense. Since we display this in a (fake) terminal I wonder if we can pass through directly in a way that handles that case so it looks the same as when you run it directly on the command line, but this looks great to me for now.

@aaronlehmann
Copy link
Author

Thanks for reviewing! I'm traveling right now but will come back to this in early December.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants