소스 검색

Add git-pull poller

Brendan Abolivier 6 년 전
부모
커밋
e81ad25091
로그인 계정: Brendan Abolivier <contact@brendanabolivier.com> GPG 키 ID: 8EF1500759F70623
1개의 변경된 파일116개의 추가작업 그리고 0개의 파일을 삭제
  1. 116
    0
      src/pusher/poller/poller.go

+ 116
- 0
src/pusher/poller/poller.go 파일 보기

@@ -0,0 +1,116 @@
1
+package poller
2
+
3
+import (
4
+	"time"
5
+
6
+	"config"
7
+	"git"
8
+	"grafana"
9
+	"pusher/common"
10
+
11
+	"github.com/sirupsen/logrus"
12
+)
13
+
14
+func Setup(cfg *config.Config, client *grafana.Client, delRemoved bool) error {
15
+	r, needsSync, err := git.NewRepository(cfg.Git)
16
+	if err != nil {
17
+		return err
18
+	}
19
+
20
+	if needsSync {
21
+		if err = r.Sync(false); err != nil {
22
+			return err
23
+		}
24
+	}
25
+
26
+	errs := make(chan error, 1)
27
+
28
+	go func() {
29
+		if err = poller(cfg, r, client, delRemoved); err != nil {
30
+			errs <- err
31
+			return
32
+		}
33
+	}()
34
+
35
+	err = <-errs
36
+	return err
37
+}
38
+
39
+func poller(
40
+	cfg *config.Config, repo *git.Repository, client *grafana.Client,
41
+	delRemoved bool,
42
+) (err error) {
43
+	latestCommit, err := repo.GetLatestCommit()
44
+	if err != nil {
45
+		return
46
+	}
47
+
48
+	previousCommit := latestCommit
49
+
50
+	for {
51
+		addedOrModified := make([]string, 0)
52
+		removed := make([]string, 0)
53
+
54
+		if err = repo.Sync(true); err != nil {
55
+			return
56
+		}
57
+
58
+		latestCommit, err = repo.GetLatestCommit()
59
+		if err != nil {
60
+			return
61
+		}
62
+
63
+		if previousCommit.Hash.String() != latestCommit.Hash.String() {
64
+			lineCounts, err := git.GetFilesLineCountsAtCommit(previousCommit)
65
+			if err != nil {
66
+				return err
67
+			}
68
+
69
+			deltas, err := repo.LineCountsDeltasIgnoreManagerCommits(previousCommit, latestCommit)
70
+			if err != nil {
71
+				return err
72
+			}
73
+
74
+			for file, delta := range deltas {
75
+				if delta == 0 {
76
+					continue
77
+				}
78
+
79
+				if delta > 0 {
80
+					addedOrModified = append(addedOrModified, file)
81
+				} else if delta < 0 {
82
+					if -delta < lineCounts[file] {
83
+						addedOrModified = append(addedOrModified, file)
84
+					} else {
85
+						removed = append(removed, file)
86
+					}
87
+				}
88
+			}
89
+		}
90
+
91
+		filesToPush := make(map[string][]byte)
92
+		for _, filename := range addedOrModified {
93
+			if err = common.PrepareForPush(filename, &filesToPush, cfg); err != nil {
94
+				return err
95
+			}
96
+		}
97
+
98
+		common.PushFiles(filesToPush, client)
99
+
100
+		if delRemoved {
101
+			for _, removedFile := range removed {
102
+				if err = common.DeleteDashboard(
103
+					removedFile, client, cfg,
104
+				); err != nil {
105
+					logrus.WithFields(logrus.Fields{
106
+						"error":    err,
107
+						"filename": removedFile,
108
+					}).Error("Failed to delete the dashboard")
109
+				}
110
+			}
111
+		}
112
+
113
+		previousCommit = latestCommit
114
+		time.Sleep(time.Duration(cfg.Pusher.Config.Interval) * time.Second)
115
+	}
116
+}