Skip to content

Commit b6094e0

Browse files
authored
Changes to point to RPC service.
1 parent a48f107 commit b6094e0

File tree

4 files changed

+43
-86
lines changed

4 files changed

+43
-86
lines changed

cmd/ghcs/logs.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ func Logs(tail bool, codespaceName string) error {
5757
return fmt.Errorf("connecting to liveshare: %v", err)
5858
}
5959

60-
tunnelPort, connClosed, err := codespaces.MakeSSHTunnel(ctx, lsclient, 0)
60+
result, remoteSSHServerPort, sshUser, _, err := codespaces.StartSSHServer(ctx, lsclient)
61+
if err != nil {
62+
return fmt.Errorf("error getting ssh server details: %v", err)
63+
}
64+
65+
if !result {
66+
return fmt.Errorf("error starting ssh: %v", err)
67+
}
68+
69+
tunnelPort, connClosed, err := codespaces.MakeSSHTunnel(ctx, lsclient, 0, remoteSSHServerPort)
6170
if err != nil {
6271
return fmt.Errorf("make ssh tunnel: %v", err)
6372
}
@@ -67,7 +76,7 @@ func Logs(tail bool, codespaceName string) error {
6776
cmdType = "tail -f"
6877
}
6978

70-
dst := fmt.Sprintf("%s@localhost", getSSHUser(codespace))
79+
dst := fmt.Sprintf("%s@localhost", sshUser)
7180
stdout, err := codespaces.RunCommand(
7281
ctx, tunnelPort, dst, fmt.Sprintf("%v /workspaces/.codespaces/.persistedshare/creation.log", cmdType),
7382
)

cmd/ghcs/ssh.go

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package main
22

33
import (
4-
"bufio"
54
"context"
65
"fmt"
76
"os"
8-
"strings"
9-
"time"
107

118
"github.com/github/ghcs/api"
129
"github.com/github/ghcs/cmd/ghcs/output"
1310
"github.com/github/ghcs/internal/codespaces"
14-
"github.com/github/go-liveshare"
1511
"github.com/spf13/cobra"
1612
)
1713

@@ -59,33 +55,23 @@ func SSH(sshProfile, codespaceName string, sshServerPort int) error {
5955
return fmt.Errorf("error connecting to liveshare: %v", err)
6056
}
6157

62-
terminal, err := liveshare.NewTerminal(lsclient)
58+
result, remoteSSHServerPort, sshUser, _, err := codespaces.StartSSHServer(ctx, lsclient)
6359
if err != nil {
64-
return fmt.Errorf("error creating liveshare terminal: %v", err)
60+
return fmt.Errorf("error getting ssh server details: %v", err)
6561
}
6662

67-
log.Println("Preparing SSH...")
68-
if sshProfile == "" {
69-
containerID, err := getContainerID(ctx, log, terminal)
70-
if err != nil {
71-
return fmt.Errorf("error getting container id: %v", err)
72-
}
73-
74-
if err := setupSSH(ctx, log, terminal, containerID, codespace.RepositoryName); err != nil {
75-
return fmt.Errorf("error creating ssh server: %v", err)
76-
}
77-
78-
log.Print("\n")
63+
if !result {
64+
return fmt.Errorf("error starting ssh: %v", err)
7965
}
8066

81-
tunnelPort, tunnelClosed, err := codespaces.MakeSSHTunnel(ctx, lsclient, sshServerPort)
67+
tunnelPort, tunnelClosed, err := codespaces.MakeSSHTunnel(ctx, lsclient, sshServerPort, remoteSSHServerPort)
8268
if err != nil {
8369
return fmt.Errorf("make ssh tunnel: %v", err)
8470
}
8571

8672
connectDestination := sshProfile
8773
if connectDestination == "" {
88-
connectDestination = fmt.Sprintf("%s@localhost", getSSHUser(codespace))
74+
connectDestination = fmt.Sprintf("%s@localhost", sshUser)
8975
}
9076

9177
usingCustomPort := tunnelPort == sshServerPort
@@ -105,65 +91,3 @@ func SSH(sshProfile, codespaceName string, sshServerPort int) error {
10591

10692
return nil
10793
}
108-
109-
func getContainerID(ctx context.Context, logger *output.Logger, terminal *liveshare.Terminal) (string, error) {
110-
logger.Print(".")
111-
112-
cmd := terminal.NewCommand(
113-
"/",
114-
"/usr/bin/docker ps -aq --filter label=Type=codespaces --filter status=running",
115-
)
116-
117-
stream, err := cmd.Run(ctx)
118-
if err != nil {
119-
return "", fmt.Errorf("error running command: %v", err)
120-
}
121-
122-
logger.Print(".")
123-
scanner := bufio.NewScanner(stream)
124-
scanner.Scan()
125-
126-
logger.Print(".")
127-
containerID := scanner.Text()
128-
if err := scanner.Err(); err != nil {
129-
return "", fmt.Errorf("error scanning stream: %v", err)
130-
}
131-
132-
logger.Print(".")
133-
if err := stream.Close(); err != nil {
134-
return "", fmt.Errorf("error closing stream: %v", err)
135-
}
136-
137-
return containerID, nil
138-
}
139-
140-
func setupSSH(ctx context.Context, logger *output.Logger, terminal *liveshare.Terminal, containerID, repositoryName string) error {
141-
setupBashProfileCmd := fmt.Sprintf(`echo "cd /workspaces/%v; export $(cat /workspaces/.codespaces/shared/.env | xargs); exec /bin/zsh;" > /home/codespace/.bash_profile`, repositoryName)
142-
143-
logger.Print(".")
144-
compositeCommand := []string{setupBashProfileCmd}
145-
cmd := terminal.NewCommand(
146-
"/",
147-
fmt.Sprintf("/usr/bin/docker exec -t %s /bin/bash -c '"+strings.Join(compositeCommand, "; ")+"'", containerID),
148-
)
149-
stream, err := cmd.Run(ctx)
150-
if err != nil {
151-
return fmt.Errorf("error running command: %v", err)
152-
}
153-
154-
logger.Print(".")
155-
if err := stream.Close(); err != nil {
156-
return fmt.Errorf("error closing stream: %v", err)
157-
}
158-
159-
time.Sleep(1 * time.Second)
160-
161-
return nil
162-
}
163-
164-
func getSSHUser(codespace *api.Codespace) string {
165-
if codespace.RepositoryNWO == "github/github" {
166-
return "root"
167-
}
168-
return "codespace"
169-
}

internal/codespaces/codespaces.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strconv"
78
"time"
89

910
"github.com/AlecAivazis/survey/v2"
@@ -117,6 +118,29 @@ func ConnectToLiveshare(ctx context.Context, log logger, apiClient *api.API, tok
117118
return lsclient, nil
118119
}
119120

121+
func StartSSHServer(ctx context.Context, client *liveshare.Client) (result bool, serverPort int, user string, message string, err error) {
122+
sshRpc, err := liveshare.NewSSHRpc(client)
123+
if err != nil {
124+
return false, 0, "", "", fmt.Errorf("error creating live share: %v", err)
125+
}
126+
127+
sshRpcResult, err := sshRpc.StartRemoteServer(ctx)
128+
if err != nil {
129+
return false, 0, "", "", fmt.Errorf("error creating live share: %v", err)
130+
}
131+
132+
if !sshRpcResult.Result {
133+
return false, 0, "", sshRpcResult.Message, nil
134+
}
135+
136+
portInt, err := strconv.Atoi(sshRpcResult.ServerPort)
137+
if err != nil {
138+
return false, 0, "", "", fmt.Errorf("error parsing port: %v", err)
139+
}
140+
141+
return sshRpcResult.Result, portInt, sshRpcResult.User, sshRpcResult.Message, err
142+
}
143+
120144
func GetOrChooseCodespace(ctx context.Context, apiClient *api.API, user *api.User, codespaceName string) (codespace *api.Codespace, token string, err error) {
121145
if codespaceName == "" {
122146
codespace, err = ChooseCodespace(ctx, apiClient, user)

internal/codespaces/ssh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/github/go-liveshare"
1515
)
1616

17-
func MakeSSHTunnel(ctx context.Context, lsclient *liveshare.Client, serverPort int) (int, <-chan error, error) {
17+
func MakeSSHTunnel(ctx context.Context, lsclient *liveshare.Client, serverPort int, remoteSSHPort int) (int, <-chan error, error) {
1818
tunnelClosed := make(chan error)
1919

2020
server, err := liveshare.NewServer(lsclient)
@@ -29,7 +29,7 @@ func MakeSSHTunnel(ctx context.Context, lsclient *liveshare.Client, serverPort i
2929
}
3030

3131
// TODO(josebalius): This port won't always be 2222
32-
if err := server.StartSharing(ctx, "sshd", 2222); err != nil {
32+
if err := server.StartSharing(ctx, "sshd", remoteSSHPort); err != nil {
3333
return 0, nil, fmt.Errorf("sharing sshd port: %v", err)
3434
}
3535

0 commit comments

Comments
 (0)