| 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 | LastSync time.Time `json:"last_sync"` |
| 25 | SyncError string `json:"sync_error,omitempty"` |
| 26 | Rollback struct { |
| 27 | State string `json:"state"` |
| 28 | Archive string `json:"archive,omitempty"` |
| 29 | Error string `json:"error,omitempty"` |
| 30 | At time.Time `json:"at,omitempty"` |
| 31 | } `json:"rollback"` |
| 32 | Interval string `json:"archive_interval"` |
| 33 | RetentionDays int `json:"retention_days"` |
| 34 | } |
| 35 | |
| 36 | // Client praat ondertekend met de gekoppelde backup server. |
| 37 | type Client struct { |
| 38 | Manager *Manager |
| 39 | HTTP *http.Client |
| 40 | Now func() time.Time |
| 41 | } |
| 42 | |
| 43 | func (c *Client) do(ctx context.Context, method, p string, in, out any) error { |
| 44 | conn, err := c.Manager.Load() |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | if !conn.Connected() { |
| 49 | return fmt.Errorf("geen backup server gekoppeld") |
| 50 | } |
| 51 | inst, err := c.Manager.InstanceID() |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | var body []byte |
| 56 | if in != nil { |
| 57 | if body, err = json.Marshal(in); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | } |
| 61 | req, err := http.NewRequestWithContext(ctx, method, conn.ServerURL+p, bytes.NewReader(body)) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | if in != nil { |
| 66 | req.Header.Set("Content-Type", "application/json") |
| 67 | } |
| 68 | now := time.Now |
| 69 | if c.Now != nil { |
| 70 | now = c.Now |
| 71 | } |
| 72 | Sign(req, conn.SignSecret, inst, body, now()) |
| 73 | hc := c.HTTP |
| 74 | if hc == nil { |
| 75 | hc = &http.Client{Timeout: 15 * time.Second} |
| 76 | } |
| 77 | resp, err := hc.Do(req) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | defer resp.Body.Close() |
| 82 | raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20)) |
| 83 | if resp.StatusCode >= 300 { |
| 84 | var e struct { |
| 85 | Error string `json:"error"` |
| 86 | } |
| 87 | _ = json.Unmarshal(raw, &e) |
| 88 | if e.Error == "" { |
| 89 | e.Error = resp.Status |
| 90 | } |
| 91 | return fmt.Errorf("backup server: %s", e.Error) |
| 92 | } |
| 93 | if out != nil { |
| 94 | return json.Unmarshal(raw, out) |
| 95 | } |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | // Archives vraagt de lijst met archieven op, nieuwste eerst. |
| 100 | func (c *Client) Archives(ctx context.Context) ([]Archive, error) { |
| 101 | var out struct { |
| 102 | Archives []Archive `json:"archives"` |
| 103 | } |
| 104 | err := c.do(ctx, http.MethodGet, "/api/v1/archives", nil, &out) |
| 105 | return out.Archives, err |
| 106 | } |
| 107 | |
| 108 | // Status vraagt de toestand van de backup server op. |
| 109 | func (c *Client) Status(ctx context.Context) (RemoteStatus, error) { |
| 110 | var out RemoteStatus |
| 111 | err := c.do(ctx, http.MethodGet, "/api/v1/status", nil, &out) |
| 112 | return out, err |
| 113 | } |
| 114 | |
| 115 | // Rollback vraagt de backup server om archive terug te zetten. De server |
| 116 | // antwoordt meteen; het terugsturen naar BlueGit gebeurt daarna. |
| 117 | func (c *Client) Rollback(ctx context.Context, archive string) error { |
| 118 | return c.do(ctx, http.MethodPost, "/api/v1/rollback", map[string]string{"archive": archive}, nil) |
| 119 | } |
| 120 | |
| 121 | // Notify meldt dat er iets veranderd is. |
| 122 | func (c *Client) Notify(ctx context.Context) error { |
| 123 | return c.do(ctx, http.MethodPost, "/api/v1/notify", map[string]any{"at": time.Now().UTC()}, nil) |
| 124 | } |
| 125 | |
| 126 | // Notifier bundelt wijzigingen: hoogstens één melding per Delay, en bij een |
| 127 | // mislukking een paar nieuwe pogingen. De backup server haalt daarnaast |
| 128 | // periodiek alles op, dus een gemiste melding kost alleen vertraging. |
| 129 | type Notifier struct { |
| 130 | Client *Client |
| 131 | Delay time.Duration |
| 132 | once sync.Once |
| 133 | kick chan struct{} |
| 134 | } |
| 135 | |
| 136 | // Changed zegt dat er iets veranderd is. Blokkeert nooit. |
| 137 | func (n *Notifier) Changed() { |
| 138 | if n == nil { |
| 139 | return |
| 140 | } |
| 141 | n.once.Do(func() { |
| 142 | n.kick = make(chan struct{}, 1) |
| 143 | go n.loop() |
| 144 | }) |
| 145 | select { |
| 146 | case n.kick <- struct{}{}: |
| 147 | default: |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func (n *Notifier) loop() { |
| 152 | delay := n.Delay |
| 153 | if delay <= 0 { |
| 154 | delay = 2 * time.Second |
| 155 | } |
| 156 | for range n.kick { |
| 157 | time.Sleep(delay) |
| 158 | // Wat in de tussentijd binnenkwam, zit in deze melding. |
| 159 | select { |
| 160 | case <-n.kick: |
| 161 | default: |
| 162 | } |
| 163 | conn, err := n.Client.Manager.Load() |
| 164 | if err != nil || !conn.Connected() { |
| 165 | continue |
| 166 | } |
| 167 | for try := 0; try < 4; try++ { |
| 168 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 169 | err := n.Client.Notify(ctx) |
| 170 | cancel() |
| 171 | if err == nil { |
| 172 | break |
| 173 | } |
| 174 | time.Sleep(time.Duration(1<<try) * 2 * time.Second) |
| 175 | } |
| 176 | } |
| 177 | } |