Skip to content

Commit e6c5eef

Browse files
committed
Only trigger the connector if a workspace was selected
- log instead of print - pass additional connection params
1 parent 8bb3378 commit e6c5eef

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

src/main/kotlin/com/coder/gateway/CoderGatewayConnectionProvider.kt

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import com.jetbrains.gateway.api.GatewayConnectionHandle
77
import com.jetbrains.gateway.api.GatewayConnectionProvider
88
import com.jetbrains.gateway.ssh.ClientOverSshTunnelConnector
99
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
10+
import kotlinx.coroutines.GlobalScope
11+
import kotlinx.coroutines.launch
1012
import java.net.URI
11-
import java.net.URL
1213
import java.util.logging.Logger
1314
import javax.swing.JComponent
1415

@@ -18,7 +19,8 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
1819
val coderUrl = parameters["coder_url"]
1920
val workspaceName = parameters["workspace_name"]
2021
val user = parameters["username"]
21-
val privateSSHKey = parameters["private_ssh_key"]
22+
val pass = parameters["password"]
23+
val token = parameters["session_token"]
2224
val projectPath = parameters["project_path"]
2325

2426
if (coderUrl != null && workspaceName != null) {
@@ -27,16 +29,19 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
2729
logger.warning("There is already a connection started on ${connection.url} using the workspace ${connection.workspaceId}")
2830
return null
2931
}
30-
val url = URL(coderUrl)
3132
val clientLifetime = LifetimeDefinition()
3233
val credentials = RemoteCredentialsHolder()
3334
credentials.apply {
3435
setHost("coder.${workspaceName}")
3536
userName = user
36-
setPrivateKeyFile(privateSSHKey)
37+
password = pass
3738
}
38-
var tcpJoinLink = "jetbrains-gateway://connect#projectPath=${projectPath}&host=${url.host}&port=22&user=${user}&type=ssh&deploy=true&buildNumber=221.5591.52&productCode=IU"
39-
ClientOverSshTunnelConnector(clientLifetime, credentials, URI(tcpJoinLink)).connect()
39+
40+
var tcpJoinLink = "jetbrains-gateway://connect#projectPath=${projectPath}&host=coder.${workspaceName}&port=22&user=${user}&token=$token&type=ssh&deploy=true&buildNumber=221.5591.52&productCode=IU"
41+
GlobalScope.launch {
42+
ClientOverSshTunnelConnector(clientLifetime, credentials, URI(tcpJoinLink)).connect()
43+
}
44+
4045
return object : GatewayConnectionHandle(clientLifetime) {
4146
override fun createComponent(): JComponent {
4247
return CoderGatewayConnectionComponent(clientLifetime, coderUrl, workspaceName)

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

+28-22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import kotlinx.coroutines.launch
2727
import kotlinx.coroutines.withContext
2828
import org.zeroturnaround.exec.ProcessExecutor
2929
import java.net.URL
30+
import java.util.logging.Logger
3031
import javax.swing.DefaultComboBoxModel
3132

3233
class CoderWorkspacesStepView : CoderWorkspacesWizardStep, Disposable {
@@ -90,35 +91,40 @@ class CoderWorkspacesStepView : CoderWorkspacesWizardStep, Disposable {
9091

9192
override suspend fun onNext(wizardModel: CoderWorkspacesWizardModel) {
9293
val workspace = workspacesView.selectedValue
93-
println(">>> ${workspace.name} was selected")
94-
cs.launch {
95-
val privateSSHKey = withContext(Dispatchers.IO) {
96-
val url = URL(wizardModel.loginModel.uriScheme.toString().toLowerCase(), wizardModel.loginModel.host, wizardModel.loginModel.port, "")
97-
val cliManager = CoderCLIManager(URL(url.protocol, url.host, url.port, ""))
98-
val cli = cliManager.download() ?: throw IllegalStateException("Could not download coder binary")
99-
val loginOutput = ProcessExecutor().command(cli.toAbsolutePath().toString(), "login", url.toString(), "--token", coderClient.sessionToken).readOutput(true).execute().outputUTF8()
100-
println(">>> coder-cli login output: $loginOutput")
101-
val sshConfigOutput = ProcessExecutor().command(cli.toAbsolutePath().toString(), "config-ssh").readOutput(true).execute().outputUTF8()
102-
println(">>> coder-cli config-ssh output: $sshConfigOutput")
103-
104-
coderClient.userSSHKeys().privateKey
105-
}
94+
if (workspace != null) {
95+
cs.launch {
96+
withContext(Dispatchers.IO) {
97+
val url = URL(wizardModel.loginModel.uriScheme.toString().toLowerCase(), wizardModel.loginModel.host, wizardModel.loginModel.port, "")
98+
val cliManager = CoderCLIManager(URL(url.protocol, url.host, url.port, ""))
99+
val cli = cliManager.download() ?: throw IllegalStateException("Could not download coder binary")
100+
val loginOutput = ProcessExecutor().command(cli.toAbsolutePath().toString(), "login", url.toString(), "--token", coderClient.sessionToken).readOutput(true).execute().outputUTF8()
101+
102+
logger.info("coder-cli login output: $loginOutput")
103+
val sshConfigOutput = ProcessExecutor().command(cli.toAbsolutePath().toString(), "config-ssh").readOutput(true).execute().outputUTF8()
104+
logger.info("coder-cli config-ssh output: $sshConfigOutput")
105+
}
106106

107-
GatewayUI.getInstance().connect(
108-
mapOf(
109-
"type" to "coder",
110-
"coder_url" to URL(wizardModel.loginModel.uriScheme.toString().toLowerCase(), wizardModel.loginModel.host, wizardModel.loginModel.port.toString()).toString(),
111-
"workspace_name" to workspace.name,
112-
"username" to coderClient.me.username,
113-
"private_ssh_key" to privateSSHKey,
114-
"project_path" to tfProject.text
107+
GatewayUI.getInstance().connect(
108+
mapOf(
109+
"type" to "coder",
110+
"coder_url" to URL(wizardModel.loginModel.uriScheme.toString().toLowerCase(), wizardModel.loginModel.host, wizardModel.loginModel.port, "").toString(),
111+
"workspace_name" to workspace.name,
112+
"username" to coderClient.me.username,
113+
"password" to wizardModel.loginModel.password!!,
114+
"session_token" to coderClient.sessionToken,
115+
"project_path" to tfProject.text
116+
)
115117
)
116-
)
118+
}
117119
}
118120

119121
}
120122

121123
override fun dispose() {
122124

123125
}
126+
127+
companion object {
128+
val logger = Logger.getLogger(CoderWorkspacesStepView::class.java.simpleName)
129+
}
124130
}

0 commit comments

Comments
 (0)