-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathworkspaceagents.go
More file actions
749 lines (666 loc) · 27.7 KB
/
workspaceagents.go
File metadata and controls
749 lines (666 loc) · 27.7 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package codersdk
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/google/uuid"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/tracing"
"github.com/coder/coder/v2/codersdk/wsjson"
"github.com/coder/websocket"
)
type WorkspaceAgentStatus string
// This is also in database/modelmethods.go and should be kept in sync.
const (
WorkspaceAgentConnecting WorkspaceAgentStatus = "connecting"
WorkspaceAgentConnected WorkspaceAgentStatus = "connected"
WorkspaceAgentDisconnected WorkspaceAgentStatus = "disconnected"
WorkspaceAgentTimeout WorkspaceAgentStatus = "timeout"
)
// WorkspaceAgentLifecycle represents the lifecycle state of a workspace agent.
//
// The agent lifecycle starts in the "created" state, and transitions to
// "starting" when the agent reports it has begun preparing (e.g. started
// executing the startup script).
type WorkspaceAgentLifecycle string
// WorkspaceAgentLifecycle enums.
const (
WorkspaceAgentLifecycleCreated WorkspaceAgentLifecycle = "created"
WorkspaceAgentLifecycleStarting WorkspaceAgentLifecycle = "starting"
WorkspaceAgentLifecycleStartTimeout WorkspaceAgentLifecycle = "start_timeout"
WorkspaceAgentLifecycleStartError WorkspaceAgentLifecycle = "start_error"
WorkspaceAgentLifecycleReady WorkspaceAgentLifecycle = "ready"
WorkspaceAgentLifecycleShuttingDown WorkspaceAgentLifecycle = "shutting_down"
WorkspaceAgentLifecycleShutdownTimeout WorkspaceAgentLifecycle = "shutdown_timeout"
WorkspaceAgentLifecycleShutdownError WorkspaceAgentLifecycle = "shutdown_error"
WorkspaceAgentLifecycleOff WorkspaceAgentLifecycle = "off"
)
// Starting returns true if the agent is in the process of starting.
func (l WorkspaceAgentLifecycle) Starting() bool {
switch l {
case WorkspaceAgentLifecycleCreated, WorkspaceAgentLifecycleStarting:
return true
default:
return false
}
}
// ShuttingDown returns true if the agent is in the process of shutting
// down or has shut down.
func (l WorkspaceAgentLifecycle) ShuttingDown() bool {
switch l {
case WorkspaceAgentLifecycleShuttingDown, WorkspaceAgentLifecycleShutdownTimeout, WorkspaceAgentLifecycleShutdownError, WorkspaceAgentLifecycleOff:
return true
default:
return false
}
}
// WorkspaceAgentLifecycleOrder is the order in which workspace agent
// lifecycle states are expected to be reported during the lifetime of
// the agent process. For instance, the agent can go from starting to
// ready without reporting timeout or error, but it should not go from
// ready to starting. This is merely a hint for the agent process, and
// is not enforced by the server.
var WorkspaceAgentLifecycleOrder = []WorkspaceAgentLifecycle{
WorkspaceAgentLifecycleCreated,
WorkspaceAgentLifecycleStarting,
WorkspaceAgentLifecycleStartTimeout,
WorkspaceAgentLifecycleStartError,
WorkspaceAgentLifecycleReady,
WorkspaceAgentLifecycleShuttingDown,
WorkspaceAgentLifecycleShutdownTimeout,
WorkspaceAgentLifecycleShutdownError,
WorkspaceAgentLifecycleOff,
}
// WorkspaceAgentStartupScriptBehavior defines whether or not the startup script
// should be considered blocking or non-blocking. The blocking behavior means
// that the agent will not be considered ready until the startup script has
// completed and, for example, SSH connections will wait for the agent to be
// ready (can be overridden).
//
// Presently, non-blocking is the default, but this may change in the future.
// Deprecated: `coder_script` allows configuration on a per-script basis.
type WorkspaceAgentStartupScriptBehavior string
const (
WorkspaceAgentStartupScriptBehaviorBlocking WorkspaceAgentStartupScriptBehavior = "blocking"
WorkspaceAgentStartupScriptBehaviorNonBlocking WorkspaceAgentStartupScriptBehavior = "non-blocking"
)
type WorkspaceAgentMetadataResult struct {
CollectedAt time.Time `json:"collected_at" format:"date-time"`
// Age is the number of seconds since the metadata was collected.
// It is provided in addition to CollectedAt to protect against clock skew.
Age int64 `json:"age"`
Value string `json:"value"`
Error string `json:"error"`
}
// WorkspaceAgentMetadataDescription is a description of dynamic metadata the agent should report
// back to coderd. It is provided via the `metadata` list in the `coder_agent`
// block.
type WorkspaceAgentMetadataDescription struct {
DisplayName string `json:"display_name"`
Key string `json:"key"`
Script string `json:"script"`
Interval int64 `json:"interval"`
Timeout int64 `json:"timeout"`
}
type WorkspaceAgentMetadata struct {
Result WorkspaceAgentMetadataResult `json:"result"`
Description WorkspaceAgentMetadataDescription `json:"description"`
}
type DisplayApp string
const (
DisplayAppVSCodeDesktop DisplayApp = "vscode"
DisplayAppVSCodeInsiders DisplayApp = "vscode_insiders"
DisplayAppWebTerminal DisplayApp = "web_terminal"
DisplayAppPortForward DisplayApp = "port_forwarding_helper"
DisplayAppSSH DisplayApp = "ssh_helper"
)
type WorkspaceAgent struct {
ID uuid.UUID `json:"id" format:"uuid"`
ParentID uuid.NullUUID `json:"parent_id" format:"uuid"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
UpdatedAt time.Time `json:"updated_at" format:"date-time"`
FirstConnectedAt *time.Time `json:"first_connected_at,omitempty" format:"date-time"`
LastConnectedAt *time.Time `json:"last_connected_at,omitempty" format:"date-time"`
DisconnectedAt *time.Time `json:"disconnected_at,omitempty" format:"date-time"`
StartedAt *time.Time `json:"started_at,omitempty" format:"date-time"`
ReadyAt *time.Time `json:"ready_at,omitempty" format:"date-time"`
Status WorkspaceAgentStatus `json:"status"`
LifecycleState WorkspaceAgentLifecycle `json:"lifecycle_state"`
Name string `json:"name"`
ResourceID uuid.UUID `json:"resource_id" format:"uuid"`
InstanceID string `json:"instance_id,omitempty"`
Architecture string `json:"architecture"`
EnvironmentVariables map[string]string `json:"environment_variables"`
OperatingSystem string `json:"operating_system"`
LogsLength int32 `json:"logs_length"`
LogsOverflowed bool `json:"logs_overflowed"`
Directory string `json:"directory,omitempty"`
ExpandedDirectory string `json:"expanded_directory,omitempty"`
Version string `json:"version"`
APIVersion string `json:"api_version"`
Apps []WorkspaceApp `json:"apps"`
// DERPLatency is mapped by region name (e.g. "New York City", "Seattle").
DERPLatency map[string]DERPRegion `json:"latency,omitempty"`
ConnectionTimeoutSeconds int32 `json:"connection_timeout_seconds"`
TroubleshootingURL string `json:"troubleshooting_url"`
Subsystems []AgentSubsystem `json:"subsystems"`
Health WorkspaceAgentHealth `json:"health"` // Health reports the health of the agent.
DisplayApps []DisplayApp `json:"display_apps"`
LogSources []WorkspaceAgentLogSource `json:"log_sources"`
Scripts []WorkspaceAgentScript `json:"scripts"`
// StartupScriptBehavior is a legacy field that is deprecated in favor
// of the `coder_script` resource. It's only referenced by old clients.
// Deprecated: Remove in the future!
StartupScriptBehavior WorkspaceAgentStartupScriptBehavior `json:"startup_script_behavior"`
}
type WorkspaceAgentLogSource struct {
WorkspaceAgentID uuid.UUID `json:"workspace_agent_id" format:"uuid"`
ID uuid.UUID `json:"id" format:"uuid"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
DisplayName string `json:"display_name"`
Icon string `json:"icon"`
}
type WorkspaceAgentScript struct {
ID uuid.UUID `json:"id" format:"uuid"`
LogSourceID uuid.UUID `json:"log_source_id" format:"uuid"`
LogPath string `json:"log_path"`
Script string `json:"script"`
Cron string `json:"cron"`
RunOnStart bool `json:"run_on_start"`
RunOnStop bool `json:"run_on_stop"`
StartBlocksLogin bool `json:"start_blocks_login"`
Timeout time.Duration `json:"timeout"`
DisplayName string `json:"display_name"`
}
type WorkspaceAgentHealth struct {
Healthy bool `json:"healthy" example:"false"` // Healthy is true if the agent is healthy.
Reason string `json:"reason,omitempty" example:"agent has lost connection"` // Reason is a human-readable explanation of the agent's health. It is empty if Healthy is true.
}
type DERPRegion struct {
Preferred bool `json:"preferred"`
LatencyMilliseconds float64 `json:"latency_ms"`
}
type WorkspaceAgentLog struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
Output string `json:"output"`
Level LogLevel `json:"level"`
SourceID uuid.UUID `json:"source_id" format:"uuid"`
}
// Text formats the log entry as human-readable text.
func (l WorkspaceAgentLog) Text(agentName, sourceName string) string {
var sb strings.Builder
_, _ = sb.WriteString(l.CreatedAt.Format(time.RFC3339))
_, _ = sb.WriteString(" [")
_, _ = sb.WriteString(string(l.Level))
_, _ = sb.WriteString("] [agent")
if agentName != "" {
_, _ = sb.WriteString(".")
_, _ = sb.WriteString(agentName)
}
if sourceName != "" {
_, _ = sb.WriteString("|")
_, _ = sb.WriteString(sourceName)
}
_, _ = sb.WriteString("] ")
_, _ = sb.WriteString(l.Output)
return sb.String()
}
type AgentSubsystem string
const (
AgentSubsystemEnvbox AgentSubsystem = "envbox"
AgentSubsystemEnvbuilder AgentSubsystem = "envbuilder"
AgentSubsystemExectrace AgentSubsystem = "exectrace"
)
func (s AgentSubsystem) Valid() bool {
switch s {
case AgentSubsystemEnvbox, AgentSubsystemEnvbuilder, AgentSubsystemExectrace:
return true
default:
return false
}
}
// WatchWorkspaceAgentMetadata watches the metadata of a workspace agent.
// The returned channel will be closed when the context is canceled. Exactly
// one error will be sent on the error channel. The metadata channel is never closed.
func (c *Client) WatchWorkspaceAgentMetadata(ctx context.Context, id uuid.UUID) (<-chan []WorkspaceAgentMetadata, <-chan error) {
ctx, span := tracing.StartSpan(ctx)
defer span.End()
metadataChan := make(chan []WorkspaceAgentMetadata, 256)
ready := make(chan struct{})
watch := func() error {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaceagents/%s/watch-metadata", id), nil)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return ReadBodyAsError(res)
}
nextEvent := ServerSentEventReader(ctx, res.Body)
defer res.Body.Close()
firstEvent := true
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
sse, err := nextEvent()
if err != nil {
return err
}
if firstEvent {
close(ready) // Only close ready after the first event is received.
firstEvent = false
}
// Ignore pings.
if sse.Type == ServerSentEventTypePing {
continue
}
b, ok := sse.Data.([]byte)
if !ok {
return xerrors.Errorf("unexpected data type: %T", sse.Data)
}
switch sse.Type {
case ServerSentEventTypeData:
var met []WorkspaceAgentMetadata
err = json.Unmarshal(b, &met)
if err != nil {
return xerrors.Errorf("unmarshal metadata: %w", err)
}
metadataChan <- met
case ServerSentEventTypeError:
var r Response
err = json.Unmarshal(b, &r)
if err != nil {
return xerrors.Errorf("unmarshal error: %w", err)
}
return xerrors.Errorf("%+v", r)
default:
return xerrors.Errorf("unexpected event type: %s", sse.Type)
}
}
}
errorChan := make(chan error, 1)
go func() {
defer close(errorChan)
err := watch()
select {
case <-ready:
default:
close(ready) // Error before first event.
}
errorChan <- err
}()
// Wait until first event is received and the subscription is registered.
<-ready
return metadataChan, errorChan
}
// WorkspaceAgent returns an agent by ID.
func (c *Client) WorkspaceAgent(ctx context.Context, id uuid.UUID) (WorkspaceAgent, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaceagents/%s", id), nil)
if err != nil {
return WorkspaceAgent{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return WorkspaceAgent{}, ReadBodyAsError(res)
}
var workspaceAgent WorkspaceAgent
err = json.NewDecoder(res.Body).Decode(&workspaceAgent)
if err != nil {
return WorkspaceAgent{}, err
}
return workspaceAgent, nil
}
type IssueReconnectingPTYSignedTokenRequest struct {
// URL is the URL of the reconnecting-pty endpoint you are connecting to.
URL string `json:"url" validate:"required"`
AgentID uuid.UUID `json:"agentID" format:"uuid" validate:"required"`
}
type IssueReconnectingPTYSignedTokenResponse struct {
SignedToken string `json:"signed_token"`
}
func (c *Client) IssueReconnectingPTYSignedToken(ctx context.Context, req IssueReconnectingPTYSignedTokenRequest) (IssueReconnectingPTYSignedTokenResponse, error) {
res, err := c.Request(ctx, http.MethodPost, "/api/v2/applications/reconnecting-pty-signed-token", req)
if err != nil {
return IssueReconnectingPTYSignedTokenResponse{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return IssueReconnectingPTYSignedTokenResponse{}, ReadBodyAsError(res)
}
var resp IssueReconnectingPTYSignedTokenResponse
return resp, json.NewDecoder(res.Body).Decode(&resp)
}
type WorkspaceAgentListeningPortsResponse struct {
// If there are no ports in the list, nothing should be displayed in the UI.
// There must not be a "no ports available" message or anything similar, as
// there will always be no ports displayed on platforms where our port
// detection logic is unsupported.
Ports []WorkspaceAgentListeningPort `json:"ports"`
}
type WorkspaceAgentListeningPort struct {
ProcessName string `json:"process_name"` // may be empty
Network string `json:"network"` // only "tcp" at the moment
Port uint16 `json:"port"`
}
// WorkspaceAgentListeningPorts returns a list of ports that are currently being
// listened on inside the workspace agent's network namespace.
func (c *Client) WorkspaceAgentListeningPorts(ctx context.Context, agentID uuid.UUID) (WorkspaceAgentListeningPortsResponse, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaceagents/%s/listening-ports", agentID), nil)
if err != nil {
return WorkspaceAgentListeningPortsResponse{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return WorkspaceAgentListeningPortsResponse{}, ReadBodyAsError(res)
}
var listeningPorts WorkspaceAgentListeningPortsResponse
return listeningPorts, json.NewDecoder(res.Body).Decode(&listeningPorts)
}
// WorkspaceAgentDevcontainerStatus is the status of a devcontainer.
type WorkspaceAgentDevcontainerStatus string
// WorkspaceAgentDevcontainerStatus enums.
const (
WorkspaceAgentDevcontainerStatusRunning WorkspaceAgentDevcontainerStatus = "running"
WorkspaceAgentDevcontainerStatusStopped WorkspaceAgentDevcontainerStatus = "stopped"
WorkspaceAgentDevcontainerStatusStarting WorkspaceAgentDevcontainerStatus = "starting"
WorkspaceAgentDevcontainerStatusStopping WorkspaceAgentDevcontainerStatus = "stopping"
WorkspaceAgentDevcontainerStatusDeleting WorkspaceAgentDevcontainerStatus = "deleting"
WorkspaceAgentDevcontainerStatusError WorkspaceAgentDevcontainerStatus = "error"
)
func (s WorkspaceAgentDevcontainerStatus) Transitioning() bool {
switch s {
case WorkspaceAgentDevcontainerStatusStarting,
WorkspaceAgentDevcontainerStatusStopping,
WorkspaceAgentDevcontainerStatusDeleting:
return true
default:
return false
}
}
// WorkspaceAgentDevcontainer defines the location of a devcontainer
// configuration in a workspace that is visible to the workspace agent.
type WorkspaceAgentDevcontainer struct {
ID uuid.UUID `json:"id" format:"uuid"`
Name string `json:"name"`
WorkspaceFolder string `json:"workspace_folder"`
ConfigPath string `json:"config_path,omitempty"`
SubagentID uuid.NullUUID `json:"subagent_id,omitempty" format:"uuid"`
// Additional runtime fields.
Status WorkspaceAgentDevcontainerStatus `json:"status"`
Dirty bool `json:"dirty"`
Container *WorkspaceAgentContainer `json:"container,omitempty"`
Agent *WorkspaceAgentDevcontainerAgent `json:"agent,omitempty"`
Error string `json:"error,omitempty"`
}
func (d WorkspaceAgentDevcontainer) Equals(other WorkspaceAgentDevcontainer) bool {
return d.ID == other.ID &&
d.Name == other.Name &&
d.WorkspaceFolder == other.WorkspaceFolder &&
d.SubagentID == other.SubagentID &&
d.Status == other.Status &&
d.Dirty == other.Dirty &&
(d.Container == nil && other.Container == nil ||
(d.Container != nil && other.Container != nil && d.Container.ID == other.Container.ID)) &&
(d.Agent == nil && other.Agent == nil ||
(d.Agent != nil && other.Agent != nil && *d.Agent == *other.Agent)) &&
d.Error == other.Error
}
// IsTerraformDefined returns true if this devcontainer has resources defined
// in Terraform.
func (d WorkspaceAgentDevcontainer) IsTerraformDefined() bool {
return d.SubagentID.Valid
}
// WorkspaceAgentDevcontainerAgent represents the sub agent for a
// devcontainer.
type WorkspaceAgentDevcontainerAgent struct {
ID uuid.UUID `json:"id" format:"uuid"`
Name string `json:"name"`
Directory string `json:"directory"`
}
// WorkspaceAgentContainer describes a devcontainer of some sort
// that is visible to the workspace agent. This struct is an abstraction
// of potentially multiple implementations, and the fields will be
// somewhat implementation-dependent.
type WorkspaceAgentContainer struct {
// CreatedAt is the time the container was created.
CreatedAt time.Time `json:"created_at" format:"date-time"`
// ID is the unique identifier of the container.
ID string `json:"id"`
// FriendlyName is the human-readable name of the container.
FriendlyName string `json:"name"`
// Image is the name of the container image.
Image string `json:"image"`
// Labels is a map of key-value pairs of container labels.
Labels map[string]string `json:"labels"`
// Running is true if the container is currently running.
Running bool `json:"running"`
// Ports includes ports exposed by the container.
Ports []WorkspaceAgentContainerPort `json:"ports"`
// Status is the current status of the container. This is somewhat
// implementation-dependent, but should generally be a human-readable
// string.
Status string `json:"status"`
// Volumes is a map of "things" mounted into the container. Again, this
// is somewhat implementation-dependent.
Volumes map[string]string `json:"volumes"`
}
func (c *WorkspaceAgentContainer) Match(idOrName string) bool {
if c.ID == idOrName {
return true
}
if c.FriendlyName == idOrName {
return true
}
return false
}
// WorkspaceAgentContainerPort describes a port as exposed by a container.
type WorkspaceAgentContainerPort struct {
// Port is the port number *inside* the container.
Port uint16 `json:"port"`
// Network is the network protocol used by the port (tcp, udp, etc).
Network string `json:"network"`
// HostIP is the IP address of the host interface to which the port is
// bound. Note that this can be an IPv4 or IPv6 address.
HostIP string `json:"host_ip,omitempty"`
// HostPort is the port number *outside* the container.
HostPort uint16 `json:"host_port,omitempty"`
}
// WorkspaceAgentListContainersResponse is the response to the list containers
// request.
type WorkspaceAgentListContainersResponse struct {
// Devcontainers is a list of devcontainers visible to the workspace agent.
Devcontainers []WorkspaceAgentDevcontainer `json:"devcontainers"`
// Containers is a list of containers visible to the workspace agent.
Containers []WorkspaceAgentContainer `json:"containers"`
// Warnings is a list of warnings that may have occurred during the
// process of listing containers. This should not include fatal errors.
Warnings []string `json:"warnings,omitempty"`
}
func workspaceAgentContainersLabelFilter(kvs map[string]string) RequestOption {
return func(r *http.Request) {
q := r.URL.Query()
for k, v := range kvs {
kv := fmt.Sprintf("%s=%s", k, v)
q.Add("label", kv)
}
r.URL.RawQuery = q.Encode()
}
}
// WorkspaceAgentListContainers returns a list of containers that are currently
// running on a Docker daemon accessible to the workspace agent.
func (c *Client) WorkspaceAgentListContainers(ctx context.Context, agentID uuid.UUID, labels map[string]string) (WorkspaceAgentListContainersResponse, error) {
lf := workspaceAgentContainersLabelFilter(labels)
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaceagents/%s/containers", agentID), nil, lf)
if err != nil {
return WorkspaceAgentListContainersResponse{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return WorkspaceAgentListContainersResponse{}, ReadBodyAsError(res)
}
var cr WorkspaceAgentListContainersResponse
return cr, json.NewDecoder(res.Body).Decode(&cr)
}
func (c *Client) WatchWorkspaceAgentContainers(ctx context.Context, agentID uuid.UUID) (<-chan WorkspaceAgentListContainersResponse, io.Closer, error) {
reqURL, err := c.URL.Parse(fmt.Sprintf("/api/v2/workspaceagents/%s/containers/watch", agentID))
if err != nil {
return nil, nil, err
}
conn, res, err := websocket.Dial(ctx, reqURL.String(), &websocket.DialOptions{
// We want `NoContextTakeover` compression to balance improving
// bandwidth cost/latency with minimal memory usage overhead.
CompressionMode: websocket.CompressionNoContextTakeover,
HTTPClient: &http.Client{
Transport: c.HTTPClient.Transport,
},
HTTPHeader: http.Header{
SessionTokenHeader: []string{c.SessionToken()},
},
})
if err != nil {
if res == nil {
return nil, nil, err
}
return nil, nil, ReadBodyAsError(res)
}
// When a workspace has a few devcontainers running, or a single devcontainer
// has a large amount of apps, then each payload can easily exceed 32KiB.
// We up the limit to 4MiB to give us plenty of headroom for workspaces that
// have lots of dev containers with lots of apps.
conn.SetReadLimit(1 << 22) // 4MiB
d := wsjson.NewDecoder[WorkspaceAgentListContainersResponse](conn, websocket.MessageText, c.logger)
return d.Chan(), d, nil
}
// WorkspaceAgentDeleteDevcontainer deletes the devcontainer with the given ID.
func (c *Client) WorkspaceAgentDeleteDevcontainer(ctx context.Context, agentID uuid.UUID, devcontainerID string) error {
res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/v2/workspaceagents/%s/containers/devcontainers/%s", agentID, devcontainerID), nil)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusNoContent {
return ReadBodyAsError(res)
}
return nil
}
// WorkspaceAgentRecreateDevcontainer recreates the devcontainer with the given ID.
func (c *Client) WorkspaceAgentRecreateDevcontainer(ctx context.Context, agentID uuid.UUID, devcontainerID string) (Response, error) {
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/workspaceagents/%s/containers/devcontainers/%s/recreate", agentID, devcontainerID), nil)
if err != nil {
return Response{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusAccepted {
return Response{}, ReadBodyAsError(res)
}
var m Response
if err := json.NewDecoder(res.Body).Decode(&m); err != nil {
return Response{}, xerrors.Errorf("decode response body: %w", err)
}
return m, nil
}
//nolint:revive // Follow is a control flag on the server as well.
func (c *Client) WorkspaceAgentLogsAfter(ctx context.Context, agentID uuid.UUID, after int64, follow bool) (<-chan []WorkspaceAgentLog, io.Closer, error) {
var queryParams []string
if after != 0 {
queryParams = append(queryParams, fmt.Sprintf("after=%d", after))
}
if follow {
queryParams = append(queryParams, "follow")
}
var query string
if len(queryParams) > 0 {
query = "?" + strings.Join(queryParams, "&")
}
reqURL, err := c.URL.Parse(fmt.Sprintf("/api/v2/workspaceagents/%s/logs%s", agentID, query))
if err != nil {
return nil, nil, err
}
if !follow {
resp, err := c.Request(ctx, http.MethodGet, reqURL.String(), nil)
if err != nil {
return nil, nil, xerrors.Errorf("execute request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, ReadBodyAsError(resp)
}
var logs []WorkspaceAgentLog
err = json.NewDecoder(resp.Body).Decode(&logs)
if err != nil {
return nil, nil, xerrors.Errorf("decode startup logs: %w", err)
}
ch := make(chan []WorkspaceAgentLog, 1)
ch <- logs
close(ch)
return ch, closeFunc(func() error { return nil }), nil
}
httpClient := &http.Client{
Transport: c.HTTPClient.Transport,
}
conn, res, err := websocket.Dial(ctx, reqURL.String(), &websocket.DialOptions{
HTTPClient: httpClient,
HTTPHeader: http.Header{
SessionTokenHeader: []string{c.SessionToken()},
},
CompressionMode: websocket.CompressionDisabled,
})
if err != nil {
if res == nil {
return nil, nil, err
}
return nil, nil, ReadBodyAsError(res)
}
d := wsjson.NewDecoder[[]WorkspaceAgentLog](conn, websocket.MessageText, c.logger)
return d.Chan(), d, nil
}
// WorkspaceAgentGitClientMessageType represents the type of a client
// message sent to the git watch WebSocket.
type WorkspaceAgentGitClientMessageType string
const (
// WorkspaceAgentGitClientMessageTypeRefresh requests an immediate
// re-scan of all subscribed repositories.
WorkspaceAgentGitClientMessageTypeRefresh WorkspaceAgentGitClientMessageType = "refresh"
)
// WorkspaceAgentGitClientMessage is a message sent from the client to
// the agent over the git watch WebSocket.
type WorkspaceAgentGitClientMessage struct {
Type WorkspaceAgentGitClientMessageType `json:"type"`
}
// WorkspaceAgentGitServerMessageType represents the type of a server
// message sent from the git watch WebSocket.
type WorkspaceAgentGitServerMessageType string
const (
// WorkspaceAgentGitServerMessageTypeChanges contains a delta of
// repository changes since the last emitted update.
WorkspaceAgentGitServerMessageTypeChanges WorkspaceAgentGitServerMessageType = "changes"
// WorkspaceAgentGitServerMessageTypeError signals a server-side
// error.
WorkspaceAgentGitServerMessageTypeError WorkspaceAgentGitServerMessageType = "error"
)
// WorkspaceAgentGitServerMessage is a message sent from the agent to
// the client over the git watch WebSocket.
type WorkspaceAgentGitServerMessage struct {
Type WorkspaceAgentGitServerMessageType `json:"type"`
ScannedAt *time.Time `json:"scanned_at,omitempty" format:"date-time"`
Repositories []WorkspaceAgentRepoChanges `json:"repositories,omitempty"`
Message string `json:"message,omitempty"`
}
// WorkspaceAgentRepoChanges describes the current state of a single
// git repository's working tree. When Removed is true the repo root
// directory or its .git subdirectory no longer exists; all other
// fields (Branch, RemoteOrigin, UnifiedDiff) are empty/zero.
type WorkspaceAgentRepoChanges struct {
RepoRoot string `json:"repo_root"`
Branch string `json:"branch"`
RemoteOrigin string `json:"remote_origin,omitempty"`
UnifiedDiff string `json:"unified_diff,omitempty"`
Removed bool `json:"removed,omitempty"`
}