blob: 5ce3534738683239e09cac4ffc3bdcb28ee7706d [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 }} >
45 {{ if not $readonly }}<option disabled selected value>Available networks</option>{{ end }}
46 {{ 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>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040051 {{ else if eq $schema.Kind 5 }}
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040052 {{ $auth := index $data $name }}
53 {{ $authEnabled := false }}
54 {{ $authGroups := "" }}
55 {{ if and $auth (index $auth "enabled") }}{{ $authEnabled = true }}{{ end }}
56 {{ if and $auth (index $auth "groups") }}{{ $authGroups = index $auth "groups" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +040057 <label {{ if $schema.Advanced }}hidden{{ end }}>
58 <input type="checkbox" role="swtich" name="authEnabled" oninput="valueChanged('{{- $name -}}.enabled', this.checked)" {{ if $readonly }}disabled{{ end }} {{ if $authEnabled }}checked{{ end }} />
59 <span>Require authentication</span>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040060 </label>
gio44f621b2024-04-29 09:44:38 +040061 <label for="authGroups">
62 <span>Authentication groups</span>
63 <input type="text" name="authGroups" oninput="valueChanged('{{- $name -}}.groups', this.value)" {{ if $readonly }}disabled{{ end }} value="{{ $authGroups }}" />
64 </label>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +040065 {{ else if eq $schema.Kind 6 }}
66 {{ $sshKey := index $data $name }}
67 {{ $public := "" }}
68 {{ $private := "" }}
69 {{ if $sshKey }}{{ $public = index $sshKey "public" }}{{ end }}
70 {{ if $sshKey }}{{ $private = index $sshKey "private" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +040071 <label {{ if $schema.Advanced }}hidden{{ end }}>
72 <span>Public Key</span>
73 <textarea name="{{ $name }}-public" disabled>{{ $public }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +040074 </label>
gio44f621b2024-04-29 09:44:38 +040075 <label {{ if $schema.Advanced }}hidden{{ end }}>
76 <span>Private Key</span>
77 <textarea name="{{ $name }}-private" disabled>{{ $private }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +040078 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040079 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040080 {{ end }}
81{{ end }}
82
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040083{{ define "header" }}
84 <h1>{{ .App.Icon }}{{ .App.Name }}</h1>
85{{ end }}
gio18d5c682024-05-02 10:30:57 +040086
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040087{{ define "extra_menu" }}
88 <li><a href="/app/{{ .App.Slug }}" class="{{ if eq $.CurrentPage .App.Name }}outline{{ else }}secondary{{ end }}">{{ .App.Name }}</a></li>
89 {{ range .Instances }}
90 <li><a href="/instance/{{ .Id }}" class="{{ if eq $.CurrentPage .Id }}outline{{ else }}secondary{{ end }}">{{ .Id }}</a></li>
gio778577f2024-04-29 09:44:38 +040091 {{ end }}
92{{ end }}
93
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040094{{ define "content"}}
95 {{ $schema := .App.Schema }}
96 {{ $networks := .AvailableNetworks }}
97 {{ $instance := .Instance }}
98 {{ $renderForm := true }}
99
100 {{ if .Task }}
101 {{if or (eq .Task.Status 0) (eq .Task.Status 1) }}
102 {{ $renderForm = false }}
103 Waiting for resources:
104 <ul class="progress">
105 {{ template "task" .Task.Subtasks }}
106 </ul>
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400107 {{ end }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400108 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400109
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400110 {{ if $renderForm }}
111 <form id="config-form">
112 {{ if $instance }}
113 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" ($instance.InputToValues $schema)) }}
114 {{ else }}
115 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" (dict)) }}
116 {{ end }}
117 {{ if $instance }}
118 <div class="grid">
119 <button type="submit" id="submit" name="update">Update</button>
120 <button type="submit" id="uninstall" name="remove">Uninstall</button>
121 </div>
122 {{ else }}
123 <button type="submit" id="submit">{{ if $instance }}Update{{ else }}Install{{ end }}</button>
124 {{ end }}
125 </form>
gio778577f2024-04-29 09:44:38 +0400126 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400127
128<div id="toast-failure" class="toast hidden">
129 <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 }}
130</div>
131
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400132<div id="toast-uninstall-failure" class="toast hidden">
133 <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
134</div>
135
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400136<script>
gio3cdee592024-04-17 10:15:56 +0400137 let config = {{ if $instance }}JSON.parse({{ toJson ($instance.InputToValues $schema) }}){{ else }}{}{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400138
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400139 function setValue(name, value, config) {
140 let items = name.split(".")
141 for (let i = 0; i < items.length - 1; i++) {
142 if (!(items[i] in config)) {
143 config[items[i]] = {}
144 }
145 config = config[items[i]];
146 }
147 config[items[items.length - 1]] = value;
148}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400149 function valueChanged(name, value) {
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400150 setValue(name, value, config);
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400151 }
152
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400153 function disableForm() {
154 document.querySelectorAll("#config-form input").forEach((i) => i.setAttribute("disabled", ""));
155 document.querySelectorAll("#config-form select").forEach((i) => i.setAttribute("disabled", ""));
156 document.querySelectorAll("#config-form button").forEach((i) => i.setAttribute("disabled", ""));
157 }
158
159 function enableForm() {
160 document.querySelectorAll("[aria-busy]").forEach((i) => i.removeAttribute("aria-busy"));
161 document.querySelectorAll("#config-form input").forEach((i) => i.removeAttribute("disabled"));
162 document.querySelectorAll("#config-form select").forEach((i) => i.removeAttribute("disabled"));
163 document.querySelectorAll("#config-form button").forEach((i) => i.removeAttribute("disabled"));
164 }
165
166 function installStarted() {
167 const submit = document.getElementById("submit");
168 submit.setAttribute("aria-busy", true);
169 submit.innerHTML = {{ if $instance }}"Updating ..."{{ else }}"Installing ..."{{ end }};
170 disableForm();
171 }
172
173 function uninstallStarted() {
174 const submit = document.getElementById("uninstall");
175 submit.setAttribute("aria-busy", true);
176 submit.innerHTML = "Uninstalling ...";
177 disableForm();
178 }
179
180 function actionFinished(toast) {
181 enableForm();
182 toast.classList.remove("hidden");
183 setTimeout(
184 () => toast.classList.add("hidden"),
185 2000,
186 );
187 }
188
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400189 function installFailed() {
190 actionFinished(document.getElementById("toast-failure"));
191 }
192
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400193 function uninstallFailed() {
194 actionFinished(document.getElementById("toast-uninstall-failure"));
195 }
196
gio44f621b2024-04-29 09:44:38 +0400197 const submitAddr = {{ if $instance }}"/api/instance/{{ $instance.Id }}/update"{{ else }}"/api/app/{{ .App.Slug }}/install"{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400198
199 async function install() {
200 installStarted();
201 const resp = await fetch(submitAddr, {
gio778577f2024-04-29 09:44:38 +0400202 method: "POST",
203 headers: {
204 "Content-Type": "application/json",
205 "Accept": "application/json",
206 },
207 body: JSON.stringify(config),
208 });
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400209 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400210 window.location = await resp.text();
211 } else {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400212 installFailed();
213 }
214 }
215
216 async function uninstall() {
217 {{ if $instance }}
218 uninstallStarted();
219 const resp = await fetch("/api/instance/{{ $instance.Id }}/remove", {
220 method: "POST",
221 });
222 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400223 window.location = await resp.text();
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400224 } else {
225 uninstallFailed();
226 }
227 {{ end }}
228 }
229
giof9f0bee2024-06-11 20:10:05 +0400230 const configForm = document.getElementById("config-form");
231 if (configForm) {
232 configForm.addEventListener("submit", (event) => {
233 event.preventDefault();
234 if (event.submitter.id === "submit") {
235 install();
236 } if (event.submitter.id === "uninstall") {
237 uninstall();
238 }
239 });
240 }
241
242 {{ if .Task }}
243 async function refresh() {
244 try {
245 const resp = await fetch(window.location.href);
246 if (resp.ok) {
247 var tmp = document.createElement("html");
248 tmp.innerHTML = await resp.text();
249 const progress = tmp.getElementsByClassName("progress")[0];
250 if (progress) {
251 document.getElementsByClassName("progress")[0].innerHTML = progress.innerHTML;
252 } else {
253 location.reload();
254 }
255 }
256 } catch (error) {
257 console.log(error);
258 } finally {
259 setTimeout(refresh, 3000);
260 }
261 }
262 setTimeout(refresh, 3000);
263 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400264</script>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400265
266{{end}}