| 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 }} > |
| 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 Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 50 | </label> |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 51 | {{ else if eq $schema.Kind 5 }} |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 52 | {{ $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 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 57 | <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 Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 60 | </label> |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 61 | <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 Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 65 | {{ 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 }} |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 71 | <label {{ if $schema.Advanced }}hidden{{ end }}> |
| 72 | <span>Public Key</span> |
| 73 | <textarea name="{{ $name }}-public" disabled>{{ $public }}</textarea> |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 74 | </label> |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 75 | <label {{ if $schema.Advanced }}hidden{{ end }}> |
| 76 | <span>Private Key</span> |
| 77 | <textarea name="{{ $name }}-private" disabled>{{ $private }}</textarea> |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 78 | </label> |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 79 | {{ end }} |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 80 | {{ end }} |
| 81 | {{ end }} |
| 82 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 83 | {{ define "header" }} |
| 84 | <h1>{{ .App.Icon }}{{ .App.Name }}</h1> |
| 85 | {{ end }} |
| gio | 18d5c68 | 2024-05-02 10:30:57 +0400 | [diff] [blame] | 86 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 87 | {{ 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> |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 91 | {{ end }} |
| 92 | {{ end }} |
| 93 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 94 | {{ 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 107 | {{ end }} |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 108 | {{ end }} |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 109 | |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 110 | {{ 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> |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 126 | {{ end }} |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 127 | |
| 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 132 | <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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 136 | <script> |
| gio | 3cdee59 | 2024-04-17 10:15:56 +0400 | [diff] [blame] | 137 | let config = {{ if $instance }}JSON.parse({{ toJson ($instance.InputToValues $schema) }}){{ else }}{}{{ end }}; |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 138 | |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 139 | 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 149 | function valueChanged(name, value) { |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 150 | setValue(name, value, config); |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 151 | } |
| 152 | |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 153 | 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 189 | function installFailed() { |
| 190 | actionFinished(document.getElementById("toast-failure")); |
| 191 | } |
| 192 | |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 193 | function uninstallFailed() { |
| 194 | actionFinished(document.getElementById("toast-uninstall-failure")); |
| 195 | } |
| 196 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 197 | 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] | 198 | |
| 199 | async function install() { |
| 200 | installStarted(); |
| 201 | const resp = await fetch(submitAddr, { |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 202 | method: "POST", |
| 203 | headers: { |
| 204 | "Content-Type": "application/json", |
| 205 | "Accept": "application/json", |
| 206 | }, |
| 207 | body: JSON.stringify(config), |
| 208 | }); |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 209 | if (resp.status === 200) { |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 210 | window.location = await resp.text(); |
| 211 | } else { |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 212 | 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) { |
| gio | 778577f | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 223 | window.location = await resp.text(); |
| Giorgi Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 224 | } else { |
| 225 | uninstallFailed(); |
| 226 | } |
| 227 | {{ end }} |
| 228 | } |
| 229 | |
| gio | f9f0bee | 2024-06-11 20:10:05 +0400 | [diff] [blame^] | 230 | 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 Lekveishvili | 4257b90 | 2023-07-07 17:08:42 +0400 | [diff] [blame] | 264 | </script> |
| Davit Tabidze | 3ec24cf | 2024-05-22 14:06:02 +0400 | [diff] [blame] | 265 | |
| 266 | {{end}} |