|
1 | 1 | package shell |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "fmt" |
6 | 5 | "os" |
7 | 6 | "os/exec" |
8 | | - "strings" |
9 | 7 | "time" |
10 | 8 |
|
11 | 9 | nodev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1" |
12 | | - "connectrpc.com/connect" |
13 | 10 |
|
14 | 11 | "github.com/brevdev/brev-cli/pkg/analytics" |
15 | 12 | "github.com/brevdev/brev-cli/pkg/cmd/completions" |
16 | 13 | "github.com/brevdev/brev-cli/pkg/cmd/hello" |
17 | 14 | "github.com/brevdev/brev-cli/pkg/cmd/refresh" |
18 | | - "github.com/brevdev/brev-cli/pkg/cmd/register" |
19 | 15 | "github.com/brevdev/brev-cli/pkg/cmd/util" |
20 | | - "github.com/brevdev/brev-cli/pkg/config" |
21 | 16 | "github.com/brevdev/brev-cli/pkg/entity" |
22 | 17 | breverrors "github.com/brevdev/brev-cli/pkg/errors" |
23 | 18 | "github.com/brevdev/brev-cli/pkg/store" |
@@ -51,7 +46,6 @@ type ShellStore interface { |
51 | 46 | refresh.RefreshStore |
52 | 47 | GetOrganizations(options *store.GetOrganizationsOptions) ([]entity.Organization, error) |
53 | 48 | GetWorkspaces(organizationID string, options *store.GetWorkspacesOptions) ([]entity.Workspace, error) |
54 | | - GetAccessToken() (string, error) |
55 | 49 | } |
56 | 50 |
|
57 | 51 | func NewCmdShell(t *terminal.Terminal, store ShellStore, noLoginStartStore ShellStore) *cobra.Command { |
@@ -87,7 +81,7 @@ func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID |
87 | 81 | workspace, err := util.GetUserWorkspaceByNameOrIDErr(sstore, workspaceNameOrID) |
88 | 82 | if err != nil { |
89 | 83 | // Workspace not found — try external nodes. |
90 | | - node, nodeErr := findExternalNode(sstore, workspaceNameOrID) |
| 84 | + node, nodeErr := util.FindExternalNode(sstore, workspaceNameOrID) |
91 | 85 | if nodeErr != nil || node == nil { |
92 | 86 | return breverrors.WrapAndTrace(err) // return original workspace error |
93 | 87 | } |
@@ -157,64 +151,14 @@ func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID |
157 | 151 | return nil |
158 | 152 | } |
159 | 153 |
|
160 | | -func findExternalNode(sstore ShellStore, name string) (*nodev1.ExternalNode, error) { |
161 | | - org, err := sstore.GetActiveOrganizationOrDefault() |
162 | | - if err != nil { |
163 | | - return nil, breverrors.WrapAndTrace(err) |
164 | | - } |
165 | | - client := register.NewNodeServiceClient(sstore, config.GlobalConfig.GetBrevPublicAPIURL()) |
166 | | - resp, err := client.ListNodes(context.Background(), connect.NewRequest(&nodev1.ListNodesRequest{ |
167 | | - OrganizationId: org.ID, |
168 | | - })) |
169 | | - if err != nil { |
170 | | - return nil, breverrors.WrapAndTrace(err) |
171 | | - } |
172 | | - for _, node := range resp.Msg.GetItems() { |
173 | | - if strings.EqualFold(node.GetName(), name) { |
174 | | - return node, nil |
175 | | - } |
176 | | - } |
177 | | - return nil, nil |
178 | | -} |
179 | | - |
180 | 154 | func shellIntoExternalNode(t *terminal.Terminal, sstore ShellStore, node *nodev1.ExternalNode) error { |
181 | | - user, err := sstore.GetCurrentUser() |
| 155 | + info, err := util.ResolveExternalNodeSSH(sstore, node) |
182 | 156 | if err != nil { |
183 | 157 | return breverrors.WrapAndTrace(err) |
184 | 158 | } |
185 | 159 |
|
186 | | - var linuxUser string |
187 | | - for _, access := range node.GetSshAccess() { |
188 | | - if access.GetUserId() == user.ID { |
189 | | - linuxUser = access.GetLinuxUser() |
190 | | - break |
191 | | - } |
192 | | - } |
193 | | - if linuxUser == "" { |
194 | | - return breverrors.New(fmt.Sprintf("you don't have SSH access to node %q — try running: brev grant-ssh", node.GetName())) |
195 | | - } |
196 | | - |
197 | | - var sshPort *nodev1.Port |
198 | | - for _, p := range node.GetPorts() { |
199 | | - if p.GetProtocol() == nodev1.PortProtocol_PORT_PROTOCOL_SSH { |
200 | | - sshPort = p |
201 | | - break |
202 | | - } |
203 | | - } |
204 | | - if sshPort == nil { |
205 | | - return breverrors.New(fmt.Sprintf("no SSH port configured for node %q", node.GetName())) |
206 | | - } |
207 | | - |
208 | | - hostname := sshPort.GetHostname() |
209 | | - if hostname == "" { |
210 | | - return breverrors.New(fmt.Sprintf("SSH port has no hostname for node %q", node.GetName())) |
211 | | - } |
212 | | - port := sshPort.GetPortNumber() |
213 | | - |
214 | | - target := fmt.Sprintf("%s@%s", linuxUser, hostname) |
215 | | - t.Vprintf("Connecting to external node %q as %s on port %d...\n", node.GetName(), linuxUser, port) |
216 | | - |
217 | | - return runSSHWithPort(target, port) |
| 160 | + t.Vprintf("Connecting to external node %q as %s on port %d...\n", node.GetName(), info.LinuxUser, info.Port) |
| 161 | + return runSSHWithPort(info.SSHTarget(), info.Port) |
218 | 162 | } |
219 | 163 |
|
220 | 164 | func runSSHWithPort(target string, port int32) error { |
|
0 commit comments