/
host.go
260 lines (210 loc) · 4.95 KB
/
host.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package ssh
import (
"context"
"errors"
"fmt"
"strings"
"golang.org/x/crypto/ssh"
"github.com/seal-io/terraform-provider-courier/pkg/target/codec"
"github.com/seal-io/terraform-provider-courier/pkg/target/proxy"
"github.com/seal-io/terraform-provider-courier/pkg/target/types"
"github.com/seal-io/terraform-provider-courier/utils/bytespool"
"github.com/seal-io/terraform-provider-courier/utils/iox"
"github.com/seal-io/terraform-provider-courier/utils/strx"
)
type Host struct {
client *ssh.Client
platform string
}
func New(opts types.HostOptions) (types.Host, error) {
if opts.Authn.Type != "ssh" {
return nil, errors.New("invalid type")
}
if opts.Address == "" {
return nil, errors.New("no address specified")
}
proxies, err := proxyWith(types.DialClosers{}, opts.Proxies)
if err != nil {
return nil, fmt.Errorf(
"failed to dail %s via proxies: %w",
opts.Address,
err,
)
}
c, err := Dial(proxies, opts.HostOption)
if err != nil {
return nil, fmt.Errorf("failed to dial %s: %w", opts.Address, err)
}
return &Host{
client: c,
platform: "linux",
}, nil
}
func proxyWith(
pds types.DialClosers,
dhs []types.HostOption,
) (d types.DialCloser, err error) {
if len(dhs) == 0 {
return pds, nil
}
da, dhs := dhs[0], dhs[1:]
switch da.Authn.Type {
default:
return nil, errors.New("unknown host type")
case "ssh":
d, err = Dial(pds, da)
case "proxy":
d, err = proxy.Dial(pds, da)
}
if err != nil {
_ = pds.Close()
return nil, err
}
if pds != nil {
pds = pds[:len(pds):len(pds)]
}
return proxyWith(append(pds, d), dhs)
}
func (h *Host) Close() error {
return h.client.Close()
}
func (h *Host) State(ctx context.Context) (types.HostStatus, error) {
t, err := h.Shell(ctx)
if err != nil {
return types.HostStatus{}, err
}
defer func() { _ = t.Close() }()
osBs, err := t.ExecuteWithOutput("uname", "-s")
if err != nil {
return types.HostStatus{}, fmt.Errorf("failed to get os: %w", err)
}
os := strings.ToLower(strx.FromBytes(&osBs))
// Refer to https://stackoverflow.com/questions/45125516/possible-values-for-uname-m.
archBs, err := t.ExecuteWithOutput("uname", "-m")
if err != nil {
return types.HostStatus{}, fmt.Errorf(
"failed to get arch: %w",
err,
)
}
arch := strings.ToLower(strx.FromBytes(&archBs))
switch {
case arch == "x86_64":
arch = "amd64"
case strings.HasSuffix(arch, "aarch64") || strings.HasSuffix(arch, "armv8"):
arch = "arm64"
case strings.HasPrefix(arch, "riscv"):
arch = "riscv64"
case arch == "i386", arch == "i686", arch == "x86":
arch = "386"
case strings.HasPrefix(arch, "arm"):
arch = "arm"
}
versionBs, err := t.ExecuteWithOutput("uname", "-r")
if err != nil {
return types.HostStatus{}, fmt.Errorf(
"failed to get kernel version: %w",
err,
)
}
version := strings.ToLower(strx.FromBytes(&versionBs))
return types.HostStatus{
Accessible: true,
OS: os,
Arch: arch,
Version: version,
}, nil
}
func (h *Host) Execute(
ctx context.Context,
cmd string,
args ...string,
) error {
if cmd == "" {
return errors.New("blank command")
}
s, err := h.getSessionWithContext(ctx)
if err != nil {
return err
}
defer func() { _ = s.Close() }()
command := codec.EncodeExecInput(h.platform, cmd, args)
return s.Run(command)
}
func (h *Host) ExecuteWithOutput(
ctx context.Context,
cmd string,
args ...string,
) ([]byte, error) {
if cmd == "" {
return nil, errors.New("blank command")
}
s, err := h.getSessionWithContext(ctx)
if err != nil {
return nil, err
}
defer func() { _ = s.Close() }()
command := codec.EncodeExecInput(h.platform, cmd, args)
return s.CombinedOutput(command)
}
type session struct {
*ssh.Session
context.Context
Cancel context.CancelFunc
}
func (h *Host) getSessionWithContext(
ctx context.Context,
) (*session, error) {
s, err := h.client.NewSession()
if err != nil {
return nil, err
}
_, err = s.SendRequest("keepalive", true, nil)
if err != nil {
return nil, fmt.Errorf("disconnected: %w", err)
}
sCtx, sCtxCancel := context.WithCancel(ctx)
cs := &session{
Session: s,
Context: sCtx,
Cancel: sCtxCancel,
}
go func() {
select {
case <-ctx.Done():
_ = s.Signal(ssh.SIGKILL)
cs.Cancel()
case <-cs.Done():
}
}()
return cs, nil
}
func (s *session) Close() error {
defer s.Cancel()
return s.Session.Close()
}
func (s *session) Output(cmd string) ([]byte, error) {
if s.Stdout != nil {
return nil, errors.New("ssh: Stdout already set")
}
buf := bytespool.GetBuffer()
defer func() { bytespool.Put(buf) }()
s.Stdout = buf
err := s.Run(cmd)
return buf.Bytes(), err
}
func (s *session) CombinedOutput(cmd string) ([]byte, error) {
if s.Stdout != nil {
return nil, errors.New("ssh: Stdout already set")
}
if s.Stderr != nil {
return nil, errors.New("ssh: Stderr already set")
}
buf := bytespool.GetBuffer()
defer func() { bytespool.Put(buf) }()
wr := iox.SingleWriter(buf)
s.Stdout = wr
s.Stderr = wr
err := s.Run(cmd)
return buf.Bytes(), err
}