2024-07-27 18:56:09 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-30 20:43:58 +02:00
|
|
|
"errors"
|
2024-07-27 18:56:09 +02:00
|
|
|
"fmt"
|
|
|
|
"slices"
|
|
|
|
|
|
|
|
"git.ar21.de/yolokube/grafana-backuper/internal/config"
|
2024-07-30 20:43:58 +02:00
|
|
|
"git.ar21.de/yolokube/grafana-backuper/internal/helper"
|
2024-07-27 18:56:09 +02:00
|
|
|
"git.ar21.de/yolokube/grafana-backuper/pkg/git"
|
|
|
|
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana"
|
|
|
|
"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.",
|
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return backup(cmd.Context(), c)
|
|
|
|
},
|
|
|
|
PreRunE: func(_ *cobra.Command, _ []string) error {
|
|
|
|
return c.Validate()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
|
2024-07-30 20:43:58 +02:00
|
|
|
project, err := helper.PrepareProject(ctx, c)
|
|
|
|
if err != nil {
|
2024-07-27 18:56:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var signer git.SignKey
|
|
|
|
if c.GPGKey != "" {
|
|
|
|
signer = git.SignKey{KeyFile: c.GPGKey}
|
2024-07-30 20:43:58 +02:00
|
|
|
if err = signer.ReadKeyFile(); err != nil {
|
2024-07-27 18:56:09 +02:00
|
|
|
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)
|
|
|
|
|
2024-07-30 20:43:58 +02:00
|
|
|
uncommitedVersion := c.ForceCommits
|
2024-07-27 18:56:09 +02:00
|
|
|
for _, version := range versions {
|
|
|
|
var dashboardVersion *grafana.DashboardVersion
|
|
|
|
dashboardVersion, _, err = client.DashboardVersion.Get(ctx, dashboardInfo.UID, version.Version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-30 20:43:58 +02:00
|
|
|
var commitMsg string
|
|
|
|
commitMsg, err = helper.CommitVersion(
|
|
|
|
dashboard,
|
|
|
|
dashboardVersion,
|
|
|
|
dashboardInfo,
|
|
|
|
project,
|
|
|
|
c,
|
|
|
|
signer,
|
|
|
|
uncommitedVersion,
|
2024-07-27 18:56:09 +02:00
|
|
|
)
|
2024-07-30 20:43:58 +02:00
|
|
|
if errors.Is(err, helper.ErrAlreadyCommited) {
|
|
|
|
fmt.Fprintf(c.Output, "%s -> %v\n", commitMsg, err)
|
2024-07-27 18:56:09 +02:00
|
|
|
continue
|
2024-07-30 20:43:58 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2024-07-27 18:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uncommitedVersion = true
|
|
|
|
fmt.Fprintln(c.Output, commitMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 20:43:58 +02:00
|
|
|
if !project.HasChanges() {
|
|
|
|
return nil
|
2024-07-27 18:56:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:43:58 +02:00
|
|
|
return project.Push(ctx)
|
2024-07-27 18:56:09 +02:00
|
|
|
}
|