grafana-backuper/internal/cmd/backup.go

115 lines
2.9 KiB
Go
Raw Normal View History

package cmd
import (
"context"
"errors"
"slices"
"git.ar21.de/yolokube/grafana-backuper/internal/config"
"git.ar21.de/yolokube/grafana-backuper/internal/helper"
"git.ar21.de/yolokube/grafana-backuper/pkg/git"
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana"
2024-07-30 21:20:18 +02:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func NewBackupCommand(c *config.Config) *cobra.Command {
backupCmd := &cobra.Command{
Use: "backup",
Short: "Back up the dashboards from grafana to a git repository.",
Long: "Back up the dashboards from grafana to a git repository.",
2024-07-30 21:20:18 +02:00
Run: func(cmd *cobra.Command, _ []string) {
if err := backup(cmd.Context(), c); err != nil {
log.WithContext(cmd.Context()).WithError(err).Fatal()
}
},
2024-07-30 21:20:18 +02:00
PreRun: func(cmd *cobra.Command, _ []string) {
if err := c.Validate(); err != nil {
log.WithContext(cmd.Context()).WithError(err).Fatal("checking flags & environment variables")
}
},
}
backupCmd.PersistentFlags().BoolVar(&c.ForceCommits, "force", false, "Force git commits / ignore existence check")
backupCmd.PersistentFlags().StringVar(&c.GitEmail, "git-email", "", "Git email address")
backupCmd.PersistentFlags().StringVar(&c.GPGKey, "signing-key", "", "Path to the GPG signing key")
return backupCmd
}
func backup(ctx context.Context, c *config.Config) error {
client := grafana.NewClient(
c.GrafanaURL,
grafana.WithToken(c.GrafanaToken),
)
project, err := helper.PrepareProject(ctx, c)
if err != nil {
return err
}
var signer git.SignKey
if c.GPGKey != "" {
signer = git.SignKey{KeyFile: c.GPGKey}
if err = signer.ReadKeyFile(); err != nil {
return err
}
}
dashboards, _, err := client.File.Search(ctx, grafana.WithType(grafana.SearchTypeDashboard))
if err != nil {
return err
}
for _, dashboardInfo := range dashboards {
var dashboard *grafana.Dashboard
dashboard, _, err = client.Dashboard.Get(ctx, dashboardInfo.UID)
if err != nil {
return err
}
var versions []*grafana.DashboardVersion
versions, _, err = client.DashboardVersion.List(ctx, dashboardInfo.UID)
if err != nil {
return err
}
slices.Reverse(versions)
uncommitedVersion := c.ForceCommits
for _, version := range versions {
var dashboardVersion *grafana.DashboardVersion
dashboardVersion, _, err = client.DashboardVersion.Get(ctx, dashboardInfo.UID, version.Version)
if err != nil {
return err
}
var commitMsg string
commitMsg, err = helper.CommitVersion(
dashboard,
dashboardVersion,
dashboardInfo,
project,
c,
signer,
uncommitedVersion,
)
if errors.Is(err, helper.ErrAlreadyCommited) {
2024-07-30 21:20:18 +02:00
log.WithContext(ctx).WithField("commit-msg", commitMsg).Info("already committed")
continue
} else if err != nil {
return err
}
uncommitedVersion = true
2024-07-30 21:20:18 +02:00
log.WithContext(ctx).WithField("commit-msg", commitMsg).Info("commit created")
}
}
if !project.HasChanges() {
return nil
}
return project.Push(ctx)
}