Skip to content

feat: add log-dir flag to vscodessh for debuggability #10514

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 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions cli/vscodessh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (r *RootCmd) vscodeSSH() *clibase.Cmd {
var (
sessionTokenFile string
urlFile string
logDir string
networkInfoDir string
networkInfoInterval time.Duration
)
Expand Down Expand Up @@ -129,13 +130,25 @@ func (r *RootCmd) vscodeSSH() *clibase.Cmd {
}
}

// The VS Code extension obtains the PID of the SSH process to
// read files to display logs and network info.
//
// We get the parent PID because it's assumed `ssh` is calling this
// command via the ProxyCommand SSH option.
pid := os.Getppid()

var logger slog.Logger
if r.verbose {
logger = slog.Make(sloghuman.Sink(inv.Stdout)).Leveled(slog.LevelDebug)
if logDir != "" {
logFilePath := filepath.Join(logDir, fmt.Sprintf("%d.log", pid))
logFile, err := fs.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return xerrors.Errorf("open log file %q: %w", logFilePath, err)
}
defer logFile.Close()
logger = slog.Make(sloghuman.Sink(logFile)).Leveled(slog.LevelDebug)
}

if r.disableDirect {
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
logger.Info(ctx, "direct connections disabled")
}
agentConn, err := client.DialWorkspaceAgent(ctx, agent.ID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger,
Expand Down Expand Up @@ -166,7 +179,7 @@ func (r *RootCmd) vscodeSSH() *clibase.Cmd {
//
// We get the parent PID because it's assumed `ssh` is calling this
// command via the ProxyCommand SSH option.
networkInfoFilePath := filepath.Join(networkInfoDir, fmt.Sprintf("%d.json", os.Getppid()))
networkInfoFilePath := filepath.Join(networkInfoDir, fmt.Sprintf("%d.json", pid))

statsErrChan := make(chan error, 1)
cb := func(start, end time.Time, virtual, _ map[netlogtype.Connection]netlogtype.Counts) {
Expand Down Expand Up @@ -213,6 +226,11 @@ func (r *RootCmd) vscodeSSH() *clibase.Cmd {
Description: "Specifies a directory to write network information periodically.",
Value: clibase.StringOf(&networkInfoDir),
},
{
Flag: "log-dir",
Description: "Specifies a directory to write logs to.",
Value: clibase.StringOf(&logDir),
},
{
Flag: "session-token-file",
Description: "Specifies a file that contains a session token.",
Expand Down
17 changes: 10 additions & 7 deletions cli/vscodessh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ func TestVSCodeSSH(t *testing.T) {
"--url-file", "/url",
"--session-token-file", "/token",
"--network-info-dir", "/net",
"--log-dir", "/log",
"--network-info-interval", "25ms",
fmt.Sprintf("coder-vscode--%s--%s", user.Username, workspace.Name),
)
ptytest.New(t).Attach(inv)

waiter := clitest.StartWithWaiter(t, inv.WithContext(ctx))

assert.Eventually(t, func() bool {
entries, err := afero.ReadDir(fs, "/net")
if err != nil {
return false
}
return len(entries) > 0
}, testutil.WaitLong, testutil.IntervalFast)
for _, dir := range []string{"/net", "/log"} {
assert.Eventually(t, func() bool {
entries, err := afero.ReadDir(fs, dir)
if err != nil {
return false
}
return len(entries) > 0
}, testutil.WaitLong, testutil.IntervalFast)
}
waiter.Cancel()

if err := waiter.Wait(); err != nil {
Expand Down