Skip to content

Commit d07aa7a

Browse files
committed
[JBGW] update jetbrains gateway (WIP)
1 parent 662bee1 commit d07aa7a

File tree

6 files changed

+155
-18
lines changed

6 files changed

+155
-18
lines changed

components/ide/jetbrains/gateway-plugin/build.gradle.kts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See License.AGPL.txt in the project root for license information.
44

55
import io.gitlab.arturbosch.detekt.Detekt
6+
import org.jetbrains.changelog.date
67
import org.jetbrains.changelog.markdownToHTML
78
import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType
89
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
@@ -36,6 +37,8 @@ if (environmentName.isNotBlank()) {
3637
pluginVersion += "-$environmentName"
3738
}
3839

40+
pluginVersion = pluginVersion.replace("{{LOCAL_VERSION}}", date("MMddhhmm") + "-local")
41+
3942
project(":") {
4043
kotlin {
4144
val excludedPackage = if (environmentName == "latest") "stable" else "latest"
@@ -176,3 +179,46 @@ tasks {
176179
}
177180
}
178181
}
182+
183+
tasks.register("installPlugin") {
184+
group = "gitpod"
185+
186+
println("Building plugin $pluginVersion")
187+
188+
dependsOn("buildPlugin")
189+
190+
doLast {
191+
val pluginTargetPath = "distributions/jetbrains-gateway-gitpod-plugin.zip"
192+
val pluginFile = layout.buildDirectory.file(pluginTargetPath).orNull?.asFile ?: {
193+
throw GradleException("Plugin file not found at $pluginTargetPath")
194+
}
195+
196+
// Example for macOS ~/Library/Application Support/JetBrains/JetBrainsGateway2024.3/plugins
197+
//
198+
// JB_GATEWAY_PLUGINS_DIR=/Users/hwen/Library/Application Support/JetBrains/JetBrainsGateway2024.3/plugins
199+
// JB_GATEWAY_IDEA_LOG_FILE=/Users/hwen/Library/Logs/JetBrains/JetBrainsGateway2024.3/idea.log
200+
val gatewayPluginsDir = System.getenv("JB_GATEWAY_PLUGINS_DIR")
201+
val gatewayIDEALogFile = System.getenv("JB_GATEWAY_IDEA_LOG_FILE")
202+
203+
if (gatewayPluginsDir.isNullOrEmpty()) {
204+
throw GradleException("Found no JB_GATEWAY_PLUGINS_DIR environment variable")
205+
}
206+
println("Copying plugin from $pluginFile to $gatewayPluginsDir")
207+
208+
copy {
209+
from(zipTree(pluginFile))
210+
into(file(gatewayPluginsDir))
211+
}
212+
213+
println("Plugin successfully copied to $gatewayPluginsDir")
214+
215+
exec {
216+
commandLine("sh", "-c", "pkill -f 'Gateway' || true")
217+
}
218+
if (!gatewayIDEALogFile.isNullOrEmpty()) {
219+
exec {
220+
commandLine("sh", "-c", "echo '' > $gatewayIDEALogFile")
221+
}
222+
}
223+
}
224+
}

components/ide/jetbrains/gateway-plugin/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pluginName=gitpod-gateway
88
latestPluginName=Gitpod Gateway
99
pluginId=io.gitpod.jetbrains.gateway
1010
# It is overriden by CI during the build.
11-
pluginVersion=0.0.1
11+
pluginVersion={{LOCAL_VERSION}}
1212
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#intellij-extension-type
1313
platformType=GW
1414
platformDownloadSources=true

components/ide/jetbrains/gateway-plugin/src/main/kotlin/io/gitpod/jetbrains/gateway/GitpodConnectionProvider.kt

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import io.gitpod.jetbrains.gateway.common.GitpodConnectionHandleFactory
4747
import io.gitpod.jetbrains.icons.GitpodIcons
4848
import kotlinx.coroutines.*
4949
import kotlinx.coroutines.future.await
50-
import java.awt.Component
5150
import java.net.URL
5251
import java.net.http.HttpClient
5352
import java.net.http.HttpRequest
@@ -58,6 +57,7 @@ import javax.swing.JLabel
5857
import kotlin.coroutines.coroutineContext
5958
import kotlin.io.path.absolutePathString
6059
import kotlin.io.path.writeText
60+
import kotlin.random.Random.Default.nextInt
6161

6262
@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
6363
class GitpodConnectionProvider : GatewayConnectionProvider {
@@ -202,6 +202,35 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
202202

203203
var lastUpdate: WorkspaceInstance? = null
204204
var canceledByGitpod = false
205+
206+
val ownerToken = client.server.getOwnerToken(connectParams.actualWorkspaceId).await()
207+
208+
if (settings.additionalHeartbeat) {
209+
thisLogger().info("gitpod: additional heartbeat enabled for ${connectParams.resolvedWorkspaceId}")
210+
connectionLifetime.launch {
211+
while (isActive) {
212+
val delaySeconds = 30 + nextInt(5, 15)
213+
try {
214+
val ideUrlStr = lastUpdate?.ideUrl
215+
val ideUrl = if (ideUrlStr.isNullOrBlank()) {
216+
null
217+
} else {
218+
URL(ideUrlStr.replace(connectParams.actualWorkspaceId, connectParams.resolvedWorkspaceId))
219+
}
220+
if (lastUpdate?.status?.phase == "running" && ideUrl != null) {
221+
sendHeartBeatThroughSupervisor(ideUrl, ownerToken, connectParams)
222+
}
223+
} catch (t: Throwable) {
224+
thisLogger().error(
225+
"gitpod: failed to send additional heartbeat for ${connectParams.resolvedWorkspaceId}",
226+
t
227+
)
228+
}
229+
delay(delaySeconds * 1000L)
230+
}
231+
}
232+
}
233+
205234
try {
206235
for (update in updates) {
207236
try {
@@ -518,7 +547,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
518547
if (!connectParams.backendPort.isNullOrBlank()) {
519548
resolveJoinLinkUrl += "?backendPort=${connectParams.backendPort}"
520549
}
521-
var rawResp = fetchWS(resolveJoinLinkUrl, connectParams, ownerToken)
550+
var rawResp = retryFetchWS(resolveJoinLinkUrl, connectParams, ownerToken)
522551
if (rawResp != null) {
523552
return with(jacksonMapper) {
524553
propertyNamingStrategy = PropertyNamingStrategies.LowerCamelCaseStrategy()
@@ -531,13 +560,34 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
531560
if (!connectParams.backendPort.isNullOrBlank()) {
532561
resolveJoinLinkUrl += "?backendPort=${connectParams.backendPort}"
533562
}
534-
rawResp = fetchWS(resolveJoinLinkUrl, connectParams, ownerToken)
563+
rawResp = retryFetchWS(resolveJoinLinkUrl, connectParams, ownerToken)
535564
if (rawResp != null) {
536565
return JoinLinkResp(-1, rawResp)
537566
}
538567
return null
539568
}
540569

570+
private var sendHeartBeatThroughSupervisorLogOnce = false
571+
private suspend fun sendHeartBeatThroughSupervisor(
572+
ideUrl: URL,
573+
ownerToken: String,
574+
connectParams: ConnectParams
575+
) {
576+
val resp = fetchWS("https://${ideUrl.host}/_supervisor/v1/send_heartbeat", ownerToken, 2000L)
577+
if (resp.statusCode != 200) {
578+
if (!resp.body.isNullOrBlank() && resp.body.contains("not implemented")) {
579+
if (!sendHeartBeatThroughSupervisorLogOnce) {
580+
thisLogger().warn("gitpod: sendHeartbeat ${connectParams.actualWorkspaceId} failed: method is not implemented in supervisor")
581+
sendHeartBeatThroughSupervisorLogOnce = true
582+
}
583+
return
584+
}
585+
thisLogger().error("gitpod: sendHeartbeat ${connectParams.actualWorkspaceId} failed: ${resp.statusCode}, body: ${resp.body}")
586+
return
587+
}
588+
thisLogger().info("gitpod: =========sendHeartbeat succeed")
589+
}
590+
541591
private fun resolveCredentials(
542592
host: String,
543593
port: Int,
@@ -589,7 +639,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
589639
ownerToken: String
590640
): CreateSSHKeyPairResponse? {
591641
val value =
592-
fetchWS("https://${ideUrl.host}/_supervisor/v1/ssh_keys/create", connectParams, ownerToken)
642+
retryFetchWS("https://${ideUrl.host}/_supervisor/v1/ssh_keys/create", connectParams, ownerToken)
593643
if (value.isNullOrBlank()) {
594644
return null
595645
}
@@ -604,7 +654,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
604654
connectParams: ConnectParams
605655
): List<SSHHostKey>? {
606656
val hostKeysValue =
607-
fetchWS("https://${ideUrl.host}/_ssh/host_keys", connectParams, null)
657+
retryFetchWS("https://${ideUrl.host}/_ssh/host_keys", connectParams, null)
608658
if (hostKeysValue.isNullOrBlank()) {
609659
return null
610660
}
@@ -671,27 +721,50 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
671721
return acceptHostKey
672722
}
673723

724+
data class HttpResponseData(val statusCode: Int, val body: String?) {
725+
fun statusCode() = statusCode
726+
fun body() = body
727+
}
728+
674729
private suspend fun fetchWS(
675730
endpointUrl: String,
676-
connectParams: ConnectParams,
677731
ownerToken: String?,
732+
timeoutMillis: Long,
733+
): HttpResponseData {
734+
var httpRequestBuilder = HttpRequest.newBuilder()
735+
.uri(URI.create(endpointUrl))
736+
.GET()
737+
.timeout(Duration.ofMillis(timeoutMillis))
738+
if (!ownerToken.isNullOrBlank()) {
739+
httpRequestBuilder = httpRequestBuilder.header("x-gitpod-owner-token", ownerToken)
740+
}
741+
val httpRequest = httpRequestBuilder.build()
742+
val responseFuture =
743+
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
744+
745+
try {
746+
val response = responseFuture.await()
747+
return HttpResponseData(response.statusCode(), response.body())
748+
} catch (e: Exception) {
749+
if (responseFuture.isCancelled) {
750+
throw CancellationException()
751+
}
752+
throw e
753+
}
754+
}
755+
756+
private suspend fun retryFetchWS(
757+
endpointUrl: String,
758+
connectParams: ConnectParams,
759+
ownerToken: String?
678760
): String? {
679761
val maxRequestTimeout = 30 * 1000L
680762
val timeoutDelayGrowFactor = 1.5
681763
var requestTimeout = 2 * 1000L
682764
while (true) {
683765
coroutineContext.job.ensureActive()
684766
try {
685-
var httpRequestBuilder = HttpRequest.newBuilder()
686-
.uri(URI.create(endpointUrl))
687-
.GET()
688-
.timeout(Duration.ofMillis(requestTimeout))
689-
if (!ownerToken.isNullOrBlank()) {
690-
httpRequestBuilder = httpRequestBuilder.header("x-gitpod-owner-token", ownerToken)
691-
}
692-
val httpRequest = httpRequestBuilder.build()
693-
val response =
694-
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()).await()
767+
val response = fetchWS(endpointUrl, ownerToken, requestTimeout)
695768
if (response.statusCode() == 200) {
696769
return response.body()
697770
}

components/ide/jetbrains/gateway-plugin/src/main/kotlin/io/gitpod/jetbrains/gateway/GitpodSettingsConfigurable.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class GitpodSettingsConfigurable : BoundConfigurable("Gitpod") {
3131
.comment("Helpful if you are behind a firewall/proxy that blocks SSH or " +
3232
"have complicated SSH setup (bastions, proxy jumps, etc.)")
3333
}
34+
row {
35+
checkBox("Additional heartbeat")
36+
.bindSelected(state::additionalHeartbeat)
37+
.comment("Additional heartbeat when connection is connected, helps to keep the workspace alive more reliably")
38+
}
3439

3540
}
3641
}

components/ide/jetbrains/gateway-plugin/src/main/kotlin/io/gitpod/jetbrains/gateway/GitpodSettingsState.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ class GitpodSettingsState : PersistentStateComponent<GitpodSettingsState> {
4545
dispatcher.multicaster.didChange()
4646
}
4747

48+
var additionalHeartbeat: Boolean = false
49+
set(value) {
50+
if (value == field) {
51+
return
52+
}
53+
field = value
54+
dispatcher.multicaster.didChange()
55+
}
56+
4857
private interface Listener : EventListener {
4958
fun didChange()
5059
}

components/ide/jetbrains/gateway-plugin/src/main/kotlin/io/gitpod/jetbrains/gateway/GitpodWorkspacesView.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package io.gitpod.jetbrains.gateway
66

77
import com.intellij.icons.AllIcons
88
import com.intellij.ide.BrowserUtil
9+
import com.intellij.ide.plugins.PluginManagerCore
910
import com.intellij.openapi.CompositeDisposable
1011
import com.intellij.openapi.actionSystem.AnActionEvent
1112
import com.intellij.openapi.application.ApplicationManager
1213
import com.intellij.openapi.components.service
1314
import com.intellij.openapi.diagnostic.thisLogger
15+
import com.intellij.openapi.extensions.PluginId
1416
import com.intellij.openapi.project.DumbAwareAction
1517
import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeScreenUIManager
1618
import com.intellij.remoteDev.util.onTerminationOrNow
@@ -106,10 +108,12 @@ class GitpodWorkspacesView(
106108
}
107109
}.visibleIf(loggedIn.not())
108110

111+
val pluginVersion = PluginManagerCore.getPlugin(PluginId.getId("io.gitpod.jetbrains.gateway"))?.version
112+
val pluginVersionLabel = if (pluginVersion?.contains("-local") == true) " (${pluginVersion})" else ""
109113
rowsRange {
110114
row {
111115
icon(GitpodIcons.Logo).gap(RightGap.SMALL)
112-
label("Gitpod").applyToComponent {
116+
label("Gitpod${pluginVersionLabel}").applyToComponent {
113117
this.font = JBFont.h3().asBold()
114118
}
115119
label("").resizableColumn().align(AlignX.FILL)

0 commit comments

Comments
 (0)