|
1 | 1 | package support
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 | "encoding/base64"
|
| 7 | + "encoding/json" |
| 8 | + "errors" |
6 | 9 | "io"
|
7 | 10 | "net/http"
|
8 | 11 | "strings"
|
9 | 12 |
|
10 | 13 | "golang.org/x/sync/errgroup"
|
11 | 14 | "golang.org/x/xerrors"
|
| 15 | + "tailscale.com/ipn/ipnstate" |
12 | 16 |
|
13 | 17 | "github.com/google/uuid"
|
14 | 18 |
|
15 | 19 | "cdr.dev/slog"
|
16 | 20 | "cdr.dev/slog/sloggers/sloghuman"
|
17 | 21 | "github.com/coder/coder/v2/coderd/rbac"
|
18 | 22 | "github.com/coder/coder/v2/codersdk"
|
| 23 | + "github.com/coder/coder/v2/codersdk/agentsdk" |
| 24 | + "github.com/coder/coder/v2/tailnet" |
19 | 25 | )
|
20 | 26 |
|
21 | 27 | // Bundle is a set of information discovered about a deployment.
|
@@ -53,8 +59,13 @@ type Workspace struct {
|
53 | 59 | }
|
54 | 60 |
|
55 | 61 | type Agent struct {
|
56 |
| - Agent codersdk.WorkspaceAgent `json:"agent"` |
57 |
| - StartupLogs []codersdk.WorkspaceAgentLog `json:"startup_logs"` |
| 62 | + Agent codersdk.WorkspaceAgent `json:"agent"` |
| 63 | + ListeningPorts *codersdk.WorkspaceAgentListeningPortsResponse `json:"listening_ports"` |
| 64 | + Logs []byte `json:"logs"` |
| 65 | + Manifest *agentsdk.Manifest `json:"manifest"` |
| 66 | + PeerDiagnostics *tailnet.PeerDiagnostics `json:"peer_diagnostics"` |
| 67 | + PingResult *ipnstate.PingResult `json:"ping_result"` |
| 68 | + StartupLogs []codersdk.WorkspaceAgentLog `json:"startup_logs"` |
58 | 69 | }
|
59 | 70 |
|
60 | 71 | // Deps is a set of dependencies for discovering information
|
@@ -303,6 +314,72 @@ func AgentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, ag
|
303 | 314 | return nil
|
304 | 315 | })
|
305 | 316 |
|
| 317 | + conn, err := client.DialWorkspaceAgent(ctx, agentID, &codersdk.DialWorkspaceAgentOptions{ |
| 318 | + Logger: log.Named("dial-agent"), |
| 319 | + BlockEndpoints: false, |
| 320 | + }) |
| 321 | + if err != nil { |
| 322 | + log.Error(ctx, "dial agent", slog.Error(err)) |
| 323 | + } else { |
| 324 | + defer conn.Close() |
| 325 | + if !conn.AwaitReachable(ctx) { |
| 326 | + log.Error(ctx, "timed out waiting for agent") |
| 327 | + } else { |
| 328 | + eg.Go(func() error { |
| 329 | + _, _, pingRes, err := conn.Ping(ctx) |
| 330 | + if err != nil { |
| 331 | + return xerrors.Errorf("ping agent: %w", err) |
| 332 | + } |
| 333 | + a.PingResult = pingRes |
| 334 | + return nil |
| 335 | + }) |
| 336 | + |
| 337 | + eg.Go(func() error { |
| 338 | + pds := conn.GetPeerDiagnostics() |
| 339 | + a.PeerDiagnostics = &pds |
| 340 | + return nil |
| 341 | + }) |
| 342 | + |
| 343 | + eg.Go(func() error { |
| 344 | + tokenBytes, err := runCmd(ctx, client, agentID, `echo $CODER_AGENT_TOKEN`) |
| 345 | + if err != nil { |
| 346 | + return xerrors.Errorf("failed to fetch agent token: %w", err) |
| 347 | + } |
| 348 | + token := string(bytes.TrimSpace(tokenBytes)) |
| 349 | + agentClient := agentsdk.New(client.URL) |
| 350 | + agentClient.SetSessionToken(token) |
| 351 | + manifestRes, err := agentClient.SDK.Request(ctx, http.MethodGet, "/api/v2/workspaceagents/me/manifest", nil) |
| 352 | + if err != nil { |
| 353 | + return xerrors.Errorf("fetch manifest: %w", err) |
| 354 | + } |
| 355 | + defer manifestRes.Body.Close() |
| 356 | + if err := json.NewDecoder(manifestRes.Body).Decode(&a.Manifest); err != nil { |
| 357 | + return xerrors.Errorf("decode agent manifest: %w", err) |
| 358 | + } |
| 359 | + |
| 360 | + return nil |
| 361 | + }) |
| 362 | + |
| 363 | + eg.Go(func() error { |
| 364 | + logBytes, err := runCmd(ctx, client, agentID, `tail -n 1000 /tmp/coder-agent.log`) |
| 365 | + if err != nil { |
| 366 | + return xerrors.Errorf("tail /tmp/coder-agent.log: %w", err) |
| 367 | + } |
| 368 | + a.Logs = logBytes |
| 369 | + return nil |
| 370 | + }) |
| 371 | + |
| 372 | + eg.Go(func() error { |
| 373 | + lps, err := conn.ListeningPorts(ctx) |
| 374 | + if err != nil { |
| 375 | + return xerrors.Errorf("get listening ports: %w", err) |
| 376 | + } |
| 377 | + a.ListeningPorts = &lps |
| 378 | + return nil |
| 379 | + }) |
| 380 | + } |
| 381 | + } |
| 382 | + |
306 | 383 | if err := eg.Wait(); err != nil {
|
307 | 384 | log.Error(ctx, "fetch agent information", slog.Error(err))
|
308 | 385 | }
|
@@ -380,3 +457,41 @@ func sanitizeEnv(kvs map[string]string) {
|
380 | 457 | }
|
381 | 458 | }
|
382 | 459 | }
|
| 460 | + |
| 461 | +// TODO: use rpty instead? which is less liable to fail? |
| 462 | +func runCmd(ctx context.Context, client *codersdk.Client, agentID uuid.UUID, cmd string) ([]byte, error) { |
| 463 | + var err error |
| 464 | + var closers []func() error |
| 465 | + defer func() { |
| 466 | + if err != nil { |
| 467 | + for _, c := range closers { |
| 468 | + if err2 := c(); err2 != nil { |
| 469 | + err = errors.Join(err, err2) |
| 470 | + } |
| 471 | + } |
| 472 | + } |
| 473 | + }() |
| 474 | + agentConn, err := client.DialWorkspaceAgent(ctx, agentID, &codersdk.DialWorkspaceAgentOptions{}) |
| 475 | + if err != nil { |
| 476 | + return nil, xerrors.Errorf("dial workspace agent: %w", err) |
| 477 | + } |
| 478 | + closers = append(closers, agentConn.Close) |
| 479 | + |
| 480 | + sshClient, err := agentConn.SSHClient(ctx) |
| 481 | + if err != nil { |
| 482 | + return nil, xerrors.Errorf("get ssh client: %w", err) |
| 483 | + } |
| 484 | + closers = append(closers, sshClient.Close) |
| 485 | + |
| 486 | + sshSession, err := sshClient.NewSession() |
| 487 | + if err != nil { |
| 488 | + return nil, xerrors.Errorf("new ssh session: %w", err) |
| 489 | + } |
| 490 | + closers = append(closers, sshSession.Close) |
| 491 | + |
| 492 | + cmdBytes, err := sshSession.CombinedOutput(cmd) |
| 493 | + if err != nil { |
| 494 | + return nil, xerrors.Errorf("shell: %w", err) |
| 495 | + } |
| 496 | + return cmdBytes, err |
| 497 | +} |
0 commit comments