From d46420e812aa34e21d667508a32378c9f3e18c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 15 Jul 2021 16:07:23 +0200 Subject: [PATCH 1/2] Improve ssh command - Ensure parent process exits when `ssh` sub-process is done - Enable connections to `github/github` when `--profile` flag wasn't given --- cmd/ghcs/ssh.go | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/cmd/ghcs/ssh.go b/cmd/ghcs/ssh.go index 366076516..bdaea644e 100644 --- a/cmd/ghcs/ssh.go +++ b/cmd/ghcs/ssh.go @@ -5,7 +5,6 @@ import ( "context" "errors" "fmt" - "log" "math/rand" "os" "os/exec" @@ -177,40 +176,25 @@ func SSH(sshProfile string) error { } }() + connectDestination := sshProfile + if connectDestination == "" { + connectDestination = fmt.Sprintf("%s@localhost", getSSHUser(codespace)) + } + fmt.Println("Ready...") - if err := connect(ctx, port, sshProfile); err != nil { + if err := connect(ctx, port, connectDestination); err != nil { return fmt.Errorf("error connecting via SSH: %v", err) } return nil } -func connect(ctx context.Context, port int, sshProfile string) error { - var cmd *exec.Cmd - if sshProfile != "" { - cmd = exec.CommandContext(ctx, "ssh", sshProfile, "-p", strconv.Itoa(port), "-C") - } else { - cmd = exec.CommandContext(ctx, "ssh", "codespace@localhost", "-C", "-p", strconv.Itoa(port), "-o", "NoHostAuthenticationForLocalhost=yes") - } - +func connect(ctx context.Context, port int, destination string) error { + cmd := exec.CommandContext(ctx, "ssh", destination, "-C", "-p", strconv.Itoa(port), "-o", "NoHostAuthenticationForLocalhost=yes") cmd.Stdout = os.Stdout cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr - - if err := cmd.Start(); err != nil { - return fmt.Errorf("error running ssh: %v", err) - } - - go func() { - if err := cmd.Wait(); err != nil { - log.Println(fmt.Errorf("error waiting for ssh to finish: %v", err)) - } - }() - - done := make(chan bool) - <-done - - return nil + return cmd.Run() } func getContainerID(ctx context.Context, terminal *liveshare.Terminal) (string, error) { @@ -263,3 +247,10 @@ func setupSSH(ctx context.Context, terminal *liveshare.Terminal, containerID, re return nil } + +func getSSHUser(codespace *api.Codespace) string { + if codespace.RepositoryNWO == "github/github" { + return "root" + } + return "codespace" +} From ecea5b821aceec24133ca88c8b2bf1e68a124841 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Thu, 15 Jul 2021 14:35:26 +0000 Subject: [PATCH 2/2] Give more time to start --- cmd/ghcs/ssh.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/ghcs/ssh.go b/cmd/ghcs/ssh.go index bdaea644e..ee33260ae 100644 --- a/cmd/ghcs/ssh.go +++ b/cmd/ghcs/ssh.go @@ -110,8 +110,8 @@ func SSH(sshProfile string) error { time.Sleep(1 * time.Second) } - if retries == 10 { - return errors.New("Failed to start codespace") + if retries == 20 { + return errors.New("Timed out waiting for Codespace to start. Try again.") } codespace, err = apiClient.GetCodespace(ctx, token, codespace.OwnerLogin, codespace.Name)