| 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 | if on && !p.CreatedAt.Before(since) { |
| 62 | return true |
| 63 | } |
| 64 | _, blocked := s.protectedBlocker(p) |
| 65 | return blocked |
| 66 | } |
| 67 | |
| 68 | // protectedBlocker: een oudere pipeline op dezelfde ref heeft een lopende |
| 69 | // doNotInterrupt-job; deze pipeline start (nog) geen nieuwe jobs. |
| 70 | func (s *Server) protectedBlocker(p store.Pipeline) (store.Job, bool) { |
| 71 | if s.Store == nil || p.Status == "success" || p.Status == "failed" || p.Status == "canceled" { |
| 72 | return store.Job{}, false |
| 73 | } |
| 74 | return s.Store.ProtectedBlocker(context.Background(), p.RepoID, p.Ref, p.ID) |
| 75 | } |
| 76 | |
| 77 | // pipeStatusFor geeft de status voor UI, API en scripts: een wachtende |
| 78 | // pipeline heet waiting_for_update. |
| 79 | func (s *Server) pipeStatusFor(p store.Pipeline) string { |
| 80 | if p.Status == "queued" || p.Status == "created" { |
| 81 | if on, since := s.UpdateGate(); on && !p.CreatedAt.Before(since) { |
| 82 | return "waiting_for_update" |
| 83 | } |
| 84 | if _, blocked := s.protectedBlocker(p); blocked { |
| 85 | return "waiting_for_job" |
| 86 | } |
| 87 | } |
| 88 | return p.Status |
| 89 | } |
| 90 | |
| 91 | func (s *Server) updateForced() bool { |
| 92 | _, err := os.Stat(filepath.Join(s.StateDir, forceFile)) |
| 93 | return err == nil |
| 94 | } |
| 95 | |
| 96 | func (s *Server) setUpdateForce(on bool) error { |
| 97 | p := filepath.Join(s.StateDir, forceFile) |
| 98 | if !on { |
| 99 | if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 100 | return err |
| 101 | } |
| 102 | return nil |
| 103 | } |
| 104 | return os.WriteFile(p, []byte(time.Now().UTC().Format(time.RFC3339)+"\n"), 0o640) |
| 105 | } |
| 106 | |
| 107 | func (s *Server) countHeld(ctx context.Context) int { |
| 108 | on, _ := s.UpdateGate() |
| 109 | if !on || s.Store == nil { |
| 110 | return 0 |
| 111 | } |
| 112 | pipes, err := s.Store.ActivePipelines(ctx) |
| 113 | if err != nil { |
| 114 | return 0 |
| 115 | } |
| 116 | n := 0 |
| 117 | for _, p := range pipes { |
| 118 | if s.HoldPipeline(p) { |
| 119 | n++ |
| 120 | } |
| 121 | } |
| 122 | return n |
| 123 | } |
| 124 | |
| 125 | // apiForceUpdate laat een beheerder de wachtende update nu installeren. |
| 126 | func (s *Server) apiForceUpdate(w http.ResponseWriter, r *http.Request) { |
| 127 | if !apiUser(r).InstanceAdmin { |
| 128 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 129 | return |
| 130 | } |
| 131 | if on, _ := s.UpdateGate(); !on { |
| 132 | s.writeAPIErr(w, r, fail(409, i18n.E("err.noUpdatePending"))) |
| 133 | return |
| 134 | } |
| 135 | if err := s.setUpdateForce(true); err != nil { |
| 136 | s.writeAPIErr(w, r, fail(500, err)) |
| 137 | return |
| 138 | } |
| 139 | writeJSON(w, http.StatusOK, map[string]any{"forced": true}) |
| 140 | } |