BlueGit

timfa bluegit

Merged

feat(update): geen self-update midden in pipelines

!46 · merged 3 weeks ago

timfa wants to merge 1 commit from cursor/update-gate into stage

Pipeline

#191 Passed
Stage: build

Reviews

  • No reviews yet.

Merged by timfa on 24 October 2026 c55ad114

Overview Commits 1 Pipelines 3 Changes 17

Changes

17 files changed +329 −15
cmd/bluegit/main.go +1 −0
@@ -123,6 +123,7 @@ func serve(args []string) error {
123 123 Timeout: time.Duration(cfg.CI.TimeoutSec) * time.Second,
124 124 Log: log, OnEvent: srv.Dispatch, Presets: cfg.CI.ImageOverrides(),
125 125 }
126 rn.Hold = srv.HoldPipeline
126 127 srv.Runner = rn
127 128 go rn.Loop(ctx, cfg.CI.Concurrency)
128 129 }
@@ -123,6 +123,7 @@ func serve(args []string) error {
123 Timeout: time.Duration(cfg.CI.TimeoutSec) * time.Second,
123 Timeout: time.Duration(cfg.CI.TimeoutSec) * time.Second,
124 Log: log, OnEvent: srv.Dispatch, Presets: cfg.CI.ImageOverrides(),
124 Log: log, OnEvent: srv.Dispatch, Presets: cfg.CI.ImageOverrides(),
125 }
125 }
126 rn.Hold = srv.HoldPipeline
126 srv.Runner = rn
127 srv.Runner = rn
127 go rn.Loop(ctx, cfg.CI.Concurrency)
128 go rn.Loop(ctx, cfg.CI.Concurrency)
128 }
129 }
docs/agents.md +9 −0
@@ -88,3 +88,12 @@ De CLI is dun. Alles staat ook in `/api/v1/openapi.json`. Handige filters:
88 88 - `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`
89 89
90 90 Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.
91
92 ## Self-update en wachtende pipelines
93
94 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.
95
96 - **Maximale wachttijd:** `ci.update_max_wait_minutes` (standaard 30). Is die verstreken, dan gaat de poort weer open.
97 - **Forceren** (installeren zonder te wachten):
98 - als beheerder: `POST /api/v1/admin/self-update/force`
99 - 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:
88- `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`
88- `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`
89
89
90Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.
90Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.
91
92## Self-update en wachtende pipelines
93
94Staat 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.
95
96- **Maximale wachttijd:** `ci.update_max_wait_minutes` (standaard 30). Is die verstreken, dan gaat de poort weer open.
97- **Forceren** (installeren zonder te wachten):
98 - als beheerder: `POST /api/v1/admin/self-update/force`
99 - met het update-token: `POST /api/v1/admin/self-update` met `{"force":true}`
internal/config/config.go +9 −6
@@ -79,12 +79,15 @@ type Runner struct {
79 79
80 80 // CI stuurt de Docker-runner.
81 81 type CI struct {
82 Enabled bool `yaml:"enabled"`
83 Docker string `yaml:"docker_binary"`
84 Concurrency int `yaml:"concurrency"`
85 Network string `yaml:"network"`
86 TimeoutSec int `yaml:"timeout_seconds"`
87 Presets map[string]PresetConfig `yaml:"presets"`
82 Enabled bool `yaml:"enabled"`
83 Docker string `yaml:"docker_binary"`
84 Concurrency int `yaml:"concurrency"`
85 Network string `yaml:"network"`
86 TimeoutSec int `yaml:"timeout_seconds"`
87 // UpdateMaxWaitMin: zo lang wacht een self-update op lopende jobs
88 // (en houdt nieuwe pipelines vast). Standaard 30.
89 UpdateMaxWaitMin int `yaml:"update_max_wait_minutes"`
90 Presets map[string]PresetConfig `yaml:"presets"`
88 91 }
89 92
90 93 // PresetConfig is één image-alias. image mapt direct. dockerfile wordt
@@ -79,12 +79,15 @@ type Runner struct {
79
79
80// CI stuurt de Docker-runner.
80// CI stuurt de Docker-runner.
81type CI struct {
81type CI struct {
82 Enabled bool `yaml:"enabled"`
82 Enabled bool `yaml:"enabled"`
83 Docker string `yaml:"docker_binary"`
83 Docker string `yaml:"docker_binary"`
84 Concurrency int `yaml:"concurrency"`
84 Concurrency int `yaml:"concurrency"`
85 Network string `yaml:"network"`
85 Network string `yaml:"network"`
86 TimeoutSec int `yaml:"timeout_seconds"`
86 TimeoutSec int `yaml:"timeout_seconds"`
87 Presets map[string]PresetConfig `yaml:"presets"`
87 // UpdateMaxWaitMin: zo lang wacht een self-update op lopende jobs
88 // (en houdt nieuwe pipelines vast). Standaard 30.
89 UpdateMaxWaitMin int `yaml:"update_max_wait_minutes"`
90 Presets map[string]PresetConfig `yaml:"presets"`
88}
91}
89
92
90// PresetConfig is één image-alias. image mapt direct. dockerfile wordt
93// PresetConfig is één image-alias. image mapt direct. dockerfile wordt
internal/i18n/catalog_cache.go +7 −0
@@ -70,3 +70,10 @@ var cacheFR = map[string]string{
70 70 "err.cacheInside": "Le chemin du cache ne peut pas se trouver dans le dossier de stockage ou d'état.",
71 71 "err.cacheDir": "Le dossier du cache n'est pas accessible en écriture : %s",
72 72 }
73
74 func init() {
75 addCatalog(en, map[string]string{"pipe.waiting_for_update": "Waiting for update", "err.noUpdatePending": "No self-update is pending."})
76 addCatalog(nl, map[string]string{"pipe.waiting_for_update": "Wacht op update", "err.noUpdatePending": "Er staat geen self-update klaar."})
77 addCatalog(de, map[string]string{"pipe.waiting_for_update": "Wartet auf Update", "err.noUpdatePending": "Es steht kein Self-Update an."})
78 addCatalog(fr, map[string]string{"pipe.waiting_for_update": "En attente de mise à jour", "err.noUpdatePending": "Aucune mise à jour en attente."})
79 }
@@ -70,3 +70,10 @@ var cacheFR = map[string]string{
70 "err.cacheInside": "Le chemin du cache ne peut pas se trouver dans le dossier de stockage ou d'état.",
70 "err.cacheInside": "Le chemin du cache ne peut pas se trouver dans le dossier de stockage ou d'état.",
71 "err.cacheDir": "Le dossier du cache n'est pas accessible en écriture : %s",
71 "err.cacheDir": "Le dossier du cache n'est pas accessible en écriture : %s",
72}
72}
73
74func init() {
75 addCatalog(en, map[string]string{"pipe.waiting_for_update": "Waiting for update", "err.noUpdatePending": "No self-update is pending."})
76 addCatalog(nl, map[string]string{"pipe.waiting_for_update": "Wacht op update", "err.noUpdatePending": "Er staat geen self-update klaar."})
77 addCatalog(de, map[string]string{"pipe.waiting_for_update": "Wartet auf Update", "err.noUpdatePending": "Es steht kein Self-Update an."})
78 addCatalog(fr, map[string]string{"pipe.waiting_for_update": "En attente de mise à jour", "err.noUpdatePending": "Aucune mise à jour en attente."})
79}
internal/runner/hold_test.go added +39 −0
@@ -0,0 +1,39 @@
1 package runner
2
3 import (
4 "context"
5 "io"
6 "log/slog"
7 "path/filepath"
8 "testing"
9
10 "gitlab.com/Timfa/bluegit/internal/store"
11 )
12
13 func TestHoldKeepsJobsQueued(t *testing.T) {
14 dir := t.TempDir()
15 s, err := store.Open(filepath.Join(dir, "b.db"), make([]byte, 32))
16 if err != nil {
17 t.Fatal(err)
18 }
19 defer s.Close()
20 ctx := context.Background()
21 tim, _ := s.UpsertUser(ctx, 1, "tim", "", false)
22 repo, err := s.CreateRepo(ctx, "user", tim.ID, "app", "", "master", "public")
23 if err != nil {
24 t.Fatal(err)
25 }
26 id, err := s.CreatePipeline(ctx, repo.ID, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "refs/heads/master", "push", "tim", "x", []store.NewJob{{Name: "build", Stage: "build", Script: "true"}})
27 if err != nil {
28 t.Fatal(err)
29 }
30 held := 0
31 r := &Runner{Store: s, Storage: dir, Docker: "/nonexistent/docker", GitBin: "git", Log: slog.New(slog.NewTextHandler(io.Discard, nil)),
32 Hold: func(p store.Pipeline) bool { held++; return p.ID == id }}
33 r.slots = make(chan struct{}, 1)
34 r.Tick(ctx)
35 jobs, _ := s.JobsByPipeline(ctx, id)
36 if held == 0 || jobs[0].Status != "queued" {
37 t.Fatalf("job niet vastgehouden: held=%d status=%s", held, jobs[0].Status)
38 }
39 }
@@ -0,0 +1,39 @@
1package runner
2
3import (
4 "context"
5 "io"
6 "log/slog"
7 "path/filepath"
8 "testing"
9
10 "gitlab.com/Timfa/bluegit/internal/store"
11)
12
13func TestHoldKeepsJobsQueued(t *testing.T) {
14 dir := t.TempDir()
15 s, err := store.Open(filepath.Join(dir, "b.db"), make([]byte, 32))
16 if err != nil {
17 t.Fatal(err)
18 }
19 defer s.Close()
20 ctx := context.Background()
21 tim, _ := s.UpsertUser(ctx, 1, "tim", "", false)
22 repo, err := s.CreateRepo(ctx, "user", tim.ID, "app", "", "master", "public")
23 if err != nil {
24 t.Fatal(err)
25 }
26 id, err := s.CreatePipeline(ctx, repo.ID, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "refs/heads/master", "push", "tim", "x", []store.NewJob{{Name: "build", Stage: "build", Script: "true"}})
27 if err != nil {
28 t.Fatal(err)
29 }
30 held := 0
31 r := &Runner{Store: s, Storage: dir, Docker: "/nonexistent/docker", GitBin: "git", Log: slog.New(slog.NewTextHandler(io.Discard, nil)),
32 Hold: func(p store.Pipeline) bool { held++; return p.ID == id }}
33 r.slots = make(chan struct{}, 1)
34 r.Tick(ctx)
35 jobs, _ := s.JobsByPipeline(ctx, id)
36 if held == 0 || jobs[0].Status != "queued" {
37 t.Fatalf("job niet vastgehouden: held=%d status=%s", held, jobs[0].Status)
38 }
39}
internal/runner/runner.go +5 −0
@@ -26,6 +26,8 @@ import (
26 26
27 27 // Runner plant jobs en stuurt het resultaat door naar webhooks.
28 28 type Runner struct {
29 // Hold houdt een pipeline vast, bijvoorbeeld tijdens een self-update.
30 Hold func(store.Pipeline) bool
29 31 Store *store.Store
30 32 GitBin string
31 33 Docker string
@@ -119,6 +121,9 @@ func (r *Runner) Tick(ctx context.Context) {
119 121
120 122 func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
121 123 r.settle(ctx, p.ID)
124 if r.Hold != nil && r.Hold(p) {
125 return // wacht op de self-update
126 }
122 127 jobs, err := r.Store.JobsByPipeline(ctx, p.ID)
123 128 if err != nil {
124 129 r.Log.Error("jobs", "err", err)
@@ -26,6 +26,8 @@ import (
26
26
27// Runner plant jobs en stuurt het resultaat door naar webhooks.
27// Runner plant jobs en stuurt het resultaat door naar webhooks.
28type Runner struct {
28type Runner struct {
29 // Hold houdt een pipeline vast, bijvoorbeeld tijdens een self-update.
30 Hold func(store.Pipeline) bool
29 Store *store.Store
31 Store *store.Store
30 GitBin string
32 GitBin string
31 Docker string
33 Docker string
@@ -119,6 +121,9 @@ func (r *Runner) Tick(ctx context.Context) {
119
121
120func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
122func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
121 r.settle(ctx, p.ID)
123 r.settle(ctx, p.ID)
124 if r.Hold != nil && r.Hold(p) {
125 return // wacht op de self-update
126 }
122 jobs, err := r.Store.JobsByPipeline(ctx, p.ID)
127 jobs, err := r.Store.JobsByPipeline(ctx, p.ID)
123 if err != nil {
128 if err != nil {
124 r.Log.Error("jobs", "err", err)
129 r.Log.Error("jobs", "err", err)
internal/server/api_flow.go +1 −1
@@ -339,7 +339,7 @@ func (s *Server) apiSetMR(w http.ResponseWriter, r *http.Request, reopen bool) {
339 339
340 340 func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any {
341 341 return map[string]any{
342 "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": p.Status, "source": p.Source,
342 "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": s.pipeStatusFor(p), "source": p.Source,
343 343 "actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt),
344 344 "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)),
345 345 }
@@ -339,7 +339,7 @@ func (s *Server) apiSetMR(w http.ResponseWriter, r *http.Request, reopen bool) {
339
339
340func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any {
340func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any {
341 return map[string]any{
341 return map[string]any{
342 "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": p.Status, "source": p.Source,
342 "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": s.pipeStatusFor(p), "source": p.Source,
343 "actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt),
343 "actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt),
344 "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)),
344 "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)),
345 }
345 }
internal/server/api_routes.go +1 −0
@@ -126,6 +126,7 @@ var apiCatalog = []apiRoute{
126 126
127 127 {http.MethodGet, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Read instance storage", (*Server).apiAdminStorage},
128 128 {http.MethodPut, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Set instance storage", (*Server).apiAdminStorage},
129 {http.MethodPost, "/api/v1/admin/self-update/force", access.ScopeAdmin, authUser, "Install the pending self-update now, without waiting for running jobs", (*Server).apiForceUpdate},
129 130 {http.MethodGet, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Read cache settings and usage", (*Server).apiAdminCache},
130 131 {http.MethodPut, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Set cache path", (*Server).apiAdminCache},
131 132 {http.MethodPost, "/api/v1/admin/cache/clear", access.ScopeAdmin, authUser, "Clear the read cache", (*Server).apiAdminCacheClear},
@@ -126,6 +126,7 @@ var apiCatalog = []apiRoute{
126
126
127 {http.MethodGet, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Read instance storage", (*Server).apiAdminStorage},
127 {http.MethodGet, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Read instance storage", (*Server).apiAdminStorage},
128 {http.MethodPut, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Set instance storage", (*Server).apiAdminStorage},
128 {http.MethodPut, "/api/v1/admin/storage", access.ScopeAdmin, authUser, "Set instance storage", (*Server).apiAdminStorage},
129 {http.MethodPost, "/api/v1/admin/self-update/force", access.ScopeAdmin, authUser, "Install the pending self-update now, without waiting for running jobs", (*Server).apiForceUpdate},
129 {http.MethodGet, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Read cache settings and usage", (*Server).apiAdminCache},
130 {http.MethodGet, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Read cache settings and usage", (*Server).apiAdminCache},
130 {http.MethodPut, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Set cache path", (*Server).apiAdminCache},
131 {http.MethodPut, "/api/v1/admin/cache", access.ScopeAdmin, authUser, "Set cache path", (*Server).apiAdminCache},
131 {http.MethodPost, "/api/v1/admin/cache/clear", access.ScopeAdmin, authUser, "Clear the read cache", (*Server).apiAdminCacheClear},
132 {http.MethodPost, "/api/v1/admin/cache/clear", access.ScopeAdmin, authUser, "Clear the read cache", (*Server).apiAdminCacheClear},
internal/server/flow_list.go +1 −1
@@ -240,7 +240,7 @@ func (s *Server) attachFlowPipe(r *http.Request, repo store.Repo, mr store.Merge
240 240 return
241 241 }
242 242 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
243 st := displayStatus(p.Status, jobs)
243 st := displayStatus(s.pipeStatusFor(p), jobs)
244 244 row.PipeStatus = st
245 245 row.PipeLabel = s.tr(r, "pipe."+st)
246 246 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
240 return
240 return
241 }
241 }
242 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
242 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
243 st := displayStatus(p.Status, jobs)
243 st := displayStatus(s.pipeStatusFor(p), jobs)
244 row.PipeStatus = st
244 row.PipeStatus = st
245 row.PipeLabel = s.tr(r, "pipe."+st)
245 row.PipeLabel = s.tr(r, "pipe."+st)
246 row.PipeHref = "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)
246 row.PipeHref = "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)
internal/server/pipeline_page.go +4 −2
@@ -155,7 +155,7 @@ func rowLive(row PipeRow) bool {
155 155 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 {
156 156 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
157 157 name, kind := ci.SplitRef(p.Ref)
158 status := displayStatus(p.Status, jobs)
158 status := displayStatus(s.pipeStatusFor(p), jobs)
159 159 actor, avatar, actorURL := s.actorOf(r, p.Actor, users)
160 160 initial := ""
161 161 if actor != "" {
@@ -264,6 +264,8 @@ func displayStatus(pipe string, jobs []store.Job) string {
264 264 return "pending"
265 265 case "canceled":
266 266 return "canceled"
267 case "waiting_for_update":
268 return "waiting_for_update"
267 269 }
268 270 warn := false
269 271 for _, j := range jobs {
@@ -548,7 +550,7 @@ func (s *Server) jobPage(r *http.Request, repo store.Repo, p store.Pipeline, job
548 550 }
549 551 name, kind := ci.SplitRef(p.Ref)
550 552 status := jobDisplay(job)
551 pipeStatus := displayStatus(p.Status, jobs)
553 pipeStatus := displayStatus(s.pipeStatusFor(p), jobs)
552 554 end := job.FinishedAt
553 555 if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") {
554 556 end = p.FinishedAt
@@ -155,7 +155,7 @@ func rowLive(row PipeRow) bool {
155func (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 {
155func (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 {
156 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
156 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
157 name, kind := ci.SplitRef(p.Ref)
157 name, kind := ci.SplitRef(p.Ref)
158 status := displayStatus(p.Status, jobs)
158 status := displayStatus(s.pipeStatusFor(p), jobs)
159 actor, avatar, actorURL := s.actorOf(r, p.Actor, users)
159 actor, avatar, actorURL := s.actorOf(r, p.Actor, users)
160 initial := ""
160 initial := ""
161 if actor != "" {
161 if actor != "" {
@@ -264,6 +264,8 @@ func displayStatus(pipe string, jobs []store.Job) string {
264 return "pending"
264 return "pending"
265 case "canceled":
265 case "canceled":
266 return "canceled"
266 return "canceled"
267 case "waiting_for_update":
268 return "waiting_for_update"
267 }
269 }
268 warn := false
270 warn := false
269 for _, j := range jobs {
271 for _, j := range jobs {
@@ -548,7 +550,7 @@ func (s *Server) jobPage(r *http.Request, repo store.Repo, p store.Pipeline, job
548 }
550 }
549 name, kind := ci.SplitRef(p.Ref)
551 name, kind := ci.SplitRef(p.Ref)
550 status := jobDisplay(job)
552 status := jobDisplay(job)
551 pipeStatus := displayStatus(p.Status, jobs)
553 pipeStatus := displayStatus(s.pipeStatusFor(p), jobs)
552 end := job.FinishedAt
554 end := job.FinishedAt
553 if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") {
555 if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") {
554 end = p.FinishedAt
556 end = p.FinishedAt
internal/server/project.go +1 −1
@@ -114,7 +114,7 @@ func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *C
114 114 return
115 115 }
116 116 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
117 status := displayStatus(p.Status, jobs)
117 status := displayStatus(s.pipeStatusFor(p), jobs)
118 118 row.PipeID = p.ID
119 119 row.PipeStatus = status
120 120 row.PipeLabel = i18n.T(lang, "pipe."+status)
@@ -114,7 +114,7 @@ func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *C
114 return
114 return
115 }
115 }
116 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
116 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
117 status := displayStatus(p.Status, jobs)
117 status := displayStatus(s.pipeStatusFor(p), jobs)
118 row.PipeID = p.ID
118 row.PipeID = p.ID
119 row.PipeStatus = status
119 row.PipeStatus = status
120 row.PipeLabel = i18n.T(lang, "pipe."+status)
120 row.PipeLabel = i18n.T(lang, "pipe."+status)
internal/server/selfupdate.go +26 −3
@@ -68,6 +68,12 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) {
68 68 http.Error(w, "unauthorized\n", http.StatusUnauthorized)
69 69 return
70 70 }
71 raw, _ := io.ReadAll(io.LimitReader(r.Body, 4096))
72 var flags struct {
73 Force bool `json:"force"`
74 }
75 _ = json.Unmarshal(raw, &flags)
76 r.Body = io.NopCloser(strings.NewReader(string(raw)))
71 77 ref, err := updateRef(r)
72 78 if err != nil {
73 79 http.Error(w, "bad request\n", http.StatusBadRequest)
@@ -78,14 +84,26 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) {
78 84 return
79 85 }
80 86 st, _ := update.ReadStatus(s.StateDir)
81 if update.FreshRunning(st, time.Now(), updateRunning) {
87 if pending, _ := s.UpdateGate(); pending || update.FreshRunning(st, time.Now(), updateRunning) {
88 if flags.Force {
89 _ = s.setUpdateForce(true)
90 w.Header().Set("Content-Type", "application/json; charset=utf-8")
91 w.WriteHeader(http.StatusAccepted)
92 _ = json.NewEncoder(w).Encode(map[string]string{"status": "forced", "ref": st.SHA})
93 return
94 }
82 95 http.Error(w, "update already running\n", http.StatusConflict)
83 96 return
84 97 }
98 force := flags.Force
85 99 if err := update.WriteRequest(s.StateDir, ref); err != nil {
86 100 http.Error(w, "unavailable\n", http.StatusServiceUnavailable)
87 101 return
88 102 }
103 _ = s.setUpdateForce(force)
104 _ = update.WriteStatus(s.StateDir, update.Status{
105 Time: time.Now().UTC().Format(time.RFC3339), SHA: ref, Result: update.ResultWaiting,
106 })
89 107 start := s.UpdateStarter
90 108 if start == nil {
91 109 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) {
147 165 "result": st.Result,
148 166 "log": logText,
149 167 "update": upd,
168 // force: de updater wacht niet langer op lopende jobs.
169 "force": s.updateForced(),
170 "waiting_pipelines": s.countHeld(r.Context()),
171 "max_wait_seconds": int(s.updateMaxWait().Seconds()),
150 172 }
151 173 if st.Error != "" {
152 174 body["error"] = st.Error
@@ -163,8 +185,9 @@ func updateRef(r *http.Request) (string, error) {
163 185 return "", nil
164 186 }
165 187 var body struct {
166 Ref string `json:"ref"`
167 SHA string `json:"sha"`
188 Ref string `json:"ref"`
189 SHA string `json:"sha"`
190 Force bool `json:"force"`
168 191 }
169 192 if err := json.Unmarshal(raw, &body); err != nil {
170 193 return "", err
@@ -68,6 +68,12 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) {
68 http.Error(w, "unauthorized\n", http.StatusUnauthorized)
68 http.Error(w, "unauthorized\n", http.StatusUnauthorized)
69 return
69 return
70 }
70 }
71 raw, _ := io.ReadAll(io.LimitReader(r.Body, 4096))
72 var flags struct {
73 Force bool `json:"force"`
74 }
75 _ = json.Unmarshal(raw, &flags)
76 r.Body = io.NopCloser(strings.NewReader(string(raw)))
71 ref, err := updateRef(r)
77 ref, err := updateRef(r)
72 if err != nil {
78 if err != nil {
73 http.Error(w, "bad request\n", http.StatusBadRequest)
79 http.Error(w, "bad request\n", http.StatusBadRequest)
@@ -78,14 +84,26 @@ func (s *Server) selfUpdate(w http.ResponseWriter, r *http.Request) {
78 return
84 return
79 }
85 }
80 st, _ := update.ReadStatus(s.StateDir)
86 st, _ := update.ReadStatus(s.StateDir)
81 if update.FreshRunning(st, time.Now(), updateRunning) {
87 if pending, _ := s.UpdateGate(); pending || update.FreshRunning(st, time.Now(), updateRunning) {
88 if flags.Force {
89 _ = s.setUpdateForce(true)
90 w.Header().Set("Content-Type", "application/json; charset=utf-8")
91 w.WriteHeader(http.StatusAccepted)
92 _ = json.NewEncoder(w).Encode(map[string]string{"status": "forced", "ref": st.SHA})
93 return
94 }
82 http.Error(w, "update already running\n", http.StatusConflict)
95 http.Error(w, "update already running\n", http.StatusConflict)
83 return
96 return
84 }
97 }
98 force := flags.Force
85 if err := update.WriteRequest(s.StateDir, ref); err != nil {
99 if err := update.WriteRequest(s.StateDir, ref); err != nil {
86 http.Error(w, "unavailable\n", http.StatusServiceUnavailable)
100 http.Error(w, "unavailable\n", http.StatusServiceUnavailable)
87 return
101 return
88 }
102 }
103 _ = s.setUpdateForce(force)
104 _ = update.WriteStatus(s.StateDir, update.Status{
105 Time: time.Now().UTC().Format(time.RFC3339), SHA: ref, Result: update.ResultWaiting,
106 })
89 start := s.UpdateStarter
107 start := s.UpdateStarter
90 if start == nil {
108 if start == nil {
91 start = func(ctx context.Context, _ string) error { return update.StartSystemd(ctx) }
109 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) {
147 "result": st.Result,
165 "result": st.Result,
148 "log": logText,
166 "log": logText,
149 "update": upd,
167 "update": upd,
168 // force: de updater wacht niet langer op lopende jobs.
169 "force": s.updateForced(),
170 "waiting_pipelines": s.countHeld(r.Context()),
171 "max_wait_seconds": int(s.updateMaxWait().Seconds()),
150 }
172 }
151 if st.Error != "" {
173 if st.Error != "" {
152 body["error"] = st.Error
174 body["error"] = st.Error
@@ -163,8 +185,9 @@ func updateRef(r *http.Request) (string, error) {
163 return "", nil
185 return "", nil
164 }
186 }
165 var body struct {
187 var body struct {
166 Ref string `json:"ref"`
188 Ref string `json:"ref"`
167 SHA string `json:"sha"`
189 SHA string `json:"sha"`
190 Force bool `json:"force"`
168 }
191 }
169 if err := json.Unmarshal(raw, &body); err != nil {
192 if err := json.Unmarshal(raw, &body); err != nil {
170 return "", err
193 return "", err
internal/server/static/style.css +1 −1
@@ -655,7 +655,7 @@ svg.ico { width: 14px; height: 14px; flex: none; }
655 655 .skipped .mark::after, .canceled .mark::after {
656 656 content: ""; position: absolute; left: 4px; top: 7px; width: 8px; height: 2px; background: #fff;
657 657 }
658 .status.pending .mark, .status.created .mark,
658 .status.pending .mark, .status.created .mark, .status.waiting_for_update .mark,
659 659 .stage-dot.pending .mark, .stage-dot.created .mark {
660 660 background: transparent;
661 661 box-shadow: inset 0 0 0 2px #8c959f;
@@ -655,7 +655,7 @@ svg.ico { width: 14px; height: 14px; flex: none; }
655.skipped .mark::after, .canceled .mark::after {
655.skipped .mark::after, .canceled .mark::after {
656 content: ""; position: absolute; left: 4px; top: 7px; width: 8px; height: 2px; background: #fff;
656 content: ""; position: absolute; left: 4px; top: 7px; width: 8px; height: 2px; background: #fff;
657}
657}
658.status.pending .mark, .status.created .mark,
658.status.pending .mark, .status.created .mark, .status.waiting_for_update .mark,
659.stage-dot.pending .mark, .stage-dot.created .mark {
659.stage-dot.pending .mark, .stage-dot.created .mark {
660 background: transparent;
660 background: transparent;
661 box-shadow: inset 0 0 0 2px #8c959f;
661 box-shadow: inset 0 0 0 2px #8c959f;
internal/server/update_gate.go added +122 −0
@@ -0,0 +1,122 @@
1 package server
2
3 import (
4 "context"
5 "errors"
6 "net/http"
7 "os"
8 "path/filepath"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/i18n"
12 "gitlab.com/Timfa/bluegit/internal/store"
13 "gitlab.com/Timfa/bluegit/internal/update"
14 )
15
16 // De update-poort: zodra een self-update klaarstaat, starten er geen jobs
17 // meer van pipelines die daarna zijn aangemaakt. Lopende pipelines mogen
18 // afmaken; de updater wacht tot er niets meer draait (running == 0).
19 // De release-pipeline zelf blokkeert niets: self_update is de laatste job
20 // en stopt direct na de POST.
21 //
22 // Veiligheidskleppen: na MaxWait gaat de poort vanzelf open (de updater
23 // geeft dan ook op), en een beheerder kan forceren (update-force), dan
24 // installeert de updater zonder te wachten.
25
26 const forceFile = "update-force"
27
28 // DefaultUpdateMaxWait is de standaard maximale wachttijd.
29 const DefaultUpdateMaxWait = 30 * time.Minute
30
31 func (s *Server) updateMaxWait() time.Duration {
32 if m := s.Cfg.CI.UpdateMaxWaitMin; m > 0 {
33 return time.Duration(m) * time.Minute
34 }
35 return DefaultUpdateMaxWait
36 }
37
38 // UpdateGate zegt of er een update in afwachting is, en sinds wanneer.
39 func (s *Server) UpdateGate() (bool, time.Time) {
40 if s.StateDir == "" {
41 return false, time.Time{}
42 }
43 st, err := update.ReadStatus(s.StateDir)
44 if err != nil || (st.Result != update.ResultWaiting && st.Result != "running") {
45 return false, time.Time{}
46 }
47 since, err := time.Parse(time.RFC3339, st.Time)
48 if err != nil {
49 return false, time.Time{}
50 }
51 // wachten + bouwen; daarna is de update mislukt of de dienst herstart
52 if time.Since(since) > s.updateMaxWait()+updateRunning {
53 return false, time.Time{}
54 }
55 return true, since
56 }
57
58 // HoldPipeline is de runnerhaak: true = deze pipeline wacht op de update.
59 func (s *Server) HoldPipeline(p store.Pipeline) bool {
60 on, since := s.UpdateGate()
61 return on && !p.CreatedAt.Before(since)
62 }
63
64 // pipeStatusFor geeft de status voor UI, API en scripts: een wachtende
65 // pipeline heet waiting_for_update.
66 func (s *Server) pipeStatusFor(p store.Pipeline) string {
67 if (p.Status == "queued" || p.Status == "created") && s.HoldPipeline(p) {
68 return "waiting_for_update"
69 }
70 return p.Status
71 }
72
73 func (s *Server) updateForced() bool {
74 _, err := os.Stat(filepath.Join(s.StateDir, forceFile))
75 return err == nil
76 }
77
78 func (s *Server) setUpdateForce(on bool) error {
79 p := filepath.Join(s.StateDir, forceFile)
80 if !on {
81 if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) {
82 return err
83 }
84 return nil
85 }
86 return os.WriteFile(p, []byte(time.Now().UTC().Format(time.RFC3339)+"\n"), 0o640)
87 }
88
89 func (s *Server) countHeld(ctx context.Context) int {
90 on, _ := s.UpdateGate()
91 if !on || s.Store == nil {
92 return 0
93 }
94 pipes, err := s.Store.ActivePipelines(ctx)
95 if err != nil {
96 return 0
97 }
98 n := 0
99 for _, p := range pipes {
100 if s.HoldPipeline(p) {
101 n++
102 }
103 }
104 return n
105 }
106
107 // apiForceUpdate laat een beheerder de wachtende update nu installeren.
108 func (s *Server) apiForceUpdate(w http.ResponseWriter, r *http.Request) {
109 if !apiUser(r).InstanceAdmin {
110 s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden")))
111 return
112 }
113 if on, _ := s.UpdateGate(); !on {
114 s.writeAPIErr(w, r, fail(409, i18n.E("err.noUpdatePending")))
115 return
116 }
117 if err := s.setUpdateForce(true); err != nil {
118 s.writeAPIErr(w, r, fail(500, err))
119 return
120 }
121 writeJSON(w, http.StatusOK, map[string]any{"forced": true})
122 }
@@ -0,0 +1,122 @@
1package server
2
3import (
4 "context"
5 "errors"
6 "net/http"
7 "os"
8 "path/filepath"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/i18n"
12 "gitlab.com/Timfa/bluegit/internal/store"
13 "gitlab.com/Timfa/bluegit/internal/update"
14)
15
16// De update-poort: zodra een self-update klaarstaat, starten er geen jobs
17// meer van pipelines die daarna zijn aangemaakt. Lopende pipelines mogen
18// afmaken; de updater wacht tot er niets meer draait (running == 0).
19// De release-pipeline zelf blokkeert niets: self_update is de laatste job
20// en stopt direct na de POST.
21//
22// Veiligheidskleppen: na MaxWait gaat de poort vanzelf open (de updater
23// geeft dan ook op), en een beheerder kan forceren (update-force), dan
24// installeert de updater zonder te wachten.
25
26const forceFile = "update-force"
27
28// DefaultUpdateMaxWait is de standaard maximale wachttijd.
29const DefaultUpdateMaxWait = 30 * time.Minute
30
31func (s *Server) updateMaxWait() time.Duration {
32 if m := s.Cfg.CI.UpdateMaxWaitMin; m > 0 {
33 return time.Duration(m) * time.Minute
34 }
35 return DefaultUpdateMaxWait
36}
37
38// UpdateGate zegt of er een update in afwachting is, en sinds wanneer.
39func (s *Server) UpdateGate() (bool, time.Time) {
40 if s.StateDir == "" {
41 return false, time.Time{}
42 }
43 st, err := update.ReadStatus(s.StateDir)
44 if err != nil || (st.Result != update.ResultWaiting && st.Result != "running") {
45 return false, time.Time{}
46 }
47 since, err := time.Parse(time.RFC3339, st.Time)
48 if err != nil {
49 return false, time.Time{}
50 }
51 // wachten + bouwen; daarna is de update mislukt of de dienst herstart
52 if time.Since(since) > s.updateMaxWait()+updateRunning {
53 return false, time.Time{}
54 }
55 return true, since
56}
57
58// HoldPipeline is de runnerhaak: true = deze pipeline wacht op de update.
59func (s *Server) HoldPipeline(p store.Pipeline) bool {
60 on, since := s.UpdateGate()
61 return on && !p.CreatedAt.Before(since)
62}
63
64// pipeStatusFor geeft de status voor UI, API en scripts: een wachtende
65// pipeline heet waiting_for_update.
66func (s *Server) pipeStatusFor(p store.Pipeline) string {
67 if (p.Status == "queued" || p.Status == "created") && s.HoldPipeline(p) {
68 return "waiting_for_update"
69 }
70 return p.Status
71}
72
73func (s *Server) updateForced() bool {
74 _, err := os.Stat(filepath.Join(s.StateDir, forceFile))
75 return err == nil
76}
77
78func (s *Server) setUpdateForce(on bool) error {
79 p := filepath.Join(s.StateDir, forceFile)
80 if !on {
81 if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) {
82 return err
83 }
84 return nil
85 }
86 return os.WriteFile(p, []byte(time.Now().UTC().Format(time.RFC3339)+"\n"), 0o640)
87}
88
89func (s *Server) countHeld(ctx context.Context) int {
90 on, _ := s.UpdateGate()
91 if !on || s.Store == nil {
92 return 0
93 }
94 pipes, err := s.Store.ActivePipelines(ctx)
95 if err != nil {
96 return 0
97 }
98 n := 0
99 for _, p := range pipes {
100 if s.HoldPipeline(p) {
101 n++
102 }
103 }
104 return n
105}
106
107// apiForceUpdate laat een beheerder de wachtende update nu installeren.
108func (s *Server) apiForceUpdate(w http.ResponseWriter, r *http.Request) {
109 if !apiUser(r).InstanceAdmin {
110 s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden")))
111 return
112 }
113 if on, _ := s.UpdateGate(); !on {
114 s.writeAPIErr(w, r, fail(409, i18n.E("err.noUpdatePending")))
115 return
116 }
117 if err := s.setUpdateForce(true); err != nil {
118 s.writeAPIErr(w, r, fail(500, err))
119 return
120 }
121 writeJSON(w, http.StatusOK, map[string]any{"forced": true})
122}
internal/server/update_gate_test.go added +94 −0
@@ -0,0 +1,94 @@
1 package server
2
3 import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "strings"
8 "testing"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/store"
12 "gitlab.com/Timfa/bluegit/internal/update"
13 )
14
15 func postUpdate(t *testing.T, url, token, body string) (int, string) {
16 t.Helper()
17 req, _ := http.NewRequest(http.MethodPost, url+"/api/v1/admin/self-update", strings.NewReader(body))
18 req.Header.Set("Authorization", "Bearer "+token)
19 req.Header.Set("Content-Type", "application/json")
20 res, err := http.DefaultClient.Do(req)
21 if err != nil {
22 t.Fatal(err)
23 }
24 return res.StatusCode, read(t, res)
25 }
26
27 func TestUpdateGateHoldsNewPipelines(t *testing.T) {
28 const tok = "sleutel-voor-de-test"
29 t.Setenv(update.TokenEnv, tok)
30 srv, ts, dir := newUpdateServer(t)
31 srv.UpdateStarter = func(context.Context, string) error { return nil }
32
33 before := store.Pipeline{ID: 1, Status: "running", CreatedAt: time.Now().Add(-time.Minute)}
34 if on, _ := srv.UpdateGate(); on || srv.HoldPipeline(before) {
35 t.Fatal("poort dicht zonder update")
36 }
37 time.Sleep(1100 * time.Millisecond) // RFC3339 heeft secondes
38 if code, body := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusAccepted {
39 t.Fatalf("POST: %d %s", code, body)
40 }
41 on, since := srv.UpdateGate()
42 if !on {
43 t.Fatal("geen update in afwachting")
44 }
45 // de pipeline die de update aanvroeg (ouder) loopt door: geen deadlock
46 if srv.HoldPipeline(before) {
47 t.Fatal("bestaande pipeline vastgehouden")
48 }
49 queued := store.Pipeline{ID: 2, Status: "queued", CreatedAt: since.Add(time.Second)}
50 if !srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "waiting_for_update" {
51 t.Fatal("nieuwe pipeline niet vastgehouden")
52 }
53 if got := srv.pipeJSON(store.Repo{OwnerPath: "tim", Name: "x"}, queued)["status"]; got != "waiting_for_update" {
54 t.Fatalf("API-status %v", got)
55 }
56
57 // tweede POST zonder force: 409; met force: vlag voor de updater
58 if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusConflict {
59 t.Fatalf("dubbele update: %d", code)
60 }
61 if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master","force":true}`); code != http.StatusAccepted {
62 t.Fatalf("force: %d", code)
63 }
64 req, _ := http.NewRequest(http.MethodGet, ts.URL+"/api/v1/admin/self-update", nil)
65 req.Header.Set("Authorization", "Bearer "+tok)
66 res, _ := http.DefaultClient.Do(req)
67 var st map[string]any
68 _ = json.NewDecoder(res.Body).Decode(&st)
69 res.Body.Close()
70 if st["force"] != true || st["status"] != "waiting" {
71 t.Fatalf("status: %v", st)
72 }
73
74 // update klaar: poort open, wachtende pipelines starten
75 _ = update.WriteStatus(dir, update.Status{Time: time.Now().UTC().Format(time.RFC3339), SHA: "x", Result: "ok"})
76 if srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "queued" {
77 t.Fatal("poort bleef dicht na update")
78 }
79 }
80
81 func TestUpdateGateSafetyValve(t *testing.T) {
82 t.Setenv(update.TokenEnv, "x")
83 srv, _, dir := newUpdateServer(t)
84 srv.Cfg.CI.UpdateMaxWaitMin = 1
85 old := time.Now().Add(-(time.Minute + updateRunning + time.Second))
86 _ = update.WriteStatus(dir, update.Status{Time: old.UTC().Format(time.RFC3339), SHA: "x", Result: update.ResultWaiting})
87 if on, _ := srv.UpdateGate(); on {
88 t.Fatal("poort bleef dicht na de maximale wachttijd")
89 }
90 p := store.Pipeline{Status: "queued", CreatedAt: time.Now()}
91 if srv.HoldPipeline(p) {
92 t.Fatal("pipeline vastgehouden na time-out")
93 }
94 }
@@ -0,0 +1,94 @@
1package server
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "strings"
8 "testing"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/store"
12 "gitlab.com/Timfa/bluegit/internal/update"
13)
14
15func postUpdate(t *testing.T, url, token, body string) (int, string) {
16 t.Helper()
17 req, _ := http.NewRequest(http.MethodPost, url+"/api/v1/admin/self-update", strings.NewReader(body))
18 req.Header.Set("Authorization", "Bearer "+token)
19 req.Header.Set("Content-Type", "application/json")
20 res, err := http.DefaultClient.Do(req)
21 if err != nil {
22 t.Fatal(err)
23 }
24 return res.StatusCode, read(t, res)
25}
26
27func TestUpdateGateHoldsNewPipelines(t *testing.T) {
28 const tok = "sleutel-voor-de-test"
29 t.Setenv(update.TokenEnv, tok)
30 srv, ts, dir := newUpdateServer(t)
31 srv.UpdateStarter = func(context.Context, string) error { return nil }
32
33 before := store.Pipeline{ID: 1, Status: "running", CreatedAt: time.Now().Add(-time.Minute)}
34 if on, _ := srv.UpdateGate(); on || srv.HoldPipeline(before) {
35 t.Fatal("poort dicht zonder update")
36 }
37 time.Sleep(1100 * time.Millisecond) // RFC3339 heeft secondes
38 if code, body := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusAccepted {
39 t.Fatalf("POST: %d %s", code, body)
40 }
41 on, since := srv.UpdateGate()
42 if !on {
43 t.Fatal("geen update in afwachting")
44 }
45 // de pipeline die de update aanvroeg (ouder) loopt door: geen deadlock
46 if srv.HoldPipeline(before) {
47 t.Fatal("bestaande pipeline vastgehouden")
48 }
49 queued := store.Pipeline{ID: 2, Status: "queued", CreatedAt: since.Add(time.Second)}
50 if !srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "waiting_for_update" {
51 t.Fatal("nieuwe pipeline niet vastgehouden")
52 }
53 if got := srv.pipeJSON(store.Repo{OwnerPath: "tim", Name: "x"}, queued)["status"]; got != "waiting_for_update" {
54 t.Fatalf("API-status %v", got)
55 }
56
57 // tweede POST zonder force: 409; met force: vlag voor de updater
58 if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusConflict {
59 t.Fatalf("dubbele update: %d", code)
60 }
61 if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master","force":true}`); code != http.StatusAccepted {
62 t.Fatalf("force: %d", code)
63 }
64 req, _ := http.NewRequest(http.MethodGet, ts.URL+"/api/v1/admin/self-update", nil)
65 req.Header.Set("Authorization", "Bearer "+tok)
66 res, _ := http.DefaultClient.Do(req)
67 var st map[string]any
68 _ = json.NewDecoder(res.Body).Decode(&st)
69 res.Body.Close()
70 if st["force"] != true || st["status"] != "waiting" {
71 t.Fatalf("status: %v", st)
72 }
73
74 // update klaar: poort open, wachtende pipelines starten
75 _ = update.WriteStatus(dir, update.Status{Time: time.Now().UTC().Format(time.RFC3339), SHA: "x", Result: "ok"})
76 if srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "queued" {
77 t.Fatal("poort bleef dicht na update")
78 }
79}
80
81func TestUpdateGateSafetyValve(t *testing.T) {
82 t.Setenv(update.TokenEnv, "x")
83 srv, _, dir := newUpdateServer(t)
84 srv.Cfg.CI.UpdateMaxWaitMin = 1
85 old := time.Now().Add(-(time.Minute + updateRunning + time.Second))
86 _ = update.WriteStatus(dir, update.Status{Time: old.UTC().Format(time.RFC3339), SHA: "x", Result: update.ResultWaiting})
87 if on, _ := srv.UpdateGate(); on {
88 t.Fatal("poort bleef dicht na de maximale wachttijd")
89 }
90 p := store.Pipeline{Status: "queued", CreatedAt: time.Now()}
91 if srv.HoldPipeline(p) {
92 t.Fatal("pipeline vastgehouden na time-out")
93 }
94}
internal/update/update.go +3 −0
@@ -35,6 +35,9 @@ const (
35 35 logTailBytes = 256 * 1024
36 36 )
37 37
38 // ResultWaiting: aangevraagd, de updater wacht tot er geen jobs meer draaien.
39 const ResultWaiting = "waiting"
40
38 41 // Status is het resultaat dat het update-script en de webpagina delen.
39 42 type Status struct {
40 43 Time string `json:"time"`
@@ -35,6 +35,9 @@ const (
35 logTailBytes = 256 * 1024
35 logTailBytes = 256 * 1024
36)
36)
37
37
38// ResultWaiting: aangevraagd, de updater wacht tot er geen jobs meer draaien.
39const ResultWaiting = "waiting"
40
38// Status is het resultaat dat het update-script en de webpagina delen.
41// Status is het resultaat dat het update-script en de webpagina delen.
39type Status struct {
42type Status struct {
40 Time string `json:"time"`
43 Time string `json:"time"`
packaging/bluegit-update.sh +5 −0
@@ -82,6 +82,11 @@ wait_until_idle() {
82 82 if [[ "$n" == "0" ]]; then
83 83 return 0
84 84 fi
85 # Een beheerder heeft geforceerd: niet langer wachten.
86 if printf '%s' "$body" | grep -q '"force":[[:space:]]*true'; then
87 echo "Geforceerd: installeren ondanks ${n} lopende jobs."
88 return 0
89 fi
85 90 echo "Steve heeft nog ${n:-onbekend} lopende jobs."
86 91 sleep "$poll"
87 92 done
@@ -82,6 +82,11 @@ wait_until_idle() {
82 if [[ "$n" == "0" ]]; then
82 if [[ "$n" == "0" ]]; then
83 return 0
83 return 0
84 fi
84 fi
85 # Een beheerder heeft geforceerd: niet langer wachten.
86 if printf '%s' "$body" | grep -q '"force":[[:space:]]*true'; then
87 echo "Geforceerd: installeren ondanks ${n} lopende jobs."
88 return 0
89 fi
85 echo "Steve heeft nog ${n:-onbekend} lopende jobs."
90 echo "Steve heeft nog ${n:-onbekend} lopende jobs."
86 sleep "$poll"
91 sleep "$poll"
87 done
92 done