forked from apenella/go-ansible
/
defaultExecute.go
275 lines (231 loc) · 9.32 KB
/
defaultExecute.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package execute
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"github.com/LinYuXinQing/gooo-ansible/pkg/stdoutcallback"
"github.com/LinYuXinQing/gooo-ansible/pkg/stdoutcallback/results"
errors "github.com/apenella/go-common-utils/error"
"github.com/fatih/color"
)
const (
// AnsiblePlaybookErrorCodeGeneralError is the error code for a general error
AnsiblePlaybookErrorCodeGeneralError = 1
// AnsiblePlaybookErrorCodeOneOrMoreHostFailed is the error code for a one or more host failed
AnsiblePlaybookErrorCodeOneOrMoreHostFailed = 2
// AnsiblePlaybookErrorCodeOneOrMoreHostUnreachable is the error code for a one or more host unreachable
AnsiblePlaybookErrorCodeOneOrMoreHostUnreachable = 3
// AnsiblePlaybookErrorCodeParserError is the error code for a parser error
AnsiblePlaybookErrorCodeParserError = 4
// AnsiblePlaybookErrorCodeBadOrIncompleteOptions is the error code for a bad or incomplete options
AnsiblePlaybookErrorCodeBadOrIncompleteOptions = 5
// AnsiblePlaybookErrorCodeUserInterruptedExecution is the error code for a user interrupted execution
AnsiblePlaybookErrorCodeUserInterruptedExecution = 99
// AnsiblePlaybookErrorCodeUnexpectedError is the error code for a unexpected error
AnsiblePlaybookErrorCodeUnexpectedError = 250
// AnsiblePlaybookErrorMessageGeneralError is the error message for a general error
AnsiblePlaybookErrorMessageGeneralError = "ansible-playbook error: general error"
// AnsiblePlaybookErrorMessageOneOrMoreHostFailed is the error message for a one or more host failed
AnsiblePlaybookErrorMessageOneOrMoreHostFailed = "ansible-playbook error: one or more host failed"
// AnsiblePlaybookErrorMessageOneOrMoreHostUnreachable is the error message for a one or more host unreachable
AnsiblePlaybookErrorMessageOneOrMoreHostUnreachable = "ansible-playbook error: one or more host unreachable"
// AnsiblePlaybookErrorMessageParserError is the error message for a parser error
AnsiblePlaybookErrorMessageParserError = "ansible-playbook error: parser error"
// AnsiblePlaybookErrorMessageBadOrIncompleteOptions is the error message for a bad or incomplete options
AnsiblePlaybookErrorMessageBadOrIncompleteOptions = "ansible-playbook error: bad or incomplete options"
// AnsiblePlaybookErrorMessageUserInterruptedExecution is the error message for a user interrupted execution
AnsiblePlaybookErrorMessageUserInterruptedExecution = "ansible-playbook error: user interrupted execution"
// AnsiblePlaybookErrorMessageUnexpectedError is the error message for a unexpected error
AnsiblePlaybookErrorMessageUnexpectedError = "ansible-playbook error: unexpected error"
)
// EnvVars represents a custom environment for an ansible playbook execution.
type EnvVars map[string]string
// Environ returns a copy of strings representing the custom environment, in the form "key=value".
func (e EnvVars) Environ() []string {
result := make([]string, 0, len(e))
for k, v := range e {
result = append(result, fmt.Sprintf("%s=%s", k, v))
}
return result
}
// DefaultExecute is a simple definition of an executor
type DefaultExecute struct {
// Writer is where is written the command stdout
Write io.Writer
// WriterError is where is written the command stderr
WriterError io.Writer
// ShowDuration enables to show the execution duration time after the command finishes
ShowDuration bool
// CmdRunDir specifies the working directory of the command.
CmdRunDir string
// EnvVars specifies env vars of the command.
EnvVars EnvVars
// OutputFormat
Transformers []results.TransformerFunc
}
// NewDefaultExecute return a new DefaultExecute instance with all options
func NewDefaultExecute(options ...ExecuteOptions) *DefaultExecute {
execute := &DefaultExecute{
EnvVars: make(map[string]string),
}
for _, opt := range options {
opt(execute)
}
return execute
}
// WithWrite set the writer to be used by DefaultExecutor
func WithWrite(w io.Writer) ExecuteOptions {
return func(e Executor) {
e.(*DefaultExecute).Write = w
}
}
// WithWriteError set the error writer to be used by DefaultExecutor
func WithWriteError(w io.Writer) ExecuteOptions {
return func(e Executor) {
e.(*DefaultExecute).WriterError = w
}
}
// WithCmdRunDir set the command run directory to be used by DefaultExecutor
func WithCmdRunDir(cmdRunDir string) ExecuteOptions {
return func(e Executor) {
e.(*DefaultExecute).CmdRunDir = cmdRunDir
}
}
// WithShowDuration enables to show command duration
func WithShowDuration() ExecuteOptions {
return func(e Executor) {
e.(*DefaultExecute).ShowDuration = true
}
}
// WithTransformers add trasformes
func WithTransformers(trans ...results.TransformerFunc) ExecuteOptions {
return func(e Executor) {
e.(*DefaultExecute).Transformers = trans
}
}
// WithEnvVar adds the provided env var to the command
func WithEnvVar(key, value string) ExecuteOptions {
return func(e Executor) {
e.(*DefaultExecute).EnvVars[key] = value
}
}
// Execute takes a command and args and runs it, streaming output to stdout
func (e *DefaultExecute) Execute(ctx context.Context, command []string, resultsFunc stdoutcallback.StdoutCallbackResultsFunc, options ...ExecuteOptions) error {
var (
err error
cmdStderr, cmdStdout io.ReadCloser
wg sync.WaitGroup
)
e.checkCompatibility()
execErrChan := make(chan error)
// apply all options to the executor
for _, opt := range options {
opt(e)
}
if resultsFunc == nil {
resultsFunc = results.DefaultStdoutCallbackResults
}
// default stdout and stderr for the main process
if e.Write == nil {
e.Write = os.Stdout
}
if e.WriterError == nil {
e.WriterError = os.Stderr
}
cmd := exec.CommandContext(ctx, command[0], command[1:]...)
if len(e.CmdRunDir) > 0 {
cmd.Dir = e.CmdRunDir
}
if len(e.EnvVars) > 0 {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, e.EnvVars.Environ()...)
}
cmd.Stdin = os.Stdin // connects the main process' stdin to ansible's stdin
cmdStdout, err = cmd.StdoutPipe()
defer cmdStdout.Close()
if err != nil {
return errors.New("(DefaultExecute::Execute)", "Error creating stdout pipe", err)
}
cmdStderr, err = cmd.StderrPipe()
defer cmdStderr.Close()
if err != nil {
return errors.New("(DefaultExecute::Execute)", "Error creating stderr pipe", err)
}
err = cmd.Start()
if err != nil {
return errors.New("(DefaultExecute::Execute)", "Error starting command", err)
}
// Waig for stdout and stderr
wg.Add(2)
// stdout management
go func() {
defer close(execErrChan)
trans := []results.TransformerFunc{}
for _, t := range e.Transformers {
trans = append(trans, t)
}
// when using the default results func DefaultStdoutCallbackResults,
// reads from ansible's stdout and writes to main process' stdout
err := resultsFunc(ctx, cmdStdout, e.Write, trans...)
wg.Done()
execErrChan <- err
}()
// stderr management
go func() {
// show stderr messages using default stdout callback results
results.DefaultStdoutCallbackResults(ctx, cmdStderr, e.WriterError, []results.TransformerFunc{}...)
wg.Done()
}()
wg.Wait()
if err := <-execErrChan; err != nil {
return errors.New("(DefaultExecute::Execute)", "Error managing results output", err)
}
err = cmd.Wait()
if err != nil {
if ctx.Err() != nil {
fmt.Fprintf(e.Write, "%s\n", fmt.Sprintf("\nWhoops! %s\n", ctx.Err()))
} else {
errorMessage := fmt.Sprintf("Command executed:\n%s\n", cmd.String())
if len(e.EnvVars) > 0 {
errorMessage = fmt.Sprintf("%s\nEnvironment variables:\n%s\n", errorMessage, strings.Join(e.EnvVars.Environ(), "\n"))
}
errorMessage = fmt.Sprintf("%s\nError:\n%s\n", errorMessage, err.Error())
stderrErrorMessage := string(err.(*exec.ExitError).Stderr)
if len(stderrErrorMessage) > 0 {
errorMessage = fmt.Sprintf("%s\n'%s'\n", errorMessage, stderrErrorMessage)
}
exitError, exists := err.(*exec.ExitError)
if exists {
ws := exitError.Sys().(syscall.WaitStatus)
switch ws.ExitStatus() {
case AnsiblePlaybookErrorCodeGeneralError:
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageGeneralError, errorMessage)
case AnsiblePlaybookErrorCodeOneOrMoreHostFailed:
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageOneOrMoreHostFailed, errorMessage)
case AnsiblePlaybookErrorCodeOneOrMoreHostUnreachable:
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageOneOrMoreHostUnreachable, errorMessage)
case AnsiblePlaybookErrorCodeParserError:
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageParserError, errorMessage)
case AnsiblePlaybookErrorCodeBadOrIncompleteOptions:
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageBadOrIncompleteOptions, errorMessage)
case AnsiblePlaybookErrorCodeUserInterruptedExecution:
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageUserInterruptedExecution, errorMessage)
case AnsiblePlaybookErrorCodeUnexpectedError:
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageUnexpectedError, errorMessage)
}
}
return errors.New("(DefaultExecute::Execute)", fmt.Sprintf("Error during command execution: %s", errorMessage))
}
}
return nil
}
func (e *DefaultExecute) checkCompatibility() {
if e.ShowDuration {
color.Cyan("[WARNING] ShowDuration argument, on DefaultExecute, is deprecated and will be removed in future versions. Use the ExecutorTimeMeasurement middleware instead.")
}
}