-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh.go
More file actions
290 lines (223 loc) · 8.02 KB
/
ssh.go
File metadata and controls
290 lines (223 loc) · 8.02 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2026 Flant JSC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pkg
import (
"context"
"errors"
"regexp"
"time"
"github.com/deckhouse/lib-dhctl/pkg/retry"
"github.com/deckhouse/lib-connection/pkg/ssh/session"
)
type StandaloneClientOpts struct {
SetSettingsFromDefaultsIfNeeded bool
}
type StandaloneClientOpt func(*StandaloneClientOpts)
func SSHClientWithSetFromDefaultsIfNeeded() StandaloneClientOpt {
return func(opts *StandaloneClientOpts) {
opts.SetSettingsFromDefaultsIfNeeded = true
}
}
type SSHProvider interface {
// Client
// get current client or initialize from defaults
// after SwitchClient and SwitchToDefault Client will return client initialized
// in SwitchClient and SwitchToDefault method
// SSHProvider can start client after creation if has option for it
Client(ctx context.Context) (SSHClient, error)
// NewAdditionalClient
// initialize new client from default configuration
// use this method if you need more clients not only current
// this method create client from current client setting or from default configuration
// for example if you call SwitchClient next calls of NewAdditionalClient
// create clients for session and private keys passed from SwitchClient
// implementations can store all created clients with NewAdditionalClient
// for stopping in Cleanup
// SSHProvider can start client after creation if has option for it
NewAdditionalClient(ctx context.Context) (SSHClient, error)
// NewStandaloneClient
// initialize new client with passed session settings
// this method create client from passed session
// private keys from default config also passes to new client with privateKeys
// implementations can store all created clients with NewAdditionalClient
// for stopping in Cleanup
// SSHProvider can start client after creation if has option for it
NewStandaloneClient(ctx context.Context, sess *session.Session, privateKeys []session.AgentPrivateKey, opts ...StandaloneClientOpt) (SSHClient, error)
// SwitchClient
// switch current client with new client with provided settings
// method will stop current client but not stop clients created with NewAdditionalClient
// SSHProvider can start client after creation if has option for it
SwitchClient(ctx context.Context, sess *session.Session, privateKeys []session.AgentPrivateKey) (SSHClient, error)
// SwitchToDefault
// switch current client to client with default settings
// method will stop current client but not stop clients created with NewAdditionalClient
// SSHProvider can start client after creation if has option for it
SwitchToDefault(ctx context.Context) (SSHClient, error)
// Cleanup
// stop current client and all clients created with NewAdditionalClient
// and remove all temporary files like private keys with content got from ConnectionConfig
// Cleanup safe for call if no any clients consumed from provider
Cleanup(ctx context.Context) error
}
type Interface interface {
Command(name string, args ...string) Command
File() File
UploadScript(scriptPath string, args ...string) Script
}
type Command interface {
Run(ctx context.Context) error
Cmd(ctx context.Context)
Sudo(ctx context.Context)
StdoutBytes() []byte
StderrBytes() []byte
Output(context.Context) ([]byte, []byte, error)
CombinedOutput(context.Context) ([]byte, error)
OnCommandStart(fn func())
WithEnv(env map[string]string)
WithTimeout(timeout time.Duration)
WithStdoutHandler(h func(line string))
WithStderrHandler(h func(line string))
WithSSHArgs(args ...string)
}
type File interface {
Upload(ctx context.Context, srcPath, dstPath string) error
Download(ctx context.Context, srcPath, dstPath string) error
UploadBytes(ctx context.Context, data []byte, remotePath string) error
DownloadBytes(ctx context.Context, remotePath string) ([]byte, error)
}
type BundlerOptions struct {
StepHeaderRegex *regexp.Regexp
NoLogStepOutOnError bool
StepsDelimiter string
Retries int
}
func (o *BundlerOptions) IsValid() error {
if o.StepsDelimiter == "" {
return errors.New("steps delimiters not set")
}
if o.StepHeaderRegex == nil {
return errors.New("step header regex not set")
}
if o.Retries < 1 {
return errors.New("retries not set")
}
return nil
}
type BundlerOption func(*BundlerOptions)
func BundlerWithStepHeaderRegex(regex *regexp.Regexp) BundlerOption {
return func(opts *BundlerOptions) {
opts.StepHeaderRegex = regex
}
}
func BundlerWithNoLogStepOutOnError(v bool) BundlerOption {
return func(opts *BundlerOptions) {
opts.NoLogStepOutOnError = v
}
}
func BundlerWithStepDelimiter(delimiter string) BundlerOption {
return func(opts *BundlerOptions) {
opts.StepsDelimiter = delimiter
}
}
func BundlerWithRetries(retries int) BundlerOption {
return func(opts *BundlerOptions) {
if retries > 0 {
opts.Retries = retries
}
}
}
type Bundler interface {
Execute(ctx context.Context, parentDir, bundleDir string) ([]byte, error)
}
type Script interface {
Execute(context.Context) (stdout []byte, err error)
// ExecuteBundle
// run script that start another list of scripts and
// log process of running
// by default running bashible bundle BundlerOption
// if need you can set your own BundlerOption with WithBundlerOpts
// to run your bundle script
ExecuteBundle(ctx context.Context, parentDir, bundleDir string) (stdout []byte, err error)
Sudo()
WithStdoutHandler(handler func(string))
WithTimeout(timeout time.Duration)
WithEnvs(envs map[string]string)
WithCleanupAfterExec(doCleanup bool)
WithNoLogStepOutOnError(enabled bool)
WithExecuteUploadDir(dir string)
WithBundlerOpts(opts ...BundlerOption)
}
type Tunnel interface {
Up(ctx context.Context) error
HealthMonitor(errorOutCh chan<- error)
Stop()
String() string
}
type ReverseTunnelChecker interface {
CheckTunnel(context.Context) (string, error)
}
type ReverseTunnelKiller interface {
KillTunnel(context.Context) (string, error)
}
type ReverseTunnel interface {
Up() error
StartHealthMonitor(ctx context.Context, checker ReverseTunnelChecker, killer ReverseTunnelKiller)
Stop()
String() string
}
type KubeProxy interface {
Start(useLocalPort int) (port string, err error)
StopAll()
Stop(startID int)
}
type Check interface {
WithDelaySeconds(seconds int) Check
AwaitAvailability(context.Context, retry.Params) error
CheckAvailability(context.Context) error
ExpectAvailable(context.Context) ([]byte, error)
String() string
}
type SSHLoopHandler func(s SSHClient) error
type SSHClient interface {
// BeforeStart safe starting without create session. Should safe for next Start call
OnlyPreparePrivateKeys() error
Start() error
// Tunnel is used to open local (L) and remote (R) tunnels
Tunnel(address string) Tunnel
// ReverseTunnel is used to open remote (R) tunnel
ReverseTunnel(address string) ReverseTunnel
// Command is used to run commands on remote server
Command(name string, arg ...string) Command
// KubeProxy is used to start kubectl proxy and create a tunnel from local port to proxy port
KubeProxy() KubeProxy
// File is used to upload and download files and directories
File() File
// UploadScript is used to upload script and execute it on remote server
UploadScript(scriptPath string, args ...string) Script
// UploadScript is used to upload script and execute it on remote server
Check() Check
// Stop the client
Stop()
// Loop Looping all available hosts
Loop(fn SSHLoopHandler) error
Session() *session.Session
PrivateKeys() []session.AgentPrivateKey
RefreshPrivateKeys() error
IsStopped() bool
}
type KubeProxyCommand interface {
Command
WaitError() error
Stop()
}