(ctx context.Context, md codersdk.WorkspaceAgentMetadataDescription, now time.Time)
| 527 | } |
| 528 | |
| 529 | func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMetadataDescription, now time.Time) *codersdk.WorkspaceAgentMetadataResult { |
| 530 | var out bytes.Buffer |
| 531 | result := &codersdk.WorkspaceAgentMetadataResult{ |
| 532 | // CollectedAt is set here for testing purposes and overrode by |
| 533 | // coderd to the time of server receipt to solve clock skew. |
| 534 | // |
| 535 | // In the future, the server may accept the timestamp from the agent |
| 536 | // if it can guarantee the clocks are synchronized. |
| 537 | CollectedAt: now, |
| 538 | } |
| 539 | cmdPty, err := a.sshServer.CreateCommand(ctx, md.Script, nil, nil) |
| 540 | if err != nil { |
| 541 | result.Error = fmt.Sprintf("create cmd: %+v", err) |
| 542 | return result |
| 543 | } |
| 544 | cmd := cmdPty.AsExec() |
| 545 | |
| 546 | cmd.Stdout = &out |
| 547 | cmd.Stderr = &out |
| 548 | cmd.Stdin = io.LimitReader(nil, 0) |
| 549 | |
| 550 | // We split up Start and Wait instead of calling Run so that we can return a more precise error. |
| 551 | err = cmd.Start() |
| 552 | if err != nil { |
| 553 | result.Error = fmt.Sprintf("start cmd: %+v", err) |
| 554 | return result |
| 555 | } |
| 556 | |
| 557 | // This error isn't mutually exclusive with useful output. |
| 558 | err = cmd.Wait() |
| 559 | const bufLimit = 10 << 10 |
| 560 | if out.Len() > bufLimit { |
| 561 | err = errors.Join( |
| 562 | err, |
| 563 | xerrors.Errorf("output truncated from %v to %v bytes", out.Len(), bufLimit), |
| 564 | ) |
| 565 | out.Truncate(bufLimit) |
| 566 | } |
| 567 | |
| 568 | // Important: if the command times out, we may see a misleading error like |
| 569 | // "exit status 1", so it's important to include the context error. |
| 570 | err = errors.Join(err, ctx.Err()) |
| 571 | if err != nil { |
| 572 | result.Error = fmt.Sprintf("run cmd: %+v", err) |
| 573 | } |
| 574 | result.Value = out.String() |
| 575 | return result |
| 576 | } |
| 577 | |
| 578 | type metadataResultAndKey struct { |
| 579 | result *codersdk.WorkspaceAgentMetadataResult |
no test coverage detected