Skip to content

Commit

Permalink
roachprod: verbose ssh logging
Browse files Browse the repository at this point in the history
Stash the verbose debug logs in a file for each remote ssh session,
and surface it with the error should one occur.

For the latest in many past incidents where this would've been useful,
see:

cockroachdb#36720 (comment)

Release note: None
  • Loading branch information
tbg committed May 12, 2019
1 parent c25518b commit 2807930
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/cmd/roachprod/install/cluster_synced.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func (c *SyncedCluster) newSession(i int) (session, error) {
if c.IsLocal() {
return newLocalSession(), nil
}
return newRemoteSession(c.user(i), c.host(i))
host := c.host(i)
return newRemoteSession(c.user(i), host)
}

// Stop TODO(peter): document
Expand Down
28 changes: 23 additions & 5 deletions pkg/cmd/roachprod/install/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ package install

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"time"

"github.com/cockroachdb/cockroach/pkg/cmd/roachprod/config"
"github.com/pkg/errors"
)

type session interface {
Expand All @@ -41,13 +45,18 @@ type session interface {

type remoteSession struct {
*exec.Cmd
cancel func()
cancel func()
logfile string // captures ssh -vvv
}

func newRemoteSession(user, host string) (*remoteSession, error) {
logfile := filepath.Join(os.TempDir(), fmt.Sprintf("ssh_%s_%s", host, time.Now().Format(time.RFC3339)))
args := []string{
user + "@" + host,
"-q",
"-vvv", "-E", logfile,
// NB: -q suppresses -E, at least on OSX. Difficult decisions will have
// to be made if omitting -q leads to annoyance on stdout/stderr.
// "-q",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
// Send keep alives every minute to prevent connections without activity
Expand All @@ -60,17 +69,26 @@ func newRemoteSession(user, host string) (*remoteSession, error) {
args = append(args, sshAuthArgs()...)
ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, "ssh", args...)
return &remoteSession{cmd, cancel}, nil
return &remoteSession{cmd, cancel, logfile}, nil
}

func (s *remoteSession) errWithDebug(err error) error {
if err != nil {
debug, _ := ioutil.ReadFile(s.logfile)
err = errors.Wrapf(err, "ssh verbose log:\n%s\n%s", s.Cmd.Args, debug)
}
return err
}

func (s *remoteSession) CombinedOutput(cmd string) ([]byte, error) {
s.Cmd.Args = append(s.Cmd.Args, cmd)
return s.Cmd.CombinedOutput()
b, err := s.Cmd.CombinedOutput()
return b, s.errWithDebug(err)
}

func (s *remoteSession) Run(cmd string) error {
s.Cmd.Args = append(s.Cmd.Args, cmd)
return s.Cmd.Run()
return s.errWithDebug(s.Cmd.Run())
}

func (s *remoteSession) SetStdin(r io.Reader) {
Expand Down

0 comments on commit 2807930

Please sign in to comment.