Skip to content

Commit 8a896dd

Browse files
authored
Start workspaces by shelling out to CLI (#518)
Replace the REST-API-based start flow with one that shells out to the coder CLI. This is the JetBrains extension equivalent to coder/vscode-coder#400.
1 parent 83efd4d commit 8a896dd

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

src/main/kotlin/com/coder/gateway/cli/CoderCLIManager.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,21 @@ class CoderCLIManager(
451451
return matches
452452
}
453453

454+
/**
455+
* Start a workspace.
456+
*
457+
* Throws if the command execution fails.
458+
*/
459+
fun startWorkspace(workspaceOwner: String, workspaceName: String): String {
460+
return exec(
461+
"--global-config",
462+
coderConfigPath.toString(),
463+
"start",
464+
"--yes",
465+
workspaceOwner+"/"+workspaceName,
466+
)
467+
}
468+
454469
private fun exec(vararg args: String): String {
455470
val stdout =
456471
ProcessExecutor()

src/main/kotlin/com/coder/gateway/sdk/CoderRestClient.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,6 @@ open class CoderRestClient(
227227
return templateResponse.body()!!
228228
}
229229

230-
/**
231-
* @throws [APIResponseException].
232-
*/
233-
fun startWorkspace(workspace: Workspace): WorkspaceBuild {
234-
val buildRequest = CreateWorkspaceBuildRequest(null, WorkspaceTransition.START)
235-
val buildResponse = retroRestClient.createWorkspaceBuild(workspace.id, buildRequest).execute()
236-
if (buildResponse.code() != HttpURLConnection.HTTP_CREATED) {
237-
throw APIResponseException("start workspace ${workspace.name}", url, buildResponse)
238-
}
239-
return buildResponse.body()!!
240-
}
241-
242230
/**
243231
* @throws [APIResponseException].
244232
*/

src/main/kotlin/com/coder/gateway/views/CoderGatewayRecentWorkspaceConnectionsView.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package com.coder.gateway.views
55
import com.coder.gateway.CoderGatewayBundle
66
import com.coder.gateway.CoderGatewayConstants
77
import com.coder.gateway.CoderRemoteConnectionHandle
8+
import com.coder.gateway.cli.CoderCLIManager
9+
import com.coder.gateway.cli.ensureCLI
810
import com.coder.gateway.icons.CoderIcons
911
import com.coder.gateway.models.WorkspaceAgentListModel
1012
import com.coder.gateway.models.WorkspaceProjectIDE
@@ -73,6 +75,8 @@ data class DeploymentInfo(
7375
var items: List<WorkspaceAgentListModel>? = null,
7476
// Null if there have not been any errors yet.
7577
var error: String? = null,
78+
// Null if unable to ensure the CLI is downloaded.
79+
var cli: CoderCLIManager? = null,
7680
)
7781

7882
class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback: (Component) -> Unit) :
@@ -232,10 +236,10 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
232236
if (enableLinks) {
233237
cell(
234238
ActionLink(workspaceProjectIDE.projectPathDisplay) {
235-
withoutNull(deployment?.client, workspaceWithAgent?.workspace) { client, workspace ->
239+
withoutNull(deployment?.cli, workspaceWithAgent?.workspace) { cli, workspace ->
236240
CoderRemoteConnectionHandle().connect {
237241
if (listOf(WorkspaceStatus.STOPPED, WorkspaceStatus.CANCELED, WorkspaceStatus.FAILED).contains(workspace.latestBuild.status)) {
238-
client.startWorkspace(workspace)
242+
cli.startWorkspace(workspace.ownerName, workspace.name)
239243
}
240244
workspaceProjectIDE
241245
}
@@ -358,6 +362,19 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
358362
throw Exception("Unable to make request; token was not found in CLI config.")
359363
}
360364

365+
val cli = ensureCLI(
366+
deploymentURL.toURL(),
367+
client.buildInfo().version,
368+
settings,
369+
)
370+
371+
// We only need to log the cli in if we have token-based auth.
372+
// Otherwise, we assume it is set up in the same way the plugin
373+
// is with mTLS.
374+
if (client.token != null) {
375+
cli.login(client.token)
376+
}
377+
361378
// This is purely to populate the current user, which is
362379
// used to match workspaces that were not recorded with owner
363380
// information.
@@ -378,6 +395,7 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
378395
}
379396

380397
deployment.client = client
398+
deployment.cli = cli
381399
deployment.items = items
382400
deployment.error = null
383401
} catch (e: Exception) {

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ class CoderWorkspacesStepView :
303303
CoderIcons.RUN,
304304
) {
305305
override fun actionPerformed(p0: AnActionEvent) {
306-
withoutNull(client, tableOfWorkspaces.selectedObject?.workspace) { c, workspace ->
306+
withoutNull(cliManager, tableOfWorkspaces.selectedObject?.workspace) { cliManager, workspace ->
307307
jobs[workspace.id]?.cancel()
308308
jobs[workspace.id] =
309309
cs.launch(ModalityState.current().asContextElement()) {
310310
withContext(Dispatchers.IO) {
311311
try {
312-
c.startWorkspace(workspace)
312+
cliManager.startWorkspace(workspace.ownerName, workspace.name)
313313
loadWorkspaces()
314314
} catch (e: Exception) {
315315
logger.error("Could not start workspace ${workspace.name}", e)
@@ -659,7 +659,7 @@ class CoderWorkspacesStepView :
659659
cs.launch(ModalityState.current().asContextElement()) {
660660
while (isActive) {
661661
loadWorkspaces()
662-
delay(5000)
662+
delay(1000)
663663
}
664664
}
665665
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy