From 547c62922050ad98fa8a20f42c2532b05c932909 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Wed, 15 Sep 2021 10:38:19 -0400 Subject: [PATCH 1/6] fix ctx cancellation errors & fix todo for X11 forwarding --- cmd/ghcs/create.go | 7 ++++++- internal/codespaces/ssh.go | 9 +++++++-- internal/codespaces/states.go | 14 +++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/ghcs/create.go b/cmd/ghcs/create.go index 093450e7d..3669d7434 100644 --- a/cmd/ghcs/create.go +++ b/cmd/ghcs/create.go @@ -153,7 +153,12 @@ func showStatus(ctx context.Context, log *output.Logger, apiClient *api.API, use } } - if err := codespaces.PollPostCreateStates(ctx, log, apiClient, user, codespace, poller); err != nil { + err := codespaces.PollPostCreateStates(ctx, log, apiClient, user, codespace, poller) + if err != nil { + if errors.Is(err, context.Canceled) && breakNextState { + return nil // we cancelled the context to stop polling, we can ignore the error + } + return fmt.Errorf("failed to poll state changes from codespace: %v", err) } diff --git a/internal/codespaces/ssh.go b/internal/codespaces/ssh.go index 14dbfbb88..7dcab3de4 100644 --- a/internal/codespaces/ssh.go +++ b/internal/codespaces/ssh.go @@ -60,9 +60,14 @@ func NewRemoteCommand(ctx context.Context, tunnelPort int, destination, command // an interactive shell) over ssh. func newSSHCommand(ctx context.Context, port int, dst, command string) (*exec.Cmd, []string) { connArgs := []string{"-p", strconv.Itoa(port), "-o", "NoHostAuthenticationForLocalhost=yes"} - // TODO(adonovan): eliminate X11 and X11Trust flags where unneeded. - cmdArgs := append([]string{dst, "-X", "-Y", "-C"}, connArgs...) // X11, X11Trust, Compression + cmdArgs := []string{dst, "-C"} // Always use Compression + if command == "" { + // if we are in a shell send X11 and X11Trust + cmdArgs = append(cmdArgs, "-X", "-Y") + } + + cmdArgs = append(cmdArgs, connArgs...) if command != "" { cmdArgs = append(cmdArgs, command) } diff --git a/internal/codespaces/states.go b/internal/codespaces/states.go index 492ce3964..2d7da9d75 100644 --- a/internal/codespaces/states.go +++ b/internal/codespaces/states.go @@ -39,12 +39,12 @@ type PostCreateState struct { func PollPostCreateStates(ctx context.Context, log logger, apiClient *api.API, user *api.User, codespace *api.Codespace, poller func([]PostCreateState)) error { token, err := apiClient.GetCodespaceToken(ctx, user.Login, codespace.Name) if err != nil { - return fmt.Errorf("getting codespace token: %v", err) + return fmt.Errorf("getting codespace token: %w", err) } session, err := ConnectToLiveshare(ctx, log, apiClient, user.Login, token, codespace) if err != nil { - return fmt.Errorf("connect to Live Share: %v", err) + return fmt.Errorf("connect to Live Share: %w", err) } // Ensure local port is listening before client (getPostCreateOutput) connects. @@ -56,7 +56,7 @@ func PollPostCreateStates(ctx context.Context, log logger, apiClient *api.API, u remoteSSHServerPort, sshUser, err := StartSSHServer(ctx, session, log) if err != nil { - return fmt.Errorf("error getting ssh server details: %v", err) + return fmt.Errorf("error getting ssh server details: %w", err) } tunnelClosed := make(chan error, 1) // buffered to avoid sender stuckness @@ -74,12 +74,12 @@ func PollPostCreateStates(ctx context.Context, log logger, apiClient *api.API, u return ctx.Err() case err := <-tunnelClosed: - return fmt.Errorf("connection failed: %v", err) + return fmt.Errorf("connection failed: %w", err) case <-t.C: states, err := getPostCreateOutput(ctx, localPort, codespace, sshUser) if err != nil { - return fmt.Errorf("get post create output: %v", err) + return fmt.Errorf("get post create output: %w", err) } poller(states) @@ -95,13 +95,13 @@ func getPostCreateOutput(ctx context.Context, tunnelPort int, codespace *api.Cod stdout := new(bytes.Buffer) cmd.Stdout = stdout if err := cmd.Run(); err != nil { - return nil, fmt.Errorf("run command: %v", err) + return nil, fmt.Errorf("run command: %w", err) } var output struct { Steps []PostCreateState `json:"steps"` } if err := json.Unmarshal(stdout.Bytes(), &output); err != nil { - return nil, fmt.Errorf("unmarshal output: %v", err) + return nil, fmt.Errorf("unmarshal output: %w", err) } return output.Steps, nil From 06719866c95e834d87fd2ecd87b5889b14d6df2b Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 15 Sep 2021 13:09:31 -0400 Subject: [PATCH 2/6] move api to internal/ --- cmd/ghcs/code.go | 2 +- cmd/ghcs/common.go | 2 +- cmd/ghcs/create.go | 2 +- cmd/ghcs/delete.go | 2 +- cmd/ghcs/list.go | 2 +- cmd/ghcs/logs.go | 2 +- cmd/ghcs/ports.go | 2 +- cmd/ghcs/ssh.go | 2 +- {api => internal/api}/api.go | 19 ++++++++++++++++++- internal/codespaces/codespaces.go | 2 +- internal/codespaces/states.go | 2 +- 11 files changed, 28 insertions(+), 11 deletions(-) rename {api => internal/api}/api.go (94%) diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index 3bd67053d..0ec363a6d 100644 --- a/cmd/ghcs/code.go +++ b/cmd/ghcs/code.go @@ -6,7 +6,7 @@ import ( "net/url" "os" - "github.com/github/ghcs/api" + "github.com/github/ghcs/internal/api" "github.com/skratchdot/open-golang/open" "github.com/spf13/cobra" ) diff --git a/cmd/ghcs/common.go b/cmd/ghcs/common.go index bfcb67496..133e6d1de 100644 --- a/cmd/ghcs/common.go +++ b/cmd/ghcs/common.go @@ -11,7 +11,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/terminal" - "github.com/github/ghcs/api" + "github.com/github/ghcs/internal/api" "golang.org/x/term" ) diff --git a/cmd/ghcs/create.go b/cmd/ghcs/create.go index 093450e7d..82414f80e 100644 --- a/cmd/ghcs/create.go +++ b/cmd/ghcs/create.go @@ -9,8 +9,8 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/fatih/camelcase" - "github.com/github/ghcs/api" "github.com/github/ghcs/cmd/ghcs/output" + "github.com/github/ghcs/internal/api" "github.com/github/ghcs/internal/codespaces" "github.com/spf13/cobra" ) diff --git a/cmd/ghcs/delete.go b/cmd/ghcs/delete.go index 7800a13c0..3f5d68684 100644 --- a/cmd/ghcs/delete.go +++ b/cmd/ghcs/delete.go @@ -7,8 +7,8 @@ import ( "os" "strings" - "github.com/github/ghcs/api" "github.com/github/ghcs/cmd/ghcs/output" + "github.com/github/ghcs/internal/api" "github.com/spf13/cobra" ) diff --git a/cmd/ghcs/list.go b/cmd/ghcs/list.go index ee26e3013..dee1b0875 100644 --- a/cmd/ghcs/list.go +++ b/cmd/ghcs/list.go @@ -5,8 +5,8 @@ import ( "fmt" "os" - "github.com/github/ghcs/api" "github.com/github/ghcs/cmd/ghcs/output" + "github.com/github/ghcs/internal/api" "github.com/spf13/cobra" ) diff --git a/cmd/ghcs/logs.go b/cmd/ghcs/logs.go index a685a364d..8d7dc475e 100644 --- a/cmd/ghcs/logs.go +++ b/cmd/ghcs/logs.go @@ -6,8 +6,8 @@ import ( "net" "os" - "github.com/github/ghcs/api" "github.com/github/ghcs/cmd/ghcs/output" + "github.com/github/ghcs/internal/api" "github.com/github/ghcs/internal/codespaces" "github.com/github/go-liveshare" "github.com/spf13/cobra" diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index f6d1f6e2a..8256c13e2 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -11,8 +11,8 @@ import ( "strconv" "strings" - "github.com/github/ghcs/api" "github.com/github/ghcs/cmd/ghcs/output" + "github.com/github/ghcs/internal/api" "github.com/github/ghcs/internal/codespaces" "github.com/github/go-liveshare" "github.com/muhammadmuzzammil1998/jsonc" diff --git a/cmd/ghcs/ssh.go b/cmd/ghcs/ssh.go index e5289e205..b020a799e 100644 --- a/cmd/ghcs/ssh.go +++ b/cmd/ghcs/ssh.go @@ -6,8 +6,8 @@ import ( "net" "os" - "github.com/github/ghcs/api" "github.com/github/ghcs/cmd/ghcs/output" + "github.com/github/ghcs/internal/api" "github.com/github/ghcs/internal/codespaces" "github.com/github/go-liveshare" "github.com/spf13/cobra" diff --git a/api/api.go b/internal/api/api.go similarity index 94% rename from api/api.go rename to internal/api/api.go index ad69f23fc..8277e903a 100644 --- a/api/api.go +++ b/internal/api/api.go @@ -1,4 +1,3 @@ -// TODO(adonovan): rename to package codespaces, and codespaces.Client. package api // For descriptions of service interfaces, see: @@ -7,6 +6,24 @@ package api // - https://github.com/github/github/blob/master/app/api/codespaces.rb (for vscs_internal) // TODO(adonovan): replace the last link with a public doc URL when available. +// TODO(adonovan): a possible reorganization would be to split this +// file into three internal packages, one per backend service, and to +// rename api.API to github.Client: +// +// - github.GetUser(github.Client) +// - github.GetRepository(Client) +// - github.ReadFile(Client, nwo, branch, path) // was GetCodespaceRepositoryContents +// - codespaces.Create(Client, user, repo, sku, branch, location) +// - codespaces.Delete(Client, user, token, name) +// - codespaces.Get(Client, token, owner, name) +// - codespaces.GetMachineTypes(Client, user, repo, branch, location) +// - codespaces.GetToken(Client, login, name) +// - codespaces.List(Client, user) +// - codespaces.Start(Client, token, codespace) +// - visualstudio.GetRegionLocation(http.Client) // no dependency on github +// +// This would make the meaning of each operation clearer. + import ( "bytes" "context" diff --git a/internal/codespaces/codespaces.go b/internal/codespaces/codespaces.go index 9aee3564c..804c2dab5 100644 --- a/internal/codespaces/codespaces.go +++ b/internal/codespaces/codespaces.go @@ -6,7 +6,7 @@ import ( "fmt" "time" - "github.com/github/ghcs/api" + "github.com/github/ghcs/internal/api" "github.com/github/go-liveshare" ) diff --git a/internal/codespaces/states.go b/internal/codespaces/states.go index 492ce3964..c7a7767ae 100644 --- a/internal/codespaces/states.go +++ b/internal/codespaces/states.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/github/ghcs/api" + "github.com/github/ghcs/internal/api" "github.com/github/go-liveshare" ) From 0f72e3d88642e36a1f3cbb2ef95866b2269541d2 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Wed, 15 Sep 2021 14:29:16 -0400 Subject: [PATCH 3/6] defer stopPolling and docs --- cmd/ghcs/create.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/ghcs/create.go b/cmd/ghcs/create.go index 3669d7434..aa4c14658 100644 --- a/cmd/ghcs/create.go +++ b/cmd/ghcs/create.go @@ -105,12 +105,16 @@ func create(opts *createOptions) error { return nil } +// showStatus polls the codespace for a list of post create states and their status. It will keep polling +// until all states have finished. Once all states have finished, we poll once more to check if any new +// states have been introduced and stop polling otherwise. func showStatus(ctx context.Context, log *output.Logger, apiClient *api.API, user *api.User, codespace *api.Codespace) error { var lastState codespaces.PostCreateState var breakNextState bool finishedStates := make(map[string]bool) ctx, stopPolling := context.WithCancel(ctx) + defer stopPolling() poller := func(states []codespaces.PostCreateState) { var inProgress bool From ecd0c7056798bcd1b0d1fb2d65d3da7b1477a7be Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Wed, 15 Sep 2021 15:15:28 -0400 Subject: [PATCH 4/6] upgrade to go-liveshare 0.16.0 --- cmd/ghcs/ssh.go | 3 ++- internal/codespaces/ssh.go | 42 ----------------------------------- internal/codespaces/states.go | 3 ++- 3 files changed, 4 insertions(+), 44 deletions(-) diff --git a/cmd/ghcs/ssh.go b/cmd/ghcs/ssh.go index e5289e205..08c3bd7ca 100644 --- a/cmd/ghcs/ssh.go +++ b/cmd/ghcs/ssh.go @@ -59,7 +59,8 @@ func ssh(ctx context.Context, sshProfile, codespaceName string, localSSHServerPo return fmt.Errorf("error connecting to Live Share: %v", err) } - remoteSSHServerPort, sshUser, err := codespaces.StartSSHServer(ctx, session, log) + log.Println("Fetching SSH Details...") + remoteSSHServerPort, sshUser, err := session.StartSSHServer(ctx) if err != nil { return fmt.Errorf("error getting ssh server details: %v", err) } diff --git a/internal/codespaces/ssh.go b/internal/codespaces/ssh.go index 14dbfbb88..28c2761b1 100644 --- a/internal/codespaces/ssh.go +++ b/internal/codespaces/ssh.go @@ -2,53 +2,11 @@ package codespaces import ( "context" - "errors" - "fmt" "os" "os/exec" "strconv" - "strings" - - "github.com/github/go-liveshare" ) -// StartSSHServer installs (if necessary) and starts the SSH in the codespace. -// It returns the remote port where it is running, the user to log in with, or an error if something failed. -func StartSSHServer(ctx context.Context, session *liveshare.Session, log logger) (serverPort int, user string, err error) { - log.Println("Fetching SSH details...") - - sshServer := session.SSHServer() - - sshServerStartResult, err := sshServer.StartRemoteServer(ctx) - if err != nil { - return 0, "", fmt.Errorf("error starting live share: %v", err) - } - - if !sshServerStartResult.Result { - return 0, "", errors.New(sshServerStartResult.Message) - } - - portInt, err := strconv.Atoi(sshServerStartResult.ServerPort) - if err != nil { - return 0, "", fmt.Errorf("error parsing port: %v", err) - } - - return portInt, sshServerStartResult.User, nil -} - -// Shell runs an interactive secure shell over an existing -// port-forwarding session. It runs until the shell is terminated -// (including by cancellation of the context). -func Shell(ctx context.Context, log logger, port int, destination string, usingCustomPort bool) error { - cmd, connArgs := newSSHCommand(ctx, port, destination, "") - - if usingCustomPort { - log.Println("Connection Details: ssh " + destination + " " + strings.Join(connArgs, " ")) - } - - return cmd.Run() -} - // NewRemoteCommand returns an exec.Cmd that will securely run a shell // command on the remote machine. func NewRemoteCommand(ctx context.Context, tunnelPort int, destination, command string) *exec.Cmd { diff --git a/internal/codespaces/states.go b/internal/codespaces/states.go index 492ce3964..f4375fa5d 100644 --- a/internal/codespaces/states.go +++ b/internal/codespaces/states.go @@ -54,7 +54,8 @@ func PollPostCreateStates(ctx context.Context, log logger, apiClient *api.API, u } localPort := listen.Addr().(*net.TCPAddr).Port - remoteSSHServerPort, sshUser, err := StartSSHServer(ctx, session, log) + log.Println("Fetching SSH Details...") + remoteSSHServerPort, sshUser, err := session.StartSSHServer(ctx) if err != nil { return fmt.Errorf("error getting ssh server details: %v", err) } From 26d3199082dae4dcf8bf75d234e94ff781b872c4 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Wed, 15 Sep 2021 15:18:54 -0400 Subject: [PATCH 5/6] add back codespaces.Shell --- internal/codespaces/ssh.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/codespaces/ssh.go b/internal/codespaces/ssh.go index 28c2761b1..d39fc17a0 100644 --- a/internal/codespaces/ssh.go +++ b/internal/codespaces/ssh.go @@ -5,8 +5,22 @@ import ( "os" "os/exec" "strconv" + "strings" ) +// Shell runs an interactive secure shell over an existing +// port-forwarding session. It runs until the shell is terminated +// (including by cancellation of the context). +func Shell(ctx context.Context, log logger, port int, destination string, usingCustomPort bool) error { + cmd, connArgs := newSSHCommand(ctx, port, destination, "") + + if usingCustomPort { + log.Println("Connection Details: ssh " + destination + " " + strings.Join(connArgs, " ")) + } + + return cmd.Run() +} + // NewRemoteCommand returns an exec.Cmd that will securely run a shell // command on the remote machine. func NewRemoteCommand(ctx context.Context, tunnelPort int, destination, command string) *exec.Cmd { From b2234969e4f728e049a9dc0f5eeff856baf91af7 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Wed, 15 Sep 2021 15:40:07 -0400 Subject: [PATCH 6/6] update logs --- cmd/ghcs/logs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/ghcs/logs.go b/cmd/ghcs/logs.go index a685a364d..be283818f 100644 --- a/cmd/ghcs/logs.go +++ b/cmd/ghcs/logs.go @@ -70,7 +70,8 @@ func logs(ctx context.Context, log *output.Logger, codespaceName string, follow defer listen.Close() localPort := listen.Addr().(*net.TCPAddr).Port - remoteSSHServerPort, sshUser, err := codespaces.StartSSHServer(ctx, session, log) + log.Println("Fetching SSH Details...") + remoteSSHServerPort, sshUser, err := session.StartSSHServer(ctx) if err != nil { return fmt.Errorf("error getting ssh server details: %v", err) }