|
@@ -1,22 +1,10 @@
|
1
|
1
|
package main
|
2
|
2
|
|
3
|
3
|
import (
|
4
|
|
- "bytes"
|
5
|
|
- "encoding/json"
|
6
|
4
|
"flag"
|
7
|
|
- "fmt"
|
8
|
|
- "io/ioutil"
|
9
|
5
|
"os"
|
10
|
|
-
|
11
|
|
- "git"
|
12
|
|
- "grafana"
|
13
|
6
|
)
|
14
|
7
|
|
15
|
|
-type diffVersion struct {
|
16
|
|
- oldVersion int
|
17
|
|
- newVersion int
|
18
|
|
-}
|
19
|
|
-
|
20
|
8
|
var (
|
21
|
9
|
grafanaURL = flag.String("grafana-url", "", "Base URL of the Grafana instance")
|
22
|
10
|
grafanaAPIKey = flag.String("api-key", "", "API key to use in authenticated requests")
|
|
@@ -26,8 +14,6 @@ var (
|
26
|
14
|
)
|
27
|
15
|
|
28
|
16
|
func main() {
|
29
|
|
- dv := make(map[string]diffVersion)
|
30
|
|
-
|
31
|
17
|
flag.Parse()
|
32
|
18
|
|
33
|
19
|
if *grafanaURL == "" {
|
|
@@ -51,115 +37,7 @@ func main() {
|
51
|
37
|
os.Exit(1)
|
52
|
38
|
}
|
53
|
39
|
|
54
|
|
- dbVersions, err := getDashboardsVersions()
|
55
|
|
- if err != nil {
|
56
|
|
- panic(err)
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- repo, err := git.Sync(
|
60
|
|
- *repoURL,
|
61
|
|
- *clonePath,
|
62
|
|
- *privateKeyPath,
|
63
|
|
- )
|
64
|
|
- if err != nil {
|
65
|
|
- panic(err)
|
66
|
|
- }
|
67
|
|
-
|
68
|
|
- w, err := repo.Worktree()
|
69
|
|
- if err != nil {
|
70
|
|
- panic(err)
|
71
|
|
- }
|
72
|
|
-
|
73
|
|
- client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
|
74
|
|
- uris, err := client.GetDashboardsURIs()
|
75
|
|
- if err != nil {
|
76
|
|
- panic(err)
|
77
|
|
- }
|
78
|
|
-
|
79
|
|
- for _, uri := range uris {
|
80
|
|
- dashboard, err := client.GetDashboard(uri)
|
81
|
|
- if err != nil {
|
82
|
|
- panic(err)
|
83
|
|
- }
|
84
|
|
-
|
85
|
|
- version, ok := dbVersions[dashboard.Slug]
|
86
|
|
- if !ok || dashboard.Version > version {
|
87
|
|
- slugExt := dashboard.Slug + ".json"
|
88
|
|
- if err = rewriteFile(
|
89
|
|
- *clonePath+"/"+slugExt,
|
90
|
|
- dashboard.RawJSON,
|
91
|
|
- ); err != nil {
|
92
|
|
- panic(err)
|
93
|
|
- }
|
94
|
|
-
|
95
|
|
- if _, err = w.Add(slugExt); err != nil {
|
96
|
|
- panic(err)
|
97
|
|
- }
|
98
|
|
-
|
99
|
|
- dv[dashboard.Slug] = diffVersion{
|
100
|
|
- oldVersion: version,
|
101
|
|
- newVersion: dashboard.Version,
|
102
|
|
- }
|
103
|
|
- }
|
104
|
|
- }
|
105
|
|
-
|
106
|
|
- status, err := w.Status()
|
107
|
|
- if err != nil {
|
108
|
|
- panic(err)
|
109
|
|
- }
|
110
|
|
-
|
111
|
|
- if !status.IsClean() {
|
112
|
|
- if err = writeVersions(dbVersions, dv); err != nil {
|
113
|
|
- panic(err)
|
114
|
|
- }
|
115
|
|
-
|
116
|
|
- if _, err = w.Add("versions.json"); err != nil {
|
117
|
|
- panic(err)
|
118
|
|
- }
|
119
|
|
-
|
120
|
|
- if _, err = git.Commit(getCommitMessage(dv), w); err != nil {
|
121
|
|
- panic(err)
|
122
|
|
- }
|
123
|
|
-
|
124
|
|
- }
|
125
|
|
-
|
126
|
|
- if err = git.Push(repo, *privateKeyPath); err != nil {
|
|
40
|
+ if err := Pull(); err != nil {
|
127
|
41
|
panic(err)
|
128
|
42
|
}
|
129
|
43
|
}
|
130
|
|
-
|
131
|
|
-func rewriteFile(filename string, content []byte) error {
|
132
|
|
- if err := os.Remove(filename); err != nil {
|
133
|
|
- pe, ok := err.(*os.PathError)
|
134
|
|
- if !ok || pe.Err.Error() != "no such file or directory" {
|
135
|
|
- return err
|
136
|
|
- }
|
137
|
|
- }
|
138
|
|
-
|
139
|
|
- indentedContent, err := indent(content)
|
140
|
|
- if err != nil {
|
141
|
|
- return err
|
142
|
|
- }
|
143
|
|
-
|
144
|
|
- return ioutil.WriteFile(filename, indentedContent, 0644)
|
145
|
|
-}
|
146
|
|
-
|
147
|
|
-func indent(srcJSON []byte) (indentedJSON []byte, err error) {
|
148
|
|
- buf := bytes.NewBuffer(nil)
|
149
|
|
- if err = json.Indent(buf, srcJSON, "", "\t"); err != nil {
|
150
|
|
- return
|
151
|
|
- }
|
152
|
|
-
|
153
|
|
- indentedJSON, err = ioutil.ReadAll(buf)
|
154
|
|
- return
|
155
|
|
-}
|
156
|
|
-
|
157
|
|
-func getCommitMessage(dv map[string]diffVersion) string {
|
158
|
|
- message := "Updated dashboards\n"
|
159
|
|
-
|
160
|
|
- for slug, diff := range dv {
|
161
|
|
- message += fmt.Sprintf("%s: %d => %d\n", slug, diff.oldVersion, diff.newVersion)
|
162
|
|
- }
|
163
|
|
-
|
164
|
|
- return message
|
165
|
|
-}
|