| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 1 | {{ define "task" }} |
| 2 | {{ range . }} |
| 3 | <li aria-busy="{{ eq .Status 1 }}"> |
| gio | f9f0bee | 2024-06-11 20:10:05 +0400 | [diff] [blame] | 4 | {{ 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 }} |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 5 | {{ if .Subtasks }} |
| 6 | <ul> |
| 7 | {{ template "task" .Subtasks }} |
| 8 | </ul> |
| 9 | {{ end }} |
| 10 | </li> |
| 11 | {{ end }} |
| 12 | {{ end }} |
| 13 | |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 14 | {{ define "schema-form" }} |
| 15 | {{ $readonly := .ReadOnly }} |
| 16 | {{ $networks := .AvailableNetworks }} |
| 17 | {{ $data := .Data }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 18 | {{ range $f := .Schema.Fields }} |
| 19 | {{ $name := $f.Name }} |
| 20 | {{ $schema := $f.Schema }} |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 21 | {{ if eq $schema.Kind 0 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 22 | <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 Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 25 | </label> |
| Giorgi Lekveishvili | b59b7c2 | 2024-04-03 22:17:50 +0400 | [diff] [blame] | 26 | {{ else if eq $schema.Kind 7 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 27 | <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 Lekveishvili | b59b7c2 | 2024-04-03 22:17:50 +0400 | [diff] [blame] | 30 | </label> |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 31 | {{ else if eq $schema.Kind 1 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 32 | <label {{ if $schema.Advanced }}hidden{{ end }}> |
| 33 | {{ $schema.Name }} |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 34 | <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" /> |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 35 | {{ else if eq $schema.Kind 4 }} |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 36 | </label> |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 37 | <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 Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 41 | {{ else if eq $schema.Kind 3 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 42 | <label {{ if $schema.Advanced }}hidden{{ end }}> |
| 43 | {{ $schema.Name }} |
| 44 | <select name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} > |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame^] | 45 | {{ if not $readonly }}<option disabled selected value></option>{{ end }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 46 | {{ range $networks }} |
| 47 | <option {{if eq .Name (index $data $name) }}selected{{ end }}>{{ .Name }}</option> |
| 48 | {{ end }} |
| 49 | </select> |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 50 | </label> |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame^] | 51 | {{ 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 Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 76 | {{ else if eq $schema.Kind 5 }} |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 77 | {{ $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 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 82 | <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 Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 85 | </label> |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 86 | <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 Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 90 | {{ 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 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 96 | <label {{ if $schema.Advanced }}hidden{{ end }}> |
| 97 | <span>Public Key</span> |
| 98 | <textarea name="{{ $name }}-public" disabled>{{ $public }}</textarea> |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 99 | </label> |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 100 | <label {{ if $schema.Advanced }}hidden{{ end }}> |
| 101 | <span>Private Key</span> |
| 102 | <textarea name="{{ $name }}-private" disabled>{{ $private }}</textarea> |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 103 | </label> |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 104 | {{ end }} |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 105 | {{ end }} |
| 106 | {{ end }} |
| 107 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 108 | {{ define "header" }} |
| Davit Tabidze | d1b742e | 2024-07-15 16:01:52 +0400 | [diff] [blame] | 109 | {{ .App.Icon }} |
| 110 | <h1>{{ .App.Name }}</h1> |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 111 | {{ end }} |
| gio | 18d5c68 | 2024-05-02 10:30:57 +0400 | [diff] [blame] | 112 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 113 | {{ define "extra_menu" }} |
| Davit Tabidze | 42a6b8d | 2024-07-16 19:56:50 +0400 | [diff] [blame] | 114 | <li><a href="/app/{{ .App.Slug }}" class="{{ if eq $.CurrentPage .App.Name }}primary{{ end }}">{{ .App.Name }}</a></li> |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 115 | {{ range .Instances }} |
| Davit Tabidze | 42a6b8d | 2024-07-16 19:56:50 +0400 | [diff] [blame] | 116 | <li><a href="/instance/{{ .Id }}" class="{{ if eq $.CurrentPage .Id }}primary{{ end }}">{{ .Id }}</a></li> |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 117 | {{ end }} |
| 118 | {{ end }} |
| 119 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 120 | {{ 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 133 | {{ end }} |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 134 | {{ end }} |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 135 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 136 | {{ 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> |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 152 | {{ end }} |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 153 | |
| 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 158 | <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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 162 | <script> |
| gio | 3cdee59 | 2024-04-17 10:15:56 +0400 | [diff] [blame] | 163 | let config = {{ if $instance }}JSON.parse({{ toJson ($instance.InputToValues $schema) }}){{ else }}{}{{ end }}; |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 164 | |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 165 | 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; |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame^] | 174 | } |
| 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 187 | function valueChanged(name, value) { |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 188 | setValue(name, value, config); |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 189 | } |
| 190 | |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame^] | 191 | 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 205 | 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 241 | function installFailed() { |
| 242 | actionFinished(document.getElementById("toast-failure")); |
| 243 | } |
| 244 | |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 245 | function uninstallFailed() { |
| 246 | actionFinished(document.getElementById("toast-uninstall-failure")); |
| 247 | } |
| 248 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 249 | const submitAddr = {{ if $instance }}"/api/instance/{{ $instance.Id }}/update"{{ else }}"/api/app/{{ .App.Slug }}/install"{{ end }}; |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 250 | |
| 251 | async function install() { |
| 252 | installStarted(); |
| 253 | const resp = await fetch(submitAddr, { |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 254 | method: "POST", |
| 255 | headers: { |
| 256 | "Content-Type": "application/json", |
| 257 | "Accept": "application/json", |
| 258 | }, |
| 259 | body: JSON.stringify(config), |
| 260 | }); |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 261 | if (resp.status === 200) { |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 262 | window.location = await resp.text(); |
| 263 | } else { |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 264 | 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) { |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 275 | window.location = await resp.text(); |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 276 | } else { |
| 277 | uninstallFailed(); |
| 278 | } |
| 279 | {{ end }} |
| 280 | } |
| 281 | |
| gio | f9f0bee | 2024-06-11 20:10:05 +0400 | [diff] [blame] | 282 | 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 316 | </script> |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 317 | |
| 318 | {{end}} |