| 1 | package runnercfg |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "html/template" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 11 | ) |
| 12 | |
| 13 | var ( |
| 14 | tmplMu sync.Mutex |
| 15 | tmpls = map[i18n.Lang]*template.Template{} |
| 16 | ) |
| 17 | |
| 18 | func uiTmpl(lang i18n.Lang) *template.Template { |
| 19 | tmplMu.Lock() |
| 20 | defer tmplMu.Unlock() |
| 21 | if t := tmpls[lang]; t != nil { |
| 22 | return t |
| 23 | } |
| 24 | t := template.Must(template.New("ui").Funcs(Funcs(lang)).Parse(uiSource)) |
| 25 | tmpls[lang] = t |
| 26 | return t |
| 27 | } |
| 28 | |
| 29 | // Funcs zijn de sjabloonfuncties (t, ago, dur, join) voor lang. |
| 30 | func Funcs(lang i18n.Lang) template.FuncMap { |
| 31 | return template.FuncMap{ |
| 32 | "t": func(key string, args ...any) string { return i18n.T(lang, key, args...) }, |
| 33 | "tip": func(key string) template.HTML { return Tip(lang, key) }, |
| 34 | "ago": func(t time.Time) string { return i18n.Ago(lang, t.Unix()) }, |
| 35 | "dur": func(a, b time.Time) string { |
| 36 | if b.IsZero() { |
| 37 | b = time.Now() |
| 38 | } |
| 39 | d := b.Sub(a).Round(time.Second) |
| 40 | if d < time.Minute { |
| 41 | return fmt.Sprintf("%ds", int(d.Seconds())) |
| 42 | } |
| 43 | return fmt.Sprintf("%dm%02ds", int(d.Minutes()), int(d.Seconds())%60) |
| 44 | }, |
| 45 | "join": func(v []string) string { return strings.Join(v, ", ") }, |
| 46 | "cpus": func(f float64) string { |
| 47 | if f <= 0 { |
| 48 | return "" |
| 49 | } |
| 50 | return trimFloat(f) |
| 51 | }, |
| 52 | "num": func(n int) string { |
| 53 | if n <= 0 { |
| 54 | return "" |
| 55 | } |
| 56 | return fmt.Sprint(n) |
| 57 | }, |
| 58 | "gb": func(mb int) string { return fmt.Sprintf("%.1f", float64(mb)/1024) }, |
| 59 | "short": func(s string) string { |
| 60 | if len(s) > 60 { |
| 61 | return s[:57] + "…" |
| 62 | } |
| 63 | return s |
| 64 | }, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const uiSource = `{{define "runnerui"}} |
| 69 | <div class="rui"> |
| 70 | {{if .Notice}}<p class="flash">{{.Notice}}</p>{{end}} |
| 71 | {{if .Problem}}<p class="flash bad">{{.Problem}}</p>{{end}} |
| 72 | <div class="rui-head"> |
| 73 | <div> |
| 74 | <h1>{{if .Status.Name}}{{.Status.Name}}{{else}}{{t "rui.title"}}{{end}}</h1> |
| 75 | <p class="muted">{{if eq .Status.Kind "builtin"}}{{t "rui.leadBuiltin"}}{{else}}{{t "rui.lead"}}{{end}}</p> |
| 76 | </div> |
| 77 | <div class="rui-state"> |
| 78 | {{if .S.Paused}}<span class="rui-badge warn">{{if .Active}}{{t "rui.state.draining"}}{{else}}{{t "rui.state.paused"}}{{end}}</span> |
| 79 | {{else if .Status.Online}}<span class="rui-badge ok">{{t "rui.state.online"}}</span> |
| 80 | {{else}}<span class="rui-badge bad">{{t "rui.state.offline"}}</span>{{end}} |
| 81 | </div> |
| 82 | </div> |
| 83 | |
| 84 | <div class="rui-cards"> |
| 85 | <section class="panel rui-card"> |
| 86 | <h2>{{t "rui.status"}}</h2> |
| 87 | <dl class="rui-dl"> |
| 88 | <dt>{{t "rui.version"}}</dt><dd><code>{{or .Status.Version "dev"}}</code></dd> |
| 89 | <dt>{{t "rui.server"}}</dt><dd>{{if .Status.ServerURL}}<a href="{{.Status.ServerURL}}">{{.Status.ServerURL}}</a>{{else}}—{{end}}</dd> |
| 90 | <dt>{{t "rui.contact"}}</dt><dd>{{if .Status.LastContact.IsZero}}—{{else}}{{ago .Status.LastContact}}{{end}}</dd> |
| 91 | {{if .Status.ServerPause}}<dt>{{t "rui.serverPause"}}</dt><dd><span class="pill">{{t "rui.state.paused"}}</span></dd>{{end}} |
| 92 | </dl> |
| 93 | </section> |
| 94 | <section class="panel rui-card"> |
| 95 | <h2>{{t "rui.hardware"}}</h2> |
| 96 | <dl class="rui-dl"> |
| 97 | <dt>{{t "rui.hw.cpu"}}</dt><dd>{{.HW.CPUs}}</dd> |
| 98 | <dt>{{t "rui.hw.mem"}}</dt><dd>{{gb .HW.MemoryMB}} GB</dd> |
| 99 | <dt>{{t "rui.hw.disk"}}</dt><dd>{{t "rui.hw.diskVal" .HW.DiskFree .HW.DiskSize}}</dd> |
| 100 | </dl> |
| 101 | </section> |
| 102 | <section class="panel rui-card rui-upgrade"> |
| 103 | <h2 class="tiphost">{{t "rui.upgrade"}} {{tip "rui.tip.pause"}}</h2> |
| 104 | <p class="rui-big">{{len .Active}} <span class="muted">{{t "rui.activeCount"}}</span></p> |
| 105 | {{if .Safe}}<p class="rui-safe ok">✓ {{t "rui.safe"}}</p>{{else}}<p class="rui-safe warn">⏳ {{t "rui.notSafe"}}</p>{{end}} |
| 106 | {{if .S.Paused}} |
| 107 | <form method="post" action="{{.Prefix}}/resume">{{if .CSRFField}}<input type="hidden" name="{{.CSRFField}}" value="{{.CSRF}}">{{end}}<button type="submit">▶ {{t "rui.resume"}}</button></form> |
| 108 | {{else}} |
| 109 | <form method="post" action="{{.Prefix}}/pause" data-confirm="{{t "rui.pauseAsk"}}">{{if .CSRFField}}<input type="hidden" name="{{.CSRFField}}" value="{{.CSRF}}">{{end}}<button type="submit" class="ghost">⏸ {{t "rui.pause"}}</button></form> |
| 110 | {{end}} |
| 111 | <p class="muted rui-small">{{t "rui.pauseHelp"}}</p> |
| 112 | </section> |
| 113 | </div> |
| 114 | |
| 115 | <section class="panel"> |
| 116 | <h2>{{t "rui.active"}}</h2> |
| 117 | {{if .Active}} |
| 118 | <div class="hscroll"><table class="rui-table"> |
| 119 | <thead><tr><th>{{t "rui.col.job"}}</th><th>{{t "rui.col.project"}}</th><th>{{t "rui.col.image"}}</th><th>{{t "rui.col.duration"}}</th></tr></thead> |
| 120 | <tbody>{{range .Active}}<tr> |
| 121 | <td>{{if .URL}}<a href="{{.URL}}">#{{.ID}} {{.Name}}</a>{{else}}#{{.ID}} {{.Name}}{{end}}</td> |
| 122 | <td>{{.Project}}{{if .Pipeline}} <span class="muted">· {{t "rui.pipeline" .Pipeline}}</span>{{end}}</td> |
| 123 | <td><code>{{short .Image}}</code></td><td>{{dur .Started .Finished}}</td> |
| 124 | </tr>{{end}}</tbody> |
| 125 | </table></div> |
| 126 | {{else}}<p class="muted">{{t "rui.noActive"}}</p>{{end}} |
| 127 | <h3>{{t "rui.recent"}}</h3> |
| 128 | {{if .Recent}} |
| 129 | <div class="hscroll"><table class="rui-table"> |
| 130 | <thead><tr><th>{{t "rui.col.job"}}</th><th>{{t "rui.col.project"}}</th><th>{{t "rui.col.status"}}</th><th>{{t "rui.col.duration"}}</th><th>{{t "rui.col.finished"}}</th></tr></thead> |
| 131 | <tbody>{{range .Recent}}<tr> |
| 132 | <td>{{if .URL}}<a href="{{.URL}}">#{{.ID}} {{.Name}}</a>{{else}}#{{.ID}} {{.Name}}{{end}}</td> |
| 133 | <td>{{.Project}}</td> |
| 134 | <td><span class="rui-st {{.Status}}">{{t (printf "rui.job.%s" .Status)}}</span></td> |
| 135 | <td>{{dur .Started .Finished}}</td><td>{{ago .Finished}}</td> |
| 136 | </tr>{{end}}</tbody> |
| 137 | </table></div> |
| 138 | {{else}}<p class="muted">{{t "rui.noRecent"}}</p>{{end}} |
| 139 | </section> |
| 140 | |
| 141 | <section class="panel" id="settings"> |
| 142 | <div class="rui-sechead"> |
| 143 | <h2>{{t "rui.settings"}}</h2> |
| 144 | <form method="post" action="{{.Prefix}}/recommend">{{if .CSRFField}}<input type="hidden" name="{{.CSRFField}}" value="{{.CSRF}}">{{end}}<button type="submit" class="ghost">✨ {{t "rui.applyRec"}}</button></form> |
| 145 | </div> |
| 146 | <p class="muted">{{t "rui.recLead"}}</p> |
| 147 | <form method="post" action="{{.Prefix}}/settings" class="rui-form"> |
| 148 | {{if .CSRFField}}<input type="hidden" name="{{.CSRFField}}" value="{{.CSRF}}">{{end}} |
| 149 | <fieldset><legend>{{t "rui.grp.general"}}</legend> |
| 150 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.name"}} {{tip "rui.tip.name"}}</span><input name="name" value="{{.S.Name}}" maxlength="63" placeholder="{{.Status.Name}}"><small class="muted">{{if eq .Status.Kind "builtin"}}{{t "rui.h.nameBuiltin"}}{{else}}{{t "rui.h.name"}}{{end}}</small></label> |
| 151 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.labels"}} {{tip "rui.tip.labels"}}</span><input name="labels" value="{{join .S.Labels}}" placeholder="docker, linux, gpu"><small class="muted">{{t "rui.h.labels"}}</small></label> |
| 152 | </fieldset> |
| 153 | <fieldset><legend>{{t "rui.grp.capacity"}}</legend> |
| 154 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.concurrency"}} {{tip "rui.tip.concurrency"}}</span><input type="number" min="1" max="64" name="concurrency" value="{{.S.Concurrency}}"><small class="rui-rec">{{t "rui.rec" .Rec.Concurrency}}</small></label> |
| 155 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.maxImages"}} {{tip "rui.tip.maxImages"}}</span><input type="number" min="1" max="1000" name="max_images" value="{{.S.MaxImages}}"><small class="rui-rec">{{t "rui.rec" .Rec.MaxImages}}</small></label> |
| 156 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.timeout"}} {{tip "rui.tip.timeout"}}</span><input type="number" min="0" name="job_timeout_min" value="{{num .S.JobTimeoutMin}}" placeholder="{{t "rui.unlimited"}}"><small class="muted">{{t "rui.h.timeout"}}</small></label> |
| 157 | </fieldset> |
| 158 | <fieldset><legend>{{t "rui.grp.limits"}}</legend> |
| 159 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.cpus"}} {{tip "rui.tip.cpus"}}</span><input inputmode="decimal" name="cpus" value="{{cpus .S.CPUs}}" placeholder="{{t "rui.unlimited"}}"><small class="rui-rec">{{t "rui.rec" (cpus .Rec.CPUs)}}</small></label> |
| 160 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.memory"}} {{tip "rui.tip.memory"}}</span><input type="number" min="0" step="64" name="memory_mb" value="{{num .S.MemoryMB}}" placeholder="{{t "rui.unlimited"}}"><small class="rui-rec">{{t "rui.rec" .Rec.MemoryMB}}</small></label> |
| 161 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.pids"}} {{tip "rui.tip.pids"}}</span><input type="number" min="0" name="pids_limit" value="{{num .S.PidsLimit}}" placeholder="{{t "rui.unlimited"}}"><small class="rui-rec">{{t "rui.rec" .Rec.PidsLimit}}</small></label> |
| 162 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.disk"}} {{tip "rui.tip.disk"}}</span><input type="number" min="0" name="max_disk_gb" value="{{num .S.MaxDiskGB}}" placeholder="{{t "rui.unlimited"}}"><small class="rui-rec">{{t "rui.rec" .Rec.MaxDiskGB}} · {{t "rui.h.disk"}}</small></label> |
| 163 | </fieldset> |
| 164 | <fieldset><legend>{{t "rui.grp.images"}}</legend> |
| 165 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.pull"}} {{tip "rui.tip.pull"}}</span> |
| 166 | <select name="pull_policy"> |
| 167 | <option value="if-not-present"{{if eq .S.PullPolicy "if-not-present"}} selected{{end}}>{{t "rui.pull.ifNotPresent"}}</option> |
| 168 | <option value="always"{{if eq .S.PullPolicy "always"}} selected{{end}}>{{t "rui.pull.always"}}</option> |
| 169 | <option value="never"{{if eq .S.PullPolicy "never"}} selected{{end}}>{{t "rui.pull.never"}}</option> |
| 170 | </select><small class="muted">{{t "rui.h.pull"}}</small></label> |
| 171 | <label class="rui-check tiphost"><input type="checkbox" name="cleanup_work"{{if .S.CleanupWork}} checked{{end}}> {{t "rui.f.cleanupWork"}} {{tip "rui.tip.cleanupWork"}}</label> |
| 172 | <label class="rui-check tiphost"><input type="checkbox" name="prune_dangling"{{if .S.PruneDangling}} checked{{end}}> {{t "rui.f.pruneDangling"}} {{tip "rui.tip.pruneDangling"}}</label> |
| 173 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.pruneCache"}} {{tip "rui.tip.pruneCache"}}</span><input type="number" min="0" name="prune_cache_gb" value="{{num .S.PruneCacheGB}}" placeholder="{{t "rui.unlimited"}}"><small class="rui-rec">{{t "rui.rec" .Rec.PruneCacheGB}}</small></label> |
| 174 | </fieldset> |
| 175 | <div class="rui-actions"><button type="submit">{{t "rui.save"}}</button></div> |
| 176 | </form> |
| 177 | </section> |
| 178 | |
| 179 | <section class="panel" id="presets"> |
| 180 | <h2 class="tiphost">{{t "rui.presets"}} {{tip "rui.tip.presets"}}</h2> |
| 181 | <p class="muted">{{t "rui.presetsLead"}}</p> |
| 182 | <div class="hscroll"><table class="rui-table"> |
| 183 | <thead><tr><th>{{t "rui.col.alias"}}</th><th>{{t "rui.col.base"}}</th><th>{{t "rui.col.kind"}}</th><th></th></tr></thead> |
| 184 | <tbody> |
| 185 | {{range .Builtin}}<tr><td><code>{{.Name}}</code></td><td><code>{{.From}}</code>{{if .Tools}} <span class="muted">· {{join .Tools}}</span>{{end}}</td><td><span class="pill">{{t "rui.kind.builtin"}}</span></td><td></td></tr>{{end}} |
| 186 | {{range .Config}}<tr><td><code>{{.}}</code></td><td class="muted">config.yml</td><td><span class="pill">{{t "rui.kind.config"}}</span></td><td></td></tr>{{end}} |
| 187 | {{range .S.Presets}}<tr><td><code>{{.Name}}</code>{{if .Note}}<br><small class="muted">{{.Note}}</small>{{end}}</td> |
| 188 | <td>{{if .Image}}<code>{{.Image}}</code>{{else if .From}}<code>{{.From}}</code>{{else}}<span class="muted">Dockerfile</span>{{end}}</td> |
| 189 | <td><span class="pill rui-own">{{t "rui.kind.own"}}</span></td> |
| 190 | <td class="act"><form method="post" action="{{$.Prefix}}/presets/delete" data-confirm="{{t "rui.presetDelAsk"}}">{{if $.CSRFField}}<input type="hidden" name="{{$.CSRFField}}" value="{{$.CSRF}}">{{end}}<input type="hidden" name="name" value="{{.Name}}"><button class="danger" type="submit">{{t "rui.delete"}}</button></form></td></tr>{{end}} |
| 191 | </tbody> |
| 192 | </table></div> |
| 193 | <details class="rui-add"{{if not .S.Presets}} open{{end}}> |
| 194 | <summary>{{t "rui.presetAdd"}}</summary> |
| 195 | <form method="post" action="{{.Prefix}}/presets" class="rui-form"> |
| 196 | {{if .CSRFField}}<input type="hidden" name="{{.CSRFField}}" value="{{.CSRF}}">{{end}} |
| 197 | <fieldset> |
| 198 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.alias"}} {{tip "rui.tip.alias"}}</span><input name="name" required placeholder="team/tools" pattern="[a-z0-9._/-]+"><small class="muted">{{t "rui.h.alias"}}</small></label> |
| 199 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.image"}} {{tip "rui.tip.image"}}</span><input name="image" placeholder="node:22-alpine"><small class="muted">{{t "rui.h.image"}}</small></label> |
| 200 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.from"}} {{tip "rui.tip.from"}}</span><input name="from" placeholder="debian:bookworm-slim"><small class="muted">{{t "rui.h.from"}}</small></label> |
| 201 | <label class="tiphost"><span class="rui-lbl">{{t "rui.f.note"}} {{tip "rui.tip.note"}}</span><input name="note" maxlength="120"></label> |
| 202 | <label class="rui-wide tiphost"><span class="rui-lbl">{{t "rui.f.dockerfile"}} {{tip "rui.tip.dockerfile"}}</span><textarea name="dockerfile" rows="6" placeholder="FROM debian:bookworm-slim RUN apt-get update && apt-get install -y git make"></textarea><small class="muted">{{t "rui.h.dockerfile"}}</small></label> |
| 203 | </fieldset> |
| 204 | <div class="rui-actions"><button type="submit">{{t "rui.presetSave"}}</button></div> |
| 205 | </form> |
| 206 | </details> |
| 207 | </section> |
| 208 | |
| 209 | <section class="panel rui-foot"> |
| 210 | <p>{{t "rui.docsLead"}} {{if .DocsURL}}<a href="{{.DocsURL}}">{{t "rui.docs"}}</a> · {{end}}<code>{{.APIBase}}/status</code></p> |
| 211 | </section> |
| 212 | </div> |
| 213 | {{end}}` |