Skip to content

Commit 437483e

Browse files
committed
Migrate "session_token" file to "session", add --global-config flag to "coder start"
1 parent 8cdbd3a commit 437483e

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/api.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export async function makeCoderSdk(baseUrl: string, token: string | undefined, s
125125
*/
126126
export async function startWorkspaceIfStoppedOrFailed(
127127
restClient: Api,
128+
globalConfigDir: string,
128129
binPath: string,
129130
workspace: Workspace,
130131
writeEmitter: vscode.EventEmitter<string>,
@@ -137,7 +138,13 @@ export async function startWorkspaceIfStoppedOrFailed(
137138
}
138139

139140
return new Promise((resolve, reject) => {
140-
const startArgs = ["start", "--yes", workspace.owner_name + "/" + workspace.name]
141+
const startArgs = [
142+
"--global-config",
143+
globalConfigDir,
144+
"start",
145+
"--yes",
146+
workspace.owner_name + "/" + workspace.name,
147+
]
141148
const startProcess = spawn(binPath, startArgs)
142149

143150
startProcess.stdout.on("data", (data: Buffer) => {

src/remote.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class Remote {
5353
private async maybeWaitForRunning(
5454
restClient: Api,
5555
workspace: Workspace,
56+
label: string,
5657
binPath: string,
5758
): Promise<Workspace | undefined> {
5859
// Maybe already running?
@@ -98,6 +99,7 @@ export class Remote {
9899
title: "Waiting for workspace build...",
99100
},
100101
async () => {
102+
const globalConfigDir = path.dirname(this.storage.getSessionTokenPath(label))
101103
while (workspace.latest_build.status !== "running") {
102104
++attempts
103105
switch (workspace.latest_build.status) {
@@ -114,7 +116,13 @@ export class Remote {
114116
}
115117
writeEmitter = initWriteEmitterAndTerminal()
116118
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
117-
workspace = await startWorkspaceIfStoppedOrFailed(restClient, binPath, workspace, writeEmitter)
119+
workspace = await startWorkspaceIfStoppedOrFailed(
120+
restClient,
121+
globalConfigDir,
122+
binPath,
123+
workspace,
124+
writeEmitter,
125+
)
118126
break
119127
case "failed":
120128
// On a first attempt, we will try starting a failed workspace
@@ -125,7 +133,13 @@ export class Remote {
125133
}
126134
writeEmitter = initWriteEmitterAndTerminal()
127135
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
128-
workspace = await startWorkspaceIfStoppedOrFailed(restClient, binPath, workspace, writeEmitter)
136+
workspace = await startWorkspaceIfStoppedOrFailed(
137+
restClient,
138+
globalConfigDir,
139+
binPath,
140+
workspace,
141+
writeEmitter,
142+
)
129143
break
130144
}
131145
// Otherwise fall through and error.
@@ -167,6 +181,9 @@ export class Remote {
167181

168182
const workspaceName = `${parts.username}/${parts.workspace}`
169183

184+
// Migrate "session_token" file to "session", if needed.
185+
this.storage.migrateSessionToken(parts.label)
186+
170187
// Get the URL and token belonging to this host.
171188
const { url: baseUrlRaw, token } = await this.storage.readCliConfig(parts.label)
172189

@@ -303,7 +320,7 @@ export class Remote {
303320
disposables.push(this.registerLabelFormatter(remoteAuthority, workspace.owner_name, workspace.name))
304321

305322
// If the workspace is not in a running state, try to get it running.
306-
const updatedWorkspace = await this.maybeWaitForRunning(workspaceRestClient, workspace, binaryPath)
323+
const updatedWorkspace = await this.maybeWaitForRunning(workspaceRestClient, workspace, parts.label, binaryPath)
307324
if (!updatedWorkspace) {
308325
// User declined to start the workspace.
309326
await this.closeRemote()

src/storage.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,20 @@ export class Storage {
405405
* The caller must ensure this directory exists before use.
406406
*/
407407
public getSessionTokenPath(label: string): string {
408+
return label
409+
? path.join(this.globalStorageUri.fsPath, label, "session")
410+
: path.join(this.globalStorageUri.fsPath, "session")
411+
}
412+
413+
/**
414+
* Return the directory for the deployment with the provided label to where
415+
* its session token was stored by older code.
416+
*
417+
* If the label is empty, read the old deployment-unaware config instead.
418+
*
419+
* The caller must ensure this directory exists before use.
420+
*/
421+
public getLegacySessionTokenPath(label: string): string {
408422
return label
409423
? path.join(this.globalStorageUri.fsPath, label, "session_token")
410424
: path.join(this.globalStorageUri.fsPath, "session_token")
@@ -488,6 +502,24 @@ export class Storage {
488502
}
489503
}
490504

505+
/**
506+
* Migrate the session token file from "session_token" to "session", if needed.
507+
*/
508+
public async migrateSessionToken(label: string) {
509+
const oldTokenPath = this.getLegacySessionTokenPath(label)
510+
try {
511+
await fs.stat(oldTokenPath)
512+
const newTokenPath = this.getSessionTokenPath(label)
513+
await fs.rename(oldTokenPath, newTokenPath)
514+
return
515+
} catch (error) {
516+
if ((error as NodeJS.ErrnoException)?.code === "ENOENT") {
517+
return
518+
}
519+
throw error
520+
}
521+
}
522+
491523
/**
492524
* Run the header command and return the generated headers.
493525
*/

0 commit comments

Comments
 (0)