Mise a jour automatique programmee (crontab) par stack

- planification crontab par stack (autoupdate.json), incompatible avec l'exclusion
- scheduler a la minute ; pull puis down + up -d seulement si une image a change (mode autoapply)
- API PUT .../targets/{t}/auto et GET /api/cron (apercu des prochaines executions)
- interface : option dans le detail, badge sarcelle, vue de suivi "Mises a jour auto"
- TZ dans docker-compose / .env.example (tzdata embarque)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNfwm6UCYFyvNQNSF5i9Gy
This commit is contained in:
claude BotandClaude Opus 5.5 committed 2026-11-23 10:12:12 +01:00
1 parent 54cbab17cb
commit 51691d7b8a
16 files changed
+1010 -7

No files matched your search

+5
View File
@@ -11,6 +11,8 @@
// POST /api/servers/{id}/test (administrateur) test de la connexion SSH, de docker et de docker compose
// POST /api/servers/{id}/targets/{t}/apply {"dir"} mise à jour d'une stack : pull, down, up -d (administrateur)
// PUT /api/servers/{id}/targets/{t}/exclude {"dir","excluded"} exclut une stack des mises à jour ou la réactive (administrateur)
// PUT /api/servers/{id}/targets/{t}/auto {"dir","schedule"} programme (crontab) ou desactive ("") la mise a jour auto d'une stack (administrateur)
// GET /api/cron?expr=... validation d'une planification et prochaines executions
// POST /api/account/password, PATCH /api/account compte local
// GET|POST /api/admin/users, PATCH|DELETE /api/admin/users/{id} comptes (administrateur)
package api
@@ -98,6 +100,8 @@ func (h *Handler) Register(mux *http.ServeMux) {
mux.HandleFunc("POST /api/servers/{id}/test", h.testServer)
mux.HandleFunc("POST /api/servers/{id}/targets/{target}/apply", h.apply)
mux.HandleFunc("PUT /api/servers/{id}/targets/{target}/exclude", h.exclude)
mux.HandleFunc("PUT /api/servers/{id}/targets/{target}/auto", h.autoUpdate)
mux.HandleFunc("GET /api/cron", h.cronPreview)
if h.Accounts != nil {
mux.HandleFunc("POST /api/account/password", h.changePassword)
mux.HandleFunc("PATCH /api/account", h.patchAccount)
@@ -155,6 +159,7 @@ func (h *Handler) getFleet(w http.ResponseWriter, r *http.Request) {
"servers": h.Fleet.Snapshot(),
"summary": h.Fleet.Summary(),
"interval": int(h.Interval.Seconds()),
"tz": tzName(),
}
if h.isAdmin(r) {
resp["publicKey"] = h.PublicKey
+58
View File
@@ -0,0 +1,58 @@
package api
import (
"log/slog"
"net/http"
"os"
"time"
"github.com/vblogio/stackwatch/internal/fleet"
)
// tzName : fuseau dans lequel les planifications sont evaluees.
func tzName() string {
if tz := os.Getenv("TZ"); tz != "" {
return tz
}
name, _ := time.Now().Zone()
return name
}
// autoUpdate programme (schedule) ou desactive (schedule vide) la mise a jour
// automatique d'une stack.
func (h *Handler) autoUpdate(w http.ResponseWriter, r *http.Request) {
if !h.admin(w, r) {
return
}
var req struct {
Dir string `json:"dir"`
Schedule string `json:"schedule"`
}
if !h.decode(w, r, &req) {
return
}
if err := h.Fleet.SetAuto(r.PathValue("id"), r.PathValue("target"), req.Dir, req.Schedule, UserID(r)); err != nil {
fail(w, err)
return
}
slog.Info("mise a jour automatique", "server", r.PathValue("id"), "target", r.PathValue("target"), "dir", req.Dir, "schedule", req.Schedule, "by", UserID(r))
writeJSON(w, http.StatusOK, map[string]string{"schedule": req.Schedule})
}
// cronPreview valide une planification et renvoie ses prochaines executions.
func (h *Handler) cronPreview(w http.ResponseWriter, r *http.Request) {
c, err := fleet.ParseCron(r.URL.Query().Get("expr"))
if err != nil {
fail(w, err)
return
}
next := []time.Time{}
t := time.Now()
for range 3 {
if t = c.Next(t); t.IsZero() {
break
}
next = append(next, t)
}
writeJSON(w, http.StatusOK, map[string]any{"next": next, "tz": tzName()})
}