123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package poller
-
- import (
- "time"
-
- "config"
- "git"
- "grafana"
- puller "puller"
- "pusher/common"
-
- "github.com/sirupsen/logrus"
- )
-
- func Setup(cfg *config.Config, client *grafana.Client, delRemoved bool) error {
- r, needsSync, err := git.NewRepository(cfg.Git)
- if err != nil {
- return err
- }
-
- if needsSync {
- if err = r.Sync(false); err != nil {
- return err
- }
- }
-
- errs := make(chan error, 1)
-
- go func() {
- if err = poller(cfg, r, client, delRemoved); err != nil {
- errs <- err
- return
- }
- }()
-
- err = <-errs
- return err
- }
-
- func poller(
- cfg *config.Config, repo *git.Repository, client *grafana.Client,
- delRemoved bool,
- ) (err error) {
- latestCommit, err := repo.GetLatestCommit()
- if err != nil {
- return
- }
-
- filesContents, err := repo.GetFilesContentsAtCommit(latestCommit)
- if err != nil {
- return
- }
-
- previousCommit := latestCommit
- previousFilesContents := filesContents
-
- for {
- if err = repo.Sync(true); err != nil {
- return
- }
-
- latestCommit, err = repo.GetLatestCommit()
- if err != nil {
- return
- }
-
- if previousCommit.Hash.String() != latestCommit.Hash.String() {
- logrus.WithFields(logrus.Fields{
- "previous_hash": previousCommit.Hash.String(),
- "new_hash": latestCommit.Hash.String(),
- }).Info("New commit(s) detected")
-
- filesContents, err = repo.GetFilesContentsAtCommit(latestCommit)
- if err != nil {
- return err
- }
-
- modified, removed, err := repo.GetModifiedAndRemovedFiles(previousCommit, latestCommit)
- if err != nil {
- return err
- }
-
- mergedContents := mergeContents(modified, removed, filesContents, previousFilesContents)
- common.PushFiles(modified, mergedContents, client)
-
- if delRemoved {
- common.DeleteDashboards(removed, mergedContents, client)
- }
-
- // Grafana will auto-update the version number after we pushed the new
- // dashboards, so we use the puller mechanic to pull the updated numbers and
- // commit them in the git repo.
- if err = puller.PullGrafanaAndCommit(client, cfg); err != nil {
- logrus.WithFields(logrus.Fields{
- "error": err,
- "repo": cfg.Git.User + "@" + cfg.Git.URL,
- "clone_path": cfg.Git.ClonePath,
- }).Error("Call to puller returned an error")
- }
- }
-
- previousCommit = latestCommit
- previousFilesContents = filesContents
- time.Sleep(time.Duration(cfg.Pusher.Config.Interval) * time.Second)
- }
- }
-
- func mergeContents(
- modified []string, removed []string,
- filesContents map[string][]byte, previousFilesContents map[string][]byte,
- ) (merged map[string][]byte) {
- merged = make(map[string][]byte)
-
- for _, modifiedFile := range modified {
- merged[modifiedFile] = filesContents[modifiedFile]
- }
-
- for _, removedFile := range removed {
- merged[removedFile] = previousFilesContents[removedFile]
- }
-
- return
- }
|