| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 12 | "gitlab.com/Timfa/bluegit/internal/state" |
| 13 | "gitlab.com/Timfa/bluegit/internal/store" |
| 14 | ) |
| 15 | |
| 16 | // dirUsage is de laatst getelde grootte van de gekozen map. |
| 17 | type dirUsage struct { |
| 18 | path string |
| 19 | n uint64 |
| 20 | at time.Time |
| 21 | running bool |
| 22 | } |
| 23 | |
| 24 | var ( |
| 25 | // usageTTL is hoe lang een volledige telling geldig blijft. |
| 26 | usageTTL = 5 * time.Minute |
| 27 | // usageWait is hoe lang de pagina op een nieuwe telling wacht. |
| 28 | usageWait = 2 * time.Second |
| 29 | // usageLimit stopt een telling die vastloopt. |
| 30 | usageLimit = 15 * time.Minute |
| 31 | ) |
| 32 | |
| 33 | // bluegitUsage geeft de grootte van de map. De telling draait op de |
| 34 | // achtergrond en is altijd volledig; de pagina wacht hooguit usageWait. |
| 35 | // Is er dan nog geen uitkomst, dan geeft counting true en, als die er is, |
| 36 | // de vorige uitkomst. ok is false als er nog niets te tonen valt. |
| 37 | func (s *Server) bluegitUsage(path string) (n uint64, counting bool, ok bool) { |
| 38 | s.usageMu.Lock() |
| 39 | cur := s.usage |
| 40 | if cur.path != path { |
| 41 | cur = dirUsage{path: path} |
| 42 | s.usage = cur |
| 43 | } |
| 44 | stale := cur.at.IsZero() || time.Since(cur.at) >= usageTTL |
| 45 | var done chan struct{} |
| 46 | if stale && !cur.running { |
| 47 | s.usage.running = true |
| 48 | done = make(chan struct{}) |
| 49 | s.usageDone = done |
| 50 | go s.countUsage(path, done) |
| 51 | } else if cur.running { |
| 52 | done = s.usageDone |
| 53 | } |
| 54 | s.usageMu.Unlock() |
| 55 | if stale && done != nil { |
| 56 | select { |
| 57 | case <-done: |
| 58 | case <-time.After(usageWait): |
| 59 | } |
| 60 | } |
| 61 | s.usageMu.Lock() |
| 62 | defer s.usageMu.Unlock() |
| 63 | cur = s.usage |
| 64 | if cur.path != path { |
| 65 | return 0, true, false |
| 66 | } |
| 67 | if cur.at.IsZero() { |
| 68 | return 0, cur.running, false |
| 69 | } |
| 70 | return cur.n, cur.running, true |
| 71 | } |
| 72 | |
| 73 | func (s *Server) countUsage(path string, done chan struct{}) { |
| 74 | defer close(done) |
| 75 | ctx, cancel := context.WithTimeout(context.Background(), usageLimit) |
| 76 | defer cancel() |
| 77 | n, err := state.DirSize(ctx, path) |
| 78 | s.usageMu.Lock() |
| 79 | defer s.usageMu.Unlock() |
| 80 | if s.usage.path != path { |
| 81 | return |
| 82 | } |
| 83 | s.usage.running = false |
| 84 | if err != nil { |
| 85 | return |
| 86 | } |
| 87 | s.usage.n = n |
| 88 | s.usage.at = time.Now() |
| 89 | } |
| 90 | |
| 91 | func (s *Server) noStorageText(r *http.Request) string { |
| 92 | return s.tr(r, "err.noStorage") + "\n" |
| 93 | } |
| 94 | |
| 95 | func allowsWithoutStorage(r *http.Request) bool { |
| 96 | switch r.URL.Path { |
| 97 | case "/healthz", "/login", "/auth/callback", "/logout", "/setup", "/-/info", "/api/v1/ci/spec", "/api/v1/ci/presets", "/api/v1/openapi.json", "/api/v1/webhooks/openapi.json", "/api/v1/admin/storage", "/api/v1/admin/self-update": |
| 98 | return true |
| 99 | } |
| 100 | return strings.HasPrefix(r.URL.Path, "/assets/") |
| 101 | } |
| 102 | |
| 103 | func isGitPath(path string) bool { |
| 104 | return strings.HasSuffix(path, "/info/refs") || |
| 105 | strings.HasSuffix(path, "/git-upload-pack") || |
| 106 | strings.HasSuffix(path, "/git-receive-pack") |
| 107 | } |
| 108 | |
| 109 | func (s *Server) gate(next http.Handler) http.Handler { |
| 110 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 111 | if allowsWithoutStorage(r) || s.StoragePath() != "" { |
| 112 | next.ServeHTTP(w, r) |
| 113 | return |
| 114 | } |
| 115 | if isGitPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/api/") { |
| 116 | http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable) |
| 117 | return |
| 118 | } |
| 119 | if p := s.who(r); p.OK && p.User.InstanceAdmin { |
| 120 | http.Redirect(w, r, "/setup", http.StatusFound) |
| 121 | return |
| 122 | } |
| 123 | next.ServeHTTP(w, r) |
| 124 | }) |
| 125 | } |
| 126 | |
| 127 | // StoragePath is de datadisk. Leeg zolang de beheerder er geen heeft gekozen. |
| 128 | func (s *Server) StoragePath() string { |
| 129 | s.storageMu.RLock() |
| 130 | set, path := s.storageSet, s.storage |
| 131 | s.storageMu.RUnlock() |
| 132 | if set { |
| 133 | return path |
| 134 | } |
| 135 | s.storageMu.Lock() |
| 136 | defer s.storageMu.Unlock() |
| 137 | if !s.storageSet { |
| 138 | s.storage = s.Cfg.StoragePath |
| 139 | s.storageSet = true |
| 140 | } |
| 141 | return s.storage |
| 142 | } |
| 143 | |
| 144 | // AdoptStorage zet het pad dat bij het starten al geldig was. |
| 145 | func (s *Server) AdoptStorage(path string) { |
| 146 | s.storageMu.Lock() |
| 147 | s.storage = path |
| 148 | s.storageSet = true |
| 149 | s.storageMu.Unlock() |
| 150 | if path != "" && s.AfterStorage != nil { |
| 151 | s.AfterStorage(path) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func (s *Server) setupPage(w http.ResponseWriter, r *http.Request) { |
| 156 | if _, ok := s.adminOnly(w, r); !ok { |
| 157 | return |
| 158 | } |
| 159 | s.renderSetup(w, r, "", "") |
| 160 | } |
| 161 | |
| 162 | func (s *Server) setupSave(w http.ResponseWriter, r *http.Request) { |
| 163 | if _, ok := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) { |
| 164 | return |
| 165 | } |
| 166 | info, moved, err := s.opSetStorage(r.FormValue("path")) |
| 167 | if err != nil { |
| 168 | s.renderSetup(w, r, s.trErr(r, err), strings.TrimSpace(r.FormValue("path"))) |
| 169 | return |
| 170 | } |
| 171 | notice := s.tr(r, "err.storageSet", info.Path, state.Format(info.Free)) |
| 172 | if moved { |
| 173 | notice = s.tr(r, "err.storageMoved", info.Path, state.Format(info.Free)) |
| 174 | } |
| 175 | s.redirect(w, r, "/", notice, "") |
| 176 | } |
| 177 | |
| 178 | func samePath(stateDir, path string) (bool, error) { |
| 179 | if stateDir == "" { |
| 180 | return false, nil |
| 181 | } |
| 182 | return strings.TrimRight(stateDir, "/") == strings.TrimRight(path, "/"), nil |
| 183 | } |
| 184 | |
| 185 | func (s *Server) adminOnly(w http.ResponseWriter, r *http.Request) (bool, bool) { |
| 186 | u, ok := s.loginRequired(w, r) |
| 187 | if !ok { |
| 188 | return false, false |
| 189 | } |
| 190 | if !u.InstanceAdmin { |
| 191 | http.Error(w, s.tr(r, "err.noStorageAdmin"), http.StatusForbidden) |
| 192 | return false, false |
| 193 | } |
| 194 | return true, true |
| 195 | } |
| 196 | |
| 197 | func (s *Server) renderSetup(w http.ResponseWriter, r *http.Request, problem, prefill string) { |
| 198 | v := s.view(w, r, "title.storage") |
| 199 | v.Page = "setup" |
| 200 | if problem != "" { |
| 201 | v.Err = problem |
| 202 | } |
| 203 | v.MinFree = state.Format(state.MinFreeBytes) |
| 204 | v.StoragePath = strings.TrimSpace(prefill) |
| 205 | if v.StoragePath == "" { |
| 206 | v.StoragePath = s.StoragePath() |
| 207 | } |
| 208 | if v.StoragePath == "" && s.StateDir != "" { |
| 209 | if saved, err := state.Read(s.StateDir); err == nil { |
| 210 | v.StoragePath = saved |
| 211 | } |
| 212 | } |
| 213 | if v.StoragePath != "" { |
| 214 | info, err := state.Inspect(v.StoragePath) |
| 215 | if info.Total > 0 { |
| 216 | v.StorageFree = state.Format(info.Free) |
| 217 | v.StorageUsed = state.Format(info.Used()) |
| 218 | v.StorageReserved = state.Format(info.Reserved) |
| 219 | v.HasReserved = info.Reserved > 0 |
| 220 | v.StorageTotal = state.Format(info.Total) |
| 221 | v.StoragePercent = state.Percent(info.Used(), info.Total) |
| 222 | n, counting, ok := s.bluegitUsage(info.Path) |
| 223 | if ok { |
| 224 | v.StorageBlueGit = state.Format(n) |
| 225 | } |
| 226 | v.StoragePartial = counting |
| 227 | } |
| 228 | if err != nil && v.Err == "" { |
| 229 | v.Err = s.trErr(r, err) |
| 230 | } |
| 231 | } |
| 232 | v.Cache = s.cacheInfo() |
| 233 | s.attachUpdate(&v, s.langOf(r)) |
| 234 | s.render(w, "setup", v) |
| 235 | } |
| 236 | |
| 237 | // RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat. |
| 238 | func (s *Server) RefreshHooks() { |
| 239 | root := s.StoragePath() |
| 240 | if root == "" || s.HookBin == "" || s.Store == nil { |
| 241 | return |
| 242 | } |
| 243 | repos, err := s.Store.AllRepos(context.Background()) |
| 244 | if err != nil { |
| 245 | return |
| 246 | } |
| 247 | for _, repo := range repos { |
| 248 | dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name) |
| 249 | if _, err := os.Stat(dir); err != nil { |
| 250 | continue |
| 251 | } |
| 252 | if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil { |
| 253 | s.Log.Error("hook", "repo", repo.FullPath(), "err", err) |
| 254 | } |
| 255 | if repo.PushedAt.IsZero() { |
| 256 | s.backfillPushed(repo, dir) |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // backfillPushed vult pushed_at eenmalig voor repos van voor deze kolom: |
| 262 | // de nieuwste committijd over alle branches en tags. Een lege repo blijft leeg. |
| 263 | func (s *Server) backfillPushed(repo store.Repo, dir string) { |
| 264 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 265 | defer cancel() |
| 266 | raw := gitOne(ctx, s.Git, dir, "for-each-ref", "--sort=-committerdate", "--count=1", "--format=%(committerdate:unix)", "refs/heads", "refs/tags") |
| 267 | sec, err := strconv.ParseInt(strings.TrimSpace(raw), 10, 64) |
| 268 | if err != nil || sec <= 0 { |
| 269 | return |
| 270 | } |
| 271 | _ = s.Store.TouchPushed(ctx, repo.ID, time.Unix(sec, 0)) |
| 272 | } |