BlueGit

timfa bluegit

Files

bluegit / internal / server / setup.go

setup.go

feat: toon map- en schijfverbruik op het opslagtabblad
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 5f22f51f History
setup.go 5.7 KiB Download Raw
1package server
2
3import (
4 "context"
5 "errors"
6 "net/http"
7 "os"
8 "strings"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/gitrepo"
12 "gitlab.com/Timfa/bluegit/internal/state"
13)
14
15// dirUsage is de laatst getelde grootte van de gekozen map.
16type dirUsage struct {
17 path string
18 n uint64
19 partial bool
20 at time.Time
21}
22
23const (
24 usageTTL = time.Minute
25 usageBudget = 2 * time.Second
26)
27
28// bluegitUsage telt de map, met een cache en een tijdslimiet.
29// Een grote repository houdt de pagina zo hooguit usageBudget tegen.
30// partial betekent dat de telling voortijdig stopte.
31func (s *Server) bluegitUsage(path string) (uint64, bool, bool) {
32 s.usageMu.Lock()
33 cached := s.usage
34 fresh := cached.path == path && !cached.at.IsZero() && time.Since(cached.at) < usageTTL
35 s.usageMu.Unlock()
36 if fresh {
37 return cached.n, cached.partial, true
38 }
39 ctx, cancel := context.WithTimeout(context.Background(), usageBudget)
40 defer cancel()
41 n, err := state.DirSize(ctx, path)
42 partial := errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled)
43 if err != nil && !partial {
44 return 0, false, false
45 }
46 s.usageMu.Lock()
47 s.usage = dirUsage{path: path, n: n, partial: partial, at: time.Now()}
48 s.usageMu.Unlock()
49 return n, partial, true
50}
51
52func (s *Server) noStorageText(r *http.Request) string {
53 return s.tr(r, "err.noStorage") + "\n"
54}
55
56func allowsWithoutStorage(r *http.Request) bool {
57 switch r.URL.Path {
58 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":
59 return true
60 }
61 return strings.HasPrefix(r.URL.Path, "/assets/")
62}
63
64func isGitPath(path string) bool {
65 return strings.HasSuffix(path, "/info/refs") ||
66 strings.HasSuffix(path, "/git-upload-pack") ||
67 strings.HasSuffix(path, "/git-receive-pack")
68}
69
70func (s *Server) gate(next http.Handler) http.Handler {
71 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
72 if allowsWithoutStorage(r) || s.StoragePath() != "" {
73 next.ServeHTTP(w, r)
74 return
75 }
76 if isGitPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/api/") {
77 http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable)
78 return
79 }
80 if p := s.who(r); p.OK && p.User.InstanceAdmin {
81 http.Redirect(w, r, "/setup", http.StatusFound)
82 return
83 }
84 next.ServeHTTP(w, r)
85 })
86}
87
88// StoragePath is de datadisk. Leeg zolang de beheerder er geen heeft gekozen.
89func (s *Server) StoragePath() string {
90 s.storageMu.RLock()
91 set, path := s.storageSet, s.storage
92 s.storageMu.RUnlock()
93 if set {
94 return path
95 }
96 s.storageMu.Lock()
97 defer s.storageMu.Unlock()
98 if !s.storageSet {
99 s.storage = s.Cfg.StoragePath
100 s.storageSet = true
101 }
102 return s.storage
103}
104
105// AdoptStorage zet het pad dat bij het starten al geldig was.
106func (s *Server) AdoptStorage(path string) {
107 s.storageMu.Lock()
108 s.storage = path
109 s.storageSet = true
110 s.storageMu.Unlock()
111 if path != "" && s.AfterStorage != nil {
112 s.AfterStorage(path)
113 }
114}
115
116func (s *Server) setupPage(w http.ResponseWriter, r *http.Request) {
117 if _, ok := s.adminOnly(w, r); !ok {
118 return
119 }
120 s.renderSetup(w, r, "", "")
121}
122
123func (s *Server) setupSave(w http.ResponseWriter, r *http.Request) {
124 if _, ok := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) {
125 return
126 }
127 info, moved, err := s.opSetStorage(r.FormValue("path"))
128 if err != nil {
129 s.renderSetup(w, r, s.trErr(r, err), strings.TrimSpace(r.FormValue("path")))
130 return
131 }
132 notice := s.tr(r, "err.storageSet", info.Path, state.Format(info.Free))
133 if moved {
134 notice = s.tr(r, "err.storageMoved", info.Path, state.Format(info.Free))
135 }
136 s.redirect(w, r, "/", notice, "")
137}
138
139func samePath(stateDir, path string) (bool, error) {
140 if stateDir == "" {
141 return false, nil
142 }
143 return strings.TrimRight(stateDir, "/") == strings.TrimRight(path, "/"), nil
144}
145
146func (s *Server) adminOnly(w http.ResponseWriter, r *http.Request) (bool, bool) {
147 u, ok := s.loginRequired(w, r)
148 if !ok {
149 return false, false
150 }
151 if !u.InstanceAdmin {
152 http.Error(w, s.tr(r, "err.noStorageAdmin"), http.StatusForbidden)
153 return false, false
154 }
155 return true, true
156}
157
158func (s *Server) renderSetup(w http.ResponseWriter, r *http.Request, problem, prefill string) {
159 v := s.view(w, r, "title.storage")
160 v.Page = "setup"
161 if problem != "" {
162 v.Err = problem
163 }
164 v.MinFree = state.Format(state.MinFreeBytes)
165 v.StoragePath = strings.TrimSpace(prefill)
166 if v.StoragePath == "" {
167 v.StoragePath = s.StoragePath()
168 }
169 if v.StoragePath == "" && s.StateDir != "" {
170 if saved, err := state.Read(s.StateDir); err == nil {
171 v.StoragePath = saved
172 }
173 }
174 if v.StoragePath != "" {
175 info, err := state.Inspect(v.StoragePath)
176 if info.Total > 0 {
177 v.StorageFree = state.Format(info.Free)
178 v.StorageUsed = state.Format(info.Used())
179 v.StorageTotal = state.Format(info.Total)
180 v.StoragePercent = state.Percent(info.Used(), info.Total)
181 if n, partial, ok := s.bluegitUsage(info.Path); ok {
182 v.StorageBlueGit = state.Format(n)
183 v.StoragePartial = partial
184 }
185 }
186 if err != nil && v.Err == "" {
187 v.Err = s.trErr(r, err)
188 }
189 }
190 s.attachUpdate(&v, s.langOf(r))
191 s.render(w, "setup", v)
192}
193
194// RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat.
195func (s *Server) RefreshHooks() {
196 root := s.StoragePath()
197 if root == "" || s.HookBin == "" || s.Store == nil {
198 return
199 }
200 repos, err := s.Store.AllRepos(context.Background())
201 if err != nil {
202 return
203 }
204 for _, repo := range repos {
205 dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name)
206 if _, err := os.Stat(dir); err != nil {
207 continue
208 }
209 if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil {
210 s.Log.Error("hook", "repo", repo.FullPath(), "err", err)
211 }
212 }
213}