-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_sync.go
183 lines (155 loc) · 4.36 KB
/
http_sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package sync
import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/services"
)
type httpSync struct {
*services.BasicService
cfg Config
logger log.Logger
storePath string
templates map[string]string // template urn & path
mutex sync.Mutex
}
func NewHttpSynchronizer(cfg Config, storePath string, logger log.Logger) *httpSync {
sync := &httpSync{
cfg: cfg,
logger: logger,
templates: make(map[string]string),
storePath: storePath,
mutex: sync.Mutex{},
}
sync.BasicService = services.NewTimerService(cfg.Interval, sync.Synchronize, sync.Synchronize, nil)
return sync
}
func (s *httpSync) Synchronize(ctx context.Context) error {
s.mutex.Lock()
defer s.mutex.Unlock()
idx, err := s.getIndex()
if err != nil {
level.Warn(s.logger).Log("msg", "failed to download templates index", "err", err)
return err
}
toAdd, toDel := compareMaps(s.templates, idx)
for name, url := range toAdd {
err := s.DownloadTemplate(name, url)
if err != nil {
level.Warn(s.logger).Log("msg", "failed to download template", "template", name, "url", url, "err", err)
continue
}
s.templates[name] = url
}
for name := range toDel {
s.RemoveTemplate(name)
delete(s.templates, name)
}
return nil
}
func (s *httpSync) DownloadTemplate(name, url string) error {
splits := strings.SplitN(name, string(filepath.Separator), 2)
if len(splits) != 2 {
return fmt.Errorf("invalid name for templates")
}
resp, err := http.Get(url)
if err != nil {
level.Warn(s.logger).Log("msg", "cannot download template.", "url", url, "err", err)
return err
}
defer resp.Body.Close()
tempFile, err := os.CreateTemp(s.storePath, fmt.Sprintf("%s.temp", splits[1]))
if err != nil {
level.Warn(s.logger).Log("msg", "cannot create temp file for downloading template.", "url", url, "err", err)
return err
}
defer tempFile.Close()
_, err = io.Copy(tempFile, resp.Body)
if err != nil {
level.Warn(s.logger).Log("msg", "cannot download template content.", "url", url, "err", err)
return err
}
resp.Body.Close()
err = os.MkdirAll(filepath.Join(s.storePath, splits[0]), 0755)
if err != nil {
level.Warn(s.logger).Log("msg", "cannot create target dir for content.", "dir", splits[0], "err", err)
return err
}
err = os.Rename(tempFile.Name(), filepath.Join(s.storePath, splits[0], splits[1]))
if err != nil {
level.Warn(s.logger).Log("msg", "cannot rename to target file.", "name", name, "err", err)
return err
}
return nil
}
func (s *httpSync) RemoveTemplate(name string) {
splits := strings.SplitN(name, string(filepath.Separator), 2)
if len(splits) != 2 {
level.Warn(s.logger).Log("msg", "cannot remove template.", "name", name)
return
}
err := os.Remove(filepath.Join(s.storePath, splits[0], splits[1]))
if err != nil {
level.Warn(s.logger).Log("msg", "cannot remove template.", "name", name, "err", err)
}
}
func (s *httpSync) getIndex() (result map[string]string, err error) {
client := http.Client{}
idxUri, err := url.JoinPath(s.cfg.Address, s.cfg.IndexFile)
if err != nil {
level.Warn(s.logger).Log("msg", "invalid address for templates synchorize.", "err", err)
return nil, err
}
resp, err := client.Get(idxUri)
if err != nil {
level.Warn(s.logger).Log("msg", "cannot download templates index.", "err", err)
return nil, err
}
defer resp.Body.Close()
result = map[string]string{}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
normalized := normalizeFilePattern(line)
if normalized == "" {
level.Error(s.logger).Log("msg", "invalid content of templates index.", "err", err)
return nil, fmt.Errorf("invalid content of templates index file, provide: '%s', need match: '%s'", line, TemplateFilePattern)
}
remote, _ := url.JoinPath(s.cfg.Address, normalized)
result[normalized] = remote
}
return result, nil
}
func compareMaps(ori, oth map[string]string) (toAdd, toDel map[string]string) {
if len(ori) == 0 {
return oth, toDel
}
if len(oth) == 0 {
return toAdd, ori
}
toDel = map[string]string{}
toAdd = map[string]string{}
for k, v := range oth {
nv, exists := ori[k]
if !exists {
toAdd[k] = v
} else if nv != v {
toAdd[k] = v
}
}
for k, v := range ori {
if _, exists := oth[k]; !exists {
toDel[k] = v
}
}
return toAdd, toDel
}