Add git functions & improve grafana functions
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
2d08f75545
commit
0fb6e0b6e9
7 changed files with 579 additions and 16 deletions
67
main.go
67
main.go
|
@ -4,15 +4,21 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"slices"
|
||||
|
||||
"git.ar21.de/yolokube/grafana-backuper/pkg/git"
|
||||
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana"
|
||||
)
|
||||
|
||||
const (
|
||||
grafanaURL = "https://grafana.services.yolokube.de"
|
||||
grafanaAPIKey = ""
|
||||
gitRepoURL = ""
|
||||
gitRepoDirectory = ""
|
||||
grafanaURL = "https://grafana.services.yolokube.de"
|
||||
grafanaAPIKey = ""
|
||||
gitBranch = "main"
|
||||
gitRepoURL = ""
|
||||
gitUsername = ""
|
||||
gitEmail = ""
|
||||
gitPassword = ""
|
||||
privateGPGKeyFile = ""
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -22,21 +28,66 @@ func main() {
|
|||
log.Fatalf("Error creating the grafana client: %v", err)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
dashboards, err := client.SearchDashboards(ctx, "", false)
|
||||
if err != nil {
|
||||
log.Fatalf("Error fetching dashboards: %v", err)
|
||||
}
|
||||
|
||||
for _, dashboard := range dashboards {
|
||||
versions, err := client.GetDashboardVersionsByDashboardID(ctx, dashboard.ID)
|
||||
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)
|
||||
if err != nil {
|
||||
log.Fatalf("Error fetching versions for dashboard %s: %v", dashboard.Title, err)
|
||||
}
|
||||
|
||||
fmt.Println(dashboard.Title)
|
||||
fmt.Println(versions)
|
||||
slices.Reverse(versions)
|
||||
|
||||
for _, version := range versions {
|
||||
fmt.Printf("%s: %s\n", version.CreatedBy, version.Message)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue