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
240
pkg/git/git.go
Normal file
240
pkg/git/git.go
Normal file
|
@ -0,0 +1,240 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana"
|
||||
"github.com/ProtonMail/go-crypto/openpgp"
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/memfs"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"github.com/go-git/go-git/v5/storage/memory"
|
||||
)
|
||||
|
||||
var (
|
||||
fs billy.Filesystem
|
||||
storer *memory.Storage
|
||||
)
|
||||
|
||||
type Payload struct {
|
||||
Author *object.Signature
|
||||
Committer *object.Signature
|
||||
Content []byte
|
||||
Dashboard grafana.FoundBoard
|
||||
DashboardInfo grafana.BoardProperties
|
||||
Directory string
|
||||
KeyFile string
|
||||
Repository *git.Repository
|
||||
Version grafana.DashboardVersion
|
||||
}
|
||||
|
||||
func NewPayload(keyFile string) *Payload {
|
||||
fs = memfs.New()
|
||||
storer = memory.NewStorage()
|
||||
|
||||
return &Payload{
|
||||
Author: nil,
|
||||
Committer: nil,
|
||||
Content: []byte{},
|
||||
Dashboard: grafana.FoundBoard{},
|
||||
DashboardInfo: grafana.BoardProperties{},
|
||||
Directory: "",
|
||||
KeyFile: keyFile,
|
||||
Repository: nil,
|
||||
Version: grafana.DashboardVersion{},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Payload) AddAuthor(name, email string) {
|
||||
p.Author = &object.Signature{
|
||||
Name: name,
|
||||
Email: email,
|
||||
When: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Payload) UpdateAuthor(timestamp time.Time) {
|
||||
p.Author.When = timestamp
|
||||
}
|
||||
|
||||
func (p *Payload) AddCommitter(name, email string) {
|
||||
p.Committer = &object.Signature{
|
||||
Name: name,
|
||||
Email: email,
|
||||
When: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Payload) UpdateCommitter() {
|
||||
p.Committer.When = time.Now()
|
||||
}
|
||||
|
||||
func (p *Payload) UpdateContent(content []byte) {
|
||||
p.Content = content
|
||||
}
|
||||
|
||||
func (p *Payload) UpdateDashboard(dashboard grafana.FoundBoard) {
|
||||
p.Dashboard = dashboard
|
||||
}
|
||||
|
||||
func (p *Payload) UpdateDashboardInfo(dashboardInfo grafana.BoardProperties) {
|
||||
p.DashboardInfo = dashboardInfo
|
||||
}
|
||||
|
||||
func (p *Payload) UpdateVersion(version grafana.DashboardVersion) {
|
||||
p.Version = version
|
||||
}
|
||||
|
||||
func (p *Payload) GetRepo(repoURL, user, password string) (err error) {
|
||||
p.Repository, err = git.Clone(
|
||||
storer,
|
||||
fs,
|
||||
&git.CloneOptions{
|
||||
Auth: genAuth(user, password),
|
||||
URL: repoURL,
|
||||
Progress: os.Stdout,
|
||||
},
|
||||
)
|
||||
|
||||
p.Directory = filepath.Base(repoURL)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Payload) IsVersionCommitted(branch string) bool {
|
||||
refName := plumbing.NewBranchReferenceName(branch)
|
||||
ref, err := p.Repository.Reference(refName, false)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
commitIter, err := p.Repository.Log(&git.LogOptions{From: ref.Hash()})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
err = commitIter.ForEach(func(commit *object.Commit) error {
|
||||
if strings.Contains(commit.Message, fmt.Sprintf("Update %s", p.Version.DashboardUID)) && strings.Contains(commit.Message, fmt.Sprintf("version %d", p.Version.ID)) {
|
||||
return fmt.Errorf("version already committed")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return err != nil
|
||||
}
|
||||
|
||||
func genAuth(user, password string) *http.BasicAuth {
|
||||
return &http.BasicAuth{
|
||||
Username: user,
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
|
||||
func commitDashboard(repo *git.Repository, content []byte, commitMsg, dashboardTitle, folderTitle, gitRepoDirectory, keyFile string, author, committer *object.Signature) (err error) {
|
||||
var (
|
||||
file billy.File
|
||||
signer *openpgp.Entity
|
||||
worktree *git.Worktree
|
||||
)
|
||||
|
||||
if strings.TrimSpace(keyFile) != "" {
|
||||
signer, err = getSigner(keyFile)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
worktree, err = repo.Worktree()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = fs.MkdirAll(folderTitle, 0755); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
filePath := filepath.Join(folderTitle, fmt.Sprintf("%s.json", dashboardTitle))
|
||||
if file, err = fs.Create(filePath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = file.Write(content); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = file.Close(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = worktree.Add(filePath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = worktree.Commit(
|
||||
commitMsg,
|
||||
&git.CommitOptions{
|
||||
Author: author,
|
||||
Committer: committer,
|
||||
SignKey: signer,
|
||||
},
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Payload) CreateCommit() error {
|
||||
var commitmsg string
|
||||
|
||||
if p.Version.Message != "" {
|
||||
commitmsg = fmt.Sprintf(
|
||||
"%s: Update %s to version %d => %s",
|
||||
p.Dashboard.Title,
|
||||
p.Version.DashboardUID,
|
||||
p.Version.ID,
|
||||
p.Version.Message,
|
||||
)
|
||||
} else {
|
||||
commitmsg = fmt.Sprintf(
|
||||
"%s: Update %s to version %d",
|
||||
p.Dashboard.Title,
|
||||
p.Version.DashboardUID,
|
||||
p.Version.ID,
|
||||
)
|
||||
}
|
||||
|
||||
p.UpdateAuthor(p.Version.Created)
|
||||
p.UpdateCommitter()
|
||||
|
||||
return commitDashboard(
|
||||
p.Repository,
|
||||
p.Content,
|
||||
commitmsg,
|
||||
p.Dashboard.Title,
|
||||
p.DashboardInfo.FolderTitle,
|
||||
p.Directory,
|
||||
p.KeyFile,
|
||||
p.Author,
|
||||
p.Committer,
|
||||
)
|
||||
}
|
||||
|
||||
func (p *Payload) PushToRemote(user, password string) error {
|
||||
origin, err := p.Repository.Remote("origin")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return origin.Push(
|
||||
&git.PushOptions{
|
||||
Auth: genAuth(user, password),
|
||||
Progress: os.Stdout,
|
||||
},
|
||||
)
|
||||
}
|
28
pkg/git/signer.go
Normal file
28
pkg/git/signer.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/ProtonMail/go-crypto/openpgp"
|
||||
"github.com/ProtonMail/go-crypto/openpgp/armor"
|
||||
)
|
||||
|
||||
func getSigner(keyFile string) (*openpgp.Entity, error) {
|
||||
file, err := os.Open(keyFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
block, err := armor.Decode(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entityList, err := openpgp.ReadKeyRing(block.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entityList[0], nil
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package grafana
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
@ -8,9 +9,30 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type BoardProperties struct {
|
||||
IsStarred bool `json:"isStarred,omitempty"`
|
||||
IsHome bool `json:"isHome,omitempty"`
|
||||
IsSnapshot bool `json:"isSnapshot,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
CanSave bool `json:"canSave"`
|
||||
CanEdit bool `json:"canEdit"`
|
||||
CanStar bool `json:"canStar"`
|
||||
Slug string `json:"slug"`
|
||||
Expires time.Time `json:"expires"`
|
||||
Created time.Time `json:"created"`
|
||||
Updated time.Time `json:"updated"`
|
||||
UpdatedBy string `json:"updatedBy"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
Version int `json:"version"`
|
||||
FolderID int `json:"folderId"`
|
||||
FolderTitle string `json:"folderTitle"`
|
||||
FolderURL string `json:"folderUrl"`
|
||||
}
|
||||
|
||||
type DashboardVersion struct {
|
||||
ID uint `json:"id"`
|
||||
DashboardID uint `json:"dashboardId"`
|
||||
DashboardUID string `json:"uid"`
|
||||
ParentVersion uint `json:"parentVersion"`
|
||||
RestoredFrom uint `json:"restoredFrom"`
|
||||
Version uint `json:"version"`
|
||||
|
@ -19,6 +41,42 @@ type DashboardVersion struct {
|
|||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (c *Client) getRawDashboardByUID(ctx context.Context, path string) ([]byte, BoardProperties, error) {
|
||||
raw, code, err := c.get(ctx, fmt.Sprintf("api/dashboards/%s", path), nil)
|
||||
if err != nil {
|
||||
return nil, BoardProperties{}, err
|
||||
}
|
||||
if code != 200 {
|
||||
return raw, BoardProperties{}, fmt.Errorf("HTTP error %d: returns %s", code, raw)
|
||||
}
|
||||
var result struct {
|
||||
Meta BoardProperties `json:"meta"`
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(raw))
|
||||
dec.UseNumber()
|
||||
if err := dec.Decode(&result); err != nil {
|
||||
return raw, BoardProperties{}, fmt.Errorf("failed unmarshalling dashboard from path %s: %v", path, err)
|
||||
}
|
||||
return raw, result.Meta, nil
|
||||
}
|
||||
|
||||
func (c *Client) getRawDashboardFromVersion(ctx context.Context, path string) ([]byte, DashboardVersion, error) {
|
||||
var versionInfo DashboardVersion
|
||||
raw, code, err := c.get(ctx, fmt.Sprintf("api/dashboards/%s", path), nil)
|
||||
if err != nil {
|
||||
return nil, versionInfo, err
|
||||
}
|
||||
if code != 200 {
|
||||
return raw, versionInfo, fmt.Errorf("HTTP error %d: returns %s", code, raw)
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(raw))
|
||||
dec.UseNumber()
|
||||
if err := dec.Decode(&versionInfo); err != nil {
|
||||
return raw, versionInfo, fmt.Errorf("failed unmarshalling dashboard from path %s: %v", path, err)
|
||||
}
|
||||
return raw, versionInfo, nil
|
||||
}
|
||||
|
||||
func queryParams(params ...QueryParam) url.Values {
|
||||
u := url.URL{}
|
||||
q := u.Query()
|
||||
|
@ -28,14 +86,14 @@ func queryParams(params ...QueryParam) url.Values {
|
|||
return q
|
||||
}
|
||||
|
||||
func (c *Client) GetDashboardVersionsByDashboardID(ctx context.Context, dashboardID uint, params ...QueryParam) ([]DashboardVersion, error) {
|
||||
func (c *Client) GetDashboardVersionsByDashboardUID(ctx context.Context, uid string, params ...QueryParam) ([]DashboardVersion, error) {
|
||||
var (
|
||||
raw []byte
|
||||
code int
|
||||
err error
|
||||
)
|
||||
|
||||
if raw, code, err = c.get(ctx, fmt.Sprintf("api/dashboards/id/%d/versions", dashboardID), queryParams(params...)); err != nil {
|
||||
if raw, code, err = c.get(ctx, fmt.Sprintf("api/dashboards/uid/%s/versions", uid), queryParams(params...)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if code != 200 {
|
||||
|
@ -47,6 +105,14 @@ func (c *Client) GetDashboardVersionsByDashboardID(ctx context.Context, dashboar
|
|||
return versions, err
|
||||
}
|
||||
|
||||
func (c *Client) GetRawDashboardByUID(ctx context.Context, uid string) ([]byte, BoardProperties, error) {
|
||||
return c.getRawDashboardByUID(ctx, "uid/"+uid)
|
||||
}
|
||||
|
||||
func (c *Client) GetRawDashboardByUIDAndVersion(ctx context.Context, uid string, version uint) ([]byte, DashboardVersion, error) {
|
||||
return c.getRawDashboardFromVersion(ctx, "uid/"+uid+"/versions/"+fmt.Sprint(version))
|
||||
}
|
||||
|
||||
func (c *Client) SearchDashboards(ctx context.Context, query string, starred bool, tags ...string) ([]FoundBoard, error) {
|
||||
params := []SearchParam{
|
||||
SearchType(SearchTypeDashboard),
|
||||
|
@ -58,3 +124,14 @@ func (c *Client) SearchDashboards(ctx context.Context, query string, starred boo
|
|||
}
|
||||
return c.Search(ctx, params...)
|
||||
}
|
||||
|
||||
func ConvertRawToIndent(raw []byte) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
err := json.Indent(&buf, raw, "", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error pritty-printing raw json string: %v", err)
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package grafana
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -74,7 +73,3 @@ func (c *Client) doRequest(ctx context.Context, method, query string, params url
|
|||
func (c *Client) get(ctx context.Context, query string, params url.Values) ([]byte, int, error) {
|
||||
return c.doRequest(ctx, "GET", query, params, nil)
|
||||
}
|
||||
|
||||
func (c *Client) post(ctx context.Context, query string, params url.Values, body []byte) ([]byte, int, error) {
|
||||
return c.doRequest(ctx, "POST", query, params, bytes.NewBuffer(body))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue