blob: e65c6121618098ebc4a9a917ba2daf1d91dd7a08 [file] [log] [blame]
gio778577f2024-04-29 09:44:38 +04001{{ define "task" }}
2{{ range . }}
3<li aria-busy="{{ eq .Status 1 }}">
giof9f0bee2024-06-11 20:10:05 +04004 {{ if eq .Status 3 }}<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="black" d="M21 7L9 19l-5.5-5.5l1.41-1.41L9 16.17L19.59 5.59z"/></svg>{{ end }}{{ .Title }}{{ if .Err }} - {{ .Err.Error }} {{ end }}
gio778577f2024-04-29 09:44:38 +04005 {{ if .Subtasks }}
6 <ul>
7 {{ template "task" .Subtasks }}
8 </ul>
9 {{ end }}
10</li>
11{{ end }}
12{{ end }}
13
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040014{{ define "schema-form" }}
15 {{ $readonly := .ReadOnly }}
16 {{ $networks := .AvailableNetworks }}
17 {{ $data := .Data }}
gio44f621b2024-04-29 09:44:38 +040018 {{ range $f := .Schema.Fields }}
19 {{ $name := $f.Name }}
20 {{ $schema := $f.Schema }}
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040021 {{ if eq $schema.Kind 0 }}
gio44f621b2024-04-29 09:44:38 +040022 <label {{ if $schema.Advanced }}hidden{{ end }}>
23 <input type="checkbox" role="swtich" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.checked)" {{ if $readonly }}disabled{{ end }} {{ if index $data $name }}checked{{ end }} />
24 {{ $schema.Name }}
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040025 </label>
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +040026 {{ else if eq $schema.Kind 7 }}
gio44f621b2024-04-29 09:44:38 +040027 <label {{ if $schema.Advanced }}hidden{{ end }}>
28 {{ $schema.Name }}
29 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, parseInt(this.value))" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +040030 </label>
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040031 {{ else if eq $schema.Kind 1 }}
gio44f621b2024-04-29 09:44:38 +040032 <label {{ if $schema.Advanced }}hidden{{ end }}>
33 {{ $schema.Name }}
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040034 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040035 {{ else if eq $schema.Kind 4 }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040036 </label>
gio44f621b2024-04-29 09:44:38 +040037 <label {{ if $schema.Advanced }}hidden{{ end }}>
38 {{ $schema.Name }}
39 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
40 </label>
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +040041 {{ else if eq $schema.Kind 3 }}
gio44f621b2024-04-29 09:44:38 +040042 <label {{ if $schema.Advanced }}hidden{{ end }}>
43 {{ $schema.Name }}
44 <select name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} >
gio4ece99c2024-07-18 11:05:50 +040045 {{ if not $readonly }}<option disabled selected value></option>{{ end }}
gio44f621b2024-04-29 09:44:38 +040046 {{ range $networks }}
47 <option {{if eq .Name (index $data $name) }}selected{{ end }}>{{ .Name }}</option>
48 {{ end }}
49 </select>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040050 </label>
gio4ece99c2024-07-18 11:05:50 +040051 {{ else if eq $schema.Kind 10 }}
52 <label {{ if $schema.Advanced }}hidden{{ end }}>
53 {{ $schema.Name }}
54 <details class="dropdown">
55 {{ $selectedNetworks := index $data $name }}
56 <summary id="{{ $name }}">{{ $selectedNetworks | join "," }}</summary>
57 <ul>
58 {{ range $networks }}
59 {{ $networkName := .Name }}
60 {{ $selected := false }}
61 {{ range $selectedNetworks }}
62 {{ if eq . $networkName }}
63 {{ $selected = true }}
64 {{ end }}
65 {{ end }}
66 <li>
67 <label>
68 <input type="checkbox" name="{{ $networkName }}" oninput="multiNetworkSelected('{{ $name }}', '{{ $networkName }}', this.checked)" {{ if $selected }}checked{{ end }} />
69 {{ .Name }}
70 </label>
71 </li>
72 {{ end }}
73 </ul>
74 </details>
75 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040076 {{ else if eq $schema.Kind 5 }}
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040077 {{ $auth := index $data $name }}
78 {{ $authEnabled := false }}
79 {{ $authGroups := "" }}
80 {{ if and $auth (index $auth "enabled") }}{{ $authEnabled = true }}{{ end }}
81 {{ if and $auth (index $auth "groups") }}{{ $authGroups = index $auth "groups" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +040082 <label {{ if $schema.Advanced }}hidden{{ end }}>
83 <input type="checkbox" role="swtich" name="authEnabled" oninput="valueChanged('{{- $name -}}.enabled', this.checked)" {{ if $readonly }}disabled{{ end }} {{ if $authEnabled }}checked{{ end }} />
84 <span>Require authentication</span>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040085 </label>
gio44f621b2024-04-29 09:44:38 +040086 <label for="authGroups">
87 <span>Authentication groups</span>
88 <input type="text" name="authGroups" oninput="valueChanged('{{- $name -}}.groups', this.value)" {{ if $readonly }}disabled{{ end }} value="{{ $authGroups }}" />
89 </label>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +040090 {{ else if eq $schema.Kind 6 }}
91 {{ $sshKey := index $data $name }}
92 {{ $public := "" }}
93 {{ $private := "" }}
94 {{ if $sshKey }}{{ $public = index $sshKey "public" }}{{ end }}
95 {{ if $sshKey }}{{ $private = index $sshKey "private" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +040096 <label {{ if $schema.Advanced }}hidden{{ end }}>
97 <span>Public Key</span>
98 <textarea name="{{ $name }}-public" disabled>{{ $public }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +040099 </label>
gio44f621b2024-04-29 09:44:38 +0400100 <label {{ if $schema.Advanced }}hidden{{ end }}>
101 <span>Private Key</span>
102 <textarea name="{{ $name }}-private" disabled>{{ $private }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400103 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400104 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400105 {{ end }}
106{{ end }}
107
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400108{{ define "header" }}
Davit Tabidzed1b742e2024-07-15 16:01:52 +0400109 {{ .App.Icon }}
110 <h1>{{ .App.Name }}</h1>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400111{{ end }}
gio18d5c682024-05-02 10:30:57 +0400112
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400113{{ define "extra_menu" }}
Davit Tabidze42a6b8d2024-07-16 19:56:50 +0400114 <li><a href="/app/{{ .App.Slug }}" class="{{ if eq $.CurrentPage .App.Name }}primary{{ end }}">{{ .App.Name }}</a></li>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400115 {{ range .Instances }}
Davit Tabidze42a6b8d2024-07-16 19:56:50 +0400116 <li><a href="/instance/{{ .Id }}" class="{{ if eq $.CurrentPage .Id }}primary{{ end }}">{{ .Id }}</a></li>
gio778577f2024-04-29 09:44:38 +0400117 {{ end }}
118{{ end }}
119
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400120{{ define "content"}}
121 {{ $schema := .App.Schema }}
122 {{ $networks := .AvailableNetworks }}
123 {{ $instance := .Instance }}
124 {{ $renderForm := true }}
125
126 {{ if .Task }}
127 {{if or (eq .Task.Status 0) (eq .Task.Status 1) }}
128 {{ $renderForm = false }}
129 Waiting for resources:
130 <ul class="progress">
131 {{ template "task" .Task.Subtasks }}
132 </ul>
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400133 {{ end }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400134 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400135
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400136 {{ if $renderForm }}
137 <form id="config-form">
138 {{ if $instance }}
139 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" ($instance.InputToValues $schema)) }}
140 {{ else }}
141 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" (dict)) }}
142 {{ end }}
143 {{ if $instance }}
144 <div class="grid">
145 <button type="submit" id="submit" name="update">Update</button>
146 <button type="submit" id="uninstall" name="remove">Uninstall</button>
147 </div>
148 {{ else }}
149 <button type="submit" id="submit">{{ if $instance }}Update{{ else }}Install{{ end }}</button>
150 {{ end }}
151 </form>
gio778577f2024-04-29 09:44:38 +0400152 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400153
154<div id="toast-failure" class="toast hidden">
155 <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2S2 6.477 2 12s4.477 10 10 10Zm3-6L9 8m0 8l6-8"/></svg> {{ if $instance }}Update failed{{ else}}Install failed{{ end }}
156</div>
157
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400158<div id="toast-uninstall-failure" class="toast hidden">
159 <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2S2 6.477 2 12s4.477 10 10 10Zm3-6L9 8m0 8l6-8"/></svg> Failed to uninstall application
160</div>
161
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400162<script>
gio3cdee592024-04-17 10:15:56 +0400163 let config = {{ if $instance }}JSON.parse({{ toJson ($instance.InputToValues $schema) }}){{ else }}{}{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400164
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400165 function setValue(name, value, config) {
166 let items = name.split(".")
167 for (let i = 0; i < items.length - 1; i++) {
168 if (!(items[i] in config)) {
169 config[items[i]] = {}
170 }
171 config = config[items[i]];
172 }
173 config[items[items.length - 1]] = value;
gio4ece99c2024-07-18 11:05:50 +0400174 }
175
176 function getValue(name, value) {
177 let items = name.split(".")
178 for (let i = 0; i < items.length - 1; i++) {
179 if (!(items[i] in config)) {
180 config[items[i]] = {}
181 }
182 config = config[items[i]];
183 }
184 return config[items[items.length - 1]];
185 }
186
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400187 function valueChanged(name, value) {
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400188 setValue(name, value, config);
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400189 }
190
gio4ece99c2024-07-18 11:05:50 +0400191 function multiNetworkSelected(name, network, selected) {
192 let v = getValue(name, config);
193 if (v === undefined) {
194 v = [];
195 }
196 if (selected) {
197 v.push(network);
198 } else {
199 v = v.filter((n) => n != network);
200 }
201 setValue(name, v, config);
202 document.getElementById(name).innerHTML = v.join(",");
203 }
204
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400205 function disableForm() {
206 document.querySelectorAll("#config-form input").forEach((i) => i.setAttribute("disabled", ""));
207 document.querySelectorAll("#config-form select").forEach((i) => i.setAttribute("disabled", ""));
208 document.querySelectorAll("#config-form button").forEach((i) => i.setAttribute("disabled", ""));
209 }
210
211 function enableForm() {
212 document.querySelectorAll("[aria-busy]").forEach((i) => i.removeAttribute("aria-busy"));
213 document.querySelectorAll("#config-form input").forEach((i) => i.removeAttribute("disabled"));
214 document.querySelectorAll("#config-form select").forEach((i) => i.removeAttribute("disabled"));
215 document.querySelectorAll("#config-form button").forEach((i) => i.removeAttribute("disabled"));
216 }
217
218 function installStarted() {
219 const submit = document.getElementById("submit");
220 submit.setAttribute("aria-busy", true);
221 submit.innerHTML = {{ if $instance }}"Updating ..."{{ else }}"Installing ..."{{ end }};
222 disableForm();
223 }
224
225 function uninstallStarted() {
226 const submit = document.getElementById("uninstall");
227 submit.setAttribute("aria-busy", true);
228 submit.innerHTML = "Uninstalling ...";
229 disableForm();
230 }
231
232 function actionFinished(toast) {
233 enableForm();
234 toast.classList.remove("hidden");
235 setTimeout(
236 () => toast.classList.add("hidden"),
237 2000,
238 );
239 }
240
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400241 function installFailed() {
242 actionFinished(document.getElementById("toast-failure"));
243 }
244
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400245 function uninstallFailed() {
246 actionFinished(document.getElementById("toast-uninstall-failure"));
247 }
248
gio44f621b2024-04-29 09:44:38 +0400249 const submitAddr = {{ if $instance }}"/api/instance/{{ $instance.Id }}/update"{{ else }}"/api/app/{{ .App.Slug }}/install"{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400250
251 async function install() {
252 installStarted();
253 const resp = await fetch(submitAddr, {
gio778577f2024-04-29 09:44:38 +0400254 method: "POST",
255 headers: {
256 "Content-Type": "application/json",
257 "Accept": "application/json",
258 },
259 body: JSON.stringify(config),
260 });
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400261 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400262 window.location = await resp.text();
263 } else {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400264 installFailed();
265 }
266 }
267
268 async function uninstall() {
269 {{ if $instance }}
270 uninstallStarted();
271 const resp = await fetch("/api/instance/{{ $instance.Id }}/remove", {
272 method: "POST",
273 });
274 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400275 window.location = await resp.text();
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400276 } else {
277 uninstallFailed();
278 }
279 {{ end }}
280 }
281
giof9f0bee2024-06-11 20:10:05 +0400282 const configForm = document.getElementById("config-form");
283 if (configForm) {
284 configForm.addEventListener("submit", (event) => {
285 event.preventDefault();
286 if (event.submitter.id === "submit") {
287 install();
288 } if (event.submitter.id === "uninstall") {
289 uninstall();
290 }
291 });
292 }
293
294 {{ if .Task }}
295 async function refresh() {
296 try {
297 const resp = await fetch(window.location.href);
298 if (resp.ok) {
299 var tmp = document.createElement("html");
300 tmp.innerHTML = await resp.text();
301 const progress = tmp.getElementsByClassName("progress")[0];
302 if (progress) {
303 document.getElementsByClassName("progress")[0].innerHTML = progress.innerHTML;
304 } else {
305 location.reload();
306 }
307 }
308 } catch (error) {
309 console.log(error);
310 } finally {
311 setTimeout(refresh, 3000);
312 }
313 }
314 setTimeout(refresh, 3000);
315 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400316</script>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400317
318{{end}}