Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## Unreleased

### Fixed
- outdated Coder CLI binaries are cleaned up

## 2.1.2 - 2022-11-23

### Added
Expand Down
27 changes: 0 additions & 27 deletions src/main/kotlin/com/coder/gateway/sdk/CoderCLIDownloader.kt

This file was deleted.

45 changes: 36 additions & 9 deletions src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package com.coder.gateway.sdk

import com.intellij.openapi.diagnostic.Logger
import java.io.InputStream
import java.net.URL
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption

class CoderCLIManager(url: URL, buildVersion: String) {
var remoteCliPath: URL
var localCliPath: Path
var remoteCli: URL
var localCli: Path
private var cliNamePrefix: String
private var tmpDir: String
private var cliFileName: String

init {
val os = getOS()
val cliName = getCoderCLIForOS(os, getArch())
val cliNameWithExt = if (os == OS.WINDOWS) "$cliName.exe" else cliName
val filename = if (os == OS.WINDOWS) "${cliName}-${buildVersion}.exe" else "${cliName}-${buildVersion}"
cliNamePrefix = getCoderCLIForOS(os, getArch())
val cliNameWithExt = if (os == OS.WINDOWS) "$cliNamePrefix.exe" else cliNamePrefix
cliFileName = if (os == OS.WINDOWS) "${cliNamePrefix}-${buildVersion}.exe" else "${cliNamePrefix}-${buildVersion}"

remoteCliPath = URL(url.protocol, url.host, url.port, "/bin/$cliNameWithExt")
localCliPath = Paths.get(System.getProperty("java.io.tmpdir"), filename)
remoteCli = URL(url.protocol, url.host, url.port, "/bin/$cliNameWithExt")
tmpDir = System.getProperty("java.io.tmpdir")
localCli = Paths.get(tmpDir, cliFileName)
}

private fun getCoderCLIForOS(os: OS?, arch: Arch?): String? {
private fun getCoderCLIForOS(os: OS?, arch: Arch?): String {
logger.info("Resolving coder cli for $os $arch")
if (os == null) {
return null
logger.error("Could not resolve client OS and architecture, defaulting to WINDOWS AMD64")
return "coder-windows-amd64"
}
return when (os) {
OS.WINDOWS -> when (arch) {
Expand All @@ -46,6 +54,25 @@ class CoderCLIManager(url: URL, buildVersion: String) {
}
}

fun downloadCLI(): Boolean {
if (Files.exists(localCli)) {
logger.info("${localCli.toAbsolutePath()} already exists, skipping download")
return false
}
logger.info("Starting Coder CLI download to ${localCli.toAbsolutePath()}")
remoteCli.openStream().use {
Files.copy(it as InputStream, localCli, StandardCopyOption.REPLACE_EXISTING)
}
return true
}

fun removeOldCli() {
Files.walk(Path.of(tmpDir)).sorted().map { it.toFile() }.filter { it.name.contains(cliNamePrefix) && !it.name.contains(cliFileName) }.forEach {
logger.info("Removing $it because it is an old coder cli")
it.delete()
}
}

companion object {
val logger = Logger.getInstance(CoderCLIManager::class.java.simpleName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.coder.gateway.models.WorkspaceAgentStatus.STOPPED
import com.coder.gateway.models.WorkspaceAgentStatus.STOPPING
import com.coder.gateway.models.WorkspaceVersionStatus
import com.coder.gateway.sdk.Arch
import com.coder.gateway.sdk.CoderCLIDownloader
import com.coder.gateway.sdk.CoderCLIManager
import com.coder.gateway.sdk.CoderRestClientService
import com.coder.gateway.sdk.OS
Expand Down Expand Up @@ -317,7 +316,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
localWizardModel.apply {
this.token = token
buildVersion = coderClient.buildVersion
localCliPath = cliManager.localCliPath.toAbsolutePath().toString()
localCliPath = cliManager.localCli.toAbsolutePath().toString()
}

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

CoderCLIDownloader().downloadCLI(cliManager.remoteCliPath, cliManager.localCliPath)
cliManager.downloadCLI()
if (getOS() != OS.WINDOWS) {
pi.fraction = 0.4
val chmodOutput = ProcessExecutor().command("chmod", "+x", localWizardModel.localCliPath).readOutput(true).execute().outputUTF8()
logger.info("chmod +x ${cliManager.localCliPath.toAbsolutePath()} $chmodOutput")
logger.info("chmod +x ${cliManager.localCli.toAbsolutePath()} $chmodOutput")
}
pi.apply {
text = "Configuring coder cli..."
Expand All @@ -345,6 +344,13 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
pi.fraction = 0.8
val sshConfigOutput = ProcessExecutor().command(localWizardModel.localCliPath, "config-ssh", "--yes", "--use-previous-options").readOutput(true).execute().outputUTF8()
logger.info("Result of `${localWizardModel.localCliPath} config-ssh --yes --use-previous-options`: $sshConfigOutput")

pi.apply {
text = "Remove old coder cli versions..."
fraction = 0.9
}
cliManager.removeOldCli()

pi.fraction = 1.0
}
}
Expand Down