Skip to content

Commit 4be829c

Browse files
authored
Merge pull request #99 from coder/clean-up-old-coder-cli
Impl: clean-up outdated binaries
2 parents 91328ad + 343e163 commit 4be829c

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
## Unreleased
66

7+
### Fixed
8+
- outdated Coder CLI binaries are cleaned up
9+
710
## 2.1.2 - 2022-11-23
811

912
### Added

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
package com.coder.gateway.sdk
22

33
import com.intellij.openapi.diagnostic.Logger
4+
import java.io.InputStream
45
import java.net.URL
6+
import java.nio.file.Files
57
import java.nio.file.Path
68
import java.nio.file.Paths
9+
import java.nio.file.StandardCopyOption
710

811
class CoderCLIManager(url: URL, buildVersion: String) {
9-
var remoteCliPath: URL
10-
var localCliPath: Path
12+
var remoteCli: URL
13+
var localCli: Path
14+
private var cliNamePrefix: String
15+
private var tmpDir: String
16+
private var cliFileName: String
1117

1218
init {
1319
val os = getOS()
14-
val cliName = getCoderCLIForOS(os, getArch())
15-
val cliNameWithExt = if (os == OS.WINDOWS) "$cliName.exe" else cliName
16-
val filename = if (os == OS.WINDOWS) "${cliName}-${buildVersion}.exe" else "${cliName}-${buildVersion}"
20+
cliNamePrefix = getCoderCLIForOS(os, getArch())
21+
val cliNameWithExt = if (os == OS.WINDOWS) "$cliNamePrefix.exe" else cliNamePrefix
22+
cliFileName = if (os == OS.WINDOWS) "${cliNamePrefix}-${buildVersion}.exe" else "${cliNamePrefix}-${buildVersion}"
1723

18-
remoteCliPath = URL(url.protocol, url.host, url.port, "/bin/$cliNameWithExt")
19-
localCliPath = Paths.get(System.getProperty("java.io.tmpdir"), filename)
24+
remoteCli = URL(url.protocol, url.host, url.port, "/bin/$cliNameWithExt")
25+
tmpDir = System.getProperty("java.io.tmpdir")
26+
localCli = Paths.get(tmpDir, cliFileName)
2027
}
2128

22-
private fun getCoderCLIForOS(os: OS?, arch: Arch?): String? {
29+
private fun getCoderCLIForOS(os: OS?, arch: Arch?): String {
2330
logger.info("Resolving coder cli for $os $arch")
2431
if (os == null) {
25-
return null
32+
logger.error("Could not resolve client OS and architecture, defaulting to WINDOWS AMD64")
33+
return "coder-windows-amd64"
2634
}
2735
return when (os) {
2836
OS.WINDOWS -> when (arch) {
@@ -46,6 +54,25 @@ class CoderCLIManager(url: URL, buildVersion: String) {
4654
}
4755
}
4856

57+
fun downloadCLI(): Boolean {
58+
if (Files.exists(localCli)) {
59+
logger.info("${localCli.toAbsolutePath()} already exists, skipping download")
60+
return false
61+
}
62+
logger.info("Starting Coder CLI download to ${localCli.toAbsolutePath()}")
63+
remoteCli.openStream().use {
64+
Files.copy(it as InputStream, localCli, StandardCopyOption.REPLACE_EXISTING)
65+
}
66+
return true
67+
}
68+
69+
fun removeOldCli() {
70+
Files.walk(Path.of(tmpDir)).sorted().map { it.toFile() }.filter { it.name.contains(cliNamePrefix) && !it.name.contains(cliFileName) }.forEach {
71+
logger.info("Removing $it because it is an old coder cli")
72+
it.delete()
73+
}
74+
}
75+
4976
companion object {
5077
val logger = Logger.getInstance(CoderCLIManager::class.java.simpleName)
5178
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import com.coder.gateway.models.WorkspaceAgentStatus.STOPPED
1313
import com.coder.gateway.models.WorkspaceAgentStatus.STOPPING
1414
import com.coder.gateway.models.WorkspaceVersionStatus
1515
import com.coder.gateway.sdk.Arch
16-
import com.coder.gateway.sdk.CoderCLIDownloader
1716
import com.coder.gateway.sdk.CoderCLIManager
1817
import com.coder.gateway.sdk.CoderRestClientService
1918
import com.coder.gateway.sdk.OS
@@ -317,7 +316,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
317316
localWizardModel.apply {
318317
this.token = token
319318
buildVersion = coderClient.buildVersion
320-
localCliPath = cliManager.localCliPath.toAbsolutePath().toString()
319+
localCliPath = cliManager.localCli.toAbsolutePath().toString()
321320
}
322321

323322
val authTask = object : Task.Modal(null, CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.cli.downloader.dialog.title"), false) {
@@ -329,11 +328,11 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
329328
fraction = 0.1
330329
}
331330

332-
CoderCLIDownloader().downloadCLI(cliManager.remoteCliPath, cliManager.localCliPath)
331+
cliManager.downloadCLI()
333332
if (getOS() != OS.WINDOWS) {
334333
pi.fraction = 0.4
335334
val chmodOutput = ProcessExecutor().command("chmod", "+x", localWizardModel.localCliPath).readOutput(true).execute().outputUTF8()
336-
logger.info("chmod +x ${cliManager.localCliPath.toAbsolutePath()} $chmodOutput")
335+
logger.info("chmod +x ${cliManager.localCli.toAbsolutePath()} $chmodOutput")
337336
}
338337
pi.apply {
339338
text = "Configuring coder cli..."
@@ -345,6 +344,13 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
345344
pi.fraction = 0.8
346345
val sshConfigOutput = ProcessExecutor().command(localWizardModel.localCliPath, "config-ssh", "--yes", "--use-previous-options").readOutput(true).execute().outputUTF8()
347346
logger.info("Result of `${localWizardModel.localCliPath} config-ssh --yes --use-previous-options`: $sshConfigOutput")
347+
348+
pi.apply {
349+
text = "Remove old coder cli versions..."
350+
fraction = 0.9
351+
}
352+
cliManager.removeOldCli()
353+
348354
pi.fraction = 1.0
349355
}
350356
}

0 commit comments

Comments
 (0)