| 1 | // Package mirror houdt de draaiende kopie van één BlueGit bij. |
| 2 | package mirror |
| 3 | |
| 4 | import ( |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "net/http" |
| 12 | "net/url" |
| 13 | "strings" |
| 14 | "time" |
| 15 | |
| 16 | "gitlab.com/Timfa/bluegit/internal/backupserver/protocol" |
| 17 | "gitlab.com/Timfa/bluegit/internal/backupserver/throttle" |
| 18 | ) |
| 19 | |
| 20 | // ErrGone betekent dat een bestand tussen manifest en download verdween. |
| 21 | var ErrGone = errors.New("bestand verdwenen") |
| 22 | |
| 23 | // Remote is de backup-API van één BlueGit, met de Backupserver-Key. |
| 24 | type Remote struct { |
| 25 | URL string |
| 26 | Key string |
| 27 | HTTP *http.Client |
| 28 | Limiter *throttle.Limiter |
| 29 | // Version gaat mee in de X-Backup-koppen. |
| 30 | Version string |
| 31 | // OnPeer krijgt het protocol van BlueGit uit elk antwoord dat het meldt. |
| 32 | OnPeer func(protocol.Peer) |
| 33 | } |
| 34 | |
| 35 | func (r *Remote) client() *http.Client { |
| 36 | if r.HTTP != nil { |
| 37 | return r.HTTP |
| 38 | } |
| 39 | return &http.Client{Timeout: 0} |
| 40 | } |
| 41 | |
| 42 | func (r *Remote) req(ctx context.Context, method, p string, body io.Reader) (*http.Response, error) { |
| 43 | req, err := http.NewRequestWithContext(ctx, method, strings.TrimRight(r.URL, "/")+p, body) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | req.Header.Set("Authorization", "Bearer "+r.Key) |
| 48 | req.Header.Set("User-Agent", "bluegit-backup") |
| 49 | protocol.SetOwnHeaders(req.Header, r.Version) |
| 50 | resp, err := r.client().Do(req) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | if p, ok := protocol.BlueGitFromHeader(resp.Header); ok && r.OnPeer != nil { |
| 55 | r.OnPeer(p) |
| 56 | } |
| 57 | if resp.StatusCode >= 300 { |
| 58 | defer resp.Body.Close() |
| 59 | raw, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<16)) |
| 60 | var e struct { |
| 61 | Error string `json:"error"` |
| 62 | } |
| 63 | _ = json.Unmarshal(raw, &e) |
| 64 | if e.Error == "" { |
| 65 | e.Error = strings.TrimSpace(string(raw)) |
| 66 | } |
| 67 | if resp.StatusCode == http.StatusNotFound && strings.HasPrefix(p, "/api/v1/backup/file") { |
| 68 | return nil, ErrGone |
| 69 | } |
| 70 | return nil, &HTTPError{Status: resp.StatusCode, Msg: e.Error} |
| 71 | } |
| 72 | return resp, nil |
| 73 | } |
| 74 | |
| 75 | // HTTPError is een foutantwoord van BlueGit. |
| 76 | type HTTPError struct { |
| 77 | Status int |
| 78 | Msg string |
| 79 | } |
| 80 | |
| 81 | func (e *HTTPError) Error() string { return fmt.Sprintf("BlueGit HTTP %d: %s", e.Status, e.Msg) } |
| 82 | |
| 83 | func (r *Remote) getJSON(ctx context.Context, p string, out any) error { |
| 84 | resp, err := r.req(ctx, http.MethodGet, p, nil) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | defer resp.Body.Close() |
| 89 | return json.NewDecoder(resp.Body).Decode(out) |
| 90 | } |
| 91 | |
| 92 | // Info vraagt het instance-id op en controleert zo ook de key. |
| 93 | func (r *Remote) Info(ctx context.Context) (protocol.Info, error) { |
| 94 | var out protocol.Info |
| 95 | err := r.getJSON(ctx, "/api/v1/backup/info", &out) |
| 96 | return out, err |
| 97 | } |
| 98 | |
| 99 | // Connect vertelt BlueGit waar deze server bereikbaar is. |
| 100 | func (r *Remote) Connect(ctx context.Context, publicURL, serverID string) error { |
| 101 | b, _ := json.Marshal(map[string]string{"server_url": publicURL, "server_id": serverID}) |
| 102 | resp, err := r.req(ctx, http.MethodPost, "/api/v1/backup/connect", bytes.NewReader(b)) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | resp.Body.Close() |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | // Manifest vraagt de volledige lijst op. BlueGit maakt daarbij een nieuwe |
| 111 | // databasekopie. |
| 112 | func (r *Remote) Manifest(ctx context.Context) (protocol.Manifest, error) { |
| 113 | var out protocol.Manifest |
| 114 | err := r.getJSON(ctx, "/api/v1/backup/manifest", &out) |
| 115 | return out, err |
| 116 | } |
| 117 | |
| 118 | // Fetch opent één bestand, begrensd door de limiter. |
| 119 | func (r *Remote) Fetch(ctx context.Context, logical string) (io.ReadCloser, error) { |
| 120 | resp, err := r.req(ctx, http.MethodGet, "/api/v1/backup/file?path="+url.QueryEscape(logical), nil) |
| 121 | if err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | return struct { |
| 125 | io.Reader |
| 126 | io.Closer |
| 127 | }{r.Limiter.Reader(ctx, resp.Body), resp.Body}, nil |
| 128 | } |
| 129 | |
| 130 | // Restore stuurt een tar.gz naar BlueGit. BlueGit herstart daarna. |
| 131 | func (r *Remote) Restore(ctx context.Context, body io.Reader) error { |
| 132 | resp, err := r.req(ctx, http.MethodPost, "/api/v1/backup/restore", body) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | resp.Body.Close() |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | // WaitUp wacht tot BlueGit na een herstart weer antwoordt. |
| 141 | func (r *Remote) WaitUp(ctx context.Context, timeout time.Duration) error { |
| 142 | deadline := time.Now().Add(timeout) |
| 143 | // Eerst kort wachten: BlueGit herstart pas na zijn 202. |
| 144 | time.Sleep(500 * time.Millisecond) |
| 145 | for { |
| 146 | c, cancel := context.WithTimeout(ctx, 3*time.Second) |
| 147 | _, err := r.Info(c) |
| 148 | cancel() |
| 149 | if err == nil { |
| 150 | return nil |
| 151 | } |
| 152 | if time.Now().After(deadline) { |
| 153 | return fmt.Errorf("BlueGit kwam niet terug: %w", err) |
| 154 | } |
| 155 | select { |
| 156 | case <-ctx.Done(): |
| 157 | return ctx.Err() |
| 158 | case <-time.After(300 * time.Millisecond): |
| 159 | } |
| 160 | } |
| 161 | } |