forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
269 lines (233 loc) · 7.22 KB
/
hook.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hook
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path"
"strings"
"syscall"
log "github.com/golang/glog"
vtenv "github.com/youtube/vitess/go/vt/env"
)
// Hook is the input structure for this library.
type Hook struct {
Name string
Parameters []string
ExtraEnv map[string]string
}
// HookResult is returned by the Execute method.
type HookResult struct {
ExitStatus int // HOOK_SUCCESS if it succeeded
Stdout string
Stderr string
}
// The hook will return a value between 0 and 255. 0 if it succeeds.
// So we have these additional values here for more information.
const (
// HOOK_SUCCESS is returned when the hook worked.
HOOK_SUCCESS = 0
// HOOK_DOES_NOT_EXIST is returned when the hook cannot be found.
HOOK_DOES_NOT_EXIST = -1
// HOOK_STAT_FAILED is returned when the hook exists, but stat
// on it fails.
HOOK_STAT_FAILED = -2
// HOOK_CANNOT_GET_EXIT_STATUS is returned when after
// execution, we fail to get the exit code for the hook.
HOOK_CANNOT_GET_EXIT_STATUS = -3
// HOOK_INVALID_NAME is returned if a hook has an invalid name.
HOOK_INVALID_NAME = -4
// HOOK_VTROOT_ERROR is returned if VTROOT is not set properly.
HOOK_VTROOT_ERROR = -5
// HOOK_GENERIC_ERROR is returned for unknown errors.
HOOK_GENERIC_ERROR = -6
)
// WaitFunc is a return type for the Pipe methods.
// It returns the process stderr and an error, if any.
type WaitFunc func() (string, error)
// NewHook returns a Hook object with the provided name and params.
func NewHook(name string, params []string) *Hook {
return &Hook{Name: name, Parameters: params}
}
// NewSimpleHook returns a Hook object with just a name.
func NewSimpleHook(name string) *Hook {
return &Hook{Name: name}
}
// findHook trie to locate the hook, and returns the exec.Cmd for it.
func (hook *Hook) findHook() (*exec.Cmd, int, error) {
// Check the hook path.
if strings.Contains(hook.Name, "/") {
return nil, HOOK_INVALID_NAME, fmt.Errorf("hooks cannot contains '/'")
}
// Find our root.
root, err := vtenv.VtRoot()
if err != nil {
return nil, HOOK_VTROOT_ERROR, fmt.Errorf("cannot get VTROOT: %v", err)
}
// See if the hook exists.
vthook := path.Join(root, "vthook", hook.Name)
_, err = os.Stat(vthook)
if err != nil {
if os.IsNotExist(err) {
return nil, HOOK_DOES_NOT_EXIST, fmt.Errorf("missing hook %v", vthook)
}
return nil, HOOK_STAT_FAILED, fmt.Errorf("cannot stat hook %v: %v", vthook, err)
}
// Configure the command.
log.Infof("hook: executing hook: %v %v", vthook, strings.Join(hook.Parameters, " "))
cmd := exec.Command(vthook, hook.Parameters...)
if len(hook.ExtraEnv) > 0 {
cmd.Env = os.Environ()
for key, value := range hook.ExtraEnv {
cmd.Env = append(cmd.Env, key+"="+value)
}
}
return cmd, HOOK_SUCCESS, nil
}
// Execute tries to execute the Hook and returns a HookResult.
func (hook *Hook) Execute() (result *HookResult) {
result = &HookResult{}
// Find the hook.
cmd, status, err := hook.findHook()
if err != nil {
result.ExitStatus = status
result.Stderr = err.Error() + "\n"
return result
}
// Run it.
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
result.Stdout = stdout.String()
result.Stderr = stderr.String()
if err == nil {
result.ExitStatus = HOOK_SUCCESS
} else {
if cmd.ProcessState != nil && cmd.ProcessState.Sys() != nil {
result.ExitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
} else {
result.ExitStatus = HOOK_CANNOT_GET_EXIT_STATUS
}
result.Stderr += "ERROR: " + err.Error() + "\n"
}
log.Infof("hook: result is %v", result.String())
return result
}
// ExecuteOptional executes an optional hook, logs if it doesn't
// exist, and returns a printable error.
func (hook *Hook) ExecuteOptional() error {
hr := hook.Execute()
switch hr.ExitStatus {
case HOOK_DOES_NOT_EXIST:
log.Infof("%v hook doesn't exist", hook.Name)
case HOOK_VTROOT_ERROR:
log.Infof("VTROOT not set, so %v hook doesn't exist", hook.Name)
case HOOK_SUCCESS:
// nothing to do here
default:
return fmt.Errorf("%v hook failed(%v): %v", hook.Name, hr.ExitStatus, hr.Stderr)
}
return nil
}
// ExecuteAsWritePipe will execute the hook as in a Unix pipe,
// directing output to the provided writer. It will return:
// - an io.WriteCloser to write data to.
// - a WaitFunc method to call to wait for the process to exit,
// that returns stderr and the cmd.Wait() error.
// - an error code and an error if anything fails.
func (hook *Hook) ExecuteAsWritePipe(out io.Writer) (io.WriteCloser, WaitFunc, int, error) {
// Find the hook.
cmd, status, err := hook.findHook()
if err != nil {
return nil, nil, status, err
}
// Configure the process's stdin, stdout, and stderr.
in, err := cmd.StdinPipe()
if err != nil {
return nil, nil, HOOK_GENERIC_ERROR, fmt.Errorf("Failed to configure stdin: %v", err)
}
cmd.Stdout = out
var stderr bytes.Buffer
cmd.Stderr = &stderr
// Start the process.
err = cmd.Start()
if err != nil {
status = HOOK_CANNOT_GET_EXIT_STATUS
if cmd.ProcessState != nil && cmd.ProcessState.Sys() != nil {
status = cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
}
return nil, nil, status, err
}
// And return
return in, func() (string, error) {
err := cmd.Wait()
return stderr.String(), err
}, HOOK_SUCCESS, nil
}
// ExecuteAsReadPipe will execute the hook as in a Unix pipe, reading
// from the provided reader. It will return:
// - an io.Reader to read piped data from.
// - a WaitFunc method to call to wait for the process to exit, that
// returns stderr and the Wait() error.
// - an error code and an error if anything fails.
func (hook *Hook) ExecuteAsReadPipe(in io.Reader) (io.Reader, WaitFunc, int, error) {
// Find the hook.
cmd, status, err := hook.findHook()
if err != nil {
return nil, nil, status, err
}
// Configure the process's stdin, stdout, and stderr.
out, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, HOOK_GENERIC_ERROR, fmt.Errorf("Failed to configure stdout: %v", err)
}
cmd.Stdin = in
var stderr bytes.Buffer
cmd.Stderr = &stderr
// Start the process.
err = cmd.Start()
if err != nil {
status = HOOK_CANNOT_GET_EXIT_STATUS
if cmd.ProcessState != nil && cmd.ProcessState.Sys() != nil {
status = cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
}
return nil, nil, status, err
}
// And return
return out, func() (string, error) {
err := cmd.Wait()
return stderr.String(), err
}, HOOK_SUCCESS, nil
}
// String returns a printable version of the HookResult
func (hr *HookResult) String() string {
result := "result: "
switch hr.ExitStatus {
case HOOK_SUCCESS:
result += "HOOK_SUCCESS"
case HOOK_DOES_NOT_EXIST:
result += "HOOK_DOES_NOT_EXIST"
case HOOK_STAT_FAILED:
result += "HOOK_STAT_FAILED"
case HOOK_CANNOT_GET_EXIT_STATUS:
result += "HOOK_CANNOT_GET_EXIT_STATUS"
case HOOK_INVALID_NAME:
result += "HOOK_INVALID_NAME"
case HOOK_VTROOT_ERROR:
result += "HOOK_VTROOT_ERROR"
default:
result += fmt.Sprintf("exit(%v)", hr.ExitStatus)
}
if hr.Stdout != "" {
result += "\nstdout:\n" + hr.Stdout
}
if hr.Stderr != "" {
result += "\nstderr:\n" + hr.Stderr
}
return result
}