BlueGit

timfa bluegit

Files

bluegit / internal / backup / client.go

client.go

client.go 6.9 KiB Download Raw
1package backup
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "io"
9 "net/http"
10 "sync"
11 "time"
12)
13
14// Archive is één archief zoals de backup server het meldt.
15type Archive struct {
16 Name string `json:"name"`
17 CreatedAt time.Time `json:"created_at"`
18 Size int64 `json:"size"`
19}
20
21// RemoteStatus is wat de backup server over zichzelf zegt.
22type RemoteStatus struct {
23 Version string `json:"version"`
24 ServerID string `json:"server_id"`
25 LastSync time.Time `json:"last_sync"`
26 SyncError string `json:"sync_error,omitempty"`
27 Syncs int `json:"syncs"`
28 Notifies int `json:"notifies"`
29 MirrorBytes int64 `json:"mirror_bytes"`
30 LastStats struct {
31 Files int `json:"files"`
32 Downloaded int `json:"downloaded"`
33 Bytes int64 `json:"bytes"`
34 Deleted int `json:"deleted"`
35 } `json:"last_stats"`
36 Archives int `json:"archives"`
37 LastArchive time.Time `json:"last_archive"`
38 ArchiveError string `json:"archive_error,omitempty"`
39 Rollback struct {
40 State string `json:"state"`
41 Archive string `json:"archive,omitempty"`
42 Error string `json:"error,omitempty"`
43 At time.Time `json:"at,omitempty"`
44 } `json:"rollback"`
45 Interval string `json:"archive_interval"`
46 RetentionDays int `json:"retention_days"`
47 RateLimitKiB int64 `json:"rate_limit_kib"`
48}
49
50// Client praat ondertekend met de backup servers.
51type Client struct {
52 Manager *Manager
53 HTTP *http.Client
54 Now func() time.Time
55}
56
57// RemoteError is een foutantwoord van de backup server.
58type RemoteError struct {
59 Status int
60 Msg string
61}
62
63func (e *RemoteError) Error() string { return "backup server: " + e.Msg }
64
65// Call stuurt een ondertekend verzoek naar backup server conn en geeft het
66// ruwe antwoord terug. in is nil of wordt JSON.
67func (c *Client) Call(ctx context.Context, conn Conn, method, p string, in any) (int, []byte, error) {
68 if !conn.Connected() {
69 return 0, nil, fmt.Errorf("backup server %s is niet gekoppeld", conn.Name)
70 }
71 inst, err := c.Manager.InstanceID()
72 if err != nil {
73 return 0, nil, err
74 }
75 var body []byte
76 if in != nil {
77 if body, err = json.Marshal(in); err != nil {
78 return 0, nil, err
79 }
80 }
81 req, err := http.NewRequestWithContext(ctx, method, conn.ServerURL+p, bytes.NewReader(body))
82 if err != nil {
83 return 0, nil, err
84 }
85 if in != nil {
86 req.Header.Set("Content-Type", "application/json")
87 }
88 now := time.Now
89 if c.Now != nil {
90 now = c.Now
91 }
92 Sign(req, conn.SignSecret, inst, body, now())
93 hc := c.HTTP
94 if hc == nil {
95 hc = &http.Client{Timeout: 15 * time.Second}
96 }
97 resp, err := hc.Do(req)
98 if err != nil {
99 return 0, nil, err
100 }
101 defer resp.Body.Close()
102 raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
103 return resp.StatusCode, raw, nil
104}
105
106func (c *Client) do(ctx context.Context, conn Conn, method, p string, in, out any) error {
107 code, raw, err := c.Call(ctx, conn, method, p, in)
108 if err != nil {
109 return err
110 }
111 if code >= 300 {
112 var e struct {
113 Error string `json:"error"`
114 }
115 _ = json.Unmarshal(raw, &e)
116 if e.Error == "" {
117 e.Error = http.StatusText(code)
118 }
119 return &RemoteError{Status: code, Msg: e.Error}
120 }
121 if out != nil {
122 return json.Unmarshal(raw, out)
123 }
124 return nil
125}
126
127// Archives vraagt de lijst met archieven op, nieuwste eerst.
128func (c *Client) Archives(ctx context.Context, conn Conn) ([]Archive, error) {
129 var out struct {
130 Archives []Archive `json:"archives"`
131 }
132 err := c.do(ctx, conn, http.MethodGet, "/api/v1/archives", nil, &out)
133 return out.Archives, err
134}
135
136// Status vraagt de toestand van de backup server op.
137func (c *Client) Status(ctx context.Context, conn Conn) (RemoteStatus, error) {
138 var out RemoteStatus
139 err := c.do(ctx, conn, http.MethodGet, "/api/v1/status", nil, &out)
140 return out, err
141}
142
143// RemoteField is één instelling van de backup server.
144type RemoteField struct {
145 Key string `json:"key"`
146 Label string `json:"label"`
147 Help string `json:"help"`
148 Kind string `json:"kind"`
149 Restart bool `json:"restart"`
150 Value string `json:"value"`
151}
152
153// RemoteConfig is GET /api/v1/admin/config van de backup server.
154type RemoteConfig struct {
155 Fields []RemoteField `json:"fields"`
156 DataDir string `json:"data_dir"`
157 Restart bool `json:"restart_pending"`
158}
159
160// Config vraagt de instellingen van de backup server op.
161func (c *Client) Config(ctx context.Context, conn Conn) (RemoteConfig, error) {
162 var out RemoteConfig
163 err := c.do(ctx, conn, http.MethodGet, "/api/v1/admin/config", nil, &out)
164 return out, err
165}
166
167// RemoteAction is één beheeractie die de backup server aanbiedt.
168type RemoteAction struct {
169 Name string `json:"name"`
170 Method string `json:"method"`
171 Path string `json:"path"`
172 Title string `json:"title"`
173 Signed bool `json:"bluegit"`
174 Params []string `json:"params"`
175 Fields []string `json:"fields"`
176}
177
178// Actions vraagt op wat de backup server aanbiedt.
179func (c *Client) Actions(ctx context.Context, conn Conn) ([]RemoteAction, error) {
180 var out struct {
181 Actions []RemoteAction `json:"actions"`
182 }
183 err := c.do(ctx, conn, http.MethodGet, "/api/v1/admin/actions", nil, &out)
184 return out.Actions, err
185}
186
187// Rollback vraagt de backup server om archive terug te zetten. De server
188// antwoordt meteen; het terugsturen naar BlueGit gebeurt daarna.
189func (c *Client) Rollback(ctx context.Context, conn Conn, archive string) error {
190 return c.do(ctx, conn, http.MethodPost, "/api/v1/rollback", map[string]string{"archive": archive}, nil)
191}
192
193// Notify meldt dat er iets veranderd is.
194func (c *Client) Notify(ctx context.Context, conn Conn) error {
195 return c.do(ctx, conn, http.MethodPost, "/api/v1/notify", map[string]any{"at": time.Now().UTC()}, nil)
196}
197
198// Notifier bundelt wijzigingen: hoogstens één melding per Delay, aan elke
199// gekoppelde backup server, met een paar nieuwe pogingen bij een fout. De
200// backup servers halen daarnaast periodiek alles op, dus een gemiste
201// melding kost alleen vertraging.
202type Notifier struct {
203 Client *Client
204 Delay time.Duration
205 once sync.Once
206 kick chan struct{}
207}
208
209// Changed zegt dat er iets veranderd is. Blokkeert nooit.
210func (n *Notifier) Changed() {
211 if n == nil {
212 return
213 }
214 n.once.Do(func() {
215 n.kick = make(chan struct{}, 1)
216 go n.loop()
217 })
218 select {
219 case n.kick <- struct{}{}:
220 default:
221 }
222}
223
224func (n *Notifier) loop() {
225 delay := n.Delay
226 if delay <= 0 {
227 delay = 2 * time.Second
228 }
229 for range n.kick {
230 time.Sleep(delay)
231 // Wat in de tussentijd binnenkwam, zit in deze melding.
232 select {
233 case <-n.kick:
234 default:
235 }
236 list, err := n.Client.Manager.List()
237 if err != nil {
238 continue
239 }
240 var wg sync.WaitGroup
241 for _, conn := range list {
242 if !conn.Connected() {
243 continue
244 }
245 wg.Add(1)
246 go func(conn Conn) {
247 defer wg.Done()
248 for try := 0; try < 4; try++ {
249 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
250 err := n.Client.Notify(ctx, conn)
251 cancel()
252 if err == nil {
253 return
254 }
255 time.Sleep(time.Duration(1<<try) * 2 * time.Second)
256 }
257 }(conn)
258 }
259 wg.Wait()
260 }
261}