e871b05b38
CI / Test (linux/amd64) (pull_request) Successful in 34s
CI / Lint (pull_request) Failing after 16s
CI / Build (windows/amd64) (pull_request) Successful in 22s
CI / Build (linux/amd64) (pull_request) Successful in 20s
CI / Build (linux/arm64) (pull_request) Successful in 21s
Cleanup pass over the repo so CI can enforce lint going forward
without the only-new-issues escape hatch:
* gofumpt -w across the tree (31 hits, all formatting)
* misspell --fix (25 hits, US-locale spelling) — but reverted on
api.JobCancelled = "cancelled" since that literal is the wire +
DB CHECK constraint value, plus matched the case in store/fleet.go
back to "cancelled" and added //nolint:misspell on both for the
next time someone reaches for the auto-fix
* Wrap every `defer rows.Close()` / `defer stmt.Close()` /
`defer res.Body.Close()` in `defer func() { _ = .Close() }()`
to satisfy errcheck without losing the close itself
* websocket.Dial callers (1 prod, 4 tests) now capture + close the
upgrade response Body — coder/websocket can return res with a nil
Body on success, so the test deferred-closes guard against that
* Annotate the two genuine-by-design nilerr cases with //nolint
comments explaining why nil-on-error is the contract (cookie
missing = no session; ctx cancelled mid-backoff = clean shutdown)
* Add brief godoc on the 10 exported const groups + types that
revive flagged (api.HostOS/HostArch/JobKind/JobStatus/LogStream/
ErrorCode, restic.EventKind, store.Role, web.FS)
* Drop the unused (*Server).userByID method
* Inline the unparam baseView(active) — every UI page is under
the dashboard primary nav today
Result: `golangci-lint run ./...` reports 0 issues. CI lint job
no longer needs only-new-issues: true; X-06 follow-up entry in
tasks.md removed.
145 lines
4.7 KiB
Go
145 lines
4.7 KiB
Go
// repo_maintenance.go — REST API for /api/hosts/{id}/repo-maintenance.
|
|
//
|
|
// Cadence rows for the three repo-wide verbs (forget / prune / check).
|
|
// Edits do NOT bump host_schedule_version: the server-side maintenance
|
|
// ticker drives execution (P2R-06), not the agent's local cron.
|
|
package http
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
stdhttp "net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
|
|
)
|
|
|
|
type repoMaintenanceView struct {
|
|
HostID string `json:"host_id"`
|
|
ForgetCron string `json:"forget_cron"`
|
|
ForgetEnabled bool `json:"forget_enabled"`
|
|
PruneCron string `json:"prune_cron"`
|
|
PruneEnabled bool `json:"prune_enabled"`
|
|
CheckCron string `json:"check_cron"`
|
|
CheckEnabled bool `json:"check_enabled"`
|
|
CheckSubsetPct int `json:"check_subset_pct"`
|
|
}
|
|
|
|
func toRepoMaintenanceView(m store.HostRepoMaintenance) repoMaintenanceView {
|
|
return repoMaintenanceView{
|
|
HostID: m.HostID,
|
|
ForgetCron: m.ForgetCron,
|
|
ForgetEnabled: m.ForgetEnabled,
|
|
PruneCron: m.PruneCron,
|
|
PruneEnabled: m.PruneEnabled,
|
|
CheckCron: m.CheckCron,
|
|
CheckEnabled: m.CheckEnabled,
|
|
CheckSubsetPct: m.CheckSubsetPct,
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleGetRepoMaintenance(w stdhttp.ResponseWriter, r *stdhttp.Request) {
|
|
if !s.authedUser(r) {
|
|
writeJSONError(w, stdhttp.StatusUnauthorized, "unauthorized", "")
|
|
return
|
|
}
|
|
hostID := chi.URLParam(r, "id")
|
|
if _, err := s.deps.Store.GetHost(r.Context(), hostID); err != nil {
|
|
writeJSONError(w, stdhttp.StatusNotFound, "host_not_found", "")
|
|
return
|
|
}
|
|
m, err := s.deps.Store.GetRepoMaintenance(r.Context(), hostID)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
// Self-heal: seed and return the defaults so the UI never
|
|
// has to handle a 404 here. Hosts enrolled before the
|
|
// migration may legitimately be missing the row.
|
|
if seedErr := s.deps.Store.CreateDefaultRepoMaintenance(r.Context(), hostID); seedErr != nil {
|
|
writeJSONError(w, stdhttp.StatusInternalServerError, "internal", "")
|
|
return
|
|
}
|
|
m, err = s.deps.Store.GetRepoMaintenance(r.Context(), hostID)
|
|
if err != nil {
|
|
writeJSONError(w, stdhttp.StatusInternalServerError, "internal", "")
|
|
return
|
|
}
|
|
} else {
|
|
writeJSONError(w, stdhttp.StatusInternalServerError, "internal", "")
|
|
return
|
|
}
|
|
}
|
|
writeJSON(w, stdhttp.StatusOK, toRepoMaintenanceView(*m))
|
|
}
|
|
|
|
type repoMaintenanceWriteRequest struct {
|
|
ForgetCron string `json:"forget_cron"`
|
|
ForgetEnabled bool `json:"forget_enabled"`
|
|
PruneCron string `json:"prune_cron"`
|
|
PruneEnabled bool `json:"prune_enabled"`
|
|
CheckCron string `json:"check_cron"`
|
|
CheckEnabled bool `json:"check_enabled"`
|
|
CheckSubsetPct int `json:"check_subset_pct"`
|
|
}
|
|
|
|
func (s *Server) handleUpdateRepoMaintenance(w stdhttp.ResponseWriter, r *stdhttp.Request) {
|
|
if !s.authedUser(r) {
|
|
writeJSONError(w, stdhttp.StatusUnauthorized, "unauthorized", "")
|
|
return
|
|
}
|
|
hostID := chi.URLParam(r, "id")
|
|
if _, err := s.deps.Store.GetHost(r.Context(), hostID); err != nil {
|
|
writeJSONError(w, stdhttp.StatusNotFound, "host_not_found", "")
|
|
return
|
|
}
|
|
var req repoMaintenanceWriteRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeJSONError(w, stdhttp.StatusBadRequest, "invalid_json", err.Error())
|
|
return
|
|
}
|
|
for label, expr := range map[string]string{
|
|
"forget_cron": req.ForgetCron,
|
|
"prune_cron": req.PruneCron,
|
|
"check_cron": req.CheckCron,
|
|
} {
|
|
if expr == "" {
|
|
writeJSONError(w, stdhttp.StatusBadRequest, "missing_field", label+" required")
|
|
return
|
|
}
|
|
if _, err := cronParser.Parse(expr); err != nil {
|
|
writeJSONError(w, stdhttp.StatusBadRequest, "invalid_cron", label+": "+err.Error())
|
|
return
|
|
}
|
|
}
|
|
if req.CheckSubsetPct < 0 || req.CheckSubsetPct > 100 {
|
|
writeJSONError(w, stdhttp.StatusBadRequest, "invalid_value",
|
|
"check_subset_pct must be 0..100")
|
|
return
|
|
}
|
|
// Ensure the row exists (older hosts may pre-date the auto-seed).
|
|
if err := s.deps.Store.CreateDefaultRepoMaintenance(r.Context(), hostID); err != nil {
|
|
writeJSONError(w, stdhttp.StatusInternalServerError, "internal", "")
|
|
return
|
|
}
|
|
m := store.HostRepoMaintenance{
|
|
HostID: hostID,
|
|
ForgetCron: req.ForgetCron,
|
|
ForgetEnabled: req.ForgetEnabled,
|
|
PruneCron: req.PruneCron,
|
|
PruneEnabled: req.PruneEnabled,
|
|
CheckCron: req.CheckCron,
|
|
CheckEnabled: req.CheckEnabled,
|
|
CheckSubsetPct: req.CheckSubsetPct,
|
|
}
|
|
if err := s.deps.Store.UpdateRepoMaintenance(r.Context(), &m); err != nil {
|
|
writeJSONError(w, stdhttp.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
out, _ := s.deps.Store.GetRepoMaintenance(r.Context(), hostID)
|
|
if out != nil {
|
|
writeJSON(w, stdhttp.StatusOK, toRepoMaintenanceView(*out))
|
|
return
|
|
}
|
|
writeJSON(w, stdhttp.StatusOK, toRepoMaintenanceView(m))
|
|
}
|