-
Notifications
You must be signed in to change notification settings - Fork 57
/
types.go
139 lines (114 loc) · 3.06 KB
/
types.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
package healthchecks
import (
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/DBCDK/morph/ssh"
"github.com/DBCDK/morph/utils"
"net/http"
"strings"
"time"
)
type Host interface {
GetName() string
GetTargetHost() string
GetTargetPort() int
GetTargetUser() string
GetHealthChecks() HealthChecks
}
type HealthChecks struct {
Http []HttpHealthCheck
Cmd []CmdHealthCheck
}
type CmdHealthCheck struct {
SshContext *ssh.SSHContext
Description string
Cmd []string
Period int
Timeout int
}
type HttpHealthCheck struct {
Description string
Headers map[string]string
Host *string
InsecureSSL bool
Path string
Port int
Scheme string
Period int
Timeout int
}
type HealthCheck interface {
GetDescription() string
GetPeriod() int
Run(Host) error
}
func (healthCheck CmdHealthCheck) GetDescription() string {
return healthCheck.Description
}
func (healthCheck CmdHealthCheck) GetPeriod() int {
return healthCheck.Period
}
func (healthCheck CmdHealthCheck) Run(host Host) error {
ctx, cancel := utils.ContextWithConditionalTimeout(context.TODO(), healthCheck.Timeout)
defer cancel()
cmd, err := healthCheck.SshContext.CmdContext(ctx, host, healthCheck.Cmd...)
if err != nil {
errorMessage := fmt.Sprintf("Health check error: %s", err.Error())
return errors.New(errorMessage)
}
data, err := cmd.CombinedOutput()
if ctx.Err() != nil {
errorMessage := fmt.Sprintf("Health check error: Timeout after %ds", healthCheck.Timeout)
return errors.New(errorMessage)
}
if err != nil {
errorMessage := fmt.Sprintf("Health check error: %s", string(data))
return errors.New(errorMessage)
}
return nil
}
func (healthCheck HttpHealthCheck) GetDescription() string {
return healthCheck.Description
}
func (healthCheck HttpHealthCheck) GetPeriod() int {
return healthCheck.Period
}
func (healthCheck HttpHealthCheck) Run(host Host) error {
// use the hosts hostname if the healthCheck host is not set
if healthCheck.Host == nil {
replacementHostname := host.GetTargetHost()
healthCheck.Host = &replacementHostname
}
// http.Client interprets a timeout of 0 as "no timeout", but we still have to avoid passing
// a negative timeout to it
if healthCheck.Timeout < 0 {
healthCheck.Timeout = 0
}
transport := &http.Transport{}
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: healthCheck.InsecureSSL}
client := &http.Client{
Timeout: time.Duration(healthCheck.Timeout) * time.Second,
Transport: transport,
}
url := fmt.Sprintf("%s://%s:%d%s", healthCheck.Scheme, *healthCheck.Host, healthCheck.Port, healthCheck.Path)
req, err := http.NewRequest("GET", url, nil)
for headerKey, headerValue := range healthCheck.Headers {
if strings.ToLower(headerKey) == "host" {
req.Host = headerValue
} else {
req.Header.Add(headerKey, headerValue)
}
}
resp, err := client.Do(req)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
} else {
return errors.New(fmt.Sprintf("Got non 2xx status code (%s)", resp.Status))
}
}