-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoderRemoteProvider.kt
362 lines (328 loc) · 13.8 KB
/
CoderRemoteProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.coder.toolbox
import com.coder.toolbox.cli.CoderCLIManager
import com.coder.toolbox.sdk.CoderRestClient
import com.coder.toolbox.sdk.v2.models.WorkspaceStatus
import com.coder.toolbox.services.CoderSecretsService
import com.coder.toolbox.services.CoderSettingsService
import com.coder.toolbox.settings.CoderSettings
import com.coder.toolbox.settings.Source
import com.coder.toolbox.util.DialogUi
import com.coder.toolbox.util.LinkHandler
import com.coder.toolbox.util.toQueryParameters
import com.coder.toolbox.views.Action
import com.coder.toolbox.views.CoderSettingsPage
import com.coder.toolbox.views.ConnectPage
import com.coder.toolbox.views.NewEnvironmentPage
import com.coder.toolbox.views.SignInPage
import com.coder.toolbox.views.TokenPage
import com.jetbrains.toolbox.api.core.ui.icons.SvgIcon
import com.jetbrains.toolbox.api.core.ui.icons.SvgIcon.IconType
import com.jetbrains.toolbox.api.core.util.LoadableState
import com.jetbrains.toolbox.api.remoteDev.ProviderVisibilityState
import com.jetbrains.toolbox.api.remoteDev.RemoteProvider
import com.jetbrains.toolbox.api.remoteDev.RemoteProviderEnvironment
import com.jetbrains.toolbox.api.ui.actions.ActionDescription
import com.jetbrains.toolbox.api.ui.components.UiPage
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import java.net.URI
import java.net.URL
import kotlin.coroutines.cancellation.CancellationException
import kotlin.time.Duration.Companion.seconds
import com.jetbrains.toolbox.api.ui.components.AccountDropdownField as DropDownMenu
import com.jetbrains.toolbox.api.ui.components.AccountDropdownField as dropDownFactory
class CoderRemoteProvider(
private val context: CoderToolboxContext,
private val httpClient: OkHttpClient,
) : RemoteProvider("Coder") {
// Current polling job.
private var pollJob: Job? = null
private var lastEnvironments: Set<CoderRemoteEnvironment>? = null
// Create our services from the Toolbox ones.
private val settingsService = CoderSettingsService(context.settingsStore)
private val settings: CoderSettings = CoderSettings(settingsService, context.logger)
private val secrets: CoderSecretsService = CoderSecretsService(context.secretsStore)
private val settingsPage: CoderSettingsPage = CoderSettingsPage(context, settingsService)
private val dialogUi = DialogUi(context, settings)
private val linkHandler = LinkHandler(context, settings, httpClient, dialogUi)
// The REST client, if we are signed in
private var client: CoderRestClient? = null
// If we have an error in the polling we store it here before going back to
// sign-in page, so we can display it there. This is mainly because there
// does not seem to be a mechanism to show errors on the environment list.
private var pollError: Exception? = null
// On the first load, automatically log in if we can.
private var firstRun = true
override val environments: MutableStateFlow<LoadableState<List<RemoteProviderEnvironment>>> = MutableStateFlow(
LoadableState.Value(emptyList())
)
/**
* With the provided client, start polling for workspaces. Every time a new
* workspace is added, reconfigure SSH using the provided cli (including the
* first time).
*/
private fun poll(client: CoderRestClient, cli: CoderCLIManager): Job = context.cs.launch {
while (isActive) {
try {
context.logger.debug("Fetching workspace agents from ${client.url}")
val resolvedEnvironments = client.workspaces().flatMap { ws ->
// Agents are not included in workspaces that are off
// so fetch them separately.
when (ws.latestBuild.status) {
WorkspaceStatus.RUNNING -> ws.latestBuild.resources
else -> emptyList()
}.ifEmpty {
client.resources(ws)
}.flatMap { resource ->
resource.agents?.distinctBy {
// There can be duplicates with coder_agent_instance.
// TODO: Can we just choose one or do they hold
// different information?
it.name
}?.map { agent ->
// If we have an environment already, update that.
val env = CoderRemoteEnvironment(context, client, ws, agent)
lastEnvironments?.firstOrNull { it == env }?.let {
it.update(ws, agent)
it
} ?: env
} ?: emptyList()
}
}.toSet()
// In case we logged out while running the query.
if (!isActive) {
return@launch
}
// Reconfigure if a new environment is found.
// TODO@JB: Should we use the add/remove listeners instead?
val newEnvironments = lastEnvironments
?.let { resolvedEnvironments.subtract(it) }
?: resolvedEnvironments
if (newEnvironments.isNotEmpty()) {
context.logger.info("Found new environment(s), reconfiguring CLI: $newEnvironments")
cli.configSsh(newEnvironments.map { it.name }.toSet())
}
environments.update {
LoadableState.Value(resolvedEnvironments.toList())
}
lastEnvironments = resolvedEnvironments
} catch (_: CancellationException) {
context.logger.debug("${client.url} polling loop canceled")
break
} catch (ex: Exception) {
context.logger.info(ex, "workspace polling error encountered")
pollError = ex
logout()
break
}
// TODO: Listening on a web socket might be better?
delay(5.seconds)
}
}
/**
* Stop polling, clear the client and environments, then go back to the
* first page.
*/
private fun logout() {
// Keep the URL and token to make it easy to log back in, but set
// rememberMe to false so we do not try to automatically log in.
secrets.rememberMe = "false"
close()
}
/**
* A dropdown that appears at the top of the environment list to the right.
*/
override fun getAccountDropDown(): DropDownMenu? {
val username = client?.me?.username
if (username != null) {
return dropDownFactory(context.i18n.pnotr(username), { logout() })
}
return null
}
override val additionalPluginActions: StateFlow<List<ActionDescription>> = MutableStateFlow(
listOf(
Action(context.i18n.ptrl("Settings")) {
context.ui.showUiPage(settingsPage)
},
)
)
/**
* Cancel polling and clear the client and environments.
*
* Called as part of our own logout but it is unclear where it is called by
* Toolbox. Maybe on uninstall?
*/
override fun close() {
pollJob?.cancel()
client = null
lastEnvironments = null
environments.value = LoadableState.Value(emptyList())
}
override val svgIcon: SvgIcon =
SvgIcon(
this::class.java.getResourceAsStream("/icon.svg")?.readAllBytes() ?: byteArrayOf(),
type = IconType.Masked
)
override val noEnvironmentsSvgIcon: SvgIcon? =
SvgIcon(
this::class.java.getResourceAsStream("/icon.svg")?.readAllBytes() ?: byteArrayOf(),
type = IconType.Masked
)
/**
* TODO@JB: It would be nice to show "loading workspaces" at first but it
* appears to be only called once.
*/
override val noEnvironmentsDescription: String? = "No workspaces yet"
/**
* TODO@JB: Supposedly, setting this to false causes the new environment
* page to not show but it shows anyway. For now we have it
* displaying the deployment URL, which is actually useful, so if
* this changes it would be nice to have a new spot to show the
* URL.
*/
override val canCreateNewEnvironments: Boolean = false
/**
* Just displays the deployment URL at the moment, but we could use this as
* a form for creating new environments.
*/
override fun getNewEnvironmentUiPage(): UiPage =
NewEnvironmentPage(context, context.i18n.pnotr(getDeploymentURL()?.first ?: ""))
/**
* We always show a list of environments.
*/
override val isSingleEnvironment: Boolean = false
/**
* TODO: Possibly a good idea to start/stop polling based on visibility, at
* the cost of momentarily stale data. It would not be bad if we had
* a place to put a timer ("last updated 10 seconds ago" for example)
* and a manual refresh button.
*/
override fun setVisible(visibilityState: ProviderVisibilityState) {}
/**
* Handle incoming links (like from the dashboard).
*/
override suspend fun handleUri(uri: URI) {
val params = uri.toQueryParameters()
context.cs.launch {
val name = linkHandler.handle(params)
// TODO@JB: Now what? How do we actually connect this workspace?
context.logger.debug("External request for $name: $uri")
}
}
/**
* Make Toolbox ask for the page again. Use any time we need to change the
* root page (for example, sign-in or the environment list).
*
* When moving between related pages, instead use ui.showUiPage() and
* ui.hideUiPage() which stacks and has built-in back navigation, rather
* than using multiple root pages.
*/
private fun goToEnvironmentsPage() {
context.envPageManager.showPluginEnvironmentsPage()
}
/**
* Return the sign-in page if we do not have a valid client.
* Otherwise return null, which causes Toolbox to display the environment
* list.
*/
override fun getOverrideUiPage(): UiPage? {
// Show sign in page if we have not configured the client yet.
if (client == null) {
// When coming back to the application, authenticate immediately.
val autologin = firstRun && secrets.rememberMe == "true"
var autologinEx: Exception? = null
secrets.lastToken.let { lastToken ->
secrets.lastDeploymentURL.let { lastDeploymentURL ->
if (autologin && lastDeploymentURL.isNotBlank() && (lastToken.isNotBlank() || !settings.requireTokenAuth)) {
try {
return createConnectPage(URL(lastDeploymentURL), lastToken)
} catch (ex: Exception) {
autologinEx = ex
}
}
}
}
firstRun = false
// Login flow.
val signInPage =
SignInPage(context, getDeploymentURL()) { deploymentURL ->
context.ui.showUiPage(
TokenPage(
context,
deploymentURL,
getToken(deploymentURL)
) { selectedToken ->
context.ui.showUiPage(createConnectPage(deploymentURL, selectedToken))
},
)
}
// We might have tried and failed to automatically log in.
autologinEx?.let { signInPage.notify("Error logging in", it) }
// We might have navigated here due to a polling error.
pollError?.let { signInPage.notify("Error fetching workspaces", it) }
return signInPage
}
return null
}
/**
* Create a connect page that starts polling and resets the UI on success.
*/
private fun createConnectPage(deploymentURL: URL, token: String?): ConnectPage = ConnectPage(
context,
deploymentURL,
token,
settings,
httpClient,
::goToEnvironmentsPage,
) { client, cli ->
// Store the URL and token for use next time.
secrets.lastDeploymentURL = client.url.toString()
secrets.lastToken = client.token ?: ""
// Currently we always remember, but this could be made an option.
secrets.rememberMe = "true"
this.client = client
pollError = null
pollJob?.cancel()
pollJob = poll(client, cli)
goToEnvironmentsPage()
}
/**
* Try to find a token.
*
* Order of preference:
*
* 1. Last used token, if it was for this deployment.
* 2. Token on disk for this deployment.
* 3. Global token for Coder, if it matches the deployment.
*/
private fun getToken(deploymentURL: URL): Pair<String, Source>? = secrets.lastToken.let {
if (it.isNotBlank() && secrets.lastDeploymentURL == deploymentURL.toString()) {
it to Source.LAST_USED
} else {
settings.token(deploymentURL)
}
}
/**
* Try to find a URL.
*
* In order of preference:
*
* 1. Last used URL.
* 2. URL in settings.
* 3. CODER_URL.
* 4. URL in global cli config.
*/
private fun getDeploymentURL(): Pair<String, Source>? = secrets.lastDeploymentURL.let {
if (it.isNotBlank()) {
it to Source.LAST_USED
} else {
settings.defaultURL()
}
}
}