-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathshell.go
More file actions
236 lines (204 loc) · 7.29 KB
/
Copy pathshell.go
File metadata and controls
236 lines (204 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package shell
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
nodev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1"
"github.com/brevdev/brev-cli/pkg/analytics"
"github.com/brevdev/brev-cli/pkg/cmd/completions"
"github.com/brevdev/brev-cli/pkg/cmd/hello"
"github.com/brevdev/brev-cli/pkg/cmd/refresh"
"github.com/brevdev/brev-cli/pkg/cmd/util"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/brevdev/brev-cli/pkg/writeconnectionevent"
"github.com/spf13/cobra"
)
var (
openLong = "[command in beta] This will shell in to your instance"
openExample = ` # SSH into an instance by name or ID
brev shell instance_id_or_name
brev shell my-instance
# Create a GPU instance and SSH into it
brev shell $(brev create my-instance)
# Create with specific GPU and connect
brev shell $(brev search --gpu-name A100 | brev create ml-box)
# SSH into the host machine instead of the container
brev shell my-instance --host
# For non-interactive command execution, use 'brev exec':
brev exec my-instance "nvidia-smi"`
)
type ShellStore interface {
util.WorkspaceStartStore
refresh.RefreshStore
GetOrganizations(options *store.GetOrganizationsOptions) ([]entity.Organization, error)
GetWorkspaces(organizationID string, options *store.GetWorkspacesOptions) ([]entity.Workspace, error)
GetAccessToken() (string, error)
}
func NewCmdShell(t *terminal.Terminal, store ShellStore, noLoginStartStore ShellStore) *cobra.Command {
var host bool
cmd := &cobra.Command{
Annotations: map[string]string{"access": ""},
Use: "shell <instance>",
Aliases: []string{"ssh"},
DisableFlagsInUseLine: true,
Short: "[beta] Open a shell in your instance",
Long: openLong,
Example: openExample,
Args: cobra.ExactArgs(1),
ValidArgsFunction: completions.GetAllWorkspaceNameCompletionHandler(noLoginStartStore, t),
RunE: func(cmd *cobra.Command, args []string) error {
instanceName := args[0]
err := runShellCommand(t, store, instanceName, host)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
}
cmd.Flags().BoolVarP(&host, "host", "", false, "ssh into the host machine instead of the container")
return cmd
}
const pollTimeout = 10 * time.Minute
func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID string, host bool) error {
if _, err := sstore.GetAccessToken(); err != nil {
return breverrors.WrapAndTrace(err)
}
s := t.NewSpinner()
target, err := util.ResolveWorkspaceOrNode(sstore, workspaceNameOrID)
if err != nil {
return breverrors.WrapAndTrace(err)
}
if target.Node != nil {
return shellIntoExternalNode(t, sstore, target.Node)
}
workspace := target.Workspace
if workspace.Status == "STOPPED" { // we start the env for the user
err = util.StartWorkspaceIfStopped(t, s, sstore, workspaceNameOrID, workspace, pollTimeout)
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
err = util.PollUntil(s, workspace.ID, "RUNNING", sstore, " waiting for instance to be ready...", pollTimeout)
if err != nil {
return breverrors.WrapAndTrace(err)
}
refreshRes := refresh.RunRefreshAsync(sstore)
workspace, err = util.GetUserWorkspaceByNameOrIDErr(sstore, workspaceNameOrID)
if err != nil {
return breverrors.WrapAndTrace(err)
}
if workspace.Status != "RUNNING" {
return breverrors.New("Instance is not running")
}
localIdentifier := workspace.GetLocalIdentifier()
if host {
localIdentifier = workspace.GetHostIdentifier()
}
sshName := string(localIdentifier)
err = refreshRes.Await()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = util.WaitForSSHToBeAvailable(sshName, s)
if err != nil {
return breverrors.WrapAndTrace(err)
}
// we don't care about the error here but should log with sentry
// legacy environments wont support this and cause errrors,
// but we don't want to block the user from using the shell
_ = writeconnectionevent.WriteWCEOnEnv(sstore, workspace.DNS)
err = runSSH(sshName, host)
if err != nil {
return breverrors.WrapAndTrace(err)
}
// Call analytics for shell
userID := ""
user, err := sstore.GetCurrentUser()
if err != nil {
userID = workspace.CreatedByUserID
} else {
userID = user.ID
}
data := analytics.EventData{
EventName: "Brev Open",
UserID: userID,
Properties: map[string]string{
"instanceId": workspace.ID,
},
}
_ = analytics.TrackEvent(data)
return nil
}
func shellIntoExternalNode(t *terminal.Terminal, sstore ShellStore, node *nodev1.ExternalNode) error {
info, err := util.ResolveExternalNodeSSH(sstore, node)
if err != nil {
return breverrors.WrapAndTrace(err)
}
privateKeyPath, err := sstore.GetPrivateKeyPath()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if _, err := os.Stat(privateKeyPath); os.IsNotExist(err) {
t.Vprintf("fetching keys...\n")
if refreshErr := refresh.RunRefreshAsync(sstore).Await(); refreshErr != nil {
return breverrors.WrapAndTrace(refreshErr)
}
}
t.Vprintf("Connecting to external node %q as %s on port %d (key: %s)...\n", node.GetName(), info.LinuxUser, info.Port, privateKeyPath)
return runSSHWithPort(info.SSHTarget(), info.Port, privateKeyPath)
}
func runSSHWithPort(target string, port int32, identityFile string) error {
sshAgentEval := "eval $(ssh-agent -s)"
cmd := fmt.Sprintf("%s && ssh -i %q -o StrictHostKeyChecking=no -p %d %s", sshAgentEval, identityFile, port, target)
sshCmd := exec.Command("bash", "-c", cmd) //nolint:gosec //cmd is constructed from API data
sshCmd.Stderr = os.Stderr
sshCmd.Stdout = os.Stdout
sshCmd.Stdin = os.Stdin
err := hello.SetHasRunShell(true)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = sshCmd.Run()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func runSSH(sshAlias string, host bool) error {
sshAgentEval := "eval $(ssh-agent -s)"
var cmd string
if host {
cmd = fmt.Sprintf("%s && ssh %s", sshAgentEval, sshAlias)
} else {
// SSH into VM and respect container WORKDIR if containerized, otherwise use default directory
cmd = fmt.Sprintf("%s && ssh -t %s 'DIR=$(readlink -f /proc/1/cwd 2>/dev/null || pwd); cd \"$DIR\" || echo \"Warning: Could not access container directory\" >&2; exec -l ${SHELL:-/bin/sh}'", sshAgentEval, sshAlias)
}
var stderrBuf bytes.Buffer
sshCmd := exec.Command("bash", "-c", cmd) //nolint:gosec //cmd is user input
sshCmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
sshCmd.Stdout = os.Stdout
sshCmd.Stdin = os.Stdin
err := hello.SetHasRunShell(true)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = sshCmd.Run()
if err != nil {
stderrStr := stderrBuf.String()
if strings.Contains(stderrStr, "unix_listener") || strings.Contains(stderrStr, "path too long") {
fmt.Fprintf(os.Stderr, "\nbrev shell failed: SSH ControlPath socket path is too long for this system.\n")
fmt.Fprintf(os.Stderr, "Fix: run 'brev refresh' to regenerate your SSH config with the updated ControlPath.\n")
} else {
fmt.Fprintf(os.Stderr, "\nbrev shell failed. If the above SSH error is unclear, try running 'brev refresh' and reconnecting.\n")
}
return breverrors.WrapAndTrace(err)
}
return nil
}