-
Notifications
You must be signed in to change notification settings - Fork 2
/
executor_utils.go
231 lines (196 loc) · 6.06 KB
/
executor_utils.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
// Copyright © NGR Softlab 2020-2024
package sshExecutor
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"time"
"golang.org/x/crypto/ssh"
)
// There are some standard scenarios of using ssh or scp
/////////////////////////////////////////////////////////////////////
// recoverExecutor recovering from panic
func recoverExecutor() {
if r := recover(); r != nil {
logger.Errorf("fatal: recovery in %v\n", r)
}
}
// scanPipe stdPipe to text
func scanPipe(r io.Reader) string {
scanner := bufio.NewScanner(r)
var scanningText string
for scanner.Scan() {
scanningText = scanningText + "\n" + scanner.Text()
}
return scanningText
}
// makeSshClientConfig get ssh client config (ssh.ClientConfig)
func makeSshClientConfig(user, password, privateKey string, timeout time.Duration) *ssh.ClientConfig {
var methods []ssh.AuthMethod
if privateKey != "" {
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
logger.Warningf("warning: parse private key %s\n", err.Error())
} else {
methods = append(methods, ssh.PublicKeys(signer))
}
}
if password != "" {
methods = append(methods, ssh.Password(password))
}
return &ssh.ClientConfig{
User: user,
Auth: methods,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil },
Timeout: timeout,
}
}
// makeSshTerminalModes get ssh.TerminalModes obj
func makeSshTerminalModes() ssh.TerminalModes {
return ssh.TerminalModes{
ssh.ECHO: 0,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
}
/////////////////////////////////////////////////////////////////////
// LocalExecContext local execution command with context
func LocalExecContext(timeout time.Duration, command string, params ...string) (output []byte, err error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, command, params...)
output, err = cmd.Output()
if err != nil {
if output == nil {
output = []byte("")
}
logger.Errorf("error: local exec err %s ::: out %s\n", err.Error(), string(output))
return output, fmt.Errorf("local ssh command error: %s", err.Error())
}
return
}
// GetSudoCommandsOutWithoutErr get result sudo (!) commands... output from ssh connection
func GetSudoCommandsOutWithoutErr(connParams ConnectParams,
timeoutConn, timeoutCmd time.Duration, commands ...string) (output []byte, err error) {
conn, err := GetSshConnection(connParams, timeoutConn)
if err != nil {
logger.Errorf("error: ssh connection %s\n", err.Error())
return output, err
}
defer func() {
errClose := conn.Close()
if errClose != nil {
logger.Warningf("warning: conn close %s\n", errClose.Error())
}
}()
ctx, cancel := context.WithTimeout(
context.Background(),
timeoutCmd)
go func(ctx context.Context) {
defer cancel()
output, err = conn.SendSudoCommandsWithoutErrOut(commands...)
}(ctx)
select {
case <-ctx.Done():
switch ctx.Err() {
case context.DeadlineExceeded:
logger.Errorf("error: sudo conn deadline timeout\n")
return output, errors.New("got sudo ssh connection deadline")
case context.Canceled:
logger.Infof("info: ssh context cancelled by force, whole process in compeleted\n")
}
}
if err != nil {
logger.Errorf("error: sudo ssh commands %s\n", err.Error())
return output, fmt.Errorf("sudo ssh command error: %s", err.Error())
}
return output, nil
}
// GetCommandOutWithErr get result command output (with errOut) from ssh connection
func GetCommandOutWithErr(connParams ConnectParams, kill chan *os.Signal,
timeoutConn, timeoutCmd time.Duration, command string) (output []byte, errOutput []byte, duration time.Duration, err error) {
defer func() {
kill = nil
}()
conn, err := GetSshConnection(connParams, timeoutConn)
if err != nil {
logger.Errorf("error: ssh connection %s\n", err.Error())
return output, errOutput, 0, err
}
defer func() {
errClose := conn.Close()
if errClose != nil {
logger.Warningf("warning: conn close %s\n", errClose.Error())
}
}()
ctx, cancel := context.WithTimeout(
context.Background(),
timeoutCmd)
go func(ctx context.Context) {
defer cancel()
output, errOutput, duration, err = conn.SendOneCommandWithErrOut(kill, command)
}(ctx)
select {
case <-ctx.Done():
switch ctx.Err() {
case context.DeadlineExceeded:
logger.Errorf("error: ssh command deadline %v\n", err)
kill <- &os.Kill
return output, errOutput, duration, errors.New("got ssh connection deadline")
case context.Canceled:
logger.Infof("info: ssh context cancelled by force, whole process is completed %v\n", err)
}
}
if err != nil {
logger.Errorf("error: ssh command %s\n", err.Error())
return output, []byte(err.Error()), duration, fmt.Errorf("ssh command error: %s", err.Error())
}
return output, errOutput, duration, nil
}
// SendFileWithScp get result command output (with errOut) from ssh connection
func SendFileWithScp(connParams ConnectParams, kill chan *os.Signal,
timeoutConn, timeoutCmd time.Duration, pathParams FilePathParams) (time.Duration, error) {
defer func() {
kill = nil
}()
conn, err := GetSshConnection(connParams, timeoutConn)
if err != nil {
logger.Errorf("error: ssh conn %s\n", err.Error())
return 0, err
}
defer func() {
errClose := conn.Close()
if errClose != nil {
logger.Warningf("warning: conn close %s\n", errClose.Error())
}
}()
ctx, cancel := context.WithTimeout(
context.Background(),
timeoutCmd)
var duration time.Duration
go func(ctx context.Context) {
defer cancel()
duration, err = conn.SendScpFile(kill, pathParams)
}(ctx)
select {
case <-ctx.Done():
switch ctx.Err() {
case context.DeadlineExceeded:
logger.Error("error: scp command deadline\n")
kill <- &os.Kill
return duration, errors.New("got scp connection deadline")
case context.Canceled:
logger.Infof("info: scp context cancelled by force, whole process is completed %v\n", err)
}
}
if err != nil {
logger.Errorf("error: scp command %s\n", err.Error())
return duration, fmt.Errorf("scp command error: %s", err.Error())
}
return duration, nil
}