blob: e6fe0963e13da41b2b3ae30ace31ffba6ea1c2c9 [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 }}
giobb338e22024-07-18 18:09:22 +040044 <details class="dropdown">
45 {{ $selectedNetwork := index $data $name }}
46 <summary id="{{ $name }}">{{ $selectedNetwork }}</summary>
47 <ul>
48 {{ range $networks }}
49 {{ $selected := eq $selectedNetwork .Name }}
50 <li>
51 <label>
52 <input type="radio" name="{{ $name }}" oninput="networkSelected('{{ $name }}', '{{ .Name }}', this.checked)" {{ if $selected }}checked{{ end }} />
53 {{ .Name }}
54 </label>
55 </li>
56 {{ end }}
57 </ul>
58 </details>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040059 </label>
gio4ece99c2024-07-18 11:05:50 +040060 {{ else if eq $schema.Kind 10 }}
61 <label {{ if $schema.Advanced }}hidden{{ end }}>
62 {{ $schema.Name }}
63 <details class="dropdown">
64 {{ $selectedNetworks := index $data $name }}
65 <summary id="{{ $name }}">{{ $selectedNetworks | join "," }}</summary>
66 <ul>
67 {{ range $networks }}
68 {{ $networkName := .Name }}
69 {{ $selected := false }}
70 {{ range $selectedNetworks }}
71 {{ if eq . $networkName }}
72 {{ $selected = true }}
73 {{ end }}
74 {{ end }}
75 <li>
76 <label>
77 <input type="checkbox" name="{{ $networkName }}" oninput="multiNetworkSelected('{{ $name }}', '{{ $networkName }}', this.checked)" {{ if $selected }}checked{{ end }} />
78 {{ .Name }}
79 </label>
80 </li>
81 {{ end }}
82 </ul>
83 </details>
84 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040085 {{ else if eq $schema.Kind 5 }}
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040086 {{ $auth := index $data $name }}
87 {{ $authEnabled := false }}
88 {{ $authGroups := "" }}
89 {{ if and $auth (index $auth "enabled") }}{{ $authEnabled = true }}{{ end }}
90 {{ if and $auth (index $auth "groups") }}{{ $authGroups = index $auth "groups" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +040091 <label {{ if $schema.Advanced }}hidden{{ end }}>
92 <input type="checkbox" role="swtich" name="authEnabled" oninput="valueChanged('{{- $name -}}.enabled', this.checked)" {{ if $readonly }}disabled{{ end }} {{ if $authEnabled }}checked{{ end }} />
93 <span>Require authentication</span>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040094 </label>
gio44f621b2024-04-29 09:44:38 +040095 <label for="authGroups">
96 <span>Authentication groups</span>
97 <input type="text" name="authGroups" oninput="valueChanged('{{- $name -}}.groups', this.value)" {{ if $readonly }}disabled{{ end }} value="{{ $authGroups }}" />
98 </label>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +040099 {{ else if eq $schema.Kind 6 }}
100 {{ $sshKey := index $data $name }}
101 {{ $public := "" }}
102 {{ $private := "" }}
103 {{ if $sshKey }}{{ $public = index $sshKey "public" }}{{ end }}
104 {{ if $sshKey }}{{ $private = index $sshKey "private" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +0400105 <label {{ if $schema.Advanced }}hidden{{ end }}>
106 <span>Public Key</span>
107 <textarea name="{{ $name }}-public" disabled>{{ $public }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400108 </label>
gio44f621b2024-04-29 09:44:38 +0400109 <label {{ if $schema.Advanced }}hidden{{ end }}>
110 <span>Private Key</span>
111 <textarea name="{{ $name }}-private" disabled>{{ $private }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400112 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400113 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400114 {{ end }}
115{{ end }}
116
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400117{{ define "header" }}
Davit Tabidzed1b742e2024-07-15 16:01:52 +0400118 {{ .App.Icon }}
119 <h1>{{ .App.Name }}</h1>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400120{{ end }}
gio18d5c682024-05-02 10:30:57 +0400121
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400122{{ define "extra_menu" }}
Davit Tabidze42a6b8d2024-07-16 19:56:50 +0400123 <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 +0400124 {{ range .Instances }}
Davit Tabidze42a6b8d2024-07-16 19:56:50 +0400125 <li><a href="/instance/{{ .Id }}" class="{{ if eq $.CurrentPage .Id }}primary{{ end }}">{{ .Id }}</a></li>
gio778577f2024-04-29 09:44:38 +0400126 {{ end }}
127{{ end }}
128
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400129{{ define "content"}}
130 {{ $schema := .App.Schema }}
131 {{ $networks := .AvailableNetworks }}
132 {{ $instance := .Instance }}
133 {{ $renderForm := true }}
134
135 {{ if .Task }}
136 {{if or (eq .Task.Status 0) (eq .Task.Status 1) }}
137 {{ $renderForm = false }}
138 Waiting for resources:
139 <ul class="progress">
140 {{ template "task" .Task.Subtasks }}
141 </ul>
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400142 {{ end }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400143 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400144
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400145 {{ if $renderForm }}
146 <form id="config-form">
147 {{ if $instance }}
148 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" ($instance.InputToValues $schema)) }}
149 {{ else }}
150 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" (dict)) }}
151 {{ end }}
152 {{ if $instance }}
153 <div class="grid">
154 <button type="submit" id="submit" name="update">Update</button>
155 <button type="submit" id="uninstall" name="remove">Uninstall</button>
156 </div>
157 {{ else }}
158 <button type="submit" id="submit">{{ if $instance }}Update{{ else }}Install{{ end }}</button>
159 {{ end }}
160 </form>
gio778577f2024-04-29 09:44:38 +0400161 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400162
163<div id="toast-failure" class="toast hidden">
164 <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 }}
165</div>
166
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400167<div id="toast-uninstall-failure" class="toast hidden">
168 <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
169</div>
170
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400171<script>
gio3cdee592024-04-17 10:15:56 +0400172 let config = {{ if $instance }}JSON.parse({{ toJson ($instance.InputToValues $schema) }}){{ else }}{}{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400173
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400174 function setValue(name, value, config) {
175 let items = name.split(".")
176 for (let i = 0; i < items.length - 1; i++) {
177 if (!(items[i] in config)) {
178 config[items[i]] = {}
179 }
180 config = config[items[i]];
181 }
182 config[items[items.length - 1]] = value;
gio4ece99c2024-07-18 11:05:50 +0400183 }
184
185 function getValue(name, value) {
186 let items = name.split(".")
187 for (let i = 0; i < items.length - 1; i++) {
188 if (!(items[i] in config)) {
189 config[items[i]] = {}
190 }
191 config = config[items[i]];
192 }
193 return config[items[items.length - 1]];
194 }
195
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400196 function valueChanged(name, value) {
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400197 setValue(name, value, config);
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400198 }
199
giobb338e22024-07-18 18:09:22 +0400200 function networkSelected(name, network, selected) {
201 console.log(selected);
202 setValue(name, network, config);
203 let summary = document.getElementById(name);
204 summary.innerHTML = network;
205 summary.parentNode.removeAttribute("open");
206 }
207
gio4ece99c2024-07-18 11:05:50 +0400208 function multiNetworkSelected(name, network, selected) {
209 let v = getValue(name, config);
210 if (v === undefined) {
211 v = [];
212 }
213 if (selected) {
214 v.push(network);
215 } else {
216 v = v.filter((n) => n != network);
217 }
218 setValue(name, v, config);
219 document.getElementById(name).innerHTML = v.join(",");
220 }
221
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400222 function disableForm() {
223 document.querySelectorAll("#config-form input").forEach((i) => i.setAttribute("disabled", ""));
224 document.querySelectorAll("#config-form select").forEach((i) => i.setAttribute("disabled", ""));
225 document.querySelectorAll("#config-form button").forEach((i) => i.setAttribute("disabled", ""));
226 }
227
228 function enableForm() {
229 document.querySelectorAll("[aria-busy]").forEach((i) => i.removeAttribute("aria-busy"));
230 document.querySelectorAll("#config-form input").forEach((i) => i.removeAttribute("disabled"));
231 document.querySelectorAll("#config-form select").forEach((i) => i.removeAttribute("disabled"));
232 document.querySelectorAll("#config-form button").forEach((i) => i.removeAttribute("disabled"));
233 }
234
235 function installStarted() {
236 const submit = document.getElementById("submit");
237 submit.setAttribute("aria-busy", true);
238 submit.innerHTML = {{ if $instance }}"Updating ..."{{ else }}"Installing ..."{{ end }};
239 disableForm();
240 }
241
242 function uninstallStarted() {
243 const submit = document.getElementById("uninstall");
244 submit.setAttribute("aria-busy", true);
245 submit.innerHTML = "Uninstalling ...";
246 disableForm();
247 }
248
249 function actionFinished(toast) {
250 enableForm();
251 toast.classList.remove("hidden");
252 setTimeout(
253 () => toast.classList.add("hidden"),
254 2000,
255 );
256 }
257
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400258 function installFailed() {
259 actionFinished(document.getElementById("toast-failure"));
260 }
261
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400262 function uninstallFailed() {
263 actionFinished(document.getElementById("toast-uninstall-failure"));
264 }
265
gio44f621b2024-04-29 09:44:38 +0400266 const submitAddr = {{ if $instance }}"/api/instance/{{ $instance.Id }}/update"{{ else }}"/api/app/{{ .App.Slug }}/install"{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400267
268 async function install() {
269 installStarted();
270 const resp = await fetch(submitAddr, {
gio778577f2024-04-29 09:44:38 +0400271 method: "POST",
272 headers: {
273 "Content-Type": "application/json",
274 "Accept": "application/json",
275 },
276 body: JSON.stringify(config),
277 });
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400278 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400279 window.location = await resp.text();
280 } else {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400281 installFailed();
282 }
283 }
284
285 async function uninstall() {
286 {{ if $instance }}
287 uninstallStarted();
288 const resp = await fetch("/api/instance/{{ $instance.Id }}/remove", {
289 method: "POST",
290 });
291 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400292 window.location = await resp.text();
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400293 } else {
294 uninstallFailed();
295 }
296 {{ end }}
297 }
298
giof9f0bee2024-06-11 20:10:05 +0400299 const configForm = document.getElementById("config-form");
300 if (configForm) {
301 configForm.addEventListener("submit", (event) => {
302 event.preventDefault();
303 if (event.submitter.id === "submit") {
304 install();
305 } if (event.submitter.id === "uninstall") {
306 uninstall();
307 }
308 });
309 }
310
311 {{ if .Task }}
312 async function refresh() {
313 try {
314 const resp = await fetch(window.location.href);
315 if (resp.ok) {
316 var tmp = document.createElement("html");
317 tmp.innerHTML = await resp.text();
318 const progress = tmp.getElementsByClassName("progress")[0];
319 if (progress) {
320 document.getElementsByClassName("progress")[0].innerHTML = progress.innerHTML;
321 } else {
322 location.reload();
323 }
324 }
325 } catch (error) {
326 console.log(error);
327 } finally {
328 setTimeout(refresh, 3000);
329 }
330 }
331 setTimeout(refresh, 3000);
332 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400333</script>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400334
335{{end}}