ec0bf0f6c3
Schedules CRUD now takes {cron, enabled, source_group_ids[]} with cron
parsed via robfig/cron/v3 and group membership scoped to the host.
New source-groups CRUD lives at /api/hosts/{id}/source-groups; delete
refuses with 409 if any schedule still references the group, returning
the schedule list so the UI can prompt 'remove from these schedules
first.' Repo-maintenance GET/PUT manages forget/prune/check cadences
on host_repo_maintenance — no version bump, the server-side ticker
(P2R-06) drives execution.
Per-source-group Run-now (POST /hosts/{id}/source-groups/{gid}/run)
resolves the group's includes/excludes/retention/tag and dispatches a
backup command.run with the new structured CommandRunPayload fields
(Includes/Excludes/Tag). Old per-host /hosts/{id}/run-backup and
/hosts/{id}/init-repo return 410 Gone with a redirect message.
schedule_push.go is rebuilt: buildScheduleSetPayload assembles the
slim wire shape, pushScheduleSetOnConn ships it during the on-hello
window, pushScheduleSetAsync fires after every CRUD mutation, and
dispatchScheduledJob handles agent schedule.fire by iterating the
schedule's source groups and dispatching one backup per group with
actor_kind=schedule and scheduled_id pointing at the schedule.
Auto-init at first WS connect: when the host has repo creds bound and
no init job in its history, server dispatches restic init. Restic's
'config file already exists' soft-success means re-runs against an
existing repo no-op; we don't auto-retry on failure (operator triggers
re-init manually via the danger zone in P2R-09).
api.Schedule drops Kind/Paths/Excludes/Tags/RetentionPolicy/Manual etc.
in favour of {id, cron, enabled, source_groups: [...]}. The agent
scheduler stops checking sch.Manual; cmd/agent's backup dispatch reads
Includes/Excludes/Tag instead of Args.
Tests cover the new HTTP surface end-to-end: source-groups CRUD with
in-use refusal, schedule validation (bad cron / missing groups /
foreign group), repo-maintenance auto-seed and validation, the 410
route, and buildScheduleSetPayload's wire-shape correctness. Full
suite passes; smoke env exercises auto-init dispatch on hello,
async push after schedule create, and per-source-group Run-now
landing the right paths/excludes/tag at the agent.
160 lines
4.8 KiB
Go
160 lines
4.8 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
stdhttp "net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/oklog/ulid/v2"
|
|
|
|
"gitea.dcglab.co.uk/steve/restic-manager/internal/api"
|
|
"gitea.dcglab.co.uk/steve/restic-manager/internal/auth"
|
|
"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
|
|
)
|
|
|
|
// runNowRequest is the body of POST /api/hosts/:id/jobs.
|
|
type runNowRequest struct {
|
|
Kind api.JobKind `json:"kind"`
|
|
Args []string `json:"args,omitempty"` // restic CLI args (paths for backup, etc.)
|
|
}
|
|
|
|
type runNowResponse struct {
|
|
JobID string `json:"job_id"`
|
|
Status string `json:"status"` // "queued"
|
|
}
|
|
|
|
// handleRunNow dispatches a job to the named host. Authenticated;
|
|
// rejects if the host isn't connected (caller should retry once
|
|
// the agent comes back).
|
|
func (s *Server) handleRunNow(w stdhttp.ResponseWriter, r *stdhttp.Request) {
|
|
user, ok := s.requireUser(r)
|
|
if !ok {
|
|
writeJSONError(w, stdhttp.StatusUnauthorized, "unauthorized", "")
|
|
return
|
|
}
|
|
hostID := chi.URLParam(r, "id")
|
|
if hostID == "" {
|
|
writeJSONError(w, stdhttp.StatusBadRequest, "missing_host_id", "")
|
|
return
|
|
}
|
|
var req runNowRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeJSONError(w, stdhttp.StatusBadRequest, "invalid_json", err.Error())
|
|
return
|
|
}
|
|
|
|
res, status, code, msg := s.dispatchJob(r.Context(), user, hostID, req.Kind, req.Args)
|
|
if code != "" {
|
|
writeJSONError(w, status, code, msg)
|
|
return
|
|
}
|
|
writeJSON(w, stdhttp.StatusAccepted, res)
|
|
}
|
|
|
|
// dispatchJob is the common path for HTTP-driven job dispatch. It
|
|
// validates the kind, checks the host is online, persists the job
|
|
// row, and ships command.run over the WS. Returns:
|
|
// - res: the queued-job response (job_id + status)
|
|
// - status: HTTP status to return on failure (or 0 on success)
|
|
// - code, msg: error code/message for the wire (empty on success)
|
|
//
|
|
// JSON callers wrap with writeJSONError; HTML callers translate to
|
|
// flash banner + redirect.
|
|
func (s *Server) dispatchJob(ctx context.Context, user *store.User,
|
|
hostID string, kind api.JobKind, args []string,
|
|
) (res runNowResponse, status int, code, msg string) {
|
|
return s.dispatchJobWithPayload(ctx, user, hostID, kind, api.CommandRunPayload{
|
|
Kind: kind,
|
|
Args: args,
|
|
})
|
|
}
|
|
|
|
// dispatchJobWithPayload is dispatchJob's variant that lets callers
|
|
// fill in structured fields (Includes/Excludes/Tag/RetentionPolicy)
|
|
// — used by the per-source-group Run-now path. JobID is filled in
|
|
// here; callers leave it zero on the input payload.
|
|
func (s *Server) dispatchJobWithPayload(ctx context.Context, user *store.User,
|
|
hostID string, kind api.JobKind, payload api.CommandRunPayload,
|
|
) (res runNowResponse, status int, code, msg string) {
|
|
if !validJobKind(kind) {
|
|
return res, stdhttp.StatusBadRequest, "invalid_kind",
|
|
"kind must be one of backup|forget|prune|check|unlock"
|
|
}
|
|
host, err := s.deps.Store.GetHost(ctx, hostID)
|
|
if err != nil {
|
|
return res, stdhttp.StatusNotFound, "host_not_found", ""
|
|
}
|
|
if !s.deps.Hub.Connected(host.ID) {
|
|
return res, stdhttp.StatusServiceUnavailable, "host_offline",
|
|
"agent is not currently connected; try again when it reconnects"
|
|
}
|
|
|
|
jobID := ulid.Make().String()
|
|
now := time.Now().UTC()
|
|
var actorID *string
|
|
actor := "system"
|
|
if user != nil {
|
|
actor = "user"
|
|
actorID = &user.ID
|
|
}
|
|
if err := s.deps.Store.CreateJob(ctx, store.Job{
|
|
ID: jobID,
|
|
HostID: host.ID,
|
|
Kind: string(kind),
|
|
ActorKind: actor,
|
|
ActorID: actorID,
|
|
CreatedAt: now,
|
|
}); err != nil {
|
|
return res, stdhttp.StatusInternalServerError, "internal", ""
|
|
}
|
|
|
|
payload.JobID = jobID
|
|
payload.Kind = kind
|
|
env, err := api.Marshal(api.MsgCommandRun, jobID, payload)
|
|
if err != nil {
|
|
return res, stdhttp.StatusInternalServerError, "internal", ""
|
|
}
|
|
if err := s.deps.Hub.Send(ctx, host.ID, env); err != nil {
|
|
return res, stdhttp.StatusServiceUnavailable, "host_offline", err.Error()
|
|
}
|
|
|
|
_ = s.deps.Store.AppendAudit(ctx, store.AuditEntry{
|
|
ID: ulid.Make().String(),
|
|
UserID: actorID,
|
|
Actor: actor,
|
|
Action: "job.run_now",
|
|
TargetKind: ptr("job"),
|
|
TargetID: &jobID,
|
|
TS: now,
|
|
})
|
|
return runNowResponse{JobID: jobID, Status: "queued"}, 0, "", ""
|
|
}
|
|
|
|
// requireUser resolves the session cookie to a user row. Stub of the
|
|
// session-auth middleware that lands in P1-04's full pass.
|
|
func (s *Server) requireUser(r *stdhttp.Request) (*store.User, bool) {
|
|
c, err := r.Cookie(sessionCookieName)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
sess, err := s.deps.Store.LookupSession(r.Context(), auth.HashToken(c.Value))
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
u, err := s.deps.Store.GetUserByID(r.Context(), sess.UserID)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
return u, true
|
|
}
|
|
|
|
func validJobKind(k api.JobKind) bool {
|
|
switch k {
|
|
case api.JobBackup, api.JobInit, api.JobForget, api.JobPrune, api.JobCheck, api.JobUnlock:
|
|
return true
|
|
}
|
|
return false
|
|
}
|