-
Notifications
You must be signed in to change notification settings - Fork 224
/
ssh_terminal.go
199 lines (159 loc) · 4.5 KB
/
ssh_terminal.go
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
package cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
"github.com/pkg/errors"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/flyerr"
"github.com/superfly/flyctl/pkg/agent"
"github.com/superfly/flyctl/pkg/ssh"
"github.com/superfly/flyctl/terminal"
)
func runSSHConsole(cc *cmdctx.CmdContext) error {
client := cc.Client.API()
ctx := createCancellableContext()
terminal.Debugf("Retrieving app info for %s\n", cc.AppName)
app, err := client.GetApp(cc.AppName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
captureError := func(err error) {
// ignore cancelled errors
if errors.Is(err, context.Canceled) {
return
}
flyerr.CaptureException(err,
flyerr.WithTag("feature", "ssh-console"),
flyerr.WithContexts(map[string]interface{}{
"app": app.Name,
"organization": app.Organization.Slug,
}),
)
}
agentclient, err := agent.Establish(ctx, client)
if err != nil {
captureError(err)
return errors.Wrap(err, "can't establish agent")
}
dialer, err := agentclient.Dialer(ctx, &app.Organization)
if err != nil {
captureError(err)
return fmt.Errorf("ssh: can't build tunnel for %s: %s\n", app.Organization.Slug, err)
}
cc.IO.StartProgressIndicatorMsg("Connecting to tunnel")
if err := agentclient.WaitForTunnel(ctx, &app.Organization); err != nil {
captureError(err)
return errors.Wrapf(err, "tunnel unavailable")
}
cc.IO.StopProgressIndicator()
var addr string
if cc.Config.GetBool("select") {
instances, err := agentclient.Instances(ctx, &app.Organization, cc.AppName)
if err != nil {
return fmt.Errorf("look up %s: %w", cc.AppName, err)
}
selected := 0
prompt := &survey.Select{
Message: "Select instance:",
Options: instances.Labels,
PageSize: 15,
}
if err := survey.AskOne(prompt, &selected); err != nil {
return fmt.Errorf("selecting instance: %w", err)
}
addr = fmt.Sprintf("[%s]", instances.Addresses[selected])
} else if len(cc.Args) != 0 {
addr = cc.Args[0]
} else {
addr = fmt.Sprintf("%s.internal", cc.AppName)
}
// wait for the addr to be resolved in dns unless it's an ip address
if !agent.IsIPv6(addr) {
cc.IO.StartProgressIndicatorMsg("Waiting for host")
if err := agentclient.WaitForHost(ctx, &app.Organization, addr); err != nil {
captureError(err)
return errors.Wrapf(err, "host unavailable")
}
cc.IO.StopProgressIndicator()
}
err = sshConnect(&SSHParams{
Ctx: cc,
Org: &app.Organization,
Dialer: dialer,
App: cc.AppName,
Cmd: cc.Config.GetString("command"),
}, addr)
if err != nil {
captureError(err)
}
return err
}
func spin(in, out string) context.CancelFunc {
ctx, cancel := context.WithCancel(context.Background())
if !helpers.IsTerminal() {
fmt.Fprintln(os.Stderr, in)
return cancel
}
go func() {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Writer = os.Stderr
s.Prefix = in
s.FinalMSG = out
s.Start()
defer s.Stop()
<-ctx.Done()
}()
return cancel
}
type SSHParams struct {
Ctx *cmdctx.CmdContext
Org *api.Organization
App string
Dialer agent.Dialer
Cmd string
}
func sshConnect(p *SSHParams, addr string) error {
terminal.Debugf("Fetching certificate for %s\n", addr)
cert, err := singleUseSSHCertificate(p.Ctx, p.Org)
if err != nil {
return fmt.Errorf("create ssh certificate: %w (if you haven't created a key for your org yet, try `flyctl ssh establish`)", err)
}
pk, err := parsePrivateKey(cert.Key)
if err != nil {
return errors.Wrap(err, "parse ssh certificate")
}
pemkey := MarshalED25519PrivateKey(pk, "single-use certificate")
terminal.Debugf("Keys for %s configured; connecting...\n", addr)
sshClient := &ssh.Client{
Addr: addr + ":22",
User: "root",
Dial: p.Dialer.DialContext,
Certificate: cert.Certificate,
PrivateKey: string(pemkey),
}
endSpin := spin(fmt.Sprintf("Connecting to %s...", addr),
fmt.Sprintf("Connecting to %s... complete\n", addr))
defer endSpin()
if err := sshClient.Connect(context.Background()); err != nil {
return errors.Wrap(err, "error connecting to SSH server")
}
defer sshClient.Close()
terminal.Debugf("Connection completed.\n", addr)
endSpin()
term := &ssh.Terminal{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Mode: "xterm",
}
if err := sshClient.Shell(context.Background(), term, p.Cmd); err != nil {
return errors.Wrap(err, "ssh shell")
}
return nil
}