This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
/
synth-publish.go
304 lines (265 loc) · 9.14 KB
/
synth-publish.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"sync"
"time"
fthealth "github.com/Financial-Times/go-fthealth/v1_1"
"github.com/Financial-Times/service-status-go/gtg"
"github.com/Financial-Times/service-status-go/httphandlers"
"github.com/dchest/uniuri"
"github.com/golang/go/src/pkg/encoding/base64"
)
const (
stateCheckInterval = time.Duration(60) * time.Second
postInterval = time.Duration(120) * time.Second
)
type syntheticPublication struct {
postEndpoint string
postCredentials string
s3Endpoint string
uuid string
latestImage chan postedData
latestPublication chan publicationResult
mutex *sync.Mutex
history []publicationResult
}
type postedData struct {
tid string
time time.Time
img string //base64 encoded string representation of the generated image
}
type publicationResult struct {
tid string
time time.Time
succeeded bool
errorMsg string
}
var postHost = flag.String("postHost", "cms-notifier-pr-uk-int.svc.ft.com", "publish entrypoint host name (e.g. address of cms-notifier in UCS)")
var postCredentials = flag.String("postCredentials", "", "Authorization header value used to connect to the postHost")
var s3Host = flag.String("s3Host", "com.ft.imagepublish.int.s3.amazonaws.com", "saved image endpoint host name (e.g. address of the s3 service)")
var tick = flag.Bool("tick", true, "true, if this service should periodially generate and post content to the post endpoint")
var uuid = flag.String("testUuid", "c94a3a57-3c99-423c-a6bd-ed8c4c10a3c3", "uuid for the mock image used in the test")
func main() {
log.Println("Starting synthetic image publication monitor...")
flag.Parse()
app := &syntheticPublication{
postEndpoint: buildPostEndpoint(*postHost),
postCredentials: *postCredentials,
s3Endpoint: buildGetEndpoint(*s3Host, *uuid),
uuid: *uuid,
latestImage: make(chan postedData),
latestPublication: make(chan publicationResult),
mutex: &sync.Mutex{},
history: make([]publicationResult, 0),
}
if *tick {
tick := time.Tick(postInterval)
go func() {
for {
app.publish()
<-tick
}
}()
}
go app.publishingMonitor()
go app.historyManager()
timedHC := fthealth.TimedHealthCheck{
HealthCheck: fthealth.HealthCheck{
SystemCode: "synth-image-pub-monitor",
Name: "Synthetic publication monitor",
Description: "End-to-end image publication & monitor",
Checks: []fthealth.Check{app.healthcheck()},
},
Timeout: 10 * time.Second,
}
http.HandleFunc("/__health", fthealth.Handler(timedHC))
http.HandleFunc(httphandlers.GTGPath, httphandlers.NewGoodToGoHandler(gtg.StatusChecker(app.GTG)))
http.HandleFunc(httphandlers.BuildInfoPath, httphandlers.BuildInfoHandler)
http.HandleFunc("/history", app.historyHandler)
http.HandleFunc("/forcePublish", app.forcePublish)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Println("Error: Could not start http server.")
}
}
func (app *syntheticPublication) GTG() gtg.Status {
statusCheck := func() gtg.Status {
return gtgCheck(app.latestPublicationStatus)
}
return gtg.FailFastParallelCheck([]gtg.StatusChecker{statusCheck})()
}
func gtgCheck(handler func() (string, error)) gtg.Status {
if _, err := handler(); err != nil {
return gtg.Status{GoodToGo: false, Message: "Image publication doesn't work"}
}
return gtg.Status{GoodToGo: true}
}
func (app *syntheticPublication) healthcheck() fthealth.Check {
return fthealth.Check{
BusinessImpact: "Image publication doesn't work",
Name: "End-to-end test of image publication",
PanicGuide: "https://runbooks.in.ft.com/synth-image-pub-monitor",
Severity: 1,
TechnicalSummary: "Lots of things could have gone wrong. Check the /history endpoint for more info",
Checker: app.latestPublicationStatus,
}
}
func (app *syntheticPublication) latestPublicationStatus() (string, error) {
n := len(app.history)
if n != 0 && !app.history[n-1].succeeded {
msg := "Publication failed."
return msg, errors.New(msg)
}
return "Ok", nil
}
func (app *syntheticPublication) historyHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("History request.")
app.mutex.Lock()
for i := len(app.history) - 1; i >= 0; i-- {
fmt.Fprintf(w, "%d. { Date: %s, Tid: %s, Succeeded: %t, Message: %s}\n\n",
len(app.history)-i,
app.history[i].time.String(),
app.history[i].tid,
app.history[i].succeeded,
app.history[i].errorMsg,
)
}
app.mutex.Unlock()
}
func (app *syntheticPublication) forcePublish(w http.ResponseWriter, r *http.Request) {
log.Printf("Force publish.")
err := app.publish()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Force publish failed. %s", err.Error())
}
}
func (app *syntheticPublication) publish() error {
eom, time := BuildRandomEOMImage(app.uuid)
json, err := json.Marshal(eom)
if err != nil {
log.Printf("JSON marshalling failed. %s", err.Error())
return err
}
buf := bytes.NewReader(json)
client := http.Client{}
req, err := http.NewRequest("POST", app.postEndpoint, buf)
if err != nil {
log.Printf("Error: Creating request failed. %s", err.Error())
return err
}
tid := "SYNTHETIC-REQ-MON_" + uniuri.NewLen(10)
req.Header.Add("X-Request-Id", tid)
req.Header.Add("X-Origin-System-Id", "methode-web-pub")
req.Header.Add("Authorization", app.postCredentials)
resp, err := client.Do(req)
if err != nil {
log.Printf("Error: Executing request failed. %s", err.Error())
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
handlePublishingErr(app.latestPublication, tid, time, fmt.Sprintf("Publishing failed at first step: could not post data to CMS notifier. Status code: %d", resp.StatusCode))
} else {
app.latestImage <- postedData{tid, time, eom.Value}
}
return nil
}
const internalErr = "Internal error: "
func (app *syntheticPublication) publishingMonitor() {
for latest := range app.latestImage {
checkPublishingStatus(latest, app.latestPublication, app.s3Endpoint)
}
}
func checkPublishingStatus(latest postedData, result chan<- publicationResult, s3Endpoint string) {
sentImg, err := base64.StdEncoding.DecodeString(latest.img)
if err != nil {
handlePublishingErr(result, latest.tid, latest.time, internalErr+"Decoding image received from channed failed. "+err.Error())
return
}
time.Sleep(stateCheckInterval)
resp, err := http.Get(s3Endpoint)
if err != nil {
handlePublishingErr(result, latest.tid, latest.time, internalErr+"Executing Get request to s3 failed. "+err.Error())
return
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
cmdR := exec.Command("kubectl", "delete", "cm", "synthetic-image-alarm", "--ignore-not-found")
cmdI := exec.Command("kubectl", "delete", "pods", "--selector=job-name=image-trace-job", "--ignore-not-found=true")
errR := cmdR.Run()
if errR != nil {
log.Printf("cmdR.Run() failed with %s\n", errR)
}
errI := cmdI.Run()
if errI != nil {
log.Printf("cmdR.Run() failed with %s\n", errI)
}
case http.StatusNotFound:
handlePublishingErr(result, latest.tid, latest.time, "Image not found. Response status code: 404.")
cmd1 := exec.Command("kubectl", "delete", "jobs", "image-trace-job", "--ignore-not-found")
cmd2 := exec.Command("kubectl", "delete", "configmap", "synthetic-tid")
cmd3 := exec.Command("kubectl", "create", "configmap", "synthetic-tid", "--from-literal=TID="+latest.tid)
cmd4 := exec.Command("kubectl", "create", "job", "--from=cronjob/image-trace", "image-trace-job")
err1 := cmd1.Run()
if err1 != nil {
log.Printf("cmd1.Run() failed with %s\n", err1)
}
err2 := cmd2.Run()
if err2 != nil {
log.Printf("cmd2.Run() failed with %s\n", err2)
}
err3 := cmd3.Run()
if err3 != nil {
log.Printf("cmd3.Run() failed with %s\n", err3)
}
err4 := cmd4.Run()
if err4 != nil {
log.Printf("cmd4.Run() failed with %s\n", err4)
}
return
default:
handlePublishingErr(result, latest.tid, latest.time, fmt.Sprintf("Get request is not successful. Response status code: %d", resp.StatusCode))
return
}
receivedImg, err := ioutil.ReadAll(resp.Body)
if err != nil {
handlePublishingErr(result, latest.tid, latest.time, internalErr+"Could not read resp body. "+err.Error())
return
}
if !bytes.Equal(sentImg, receivedImg) {
handlePublishingErr(result, latest.tid, latest.time, "Posted image content differs from the image in s3.")
return
}
result <- publicationResult{latest.tid, latest.time, true, ""}
}
func handlePublishingErr(latestPublication chan<- publicationResult, tid string, time time.Time, errMsg string) {
log.Printf("ERROR Publish failed. TID: " + tid + ". " + errMsg)
latestPublication <- publicationResult{tid, time, false, errMsg}
}
func (app *syntheticPublication) historyManager() {
for {
latest := <-app.latestPublication
app.mutex.Lock()
if len(app.history) == 10 {
app.history = app.history[1:len(app.history)]
}
app.history = append(app.history, latest)
app.mutex.Unlock()
}
}
func buildPostEndpoint(host string) string {
return "http://" + host + "/notify"
}
func buildGetEndpoint(host, uuid string) string {
return "http://" + host + "/" + uuid
}