| 1 | package backup |
| 2 | |
| 3 | import ( |
| 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. |
| 15 | type 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. |
| 22 | type 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. |
| 51 | type 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. |
| 58 | type RemoteError struct { |
| 59 | Status int |
| 60 | Msg string |
| 61 | } |
| 62 | |
| 63 | func (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. |
| 67 | func (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 | SetOwnHeaders(req.Header) |
| 94 | hc := c.HTTP |
| 95 | if hc == nil { |
| 96 | hc = &http.Client{Timeout: 15 * time.Second} |
| 97 | } |
| 98 | resp, err := hc.Do(req) |
| 99 | if err != nil { |
| 100 | return 0, nil, err |
| 101 | } |
| 102 | defer resp.Body.Close() |
| 103 | // Alleen een antwoord met protocolkop zegt iets over de backup server |
| 104 | // (een proxy-fout heeft er geen). |
| 105 | if p, ok := PeerFromHeader(resp.Header); ok { |
| 106 | c.Manager.SetPeer(conn.ID, p) |
| 107 | } |
| 108 | raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20)) |
| 109 | return resp.StatusCode, raw, nil |
| 110 | } |
| 111 | |
| 112 | func (c *Client) do(ctx context.Context, conn Conn, method, p string, in, out any) error { |
| 113 | code, raw, err := c.Call(ctx, conn, method, p, in) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | if code >= 300 { |
| 118 | var e struct { |
| 119 | Error string `json:"error"` |
| 120 | } |
| 121 | _ = json.Unmarshal(raw, &e) |
| 122 | if e.Error == "" { |
| 123 | e.Error = http.StatusText(code) |
| 124 | } |
| 125 | return &RemoteError{Status: code, Msg: e.Error} |
| 126 | } |
| 127 | if out != nil { |
| 128 | return json.Unmarshal(raw, out) |
| 129 | } |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | // Archives vraagt de lijst met archieven op, nieuwste eerst. |
| 134 | func (c *Client) Archives(ctx context.Context, conn Conn) ([]Archive, error) { |
| 135 | var out struct { |
| 136 | Archives []Archive `json:"archives"` |
| 137 | } |
| 138 | err := c.do(ctx, conn, http.MethodGet, "/api/v1/archives", nil, &out) |
| 139 | return out.Archives, err |
| 140 | } |
| 141 | |
| 142 | // Status vraagt de toestand van de backup server op. |
| 143 | func (c *Client) Status(ctx context.Context, conn Conn) (RemoteStatus, error) { |
| 144 | var out RemoteStatus |
| 145 | err := c.do(ctx, conn, http.MethodGet, "/api/v1/status", nil, &out) |
| 146 | return out, err |
| 147 | } |
| 148 | |
| 149 | // RemoteField is één instelling van de backup server. |
| 150 | type RemoteField struct { |
| 151 | Key string `json:"key"` |
| 152 | Label string `json:"label"` |
| 153 | Help string `json:"help"` |
| 154 | Kind string `json:"kind"` |
| 155 | Restart bool `json:"restart"` |
| 156 | Value string `json:"value"` |
| 157 | } |
| 158 | |
| 159 | // RemoteConfig is GET /api/v1/admin/config van de backup server. |
| 160 | type RemoteConfig struct { |
| 161 | Fields []RemoteField `json:"fields"` |
| 162 | DataDir string `json:"data_dir"` |
| 163 | Restart bool `json:"restart_pending"` |
| 164 | } |
| 165 | |
| 166 | // Config vraagt de instellingen van de backup server op. |
| 167 | func (c *Client) Config(ctx context.Context, conn Conn) (RemoteConfig, error) { |
| 168 | var out RemoteConfig |
| 169 | err := c.do(ctx, conn, http.MethodGet, "/api/v1/admin/config", nil, &out) |
| 170 | return out, err |
| 171 | } |
| 172 | |
| 173 | // RemoteAction is één beheeractie die de backup server aanbiedt. |
| 174 | type RemoteAction struct { |
| 175 | Name string `json:"name"` |
| 176 | Method string `json:"method"` |
| 177 | Path string `json:"path"` |
| 178 | Title string `json:"title"` |
| 179 | Signed bool `json:"bluegit"` |
| 180 | Params []string `json:"params"` |
| 181 | Fields []string `json:"fields"` |
| 182 | } |
| 183 | |
| 184 | // Actions vraagt op wat de backup server aanbiedt. |
| 185 | func (c *Client) Actions(ctx context.Context, conn Conn) ([]RemoteAction, error) { |
| 186 | var out struct { |
| 187 | Actions []RemoteAction `json:"actions"` |
| 188 | } |
| 189 | err := c.do(ctx, conn, http.MethodGet, "/api/v1/admin/actions", nil, &out) |
| 190 | return out.Actions, err |
| 191 | } |
| 192 | |
| 193 | // Rollback vraagt de backup server om archive terug te zetten. De server |
| 194 | // antwoordt meteen; het terugsturen naar BlueGit gebeurt daarna. |
| 195 | func (c *Client) Rollback(ctx context.Context, conn Conn, archive string) error { |
| 196 | return c.do(ctx, conn, http.MethodPost, "/api/v1/rollback", map[string]string{"archive": archive}, nil) |
| 197 | } |
| 198 | |
| 199 | // Notify meldt dat er iets veranderd is. |
| 200 | func (c *Client) Notify(ctx context.Context, conn Conn) error { |
| 201 | return c.do(ctx, conn, http.MethodPost, "/api/v1/notify", map[string]any{"at": time.Now().UTC()}, nil) |
| 202 | } |
| 203 | |
| 204 | // Notifier bundelt wijzigingen: hoogstens één melding per Delay, aan elke |
| 205 | // gekoppelde backup server, met een paar nieuwe pogingen bij een fout. De |
| 206 | // backup servers halen daarnaast periodiek alles op, dus een gemiste |
| 207 | // melding kost alleen vertraging. |
| 208 | type Notifier struct { |
| 209 | Client *Client |
| 210 | Delay time.Duration |
| 211 | once sync.Once |
| 212 | kick chan struct{} |
| 213 | } |
| 214 | |
| 215 | // Changed zegt dat er iets veranderd is. Blokkeert nooit. |
| 216 | func (n *Notifier) Changed() { |
| 217 | if n == nil { |
| 218 | return |
| 219 | } |
| 220 | n.once.Do(func() { |
| 221 | n.kick = make(chan struct{}, 1) |
| 222 | go n.loop() |
| 223 | }) |
| 224 | select { |
| 225 | case n.kick <- struct{}{}: |
| 226 | default: |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func (n *Notifier) loop() { |
| 231 | delay := n.Delay |
| 232 | if delay <= 0 { |
| 233 | delay = 2 * time.Second |
| 234 | } |
| 235 | for range n.kick { |
| 236 | time.Sleep(delay) |
| 237 | // Wat in de tussentijd binnenkwam, zit in deze melding. |
| 238 | select { |
| 239 | case <-n.kick: |
| 240 | default: |
| 241 | } |
| 242 | list, err := n.Client.Manager.List() |
| 243 | if err != nil { |
| 244 | continue |
| 245 | } |
| 246 | var wg sync.WaitGroup |
| 247 | for _, conn := range list { |
| 248 | if !conn.Connected() { |
| 249 | continue |
| 250 | } |
| 251 | wg.Add(1) |
| 252 | go func(conn Conn) { |
| 253 | defer wg.Done() |
| 254 | for try := 0; try < 4; try++ { |
| 255 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 256 | err := n.Client.Notify(ctx, conn) |
| 257 | cancel() |
| 258 | if err == nil { |
| 259 | return |
| 260 | } |
| 261 | time.Sleep(time.Duration(1<<try) * 2 * time.Second) |
| 262 | } |
| 263 | }(conn) |
| 264 | } |
| 265 | wg.Wait() |
| 266 | } |
| 267 | } |