fix error codes on cli

This commit is contained in:
Pascal Fischer 2023-06-23 16:27:10 +02:00
parent 2691e729cd
commit f7d97b02fd
2 changed files with 14 additions and 28 deletions

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"syscall" "syscall"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/client/internal"
@ -72,8 +73,8 @@ var sshCmd = &cobra.Command{
go func() { go func() {
// blocking // blocking
if err := runSSH(sshctx, host, []byte(config.SSHKey), cmd); err != nil { if err := runSSH(sshctx, host, []byte(config.SSHKey), cmd); err != nil {
log.Debug(err)
os.Exit(1) os.Exit(1)
// log.Print(err)
} }
cancel() cancel()
}() }()
@ -95,7 +96,7 @@ func runSSH(ctx context.Context, addr string, pemKey []byte, cmd *cobra.Command)
cmd.Printf("Couldn't connect. Please check the connection status or if the ssh server is enabled on the other peer" + cmd.Printf("Couldn't connect. Please check the connection status or if the ssh server is enabled on the other peer" +
"You can verify the connection by running:\n\n" + "You can verify the connection by running:\n\n" +
" netbird status\n\n") " netbird status\n\n")
return nil return err
} }
go func() { go func() {
<-ctx.Done() <-ctx.Done()

View File

@ -20,6 +20,9 @@ import (
// DefaultSSHPort is the default SSH port of the NetBird's embedded SSH server // DefaultSSHPort is the default SSH port of the NetBird's embedded SSH server
const DefaultSSHPort = 44338 const DefaultSSHPort = 44338
// TerminalTimeout is the timeout for terminal session to be ready
const TerminalTimeout = 10 * time.Second
// DefaultSSHServer is a function that creates DefaultServer // DefaultSSHServer is a function that creates DefaultServer
func DefaultSSHServer(hostKeyPEM []byte, addr string) (Server, error) { func DefaultSSHServer(hostKeyPEM []byte, addr string) (Server, error) {
return newDefaultServer(hostKeyPEM, addr) return newDefaultServer(hostKeyPEM, addr)
@ -213,42 +216,24 @@ func (srv *DefaultServer) stdInOut(file *os.File, session ssh.Session) {
io.Copy(file, session) io.Copy(file, session)
}() }()
// For nodes on AWS the terminal takes a while to be ready so we need to wait timer := time.NewTimer(TerminalTimeout)
terminalIsReady := make(chan bool)
go func() {
for {
log.Debugf("Checking if terminal is ready")
if checkIfFileIsReady(file) {
terminalIsReady <- true
}
time.Sleep(100 * time.Millisecond)
}
}()
timer := time.NewTimer(30 * time.Second)
for { for {
select { select {
case <-timer.C: case <-timer.C:
session.Write([]byte("Reached timeout while opening connection\n")) session.Write([]byte("Reached timeout while opening connection\n"))
session.Exit(1) session.Exit(1)
case <-terminalIsReady: return
default:
// stdout // stdout
io.Copy(session, file) writtenBytes, err := io.Copy(session, file)
session.Exit(0) if err != nil && writtenBytes != 0 {
session.Exit(0)
return
}
} }
} }
} }
func checkIfFileIsReady(file *os.File) bool {
buffer := make([]byte, 0)
_, err := file.Read(buffer)
// _, err := file.Stat()
// log.Infof("file stat: %v", err)
if err == nil {
return true
}
return false
}
// Start starts SSH server. Blocking // Start starts SSH server. Blocking
func (srv *DefaultServer) Start() error { func (srv *DefaultServer) Start() error {
log.Infof("starting SSH server on addr: %s", srv.listener.Addr().String()) log.Infof("starting SSH server on addr: %s", srv.listener.Addr().String())