Files
restic-manager/internal/server/http/job_download.go
T
steve a781e95c94 P3 follow-up: editable target dir, conditional --no-ownership, UK lint
Three small follow-ups from review:

1. Restore target is now operator-editable. Default value is the
   literal '\$HOME/rm-restore/<job-id>/' (agent expands \$HOME at
   run time using os.UserHomeDir(); also handles \${HOME} and ~/
   prefixes). Operator can replace with any absolute path.
   - ui_restore.go validates the input is either absolute or starts
     with one of the recognised prefixes; other env-var refs (\$PATH
     etc.) are deliberately rejected so operator paths can't pick up
     arbitrary agent env values.
   - host_restore.html replaces the read-only mono-text display with
     a real <input>; help text spells out that \$HOME resolves
     agent-side and <job-id> is substituted on dispatch.
   - install.sh + the systemd unit prep /root/rm-restore so the
     default works under the sandbox: ReadWritePaths gains a soft
     '-/root/rm-restore' entry (the '-' makes the bind-mount soft-fail
     if missing, but install.sh pre-creates it root-owned 0700).

2. --no-ownership flag now gated on restic version. The flag was
   added in restic 0.17 and 0.16 rejects it. Previously dropped it
   wholesale — that meant new-dir restores silently preserved
   ownership against design intent on 0.17+. Now the agent threads
   its detected restic version (sysinfo already collects it) through
   runner.Config -> restic.Env, and RunRestore appends --no-ownership
   only when AtLeastVersion(0, 17) returns true. 0.16 hosts still
   restore with original uid/gid; help text in the wizard explicitly
   notes this. The previous 'Original ownership is preserved' copy
   was wrong for new-dir mode and is corrected.

3. golangci-lint misspell locale switched US -> UK and the codebase
   swept (73 corrections, mostly behaviour/serialise/recognise/honour).
   Wire-format ErrorCode 'unauthorized' -> 'unauthorised' is a tiny
   contract change but the agent doesn't parse those codes today and
   no external API consumers exist yet. Tests passed before + after.

Tests:
- internal/restic/version_test.go covers Env.AtLeastVersion across
  edge cases (empty, exact match, patch above, minor below, non-
  numeric) and expandHome on \$HOME / \${HOME} / ~/, plus
  pass-through for absolute paths and refusal of other env vars.
- ui_restore_test updated: TargetDir now starts '\$HOME/rm-restore/'
  with the job_id substituted into the placeholder.

Live verified on the smoke env: default target restored to
/root/rm-restore/<job-id>/ as the agent's expanded \$HOME (2 files,
14 bytes); custom override '/tmp/custom-restore/<job-id>/' restored
into the agent's PrivateTmp namespace (1 file, 6 bytes); both jobs
'succeeded', exit 0.
2026-05-04 17:27:52 +01:00

136 lines
4.0 KiB
Go

package http
import (
"bufio"
"encoding/json"
"fmt"
stdhttp "net/http"
"strings"
"github.com/go-chi/chi/v5"
"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
)
// handleJobLogDownload is GET /api/jobs/{id}/log{.txt,.ndjson}.
//
// Source of truth is the persisted job_logs table — works any time,
// regardless of whether the job is running or already finished. The
// download is "everything the server has up to right now"; the live
// stream is unaffected (no pause needed). If the operator wants a
// fuller snapshot of a still-running job, they hit Download again.
//
// Format is picked from the URL suffix (.txt | .ndjson) for a
// sensible filename in the browser, or the ?format= query param for
// REST callers. Default is txt.
func (s *Server) handleJobLogDownload(w stdhttp.ResponseWriter, r *stdhttp.Request) {
if _, ok := s.requireUser(r); !ok {
writeJSONError(w, stdhttp.StatusUnauthorized, "unauthorised", "")
return
}
jobID := chi.URLParam(r, "id")
if jobID == "" {
writeJSONError(w, stdhttp.StatusBadRequest, "missing_job_id", "")
return
}
job, err := s.deps.Store.GetJob(r.Context(), jobID)
if err != nil {
writeJSONError(w, stdhttp.StatusNotFound, "job_not_found", "")
return
}
format := r.URL.Query().Get("format")
if format == "" {
// Sniff the URL — chi routes both /log.txt and /log.ndjson here
// (or .log if a future route adds it) via the {format} matcher.
fmtParam := chi.URLParam(r, "format")
switch fmtParam {
case "ndjson":
format = "ndjson"
default:
format = "txt"
}
}
logs, err := s.deps.Store.ListJobLogs(r.Context(), jobID, 0, 0)
if err != nil {
writeJSONError(w, stdhttp.StatusInternalServerError, "internal", err.Error())
return
}
short := jobID
if len(short) > 8 {
short = short[:8]
}
filename := "job-" + job.Kind + "-" + short
switch format {
case "ndjson":
w.Header().Set("Content-Type", "application/x-ndjson; charset=utf-8")
w.Header().Set("Content-Disposition",
`attachment; filename="`+filename+`.ndjson"`)
writeLogsNDJSON(w, logs)
default:
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Content-Disposition",
`attachment; filename="`+filename+`.txt"`)
writeLogsText(w, job, logs)
}
}
// writeLogsText renders the logs in the same shape the live page shows:
// "HH:MM:SS.mmm TAG payload". Adds a small header so the file is
// useful as a standalone artefact (operator pastes it into a ticket).
func writeLogsText(w stdhttp.ResponseWriter, job *store.Job, logs []store.JobLogLine) {
bw := bufio.NewWriter(w)
defer func() { _ = bw.Flush() }()
_, _ = fmt.Fprintf(bw, "# job %s · kind %s · status %s\n",
job.ID, job.Kind, job.Status)
if job.StartedAt != nil {
_, _ = fmt.Fprintf(bw, "# started %s\n", job.StartedAt.UTC().Format("2006-01-02T15:04:05.000Z"))
}
if job.FinishedAt != nil {
_, _ = fmt.Fprintf(bw, "# finished %s\n", job.FinishedAt.UTC().Format("2006-01-02T15:04:05.000Z"))
}
_, _ = fmt.Fprintf(bw, "# %d log lines\n\n", len(logs))
for _, l := range logs {
tag := streamTag(l.Stream)
ts := l.TS.UTC().Format("15:04:05.000")
// Strip embedded newlines from payload — log lines should be
// single-line, but defensive: a stray '\n' in stderr would
// break grep -n.
payload := strings.ReplaceAll(l.Payload, "\n", " ")
_, _ = fmt.Fprintf(bw, "%s %s %s\n", ts, tag, payload)
}
}
// writeLogsNDJSON emits one JSON object per line. Each object stands
// alone — appending to the file remains valid NDJSON.
func writeLogsNDJSON(w stdhttp.ResponseWriter, logs []store.JobLogLine) {
enc := json.NewEncoder(w)
for _, l := range logs {
_ = enc.Encode(struct {
Seq int64 `json:"seq"`
TS string `json:"ts"`
Stream string `json:"stream"`
Payload string `json:"payload"`
}{
Seq: l.Seq,
TS: l.TS.UTC().Format("2006-01-02T15:04:05.000Z"),
Stream: l.Stream,
Payload: l.Payload,
})
}
}
func streamTag(s string) string {
switch s {
case "stdout":
return "OUT"
case "stderr":
return "ERR"
case "event":
return "EVENT"
}
return strings.ToUpper(s)
}