Merged
feat(update): geen self-update midden in pipelines
timfa
wants to merge
1 commit
from
cursor/update-gate
into
stage
Overview Commits 1 Pipelines 3 Changes 17
Zodra een self-update is aangevraagd (status waiting), starten jobs van pipelines die daarna zijn aangemaakt niet meer. UI, API en bg tonen ze als waiting_for_update / Wacht op update. Lopende pipelines maken af; de updater wacht al op running == 0. Na de herstart (status ok) of bij een mislukte update gaat de poort open en starten ze vanzelf.
Geen deadlock: de release-pipeline is ouder dan de aanvraag, en self_update is de laatste job en stopt direct na de POST.
Veiligheidskleppen:
ci.update_max_wait_minutes(standaard 30): daarna gaat de poort open.- Forceren:
POST /api/v1/admin/self-update/force(beheerder), of{"force":true}met het update-token. De updater (bluegit-update.sh) zietforcein de status-API en installeert zonder te wachten.
Een tweede aanvraag terwijl er al een update wacht geeft 409 (met force: 202).
Tests: update_gate_test.go (vasthouden, geen blokkade van de oudere pipeline, API-status, 409/force, poort open na ok, time-out) en runner/hold_test.go.
Activity
- timfa opened this merge request 24 October 2026
- timfa Pipeline #188 is Passed 24 October 2026
- timfa Pipeline #190 is Passed 24 October 2026
- timfa Pipeline #191 is Passed 24 October 2026
-
timfa
pushed
24 October 2026
feat(update): self-update wacht op lopende jobs en houdt nieuwe pipelines vast (waiting_for_update), met time-out en force Merge branch 'cursor/mobile-ui' into 'stage' fix(ui): mobiel geen horizontale overflow (commitbalk wrapt, tabellen/code scrollen binnen de README)
- timfa merged this merge request 24 October 2026
Commits
Pipelines
Changes
@@ -123,6 +123,7 @@ func serve(args []string) error {
Timeout: time.Duration(cfg.CI.TimeoutSec) * time.Second,
Log: log, OnEvent: srv.Dispatch, Presets: cfg.CI.ImageOverrides(),
}
rn.Hold = srv.HoldPipeline
srv.Runner = rn
go rn.Loop(ctx, cfg.CI.Concurrency)
}
@@ -123,6 +123,7 @@ func serve(args []string) error { Timeout: time.Duration(cfg.CI.TimeoutSec) * time.Second, Timeout: time.Duration(cfg.CI.TimeoutSec) * time.Second, Log: log, OnEvent: srv.Dispatch, Presets: cfg.CI.ImageOverrides(), Log: log, OnEvent: srv.Dispatch, Presets: cfg.CI.ImageOverrides(), } } rn.Hold = srv.HoldPipeline srv.Runner = rn srv.Runner = rn go rn.Loop(ctx, cfg.CI.Concurrency) go rn.Loop(ctx, cfg.CI.Concurrency) } }@@ -88,3 +88,12 @@ De CLI is dun. Alles staat ook in `/api/v1/openapi.json`. Handige filters:
- `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`
Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.
## Self-update en wachtende pipelines
Staat er een self-update klaar, dan starten nieuwe pipelines niet. Ze hebben dan status `waiting_for_update` (in de UI "Wacht op update"), en `bg pipeline wait` blijft gewoon wachten. Lopende pipelines mogen afmaken. Daarna installeert de updater, en na de herstart starten de wachtende pipelines vanzelf.
- **Maximale wachttijd:** `ci.update_max_wait_minutes` (standaard 30). Is die verstreken, dan gaat de poort weer open.
- **Forceren** (installeren zonder te wachten):
- als beheerder: `POST /api/v1/admin/self-update/force`
- met het update-token: `POST /api/v1/admin/self-update` met `{"force":true}`
@@ -88,3 +88,12 @@ De CLI is dun. Alles staat ook in `/api/v1/openapi.json`. Handige filters:- `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`- `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.## Self-update en wachtende pipelinesStaat er een self-update klaar, dan starten nieuwe pipelines niet. Ze hebben dan status `waiting_for_update` (in de UI "Wacht op update"), en `bg pipeline wait` blijft gewoon wachten. Lopende pipelines mogen afmaken. Daarna installeert de updater, en na de herstart starten de wachtende pipelines vanzelf.- **Maximale wachttijd:** `ci.update_max_wait_minutes` (standaard 30). Is die verstreken, dan gaat de poort weer open.- **Forceren** (installeren zonder te wachten): - als beheerder: `POST /api/v1/admin/self-update/force` - met het update-token: `POST /api/v1/admin/self-update` met `{"force":true}`@@ -79,12 +79,15 @@ type Runner struct {
// CI stuurt de Docker-runner.
type CI struct {
Enabled bool `yaml:"enabled"`
Docker string `yaml:"docker_binary"`
Concurrency int `yaml:"concurrency"`
Network string `yaml:"network"`
TimeoutSec int `yaml:"timeout_seconds"`
Presets map[string]PresetConfig `yaml:"presets"`
Enabled bool `yaml:"enabled"`
Docker string `yaml:"docker_binary"`
Concurrency int `yaml:"concurrency"`
Network string `yaml:"network"`
TimeoutSec int `yaml:"timeout_seconds"`
// UpdateMaxWaitMin: zo lang wacht een self-update op lopende jobs
// (en houdt nieuwe pipelines vast). Standaard 30.
UpdateMaxWaitMin int `yaml:"update_max_wait_minutes"`
Presets map[string]PresetConfig `yaml:"presets"`
}
// PresetConfig is één image-alias. image mapt direct. dockerfile wordt
@@ -79,12 +79,15 @@ type Runner struct {// CI stuurt de Docker-runner.// CI stuurt de Docker-runner.type CI struct {type CI struct { Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"` Docker string `yaml:"docker_binary"` Docker string `yaml:"docker_binary"` Concurrency int `yaml:"concurrency"` Concurrency int `yaml:"concurrency"` Network string `yaml:"network"` Network string `yaml:"network"` TimeoutSec int `yaml:"timeout_seconds"` TimeoutSec int `yaml:"timeout_seconds"` Presets map[string]PresetConfig `yaml:"presets"` // UpdateMaxWaitMin: zo lang wacht een self-update op lopende jobs // (en houdt nieuwe pipelines vast). Standaard 30. UpdateMaxWaitMin int `yaml:"update_max_wait_minutes"` Presets map[string]PresetConfig `yaml:"presets"`}}// PresetConfig is één image-alias. image mapt direct. dockerfile wordt// PresetConfig is één image-alias. image mapt direct. dockerfile wordt@@ -70,3 +70,10 @@ var cacheFR = map[string]string{
"err.cacheInside": "Le chemin du cache ne peut pas se trouver dans le dossier de stockage ou d'état.",
"err.cacheDir": "Le dossier du cache n'est pas accessible en écriture : %s",
}
func init() {
addCatalog(en, map[string]string{"pipe.waiting_for_update": "Waiting for update", "err.noUpdatePending": "No self-update is pending."})
addCatalog(nl, map[string]string{"pipe.waiting_for_update": "Wacht op update", "err.noUpdatePending": "Er staat geen self-update klaar."})
addCatalog(de, map[string]string{"pipe.waiting_for_update": "Wartet auf Update", "err.noUpdatePending": "Es steht kein Self-Update an."})
addCatalog(fr, map[string]string{"pipe.waiting_for_update": "En attente de mise à jour", "err.noUpdatePending": "Aucune mise à jour en attente."})
}
@@ -70,3 +70,10 @@ var cacheFR = map[string]string{ "err.cacheInside": "Le chemin du cache ne peut pas se trouver dans le dossier de stockage ou d'état.", "err.cacheInside": "Le chemin du cache ne peut pas se trouver dans le dossier de stockage ou d'état.", "err.cacheDir": "Le dossier du cache n'est pas accessible en écriture : %s", "err.cacheDir": "Le dossier du cache n'est pas accessible en écriture : %s",}}func init() { addCatalog(en, map[string]string{"pipe.waiting_for_update": "Waiting for update", "err.noUpdatePending": "No self-update is pending."}) addCatalog(nl, map[string]string{"pipe.waiting_for_update": "Wacht op update", "err.noUpdatePending": "Er staat geen self-update klaar."}) addCatalog(de, map[string]string{"pipe.waiting_for_update": "Wartet auf Update", "err.noUpdatePending": "Es steht kein Self-Update an."}) addCatalog(fr, map[string]string{"pipe.waiting_for_update": "En attente de mise à jour", "err.noUpdatePending": "Aucune mise à jour en attente."})}@@ -0,0 +1,39 @@
package runner
import (
"context"
"io"
"log/slog"
"path/filepath"
"testing"
"gitlab.com/Timfa/bluegit/internal/store"
)
func TestHoldKeepsJobsQueued(t *testing.T) {
dir := t.TempDir()
s, err := store.Open(filepath.Join(dir, "b.db"), make([]byte, 32))
if err != nil {
t.Fatal(err)
}
defer s.Close()
ctx := context.Background()
tim, _ := s.UpsertUser(ctx, 1, "tim", "", false)
repo, err := s.CreateRepo(ctx, "user", tim.ID, "app", "", "master", "public")
if err != nil {
t.Fatal(err)
}
id, err := s.CreatePipeline(ctx, repo.ID, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "refs/heads/master", "push", "tim", "x", []store.NewJob{{Name: "build", Stage: "build", Script: "true"}})
if err != nil {
t.Fatal(err)
}
held := 0
r := &Runner{Store: s, Storage: dir, Docker: "/nonexistent/docker", GitBin: "git", Log: slog.New(slog.NewTextHandler(io.Discard, nil)),
Hold: func(p store.Pipeline) bool { held++; return p.ID == id }}
r.slots = make(chan struct{}, 1)
r.Tick(ctx)
jobs, _ := s.JobsByPipeline(ctx, id)
if held == 0 || jobs[0].Status != "queued" {
t.Fatalf("job niet vastgehouden: held=%d status=%s", held, jobs[0].Status)
}
}
@@ -0,0 +1,39 @@package runnerimport ( "context" "io" "log/slog" "path/filepath" "testing" "gitlab.com/Timfa/bluegit/internal/store")func TestHoldKeepsJobsQueued(t *testing.T) { dir := t.TempDir() s, err := store.Open(filepath.Join(dir, "b.db"), make([]byte, 32)) if err != nil { t.Fatal(err) } defer s.Close() ctx := context.Background() tim, _ := s.UpsertUser(ctx, 1, "tim", "", false) repo, err := s.CreateRepo(ctx, "user", tim.ID, "app", "", "master", "public") if err != nil { t.Fatal(err) } id, err := s.CreatePipeline(ctx, repo.ID, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "refs/heads/master", "push", "tim", "x", []store.NewJob{{Name: "build", Stage: "build", Script: "true"}}) if err != nil { t.Fatal(err) } held := 0 r := &Runner{Store: s, Storage: dir, Docker: "/nonexistent/docker", GitBin: "git", Log: slog.New(slog.NewTextHandler(io.Discard, nil)), Hold: func(p store.Pipeline) bool { held++; return p.ID == id }} r.slots = make(chan struct{}, 1) r.Tick(ctx) jobs, _ := s.JobsByPipeline(ctx, id) if held == 0 || jobs[0].Status != "queued" { t.Fatalf("job niet vastgehouden: held=%d status=%s", held, jobs[0].Status) }}@@ -26,6 +26,8 @@ import (
// Runner plant jobs en stuurt het resultaat door naar webhooks.
type Runner struct {
// Hold houdt een pipeline vast, bijvoorbeeld tijdens een self-update.
Hold func(store.Pipeline) bool
Store *store.Store
GitBin string
Docker string
@@ -119,6 +121,9 @@ func (r *Runner) Tick(ctx context.Context) {
func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
r.settle(ctx, p.ID)
if r.Hold != nil && r.Hold(p) {
return // wacht op de self-update
}
jobs, err := r.Store.JobsByPipeline(ctx, p.ID)
if err != nil {
r.Log.Error("jobs", "err", err)
@@ -26,6 +26,8 @@ import (// Runner plant jobs en stuurt het resultaat door naar webhooks.// Runner plant jobs en stuurt het resultaat door naar webhooks.type Runner struct {type Runner struct { // Hold houdt een pipeline vast, bijvoorbeeld tijdens een self-update. Hold func(store.Pipeline) bool Store *store.Store Store *store.Store GitBin string GitBin string Docker string Docker string@@ -119,6 +121,9 @@ func (r *Runner) Tick(ctx context.Context) {func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {func (r *Runner) schedule(ctx context.Context, p store.Pipeline) { r.settle(ctx, p.ID) r.settle(ctx, p.ID) if r.Hold != nil && r.Hold(p) { return // wacht op de self-update } jobs, err := r.Store.JobsByPipeline(ctx, p.ID) jobs, err := r.Store.JobsByPipeline(ctx, p.ID) if err != nil { if err != nil { r.Log.Error("jobs", "err", err) r.Log.Error("jobs", "err", err)@@ -339,7 +339,7 @@ func (s *Server) apiSetMR(w http.ResponseWriter, r *http.Request, reopen bool) {
func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any {
return map[string]any{
"id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": p.Status, "source": p.Source,
"id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": s.pipeStatusFor(p), "source": p.Source,
"actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt),
"web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)),
}
@@ -339,7 +339,7 @@ func (s *Server) apiSetMR(w http.ResponseWriter, r *http.Request, reopen bool) {func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any {func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any { return map[string]any{ return map[string]any{ "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": p.Status, "source": p.Source, "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": s.pipeStatusFor(p), "source": p.Source, "actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt), "actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt), "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)), "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)), } }@@ -126,6 +126,7 @@ var apiCatalog = []apiRoute{
{http.MethodGet, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Read instance storage", (*Server).apiAdminStorage},
{http.MethodPut, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Set instance storage", (*Server).apiAdminStorage},
{http.MethodPost, "/api/v1/admin/self-update/force", access.ScopeAdmin, authUser, "Install the pending self-update now, without waiting for running jobs", (*Server).apiForceUpdate},
{http.MethodGet, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Read cache settings and usage", (*Server).apiAdminCache},
{http.MethodPut, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Set cache path", (*Server).apiAdminCache},
{http.MethodPost, "/api/v1/admin/cache/clear", access.ScopeAdmin, authUser, "Clear the read cache", (*Server).apiAdminCacheClear},
@@ -126,6 +126,7 @@ var apiCatalog = []apiRoute{ {http.MethodGet, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Read instance storage", (*Server).apiAdminStorage}, {http.MethodGet, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Read instance storage", (*Server).apiAdminStorage}, {http.MethodPut, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Set instance storage", (*Server).apiAdminStorage}, {http.MethodPut, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Set instance storage", (*Server).apiAdminStorage}, {http.MethodPost, "/api/v1/admin/self-update/force", access.ScopeAdmin, authUser, "Install the pending self-update now, without waiting for running jobs", (*Server).apiForceUpdate}, {http.MethodGet, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Read cache settings and usage", (*Server).apiAdminCache}, {http.MethodGet, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Read cache settings and usage", (*Server).apiAdminCache}, {http.MethodPut, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Set cache path", (*Server).apiAdminCache}, {http.MethodPut, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Set cache path", (*Server).apiAdminCache}, {http.MethodPost, "/api/v1/admin/cache/clear", access.ScopeAdmin, authUser, "Clear the read cache", (*Server).apiAdminCacheClear}, {http.MethodPost, "/api/v1/admin/cache/clear", access.ScopeAdmin, authUser, "Clear the read cache", (*Server).apiAdminCacheClear},@@ -240,7 +240,7 @@ func (s *Server) attachFlowPipe(r *http.Request, repo store.Repo, mr store.Merge
return
}
jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
st := displayStatus(p.Status, jobs)
st := displayStatus(s.pipeStatusFor(p), jobs)
row.PipeStatus = st
row.PipeLabel = s.tr(r, "pipe."+st)
row.PipeHref = "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)
@@ -240,7 +240,7 @@ func (s *Server) attachFlowPipe(r *http.Request, repo store.Repo, mr store.Merge return return } } jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) st := displayStatus(p.Status, jobs) st := displayStatus(s.pipeStatusFor(p), jobs) row.PipeStatus = st row.PipeStatus = st row.PipeLabel = s.tr(r, "pipe."+st) row.PipeLabel = s.tr(r, "pipe."+st) row.PipeHref = "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10) row.PipeHref = "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)@@ -155,7 +155,7 @@ func rowLive(row PipeRow) bool {
func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p store.Pipeline, users map[string]store.User, ranks map[int64]int) PipeRow {
jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
name, kind := ci.SplitRef(p.Ref)
status := displayStatus(p.Status, jobs)
status := displayStatus(s.pipeStatusFor(p), jobs)
actor, avatar, actorURL := s.actorOf(r, p.Actor, users)
initial := ""
if actor != "" {
@@ -264,6 +264,8 @@ func displayStatus(pipe string, jobs []store.Job) string {
return "pending"
case "canceled":
return "canceled"
case "waiting_for_update":
return "waiting_for_update"
}
warn := false
for _, j := range jobs {
@@ -548,7 +550,7 @@ func (s *Server) jobPage(r *http.Request, repo store.Repo, p store.Pipeline, job
}
name, kind := ci.SplitRef(p.Ref)
status := jobDisplay(job)
pipeStatus := displayStatus(p.Status, jobs)
pipeStatus := displayStatus(s.pipeStatusFor(p), jobs)
end := job.FinishedAt
if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") {
end = p.FinishedAt
@@ -155,7 +155,7 @@ func rowLive(row PipeRow) bool {func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p store.Pipeline, users map[string]store.User, ranks map[int64]int) PipeRow {func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p store.Pipeline, users map[string]store.User, ranks map[int64]int) PipeRow { jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) name, kind := ci.SplitRef(p.Ref) name, kind := ci.SplitRef(p.Ref) status := displayStatus(p.Status, jobs) status := displayStatus(s.pipeStatusFor(p), jobs) actor, avatar, actorURL := s.actorOf(r, p.Actor, users) actor, avatar, actorURL := s.actorOf(r, p.Actor, users) initial := "" initial := "" if actor != "" { if actor != "" {@@ -264,6 +264,8 @@ func displayStatus(pipe string, jobs []store.Job) string { return "pending" return "pending" case "canceled": case "canceled": return "canceled" return "canceled" case "waiting_for_update": return "waiting_for_update" } } warn := false warn := false for _, j := range jobs { for _, j := range jobs {@@ -548,7 +550,7 @@ func (s *Server) jobPage(r *http.Request, repo store.Repo, p store.Pipeline, job } } name, kind := ci.SplitRef(p.Ref) name, kind := ci.SplitRef(p.Ref) status := jobDisplay(job) status := jobDisplay(job) pipeStatus := displayStatus(p.Status, jobs) pipeStatus := displayStatus(s.pipeStatusFor(p), jobs) end := job.FinishedAt end := job.FinishedAt if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") { if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") { end = p.FinishedAt end = p.FinishedAt@@ -114,7 +114,7 @@ func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *C
return
}
jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
status := displayStatus(p.Status, jobs)
status := displayStatus(s.pipeStatusFor(p), jobs)
row.PipeID = p.ID
row.PipeStatus = status
row.PipeLabel = i18n.T(lang, "pipe."+status)
@@ -114,7 +114,7 @@ func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *C return return } } jobs, _ := s.Store.JobsByPipeline(ctx, p.ID) jobs, _ := s.Store.JobsByPipeline(ctx, p.ID) status := displayStatus(p.Status, jobs) status := displayStatus(s.pipeStatusFor(p), jobs) row.PipeID = p.ID row.PipeID = p.ID row.PipeStatus = status row.PipeStatus = status row.PipeLabel = i18n.T(lang, "pipe."+status) row.PipeLabel = i18n.T(lang, "pipe."+status)@@ -68,6 +68,12 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) {
http.Error(w, "unauthorized\n", http.StatusUnauthorized)
return
}
raw, _ := io.ReadAll(io.LimitReader(r.Body, 4096))
var flags struct {
Force bool `json:"force"`
}
_ = json.Unmarshal(raw, &flags)
r.Body = io.NopCloser(strings.NewReader(string(raw)))
ref, err := updateRef(r)
if err != nil {
http.Error(w, "bad request\n", http.StatusBadRequest)
@@ -78,14 +84,26 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) {
return
}
st, _ := update.ReadStatus(s.StateDir)
if update.FreshRunning(st, time.Now(), updateRunning) {
if pending, _ := s.UpdateGate(); pending || update.FreshRunning(st, time.Now(), updateRunning) {
if flags.Force {
_ = s.setUpdateForce(true)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusAccepted)
_ = json.NewEncoder(w).Encode(map[string]string{"status": "forced", "ref": st.SHA})
return
}
http.Error(w, "update already running\n", http.StatusConflict)
return
}
force := flags.Force
if err := update.WriteRequest(s.StateDir, ref); err != nil {
http.Error(w, "unavailable\n", http.StatusServiceUnavailable)
return
}
_ = s.setUpdateForce(force)
_ = update.WriteStatus(s.StateDir, update.Status{
Time: time.Now().UTC().Format(time.RFC3339), SHA: ref, Result: update.ResultWaiting,
})
start := s.UpdateStarter
if start == nil {
start = func(ctx context.Context, _ string) error { return update.StartSystemd(ctx) }
@@ -147,6 +165,10 @@ func (s *Server) selfUpdateStatus(w http.ResponseWriter, r *http.Request) {
"result": st.Result,
"log": logText,
"update": upd,
// force: de updater wacht niet langer op lopende jobs.
"force": s.updateForced(),
"waiting_pipelines": s.countHeld(r.Context()),
"max_wait_seconds": int(s.updateMaxWait().Seconds()),
}
if st.Error != "" {
body["error"] = st.Error
@@ -163,8 +185,9 @@ func updateRef(r *http.Request) (string, error) {
return "", nil
}
var body struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
Ref string `json:"ref"`
SHA string `json:"sha"`
Force bool `json:"force"`
}
if err := json.Unmarshal(raw, &body); err != nil {
return "", err
@@ -68,6 +68,12 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) { http.Error(w, "unauthorized\n", http.StatusUnauthorized) http.Error(w, "unauthorized\n", http.StatusUnauthorized) return return } } raw, _ := io.ReadAll(io.LimitReader(r.Body, 4096)) var flags struct { Force bool `json:"force"` } _ = json.Unmarshal(raw, &flags) r.Body = io.NopCloser(strings.NewReader(string(raw))) ref, err := updateRef(r) ref, err := updateRef(r) if err != nil { if err != nil { http.Error(w, "bad request\n", http.StatusBadRequest) http.Error(w, "bad request\n", http.StatusBadRequest)@@ -78,14 +84,26 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) { return return } } st, _ := update.ReadStatus(s.StateDir) st, _ := update.ReadStatus(s.StateDir) if update.FreshRunning(st, time.Now(), updateRunning) { if pending, _ := s.UpdateGate(); pending || update.FreshRunning(st, time.Now(), updateRunning) { if flags.Force { _ = s.setUpdateForce(true) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusAccepted) _ = json.NewEncoder(w).Encode(map[string]string{"status": "forced", "ref": st.SHA}) return } http.Error(w, "update already running\n", http.StatusConflict) http.Error(w, "update already running\n", http.StatusConflict) return return } } force := flags.Force if err := update.WriteRequest(s.StateDir, ref); err != nil { if err := update.WriteRequest(s.StateDir, ref); err != nil { http.Error(w, "unavailable\n", http.StatusServiceUnavailable) http.Error(w, "unavailable\n", http.StatusServiceUnavailable) return return } } _ = s.setUpdateForce(force) _ = update.WriteStatus(s.StateDir, update.Status{ Time: time.Now().UTC().Format(time.RFC3339), SHA: ref, Result: update.ResultWaiting, }) start := s.UpdateStarter start := s.UpdateStarter if start == nil { if start == nil { start = func(ctx context.Context, _ string) error { return update.StartSystemd(ctx) } start = func(ctx context.Context, _ string) error { return update.StartSystemd(ctx) }@@ -147,6 +165,10 @@ func (s *Server) selfUpdateStatus(w http.ResponseWriter, r *http.Request) { "result": st.Result, "result": st.Result, "log": logText, "log": logText, "update": upd, "update": upd, // force: de updater wacht niet langer op lopende jobs. "force": s.updateForced(), "waiting_pipelines": s.countHeld(r.Context()), "max_wait_seconds": int(s.updateMaxWait().Seconds()), } } if st.Error != "" { if st.Error != "" { body["error"] = st.Error body["error"] = st.Error@@ -163,8 +185,9 @@ func updateRef(r *http.Request) (string, error) { return "", nil return "", nil } } var body struct { var body struct { Ref string `json:"ref"` Ref string `json:"ref"` SHA string `json:"sha"` SHA string `json:"sha"` Force bool `json:"force"` } } if err := json.Unmarshal(raw, &body); err != nil { if err := json.Unmarshal(raw, &body); err != nil { return "", err return "", err@@ -655,7 +655,7 @@ svg.ico { width: 14px; height: 14px; flex: none; }
.skipped .mark::after, .canceled .mark::after {
content: ""; position: absolute; left: 4px; top: 7px; width: 8px; height: 2px; background: #fff;
}
.status.pending .mark, .status.created .mark,
.status.pending .mark, .status.created .mark, .status.waiting_for_update .mark,
.stage-dot.pending .mark, .stage-dot.created .mark {
background: transparent;
box-shadow: inset 0 0 0 2px #8c959f;
@@ -655,7 +655,7 @@ svg.ico { width: 14px; height: 14px; flex: none; }.skipped .mark::after, .canceled .mark::after {.skipped .mark::after, .canceled .mark::after { content: ""; position: absolute; left: 4px; top: 7px; width: 8px; height: 2px; background: #fff; content: ""; position: absolute; left: 4px; top: 7px; width: 8px; height: 2px; background: #fff;}}.status.pending .mark, .status.created .mark,.status.pending .mark, .status.created .mark, .status.waiting_for_update .mark,.stage-dot.pending .mark, .stage-dot.created .mark {.stage-dot.pending .mark, .stage-dot.created .mark { background: transparent; background: transparent; box-shadow: inset 0 0 0 2px #8c959f; box-shadow: inset 0 0 0 2px #8c959f;@@ -0,0 +1,122 @@
package server
import (
"context"
"errors"
"net/http"
"os"
"path/filepath"
"time"
"gitlab.com/Timfa/bluegit/internal/i18n"
"gitlab.com/Timfa/bluegit/internal/store"
"gitlab.com/Timfa/bluegit/internal/update"
)
// De update-poort: zodra een self-update klaarstaat, starten er geen jobs
// meer van pipelines die daarna zijn aangemaakt. Lopende pipelines mogen
// afmaken; de updater wacht tot er niets meer draait (running == 0).
// De release-pipeline zelf blokkeert niets: self_update is de laatste job
// en stopt direct na de POST.
//
// Veiligheidskleppen: na MaxWait gaat de poort vanzelf open (de updater
// geeft dan ook op), en een beheerder kan forceren (update-force), dan
// installeert de updater zonder te wachten.
const forceFile = "update-force"
// DefaultUpdateMaxWait is de standaard maximale wachttijd.
const DefaultUpdateMaxWait = 30 * time.Minute
func (s *Server) updateMaxWait() time.Duration {
if m := s.Cfg.CI.UpdateMaxWaitMin; m > 0 {
return time.Duration(m) * time.Minute
}
return DefaultUpdateMaxWait
}
// UpdateGate zegt of er een update in afwachting is, en sinds wanneer.
func (s *Server) UpdateGate() (bool, time.Time) {
if s.StateDir == "" {
return false, time.Time{}
}
st, err := update.ReadStatus(s.StateDir)
if err != nil || (st.Result != update.ResultWaiting && st.Result != "running") {
return false, time.Time{}
}
since, err := time.Parse(time.RFC3339, st.Time)
if err != nil {
return false, time.Time{}
}
// wachten + bouwen; daarna is de update mislukt of de dienst herstart
if time.Since(since) > s.updateMaxWait()+updateRunning {
return false, time.Time{}
}
return true, since
}
// HoldPipeline is de runnerhaak: true = deze pipeline wacht op de update.
func (s *Server) HoldPipeline(p store.Pipeline) bool {
on, since := s.UpdateGate()
return on && !p.CreatedAt.Before(since)
}
// pipeStatusFor geeft de status voor UI, API en scripts: een wachtende
// pipeline heet waiting_for_update.
func (s *Server) pipeStatusFor(p store.Pipeline) string {
if (p.Status == "queued" || p.Status == "created") && s.HoldPipeline(p) {
return "waiting_for_update"
}
return p.Status
}
func (s *Server) updateForced() bool {
_, err := os.Stat(filepath.Join(s.StateDir, forceFile))
return err == nil
}
func (s *Server) setUpdateForce(on bool) error {
p := filepath.Join(s.StateDir, forceFile)
if !on {
if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
return os.WriteFile(p, []byte(time.Now().UTC().Format(time.RFC3339)+"\n"), 0o640)
}
func (s *Server) countHeld(ctx context.Context) int {
on, _ := s.UpdateGate()
if !on || s.Store == nil {
return 0
}
pipes, err := s.Store.ActivePipelines(ctx)
if err != nil {
return 0
}
n := 0
for _, p := range pipes {
if s.HoldPipeline(p) {
n++
}
}
return n
}
// apiForceUpdate laat een beheerder de wachtende update nu installeren.
func (s *Server) apiForceUpdate(w http.ResponseWriter, r *http.Request) {
if !apiUser(r).InstanceAdmin {
s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden")))
return
}
if on, _ := s.UpdateGate(); !on {
s.writeAPIErr(w, r, fail(409, i18n.E("err.noUpdatePending")))
return
}
if err := s.setUpdateForce(true); err != nil {
s.writeAPIErr(w, r, fail(500, err))
return
}
writeJSON(w, http.StatusOK, map[string]any{"forced": true})
}
@@ -0,0 +1,122 @@package serverimport ( "context" "errors" "net/http" "os" "path/filepath" "time" "gitlab.com/Timfa/bluegit/internal/i18n" "gitlab.com/Timfa/bluegit/internal/store" "gitlab.com/Timfa/bluegit/internal/update")// De update-poort: zodra een self-update klaarstaat, starten er geen jobs// meer van pipelines die daarna zijn aangemaakt. Lopende pipelines mogen// afmaken; de updater wacht tot er niets meer draait (running == 0).// De release-pipeline zelf blokkeert niets: self_update is de laatste job// en stopt direct na de POST.//// Veiligheidskleppen: na MaxWait gaat de poort vanzelf open (de updater// geeft dan ook op), en een beheerder kan forceren (update-force), dan// installeert de updater zonder te wachten.const forceFile = "update-force"// DefaultUpdateMaxWait is de standaard maximale wachttijd.const DefaultUpdateMaxWait = 30 * time.Minutefunc (s *Server) updateMaxWait() time.Duration { if m := s.Cfg.CI.UpdateMaxWaitMin; m > 0 { return time.Duration(m) * time.Minute } return DefaultUpdateMaxWait}// UpdateGate zegt of er een update in afwachting is, en sinds wanneer.func (s *Server) UpdateGate() (bool, time.Time) { if s.StateDir == "" { return false, time.Time{} } st, err := update.ReadStatus(s.StateDir) if err != nil || (st.Result != update.ResultWaiting && st.Result != "running") { return false, time.Time{} } since, err := time.Parse(time.RFC3339, st.Time) if err != nil { return false, time.Time{} } // wachten + bouwen; daarna is de update mislukt of de dienst herstart if time.Since(since) > s.updateMaxWait()+updateRunning { return false, time.Time{} } return true, since}// HoldPipeline is de runnerhaak: true = deze pipeline wacht op de update.func (s *Server) HoldPipeline(p store.Pipeline) bool { on, since := s.UpdateGate() return on && !p.CreatedAt.Before(since)}// pipeStatusFor geeft de status voor UI, API en scripts: een wachtende// pipeline heet waiting_for_update.func (s *Server) pipeStatusFor(p store.Pipeline) string { if (p.Status == "queued" || p.Status == "created") && s.HoldPipeline(p) { return "waiting_for_update" } return p.Status}func (s *Server) updateForced() bool { _, err := os.Stat(filepath.Join(s.StateDir, forceFile)) return err == nil}func (s *Server) setUpdateForce(on bool) error { p := filepath.Join(s.StateDir, forceFile) if !on { if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) { return err } return nil } return os.WriteFile(p, []byte(time.Now().UTC().Format(time.RFC3339)+"\n"), 0o640)}func (s *Server) countHeld(ctx context.Context) int { on, _ := s.UpdateGate() if !on || s.Store == nil { return 0 } pipes, err := s.Store.ActivePipelines(ctx) if err != nil { return 0 } n := 0 for _, p := range pipes { if s.HoldPipeline(p) { n++ } } return n}// apiForceUpdate laat een beheerder de wachtende update nu installeren.func (s *Server) apiForceUpdate(w http.ResponseWriter, r *http.Request) { if !apiUser(r).InstanceAdmin { s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) return } if on, _ := s.UpdateGate(); !on { s.writeAPIErr(w, r, fail(409, i18n.E("err.noUpdatePending"))) return } if err := s.setUpdateForce(true); err != nil { s.writeAPIErr(w, r, fail(500, err)) return } writeJSON(w, http.StatusOK, map[string]any{"forced": true})}@@ -0,0 +1,94 @@
package server
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
"time"
"gitlab.com/Timfa/bluegit/internal/store"
"gitlab.com/Timfa/bluegit/internal/update"
)
func postUpdate(t *testing.T, url, token, body string) (int, string) {
t.Helper()
req, _ := http.NewRequest(http.MethodPost, url+"/api/v1/admin/self-update", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
return res.StatusCode, read(t, res)
}
func TestUpdateGateHoldsNewPipelines(t *testing.T) {
const tok = "sleutel-voor-de-test"
t.Setenv(update.TokenEnv, tok)
srv, ts, dir := newUpdateServer(t)
srv.UpdateStarter = func(context.Context, string) error { return nil }
before := store.Pipeline{ID: 1, Status: "running", CreatedAt: time.Now().Add(-time.Minute)}
if on, _ := srv.UpdateGate(); on || srv.HoldPipeline(before) {
t.Fatal("poort dicht zonder update")
}
time.Sleep(1100 * time.Millisecond) // RFC3339 heeft secondes
if code, body := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusAccepted {
t.Fatalf("POST: %d %s", code, body)
}
on, since := srv.UpdateGate()
if !on {
t.Fatal("geen update in afwachting")
}
// de pipeline die de update aanvroeg (ouder) loopt door: geen deadlock
if srv.HoldPipeline(before) {
t.Fatal("bestaande pipeline vastgehouden")
}
queued := store.Pipeline{ID: 2, Status: "queued", CreatedAt: since.Add(time.Second)}
if !srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "waiting_for_update" {
t.Fatal("nieuwe pipeline niet vastgehouden")
}
if got := srv.pipeJSON(store.Repo{OwnerPath: "tim", Name: "x"}, queued)["status"]; got != "waiting_for_update" {
t.Fatalf("API-status %v", got)
}
// tweede POST zonder force: 409; met force: vlag voor de updater
if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusConflict {
t.Fatalf("dubbele update: %d", code)
}
if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master","force":true}`); code != http.StatusAccepted {
t.Fatalf("force: %d", code)
}
req, _ := http.NewRequest(http.MethodGet, ts.URL+"/api/v1/admin/self-update", nil)
req.Header.Set("Authorization", "Bearer "+tok)
res, _ := http.DefaultClient.Do(req)
var st map[string]any
_ = json.NewDecoder(res.Body).Decode(&st)
res.Body.Close()
if st["force"] != true || st["status"] != "waiting" {
t.Fatalf("status: %v", st)
}
// update klaar: poort open, wachtende pipelines starten
_ = update.WriteStatus(dir, update.Status{Time: time.Now().UTC().Format(time.RFC3339), SHA: "x", Result: "ok"})
if srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "queued" {
t.Fatal("poort bleef dicht na update")
}
}
func TestUpdateGateSafetyValve(t *testing.T) {
t.Setenv(update.TokenEnv, "x")
srv, _, dir := newUpdateServer(t)
srv.Cfg.CI.UpdateMaxWaitMin = 1
old := time.Now().Add(-(time.Minute + updateRunning + time.Second))
_ = update.WriteStatus(dir, update.Status{Time: old.UTC().Format(time.RFC3339), SHA: "x", Result: update.ResultWaiting})
if on, _ := srv.UpdateGate(); on {
t.Fatal("poort bleef dicht na de maximale wachttijd")
}
p := store.Pipeline{Status: "queued", CreatedAt: time.Now()}
if srv.HoldPipeline(p) {
t.Fatal("pipeline vastgehouden na time-out")
}
}
@@ -0,0 +1,94 @@package serverimport ( "context" "encoding/json" "net/http" "strings" "testing" "time" "gitlab.com/Timfa/bluegit/internal/store" "gitlab.com/Timfa/bluegit/internal/update")func postUpdate(t *testing.T, url, token, body string) (int, string) { t.Helper() req, _ := http.NewRequest(http.MethodPost, url+"/api/v1/admin/self-update", strings.NewReader(body)) req.Header.Set("Authorization", "Bearer "+token) req.Header.Set("Content-Type", "application/json") res, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } return res.StatusCode, read(t, res)}func TestUpdateGateHoldsNewPipelines(t *testing.T) { const tok = "sleutel-voor-de-test" t.Setenv(update.TokenEnv, tok) srv, ts, dir := newUpdateServer(t) srv.UpdateStarter = func(context.Context, string) error { return nil } before := store.Pipeline{ID: 1, Status: "running", CreatedAt: time.Now().Add(-time.Minute)} if on, _ := srv.UpdateGate(); on || srv.HoldPipeline(before) { t.Fatal("poort dicht zonder update") } time.Sleep(1100 * time.Millisecond) // RFC3339 heeft secondes if code, body := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusAccepted { t.Fatalf("POST: %d %s", code, body) } on, since := srv.UpdateGate() if !on { t.Fatal("geen update in afwachting") } // de pipeline die de update aanvroeg (ouder) loopt door: geen deadlock if srv.HoldPipeline(before) { t.Fatal("bestaande pipeline vastgehouden") } queued := store.Pipeline{ID: 2, Status: "queued", CreatedAt: since.Add(time.Second)} if !srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "waiting_for_update" { t.Fatal("nieuwe pipeline niet vastgehouden") } if got := srv.pipeJSON(store.Repo{OwnerPath: "tim", Name: "x"}, queued)["status"]; got != "waiting_for_update" { t.Fatalf("API-status %v", got) } // tweede POST zonder force: 409; met force: vlag voor de updater if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusConflict { t.Fatalf("dubbele update: %d", code) } if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master","force":true}`); code != http.StatusAccepted { t.Fatalf("force: %d", code) } req, _ := http.NewRequest(http.MethodGet, ts.URL+"/api/v1/admin/self-update", nil) req.Header.Set("Authorization", "Bearer "+tok) res, _ := http.DefaultClient.Do(req) var st map[string]any _ = json.NewDecoder(res.Body).Decode(&st) res.Body.Close() if st["force"] != true || st["status"] != "waiting" { t.Fatalf("status: %v", st) } // update klaar: poort open, wachtende pipelines starten _ = update.WriteStatus(dir, update.Status{Time: time.Now().UTC().Format(time.RFC3339), SHA: "x", Result: "ok"}) if srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "queued" { t.Fatal("poort bleef dicht na update") }}func TestUpdateGateSafetyValve(t *testing.T) { t.Setenv(update.TokenEnv, "x") srv, _, dir := newUpdateServer(t) srv.Cfg.CI.UpdateMaxWaitMin = 1 old := time.Now().Add(-(time.Minute + updateRunning + time.Second)) _ = update.WriteStatus(dir, update.Status{Time: old.UTC().Format(time.RFC3339), SHA: "x", Result: update.ResultWaiting}) if on, _ := srv.UpdateGate(); on { t.Fatal("poort bleef dicht na de maximale wachttijd") } p := store.Pipeline{Status: "queued", CreatedAt: time.Now()} if srv.HoldPipeline(p) { t.Fatal("pipeline vastgehouden na time-out") }}@@ -35,6 +35,9 @@ const (
logTailBytes = 256 * 1024
)
// ResultWaiting: aangevraagd, de updater wacht tot er geen jobs meer draaien.
const ResultWaiting = "waiting"
// Status is het resultaat dat het update-script en de webpagina delen.
type Status struct {
Time string `json:"time"`
@@ -35,6 +35,9 @@ const ( logTailBytes = 256 * 1024 logTailBytes = 256 * 1024))// ResultWaiting: aangevraagd, de updater wacht tot er geen jobs meer draaien.const ResultWaiting = "waiting"// Status is het resultaat dat het update-script en de webpagina delen.// Status is het resultaat dat het update-script en de webpagina delen.type Status struct {type Status struct { Time string `json:"time"` Time string `json:"time"`@@ -82,6 +82,11 @@ wait_until_idle() {
if [[ "$n" == "0" ]]; then
return 0
fi
# Een beheerder heeft geforceerd: niet langer wachten.
if printf '%s' "$body" | grep -q '"force":[[:space:]]*true'; then
echo "Geforceerd: installeren ondanks ${n} lopende jobs."
return 0
fi
echo "Steve heeft nog ${n:-onbekend} lopende jobs."
sleep "$poll"
done
@@ -82,6 +82,11 @@ wait_until_idle() { if [[ "$n" == "0" ]]; then if [[ "$n" == "0" ]]; then return 0 return 0 fi fi # Een beheerder heeft geforceerd: niet langer wachten. if printf '%s' "$body" | grep -q '"force":[[:space:]]*true'; then echo "Geforceerd: installeren ondanks ${n} lopende jobs." return 0 fi echo "Steve heeft nog ${n:-onbekend} lopende jobs." echo "Steve heeft nog ${n:-onbekend} lopende jobs." sleep "$poll" sleep "$poll" done done