-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand.go
163 lines (141 loc) · 4.51 KB
/
command.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
package healthcheck
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"time"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// CommandHealthcheckConfiguration defines a COMMAND healthcheck configuration
type CommandHealthcheckConfiguration struct {
Base `json:",inline" yaml:",inline"`
Command string `json:"command"`
Arguments []string `json:"arguments"`
Timeout Duration `json:"timeout"`
}
// CommandHealthcheck defines an HTTP healthcheck
type CommandHealthcheck struct {
Logger *zap.Logger
Config *CommandHealthcheckConfiguration
URL string
Tick *time.Ticker
}
// Validate validates the healthcheck configuration
func (config *CommandHealthcheckConfiguration) Validate() error {
if config.Base.Name == "" {
return errors.New("The healthcheck name is missing")
}
if config.Command == "" {
return errors.New("The healthcheck command is missing")
}
if config.Timeout == 0 {
return errors.New("The healthcheck timeout is missing")
}
if !config.Base.OneOff {
if config.Base.Interval < Duration(2*time.Second) {
return errors.New("The healthcheck interval should be greater than 2 second")
}
if config.Base.Interval < config.Timeout {
return errors.New("The healthcheck interval should be greater than the timeout")
}
}
return nil
}
// Initialize the healthcheck.
func (h *CommandHealthcheck) Initialize() error {
return nil
}
// GetConfig get the config
func (h *CommandHealthcheck) GetConfig() interface{} {
return h.Config
}
// Base get the base configuration
func (h *CommandHealthcheck) Base() Base {
return h.Config.Base
}
// SetSource set the healthcheck source
func (h *CommandHealthcheck) SetSource(source string) {
h.Config.Base.Source = source
}
// Summary returns an healthcheck summary
func (h *CommandHealthcheck) Summary() string {
summary := ""
if h.Config.Base.Description != "" {
summary = fmt.Sprintf("%s, command %s", h.Config.Base.Description, h.Config.Command)
} else {
summary = fmt.Sprintf("command %s", h.Config.Command)
}
return summary
}
// LogError logs an error with context
func (h *CommandHealthcheck) LogError(err error, message string) {
h.Logger.Error(err.Error(),
zap.String("extra", message),
zap.String("command", h.Config.Command),
zap.String("name", h.Config.Base.Name))
}
// LogDebug logs a message with context
func (h *CommandHealthcheck) LogDebug(message string) {
h.Logger.Debug(message,
zap.String("command", h.Config.Command),
zap.String("name", h.Config.Base.Name))
}
// LogInfo logs a message with context
func (h *CommandHealthcheck) LogInfo(message string) {
h.Logger.Info(message,
zap.String("command", h.Config.Command),
zap.String("name", h.Config.Base.Name))
}
// Execute executes an healthcheck on the given domain
func (h *CommandHealthcheck) Execute(ctx context.Context) error {
h.LogDebug("start executing healthcheck")
ctx, cancel := context.WithTimeout(ctx, time.Duration(h.Config.Timeout)*time.Second)
defer cancel()
var stdErr bytes.Buffer
cmd := exec.CommandContext(ctx, h.Config.Command, h.Config.Arguments...)
cmd.Stderr = &stdErr
if err := cmd.Run(); err != nil {
var errorMsg string
exitErr, isExitError := err.(*exec.ExitError)
if isExitError {
errorMsg = fmt.Sprintf("The command failed with code=%d, stderr=%s", exitErr.ExitCode(), stdErr.String())
} else {
errorMsg = fmt.Sprintf("The command failed, stderr=%s", stdErr.String())
}
return errors.Wrap(err, errorMsg)
}
return nil
}
// NewCommandHealthcheck creates a Command healthcheck from a logger and a configuration
func NewCommandHealthcheck(logger *zap.Logger, config *CommandHealthcheckConfiguration) *CommandHealthcheck {
return &CommandHealthcheck{
Logger: logger,
Config: config,
}
}
// MarshalJSON marshal to json a command healthcheck
func (h *CommandHealthcheck) MarshalJSON() ([]byte, error) {
return json.Marshal(h.Config)
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *CommandHealthcheckConfiguration) DeepCopyInto(out *CommandHealthcheckConfiguration) {
*out = *in
in.Base.DeepCopyInto(&out.Base)
if in.Arguments != nil {
in, out := &in.Arguments, &out.Arguments
*out = make([]string, len(*in))
copy(*out, *in)
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommandHealthcheckConfiguration.
func (in *CommandHealthcheckConfiguration) DeepCopy() *CommandHealthcheckConfiguration {
if in == nil {
return nil
}
out := new(CommandHealthcheckConfiguration)
in.DeepCopyInto(out)
return out
}