-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
305 lines (249 loc) · 6.54 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
// Copyright 2022 Eurac Research. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// gfdashsync is a command for syncing all Grafana dashboards to a Gitlab
// repository.
package main
import (
"bufio"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
gapi "github.com/grafana/grafana-api-golang-client"
"github.com/xanzy/go-gitlab"
)
func main() {
var (
gfAPI = flag.String("grafana.api", "", "Grafana API URL")
gfToken = flag.String("grafana.token", "", "Grafana API token")
gitAPI = flag.String("git.api", "", "Git service API URL")
gitToken = flag.String("git.token", "", "Git service API token")
gitPID = flag.Int("git.pid", -1, "Git project ID")
gitBranch = flag.String("git.branch", "main", "Git repository branch")
config = flag.String("config", "", "Config file (optional)")
)
flag.Parse()
if err := setFlagsFromFile(*config); err != nil {
log.Fatal(err)
}
switch {
case *gfAPI == "":
log.Fatal("error missing -grafana.api")
case *gfToken == "":
log.Fatal("error missing -token.api")
case *gitAPI == "":
log.Fatal("error missing -git.api")
case *gitToken == "":
log.Fatal("error missing -git.token")
case *gitPID == -1:
log.Fatal("error missing -git.pid")
}
gf, err := gapi.New(*gfAPI, gapi.Config{APIKey: *gfToken})
if err != nil {
log.Fatalf("failed to create grafana client: %v", err)
}
git, err := NewGitlab(*gitAPI, *gitToken, *gitBranch, *gitPID)
if err != nil {
log.Fatal(err)
}
dashboards, err := gf.Dashboards()
if err != nil {
log.Fatal(err)
}
for _, d := range dashboards {
b, err := gf.DashboardByUID(d.UID)
if err != nil {
log.Printf("error getting dashboard %q with ID %d: %v", d.Title, d.ID, err)
continue
}
data, err := json.MarshalIndent(b, "", " ")
if err != nil {
log.Printf("error converting dashboard %q with ID %d: %v", d.Title, d.ID, err)
continue
}
f := &File{
UID: d.UID,
Path: fmt.Sprintf("/%s/%s.json", d.FolderTitle, d.Title),
SHA256: hash(data),
content: data,
}
git.Add(f)
}
if err := git.Commit(); err != nil {
log.Fatal(err)
}
}
func hash(data []byte) string {
h := sha256.New()
h.Write(data)
return hex.EncodeToString(h.Sum(nil))
}
type History map[string]*File
type File struct {
UID string `json:"uid"`
Path string `json:"path"`
SHA256 string `json:"sha256"`
content []byte
processed bool
}
func (f *File) moved(hf *File) bool {
return (f.Path != hf.Path) && (f.UID == hf.UID) && (f.SHA256 != hf.SHA256)
}
func (f *File) modified(hf *File) bool {
return (f.Path == hf.Path) && (f.UID == hf.UID) && (f.SHA256 != hf.SHA256)
}
type Gitlab struct {
client *gitlab.Client
pid int
branch string
history History
historyFile string
historyAction gitlab.FileActionValue
actions []*gitlab.CommitActionOptions
}
func NewGitlab(baseURL, token, branch string, pid int) (*Gitlab, error) {
c, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL))
if err != nil {
return nil, fmt.Errorf("gitlab: error creating client: %w", err)
}
g := &Gitlab{
client: c,
pid: pid,
branch: branch,
history: make(History),
historyFile: "history.json",
historyAction: gitlab.FileUpdate,
}
if err := g.parseHistory(); err != nil {
return nil, fmt.Errorf("gitlab: error parsing history: %w", err)
}
return g, nil
}
// parseHistory reads "history.json" from the repository.
func (g *Gitlab) parseHistory() error {
f, resp, err := g.client.RepositoryFiles.GetFile(g.pid, g.historyFile, &gitlab.GetFileOptions{
Ref: gitlab.String(g.branch),
}, nil)
if err != nil {
// If the error is a 404 File Not Found we will assume there is no
// history and processed without error but setting the action to create
// a new history file. All other errors will be returned as such.
if resp != nil && resp.StatusCode == http.StatusNotFound {
g.historyAction = gitlab.FileCreate
return nil
}
return err
}
data, err := base64.StdEncoding.DecodeString(f.Content)
if err != nil {
return err
}
return json.Unmarshal([]byte(data), &g.history)
}
// Add adds the file to be committed.
func (g *Gitlab) Add(in *File) {
hf, ok := g.history[in.UID]
if !ok {
g.add(in, gitlab.FileCreate, "")
return
}
switch {
case in.moved(hf):
g.add(in, gitlab.FileMove, hf.Path)
case in.modified(hf):
g.add(in, gitlab.FileUpdate, "")
default:
// If no action is preformed set the processed flag anyway.
hf.processed = true
}
}
func (g *Gitlab) add(in *File, action gitlab.FileActionValue, prevPath string) {
in.processed = true
opt := &gitlab.CommitActionOptions{
Action: gitlab.FileAction(action),
FilePath: gitlab.String(in.Path),
Content: gitlab.String(string(in.content)),
}
if prevPath != "" {
opt.PreviousPath = gitlab.String(prevPath)
}
g.actions = append(g.actions, opt)
g.history[in.UID] = in
}
func (g *Gitlab) updateHistory() error {
if len(g.history) == 0 {
return nil
}
data, err := json.Marshal(g.history)
if err != nil {
return err
}
opt := &gitlab.CommitActionOptions{
Action: gitlab.FileAction(g.historyAction),
FilePath: gitlab.String(g.historyFile),
Content: gitlab.String(string(data)),
}
g.actions = append(g.actions, opt)
return nil
}
func (g *Gitlab) deleteOrphans() {
for _, f := range g.history {
if f.processed {
continue
}
g.actions = append(g.actions, &gitlab.CommitActionOptions{
Action: gitlab.FileAction(gitlab.FileDelete),
FilePath: gitlab.String(f.Path),
})
delete(g.history, f.UID)
}
}
// Commit commits all pending commits to the repository.
func (g *Gitlab) Commit() error {
g.deleteOrphans()
// nothing to commit
if len(g.actions) == 0 {
return nil
}
if err := g.updateHistory(); err != nil {
return err
}
opt := &gitlab.CreateCommitOptions{
Branch: gitlab.String(g.branch),
CommitMessage: gitlab.String("ʕ◔ϖ◔ʔ: backup done."),
Actions: g.actions,
}
_, _, err := g.client.Commits.CreateCommit(g.pid, opt, nil)
if err != nil {
return fmt.Errorf("gitlab: commit error: %w", err)
}
return nil
}
func setFlagsFromFile(filename string) error {
// no config file given so we assume parameters are passed using the flags.
if filename == "" {
return nil
}
c, err := os.Open(filename)
if err != nil {
return err
}
flag.VisitAll(func(f *flag.Flag) {
s := bufio.NewScanner(c)
for s.Scan() {
f := strings.Fields(s.Text())
if len(f) != 2 {
continue
}
flag.Set(f[0], f[1])
}
})
return nil
}