Skip to content

feat(support): fetch agent network info over tailnet #12577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 15, 2024
Prev Previous commit
Next Next commit
use workspaceagentconn methods added in #12593
  • Loading branch information
johnstcn committed Mar 14, 2024
commit df184a09eafb6c89cd5ecb04b66ffbe75a0259a6
9 changes: 8 additions & 1 deletion cli/support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"encoding/json"
"io"
"os"
"path/filepath"
"runtime"
"testing"
Expand All @@ -13,6 +14,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/agent"
"github.com/coder/coder/v2/agent/agenttest"
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
Expand Down Expand Up @@ -42,7 +44,12 @@ func TestSupportBundle(t *testing.T) {
}).WithAgent().Do()
ws, err := client.Workspace(ctx, r.Workspace.ID)
require.NoError(t, err)
agt := agenttest.New(t, client.URL, r.AgentToken)
tempDir := t.TempDir()
logPath := filepath.Join(tempDir, "coder-agent.log")
require.NoError(t, os.WriteFile(logPath, []byte("hello from the agent"), 0o600))
agt := agenttest.New(t, client.URL, r.AgentToken, func(o *agent.Options) {
o.LogDir = tempDir
})
defer agt.Close()
coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).Wait()

Expand Down
43 changes: 4 additions & 39 deletions support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,29 +356,21 @@ func AgentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, ag
})

eg.Go(func() error {
tokenBytes, err := runCmd(ctx, client, agentID, `echo $CODER_AGENT_TOKEN`)
if err != nil {
return xerrors.Errorf("failed to fetch agent token: %w", err)
}
token := string(bytes.TrimSpace(tokenBytes))
agentClient := agentsdk.New(client.URL)
agentClient.SetSessionToken(token)
manifestRes, err := agentClient.SDK.Request(ctx, http.MethodGet, "/api/v2/workspaceagents/me/manifest", nil)
manifestRes, err := conn.DebugManifest(ctx)
if err != nil {
return xerrors.Errorf("fetch manifest: %w", err)
}
defer manifestRes.Body.Close()
if err := json.NewDecoder(manifestRes.Body).Decode(&a.Manifest); err != nil {
if err := json.NewDecoder(bytes.NewReader(manifestRes)).Decode(&a.Manifest); err != nil {
return xerrors.Errorf("decode agent manifest: %w", err)
}

return nil
})

eg.Go(func() error {
logBytes, err := runCmd(ctx, client, agentID, `tail -n 1000 /tmp/coder-agent.log`)
logBytes, err := conn.DebugLogs(ctx)
if err != nil {
return xerrors.Errorf("tail /tmp/coder-agent.log: %w", err)
return xerrors.Errorf("fetch coder agent logs: %w", err)
}
a.Logs = logBytes
return nil
Expand Down Expand Up @@ -472,30 +464,3 @@ func sanitizeEnv(kvs map[string]string) {
}
}
}

// TODO: use rpty instead? which is less liable to fail?
func runCmd(ctx context.Context, client *codersdk.Client, agentID uuid.UUID, cmd string) ([]byte, error) {
agentConn, err := client.DialWorkspaceAgent(ctx, agentID, &codersdk.DialWorkspaceAgentOptions{})
if err != nil {
return nil, xerrors.Errorf("dial workspace agent: %w", err)
}
defer agentConn.Close()

sshClient, err := agentConn.SSHClient(ctx)
if err != nil {
return nil, xerrors.Errorf("get ssh client: %w", err)
}
defer sshClient.Close()

sshSession, err := sshClient.NewSession()
if err != nil {
return nil, xerrors.Errorf("new ssh session: %w", err)
}
defer sshSession.Close()

cmdBytes, err := sshSession.CombinedOutput(cmd)
if err != nil {
return nil, xerrors.Errorf("shell: %w", err)
}
return cmdBytes, err
}
10 changes: 9 additions & 1 deletion support/support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"

Expand All @@ -15,6 +17,7 @@ import (
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/agent"
"github.com/coder/coder/v2/agent/agenttest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
Expand Down Expand Up @@ -212,7 +215,12 @@ func setupWorkspaceAndAgent(ctx context.Context, t *testing.T, client *codersdk.
})
require.NoError(t, err)

_ = agenttest.New(t, client.URL, wbr.AgentToken)
tempDir := t.TempDir()
logPath := filepath.Join(tempDir, "coder-agent.log")
require.NoError(t, os.WriteFile(logPath, []byte("hello from the agent"), 0o600))
_ = agenttest.New(t, client.URL, wbr.AgentToken, func(o *agent.Options) {
o.LogDir = tempDir
})
coderdtest.NewWorkspaceAgentWaiter(t, client, wbr.Workspace.ID).Wait()

return ws, agt
Expand Down