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 22:16:29 +01:00
|
|
|
"git.ar21.de/yolokube/grafana-backuper/cfg"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-16 22:16:29 +01:00
|
|
|
appSettings := cfg.Parse()
|
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
ctx := context.Background()
|
2024-02-16 22:16:29 +01:00
|
|
|
client, err := grafana.NewClient(appSettings.GrafanaURL, appSettings.GrafanaToken, grafana.DefaultHTTPClient)
|
2024-02-15 01:12:57 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error creating the grafana client: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:16:29 +01:00
|
|
|
gitdata := git.NewPayload(appSettings.GPGKey)
|
|
|
|
if err = gitdata.GetRepo(appSettings.GitRepoURL, appSettings.GitUser, appSettings.GitPass); err != nil {
|
2024-02-16 21:56:27 +01:00
|
|
|
log.Fatalf("Error cloning git repo: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:16:29 +01:00
|
|
|
gitdata.AddCommitter(appSettings.GitUser, appSettings.GitEmail)
|
2024-02-16 21:56:27 +01:00
|
|
|
|
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)
|
|
|
|
|
2024-02-16 22:16:29 +01:00
|
|
|
if gitdata.IsVersionCommitted(appSettings.GitBranch) {
|
2024-02-16 21:56:27 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:16:29 +01:00
|
|
|
if err = gitdata.PushToRemote(appSettings.GitUser, appSettings.GitPass); err != nil {
|
|
|
|
log.Fatalf("Error pushing to remote repo %s: %v", appSettings.GitRepoURL, err)
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|