2024-02-15 01:12:57 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-02-16 21:56:27 +01:00
|
|
|
"slices"
|
2024-02-15 01:12:57 +01:00
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
"git.ar21.de/yolokube/grafana-backuper/pkg/git"
|
2024-02-15 01:12:57 +01:00
|
|
|
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-02-16 21:56:27 +01:00
|
|
|
grafanaURL = "https://grafana.services.yolokube.de"
|
|
|
|
grafanaAPIKey = ""
|
|
|
|
gitBranch = "main"
|
|
|
|
gitRepoURL = ""
|
|
|
|
gitUsername = ""
|
|
|
|
gitEmail = ""
|
|
|
|
gitPassword = ""
|
|
|
|
privateGPGKeyFile = ""
|
2024-02-15 01:12:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
client, err := grafana.NewClient(grafanaURL, grafanaAPIKey, grafana.DefaultHTTPClient)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error creating the grafana client: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
gitdata := git.NewPayload(privateGPGKeyFile)
|
|
|
|
if err = gitdata.GetRepo(gitRepoURL, gitUsername, gitPassword); err != nil {
|
|
|
|
log.Fatalf("Error cloning git repo: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitdata.AddCommitter(gitUsername, gitEmail)
|
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
dashboards, err := client.SearchDashboards(ctx, "", false)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error fetching dashboards: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dashboard := range dashboards {
|
2024-02-16 21:56:27 +01:00
|
|
|
gitdata.UpdateDashboard(dashboard)
|
|
|
|
|
|
|
|
_, dashboardInfo, err := client.GetRawDashboardByUID(ctx, dashboard.UID)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error fetching information of dashboard %s: %v", dashboard.Title, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitdata.UpdateDashboardInfo(dashboardInfo)
|
|
|
|
|
|
|
|
versions, err := client.GetDashboardVersionsByDashboardUID(ctx, dashboard.UID)
|
2024-02-15 01:12:57 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error fetching versions for dashboard %s: %v", dashboard.Title, err)
|
|
|
|
}
|
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
slices.Reverse(versions)
|
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
for _, version := range versions {
|
2024-02-16 21:56:27 +01:00
|
|
|
gitdata.UpdateVersion(version)
|
|
|
|
|
|
|
|
if gitdata.IsVersionCommitted(gitBranch) {
|
|
|
|
fmt.Printf("%s/%s - %s: %s -> already committed\n", dashboardInfo.FolderTitle, dashboard.Title, version.CreatedBy, version.Message)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s/%s - %s: %s\n", dashboardInfo.FolderTitle, dashboard.Title, version.CreatedBy, version.Message)
|
|
|
|
|
|
|
|
raw, info, err := client.GetRawDashboardByUIDAndVersion(ctx, dashboard.UID, version.Version)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error fetching dashboard %s version %d: %v", dashboard.Title, version.Version, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitdata.AddAuthor(info.CreatedBy, "")
|
|
|
|
|
|
|
|
output, err := grafana.ConvertRawToIndent(raw)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error pritty-printing dashboard %s version %d: %v", dashboard.Title, info.Version, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitdata.UpdateContent([]byte(output))
|
|
|
|
gitdata.UpdateCommitter()
|
|
|
|
if err = gitdata.CreateCommit(); err != nil {
|
|
|
|
log.Fatalf("Error creating commit for dashboard %s version %d: %v", dashboard.Title, info.Version, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = gitdata.PushToRemote(gitUsername, gitPassword); err != nil {
|
|
|
|
log.Fatalf("Error pushing to remote repo %s: %v", gitRepoURL, err)
|
|
|
|
}
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|