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:
1 parent
54cbab17cb
commit
51691d7b8a
16 files changed
+1010
-7
No files matched your search
@@ -0,0 +1,158 @@
|
||||
package fleet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
_ "time/tzdata" // fuseaux horaires embarques : TZ=Europe/Paris fonctionne sans tzdata dans l'image
|
||||
)
|
||||
|
||||
// Cron : planification au format crontab (5 champs : minute heure jour mois
|
||||
// jour-de-semaine), evaluee dans le fuseau du conteneur (variable TZ).
|
||||
// Acceptes : *, listes (1,15), intervalles (1-5), pas (*/15, 0-30/10), noms
|
||||
// anglais (jan..dec, sun..sat) et raccourcis @hourly, @daily, @weekly,
|
||||
// @monthly, @yearly. Comme cron, si le jour du mois ET le jour de semaine sont
|
||||
// restreints, l'un ou l'autre suffit.
|
||||
type Cron struct {
|
||||
min, hour, dom, month, dow uint64 // bits autorises
|
||||
domStar, dowStar bool
|
||||
}
|
||||
|
||||
var cronMacros = map[string]string{
|
||||
"@hourly": "0 * * * *",
|
||||
"@daily": "0 0 * * *",
|
||||
"@midnight": "0 0 * * *",
|
||||
"@weekly": "0 0 * * 0",
|
||||
"@monthly": "0 0 1 * *",
|
||||
"@yearly": "0 0 1 1 *",
|
||||
"@annually": "0 0 1 1 *",
|
||||
}
|
||||
|
||||
var monthNames = map[string]int{"jan": 1, "feb": 2, "mar": 3, "apr": 4, "may": 5, "jun": 6, "jul": 7, "aug": 8, "sep": 9, "oct": 10, "nov": 11, "dec": 12}
|
||||
var dowNames = map[string]int{"sun": 0, "mon": 1, "tue": 2, "wed": 3, "thu": 4, "fri": 5, "sat": 6}
|
||||
|
||||
// ParseCron analyse une expression crontab.
|
||||
func ParseCron(expr string) (*Cron, error) {
|
||||
expr = strings.TrimSpace(expr)
|
||||
if m, ok := cronMacros[strings.ToLower(expr)]; ok {
|
||||
expr = m
|
||||
}
|
||||
f := strings.Fields(expr)
|
||||
if len(f) != 5 {
|
||||
return nil, fmt.Errorf("%w : planification %q (5 champs attendus : minute heure jour mois jour-semaine)", ErrInvalid, expr)
|
||||
}
|
||||
c := &Cron{}
|
||||
var err error
|
||||
if c.min, _, err = cronField(f[0], 0, 59, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.hour, _, err = cronField(f[1], 0, 23, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.dom, c.domStar, err = cronField(f[2], 1, 31, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.month, _, err = cronField(f[3], 1, 12, monthNames); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.dow, c.dowStar, err = cronField(f[4], 0, 7, dowNames); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.dow&(1<<7) != 0 { // 7 = dimanche
|
||||
c.dow |= 1
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func cronField(s string, lo, hi int, names map[string]int) (bits uint64, star bool, err error) {
|
||||
bad := func() (uint64, bool, error) {
|
||||
return 0, false, fmt.Errorf("%w : champ cron %q (valeurs %d a %d)", ErrInvalid, s, lo, hi)
|
||||
}
|
||||
num := func(v string) (int, bool) {
|
||||
if n, ok := names[strings.ToLower(v)]; ok {
|
||||
return n, true
|
||||
}
|
||||
n, e := strconv.Atoi(v)
|
||||
return n, e == nil && n >= lo && n <= hi
|
||||
}
|
||||
star = s == "*" || strings.HasPrefix(s, "*/")
|
||||
for _, part := range strings.Split(s, ",") {
|
||||
rng, step := part, 1
|
||||
if i := strings.IndexByte(part, '/'); i >= 0 {
|
||||
n, e := strconv.Atoi(part[i+1:])
|
||||
if e != nil || n < 1 || n > hi {
|
||||
return bad()
|
||||
}
|
||||
rng, step = part[:i], n
|
||||
}
|
||||
var a, b int
|
||||
switch {
|
||||
case rng == "*":
|
||||
a, b = lo, hi
|
||||
case strings.Contains(rng, "-"):
|
||||
x, y, _ := strings.Cut(rng, "-")
|
||||
var ok1, ok2 bool
|
||||
if a, ok1 = num(x); !ok1 {
|
||||
return bad()
|
||||
}
|
||||
if b, ok2 = num(y); !ok2 || b < a {
|
||||
return bad()
|
||||
}
|
||||
default:
|
||||
var ok bool
|
||||
if a, ok = num(rng); !ok {
|
||||
return bad()
|
||||
}
|
||||
b = a
|
||||
if step > 1 { // "5/15" : de 5 jusqu'a la fin
|
||||
b = hi
|
||||
}
|
||||
}
|
||||
for v := a; v <= b; v += step {
|
||||
bits |= 1 << uint(v)
|
||||
}
|
||||
}
|
||||
if bits == 0 {
|
||||
return bad()
|
||||
}
|
||||
return bits, star, nil
|
||||
}
|
||||
|
||||
func (c *Cron) dayOK(t time.Time) bool {
|
||||
d := c.dom&(1<<uint(t.Day())) != 0
|
||||
w := c.dow&(1<<uint(t.Weekday())) != 0
|
||||
if c.domStar || c.dowStar {
|
||||
return d && w
|
||||
}
|
||||
return d || w
|
||||
}
|
||||
|
||||
// Match indique si la minute de t est planifiee.
|
||||
func (c *Cron) Match(t time.Time) bool {
|
||||
return c.min&(1<<uint(t.Minute())) != 0 && c.hour&(1<<uint(t.Hour())) != 0 &&
|
||||
c.month&(1<<uint(t.Month())) != 0 && c.dayOK(t)
|
||||
}
|
||||
|
||||
// Next renvoie la premiere minute planifiee strictement apres t (zero si
|
||||
// aucune dans les 5 ans, ex. 30 fevrier).
|
||||
func (c *Cron) Next(t time.Time) time.Time {
|
||||
t = t.Truncate(time.Minute).Add(time.Minute)
|
||||
limit := t.AddDate(5, 0, 0)
|
||||
for t.Before(limit) {
|
||||
loc := t.Location()
|
||||
switch {
|
||||
case c.month&(1<<uint(t.Month())) == 0:
|
||||
t = time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, loc)
|
||||
case !c.dayOK(t):
|
||||
t = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, loc)
|
||||
case c.hour&(1<<uint(t.Hour())) == 0:
|
||||
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+1, 0, 0, 0, loc)
|
||||
case c.min&(1<<uint(t.Minute())) == 0:
|
||||
t = t.Add(time.Minute)
|
||||
default:
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
Reference in new issue
Block a user