-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc.go
182 lines (154 loc) · 4.3 KB
/
grpc.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
package goplum
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"github.com/csmith/goplum/api"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var (
apiPort = flag.Int("api-port", 7586, "Port to use for the GoPlum API")
caCert = flag.String("ca-cert", "ca.crt", "Path to the certificate of the certificate authority for the API")
localCert = flag.String("cert", "goplum.crt", "Path to the certificate to use for the API")
localKey = flag.String("key", "goplum.key", "Path to the key to use for the API")
)
type GrpcServer struct {
plum *Plum
server *grpc.Server
}
func NewGrpcServer(plum *Plum) *GrpcServer {
return &GrpcServer{
plum: plum,
}
}
func (s *GrpcServer) Start() {
certs, pool, err := api.LoadCertificates(*localCert, *localKey, *caCert)
if err != nil {
log.Printf("Not starting API: unable to load certificates: %v", err)
return
}
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", *apiPort))
if err != nil {
log.Fatalf("Unable to listen on port %d for API requests: %v", *apiPort, err)
}
log.Printf("Starting API server on port %d", *apiPort)
s.server = grpc.NewServer(grpc.Creds(credentials.NewTLS(&tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: certs,
ClientCAs: pool,
MinVersion: tls.VersionTLS13,
})))
s.server.RegisterService(api.ServiceDesc, s)
if err := s.server.Serve(lis); err != nil {
log.Printf("Error serving API: %v", err)
}
}
func (s *GrpcServer) Stop() {
if s.server != nil {
s.server.Stop()
}
}
func (s *GrpcServer) Results(_ *api.Empty, rs api.GoPlum_ResultsServer) error {
var (
l CheckListener
c = make(chan error, 1)
)
l = func(check *ScheduledCheck, result Result) {
err := rs.Send(&api.Result{
Check: check.Name,
Time: check.LastRun.Unix(),
Result: s.convertState(result.State),
Detail: result.Detail,
Facts: s.convertFacts(result.Facts),
})
if err != nil {
s.plum.RemoveCheckListener(l)
c <- err
}
}
defer close(c)
s.plum.AddCheckListener(l)
return <-c
}
func (s *GrpcServer) GetChecks(_ context.Context, _ *api.Empty) (*api.CheckList, error) {
var checks []*api.Check
for i := range s.plum.Checks {
checks = append(checks, s.convertCheck(s.plum.Checks[i]))
}
return &api.CheckList{Checks: checks}, nil
}
func (s *GrpcServer) GetCheck(_ context.Context, name *api.CheckName) (*api.Check, error) {
if name == nil || len(name.Name) == 0 {
return nil, fmt.Errorf("no name specified")
}
check, ok := s.plum.Checks[name.Name]
if ok {
return s.convertCheck(check), nil
}
return nil, fmt.Errorf("no check found with name: %s", name.Name)
}
func (s *GrpcServer) SuspendCheck(_ context.Context, name *api.CheckName) (*api.Check, error) {
if name == nil || len(name.Name) == 0 {
return nil, fmt.Errorf("no name specified")
}
check := s.plum.Suspend(name.Name)
if check == nil {
return nil, fmt.Errorf("no check found with name: %s", name.Name)
}
return s.convertCheck(check), nil
}
func (s *GrpcServer) ResumeCheck(_ context.Context, name *api.CheckName) (*api.Check, error) {
if name == nil || len(name.Name) == 0 {
return nil, fmt.Errorf("no name specified")
}
check := s.plum.Unsuspend(name.Name)
if check == nil {
return nil, fmt.Errorf("no check found with name: %s", name.Name)
}
return s.convertCheck(check), nil
}
func (s *GrpcServer) convertCheck(check *ScheduledCheck) *api.Check {
return &api.Check{
Name: check.Name,
Type: check.Type,
LastRun: check.LastRun.Unix(),
Settled: check.Settled,
State: s.convertState(check.State),
Suspended: check.Suspended,
}
}
func (s *GrpcServer) convertState(state CheckState) api.Status {
switch state {
case StateIndeterminate:
return api.Status_INDETERMINATE
case StateGood:
return api.Status_GOOD
case StateFailing:
return api.Status_FAILING
default:
return api.Status_INDETERMINATE
}
}
func (s *GrpcServer) convertFacts(facts map[Fact]interface{}) []*api.Fact {
res := make([]*api.Fact, 0, len(facts))
for i := range facts {
res = append(res, &api.Fact{
Name: string(i),
Value: s.convertFactValue(facts[i]),
})
}
return res
}
func (s *GrpcServer) convertFactValue(i interface{}) api.FactValue {
if v, ok := i.(int64); ok {
return &api.Fact_Int{Int: v}
}
if v, ok := i.(string); ok {
return &api.Fact_Str{Str: v}
}
return nil
}