-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
320 lines (245 loc) · 7.52 KB
/
main.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"bytes"
"context"
"fmt"
"errors"
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/rs/zerolog/log"
"io/ioutil"
"os"
"os/exec"
"time"
)
const (
localSkycryptPath = "skycrypt"
tarFile = "skycrypt.tar.gz"
)
func main() {
clone()
defer deleteFork()
patches()
patchCredentials()
pack()
defer deleteArchive()
pushArtifact()
log.Warn().Msg("not sending metric, push gateway is not available yet")
// updateMetrics()
}
func clone() {
log.Info().Msgf("cloning skycrypt fork")
_, err := git.PlainClone(localSkycryptPath, false, &git.CloneOptions{
URL: gitUrl(),
Progress: os.Stdout,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", "development")),
SingleBranch: true,
})
if err != nil {
log.Panic().Err(err).Msgf("error cloning")
}
log.Info().Msgf("cloned skycrypt fork")
}
func deleteFork() {
err := os.RemoveAll(localSkycryptPath)
if err != nil {
log.Panic().Err(err).Msgf("error deleting local fork")
}
}
func patches() {
// patchLinkToAhHistory()
patchDockerfile()
}
func patchLinkToAhHistory() {
patchFile := "./patches/add_link_to_ah_history.patch"
toPatchFile := fmt.Sprintf("%s%s", localSkycryptPath, "/views/stats.ejs")
patch(patchFile, toPatchFile)
}
func patchDockerfile() {
patchFile := "./patches/0001-set-maintainer-label-to-coflnet.patch"
toPatchFile := fmt.Sprintf("%s%s", localSkycryptPath, "/Dockerfile")
patch(patchFile, toPatchFile)
patchFile = "./patches/0002-overwrite-cmd-in-dockerfile.patch"
toPatchFile = fmt.Sprintf("%s%s", localSkycryptPath, "/Dockerfile")
patch(patchFile, toPatchFile)
}
// patch changes the coflnet specific patches
func patch(patchFile, toPatchFile string) {
fileExists := doesFileExist(patchFile)
if !fileExists {
log.Warn().Msgf("the file %s does not exist, can not apply patch", patchFile)
return
}
log.Info().Msgf("patch file %s exists; gonna patch the file %s" , patchFile, toPatchFile)
patch, err := os.Open(patchFile)
if err != nil {
log.Panic().Err(err).Msgf("error opening patch file: %s", patchFile)
}
defer func(patch *os.File) {
err := patch.Close()
if err != nil {
log.Error().Err(err).Msgf("could not gracefully close patch file: %s", patchFile)
}
}(patch)
log.Debug().Msgf("applying patching: %s", patchFile)
files, _, err := gitdiff.Parse(patch)
if err != nil {
log.Panic().Err(err).Msgf("error parsing patch file: %s", patchFile)
}
if len(files) == 0 {
log.Warn().Msgf("cant patch file %s, skip it", patchFile)
fmt.Println(patch)
return
}
toPatch, err := os.OpenFile(toPatchFile, os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
log.Panic().Err(err).Msgf("error opening patch file: %s", toPatchFile)
}
log.Debug().Msgf("applying patching: %s", toPatchFile)
var output bytes.Buffer
err = gitdiff.Apply(&output, toPatch, files[0])
if err != nil {
log.Panic().Err(err).Msgf("error patching")
}
err = toPatch.Close()
if err != nil {
log.Error().Err(err).Msgf("could not gracefully close patch file: %s", toPatchFile)
}
err = ioutil.WriteFile(toPatchFile, output.Bytes(), os.ModePerm)
if err != nil {
log.Panic().Err(err).Msgf("error writing patch file: %s", toPatchFile)
}
log.Info().Msgf("patched %s with %s", toPatchFile, patchFile)
}
// patchCredentials configures the credentials for the git repository
func patchCredentials() {
patchFile := "./creds/credentials.patch"
toPatchFile := fmt.Sprintf("%s%s", localSkycryptPath, "/src/credentials.js")
patch, err := os.Open(patchFile)
if err != nil {
log.Panic().Err(err).Msgf("error opening patch file: %s", patchFile)
}
defer func(patch *os.File) {
err := patch.Close()
if err != nil {
log.Error().Err(err).Msgf("could not gracefully close patch file: %s", patchFile)
}
}(patch)
log.Debug().Msgf("applying patching: %s", patchFile)
files, _, err := gitdiff.Parse(patch)
if err != nil {
log.Panic().Err(err).Msgf("error parsing patch file: %s", patchFile)
}
toPatch, err := os.OpenFile(toPatchFile, os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
log.Panic().Err(err).Msgf("error opening patch file: %s", toPatchFile)
}
log.Debug().Msgf("applying patching: %s", toPatchFile)
var output bytes.Buffer
err = gitdiff.Apply(&output, toPatch, files[0])
if err != nil {
log.Panic().Err(err).Msgf("error patching")
}
err = toPatch.Close()
if err != nil {
log.Error().Err(err).Msgf("could not gracefully close patch file: %s", toPatchFile)
}
err = ioutil.WriteFile(toPatchFile, output.Bytes(), os.ModePerm)
if err != nil {
log.Panic().Err(err).Msgf("error writing patch file: %s", toPatchFile)
}
log.Info().Msgf("patched %s with %s", toPatchFile, patchFile)
}
// create a .tar.gz file from the skycrypt folder
func pack() {
output, err := exec.Command("tar", "-czf", tarFile, localSkycryptPath).CombinedOutput()
if err != nil {
log.Panic().Err(err).Msgf("error creating tar file: %s", tarFile)
}
log.Info().Msgf("created tar file\n%s", output)
log.Info().Msgf("packed skycrypt")
}
// push create a .tar.gz file and pushes it to minio
func pushArtifact() {
client := minioClient()
bucket := os.Getenv("MINIO_BUCKET")
if bucket == "" {
log.Panic().Msgf("MINIO_BUCKET is not set")
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
b, err := client.BucketExists(ctx, bucket)
if err != nil {
log.Panic().Err(err).Msgf("error checking if bucket exists: %s", bucket)
}
if !b {
log.Panic().Msgf("bucket does not exist: %s", bucket)
}
contentType := "application/x-gzip"
_, err = client.FPutObject(ctx, bucket, tarFile, tarFile, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Panic().Err(err).Msgf("error uploading tar file: %s", tarFile)
}
log.Info().Msg("uploaded tar file")
}
func minioClient() *minio.Client {
endpoint := os.Getenv("MINIO_HOST")
accessKeyID := os.Getenv("MINIO_ACCESS_ID")
secretAccessKey := os.Getenv("MINIO_ACCESS_KEY")
if endpoint == "" || accessKeyID == "" || secretAccessKey == "" {
log.Panic().Msgf("missing minio credentials")
}
// Initialize minio client object.
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
})
if err != nil {
log.Panic().Err(err).Msgf("error creating minio client")
}
return client
}
func gitUrl() string {
g := os.Getenv("SKYCRYPT_FORK")
if g == "" {
log.Panic().Msgf("SKYCRYPT_FORK is not set")
}
return g
}
func deleteArchive() {
err := os.Remove(tarFile)
if err != nil {
log.Error().Err(err).Msgf("error deleting tar file: %s", tarFile)
}
}
func updateMetrics() {
completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "db_backup_last_completion_timestamp_seconds",
Help: "The timestamp of the last successful completion of a DB backup.",
})
completionTime.SetToCurrentTime()
err := push.New(prometheusHost(), "sky_crypt_updater_patched").
Collector(completionTime).
Push()
if err != nil {
log.Panic().Err(err).Msgf("error pushing prometheus metrics")
}
}
func prometheusHost() string {
s := os.Getenv("PROMETHEUS_HOST")
if s == "" {
log.Panic().Msgf("PROMETHEUS_HOST is not set")
return ""
}
return s
}
func doesFileExist(path string) bool {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return false
}
return true
}