feat: add auto update

This commit is contained in:
2026-05-19 15:53:30 +02:00
parent bd3121d688
commit ba4de62a34
22 changed files with 3323 additions and 110 deletions

View File

@ -49,6 +49,16 @@ func (s *Store) migrate() error {
last_seen_at DATETIME,
online INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS auto_update_policies (
agent_id TEXT NOT NULL,
container_id TEXT NOT NULL,
enabled INTEGER NOT NULL DEFAULT 1,
interval_minutes INTEGER NOT NULL DEFAULT 1440,
last_checked_at DATETIME,
last_updated_at DATETIME,
PRIMARY KEY (agent_id, container_id),
FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE
);
`)
if err != nil {
return err
@ -186,3 +196,106 @@ func boolToInt(b bool) int {
}
return 0
}
// ── AutoUpdatePolicies ────────────────────────────────────────────────────────
type AutoUpdatePolicy struct {
AgentID string
ContainerID string
Enabled bool
IntervalMinutes int
LastCheckedAt *time.Time
LastUpdatedAt *time.Time
}
func (s *Store) UpsertAutoUpdatePolicy(p *AutoUpdatePolicy) error {
_, err := s.db.Exec(`
INSERT OR REPLACE INTO auto_update_policies
(agent_id, container_id, enabled, interval_minutes, last_checked_at, last_updated_at)
VALUES (?, ?, ?, ?, ?, ?)
`, p.AgentID, p.ContainerID, boolToInt(p.Enabled), p.IntervalMinutes, p.LastCheckedAt, p.LastUpdatedAt)
return err
}
func (s *Store) GetAutoUpdatePolicy(agentID, containerID string) (*AutoUpdatePolicy, error) {
row := s.db.QueryRow(`
SELECT agent_id, container_id, enabled, interval_minutes, last_checked_at, last_updated_at
FROM auto_update_policies WHERE agent_id = ? AND container_id = ?
`, agentID, containerID)
p := &AutoUpdatePolicy{}
var enabled int
var lastChecked, lastUpdated sql.NullTime
err := row.Scan(&p.AgentID, &p.ContainerID, &enabled, &p.IntervalMinutes, &lastChecked, &lastUpdated)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
p.Enabled = enabled == 1
if lastChecked.Valid {
t := lastChecked.Time
p.LastCheckedAt = &t
}
if lastUpdated.Valid {
t := lastUpdated.Time
p.LastUpdatedAt = &t
}
return p, nil
}
func (s *Store) ListDueAutoUpdatePolicies(now time.Time) ([]*AutoUpdatePolicy, error) {
rows, err := s.db.Query(`
SELECT agent_id, container_id, enabled, interval_minutes, last_checked_at, last_updated_at
FROM auto_update_policies
WHERE enabled = 1
AND (last_checked_at IS NULL
OR (julianday(?) - julianday(last_checked_at)) * 1440 >= interval_minutes)
`, now)
if err != nil {
return nil, err
}
defer rows.Close()
var policies []*AutoUpdatePolicy
for rows.Next() {
p := &AutoUpdatePolicy{}
var enabled int
var lastChecked, lastUpdated sql.NullTime
if err := rows.Scan(&p.AgentID, &p.ContainerID, &enabled, &p.IntervalMinutes, &lastChecked, &lastUpdated); err != nil {
return nil, err
}
p.Enabled = enabled == 1
if lastChecked.Valid {
t := lastChecked.Time
p.LastCheckedAt = &t
}
if lastUpdated.Valid {
t := lastUpdated.Time
p.LastUpdatedAt = &t
}
policies = append(policies, p)
}
return policies, rows.Err()
}
func (s *Store) UpdateAutoUpdateChecked(agentID, containerID string, at time.Time) error {
_, err := s.db.Exec(`
UPDATE auto_update_policies SET last_checked_at = ? WHERE agent_id = ? AND container_id = ?
`, at, agentID, containerID)
return err
}
func (s *Store) UpdateAutoUpdateDone(agentID, containerID string, at time.Time) error {
_, err := s.db.Exec(`
UPDATE auto_update_policies SET last_updated_at = ? WHERE agent_id = ? AND container_id = ?
`, at, agentID, containerID)
return err
}
func (s *Store) DeleteAutoUpdatePolicy(agentID, containerID string) error {
_, err := s.db.Exec(`
DELETE FROM auto_update_policies WHERE agent_id = ? AND container_id = ?
`, agentID, containerID)
return err
}