blob: 4fecf363bd90ba8393d4a2576fbeef68aa342275 [file] [log] [blame]
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04001package welcome
2
3import (
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +04004 "context"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04005 "embed"
6 "encoding/json"
7 "fmt"
8 "html/template"
9 "io/ioutil"
10 "log"
11 "net/http"
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040012 "time"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040013
14 "github.com/Masterminds/sprig/v3"
gioaa0fcdb2024-06-10 22:19:25 +040015 "github.com/gorilla/mux"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040016
17 "github.com/giolekva/pcloud/core/installer"
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040018 "github.com/giolekva/pcloud/core/installer/tasks"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040019)
20
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040021//go:embed appmanager-tmpl/*
22var appTmpls embed.FS
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040023
24type AppManagerServer struct {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040025 port int
26 m *installer.AppManager
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040027 r installer.AppRepository
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040028 reconciler tasks.Reconciler
gio778577f2024-04-29 09:44:38 +040029 h installer.HelmReleaseMonitor
30 tasks map[string]tasks.Task
gio1cd65152024-08-16 08:18:49 +040031 ta map[string]installer.EnvApp
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040032 tmpl tmplts
33}
34
35type tmplts struct {
36 index *template.Template
37 app *template.Template
38}
39
40func parseTemplatesAppManager(fs embed.FS) (tmplts, error) {
41 base, err := template.New("base.html").Funcs(template.FuncMap(sprig.FuncMap())).ParseFS(fs, "appmanager-tmpl/base.html")
42 if err != nil {
43 return tmplts{}, err
44 }
45 parse := func(path string) (*template.Template, error) {
46 if b, err := base.Clone(); err != nil {
47 return nil, err
48 } else {
49 return b.ParseFS(fs, path)
50 }
51 }
52 index, err := parse("appmanager-tmpl/index.html")
53 if err != nil {
54 return tmplts{}, err
55 }
56 app, err := parse("appmanager-tmpl/app.html")
57 if err != nil {
58 return tmplts{}, err
59 }
60 return tmplts{index, app}, nil
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040061}
62
63func NewAppManagerServer(
64 port int,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040065 m *installer.AppManager,
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040066 r installer.AppRepository,
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040067 reconciler tasks.Reconciler,
gio778577f2024-04-29 09:44:38 +040068 h installer.HelmReleaseMonitor,
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040069) (*AppManagerServer, error) {
70 tmpl, err := parseTemplatesAppManager(appTmpls)
71 if err != nil {
72 return nil, err
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040073 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040074 return &AppManagerServer{
75 port: port,
76 m: m,
77 r: r,
78 reconciler: reconciler,
79 h: h,
80 tasks: make(map[string]tasks.Task),
gio1cd65152024-08-16 08:18:49 +040081 ta: make(map[string]installer.EnvApp),
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040082 tmpl: tmpl,
83 }, nil
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040084}
85
gio09f8efa2024-06-10 22:35:24 +040086type cachingHandler struct {
87 h http.Handler
88}
89
90func (h cachingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
91 w.Header().Set("Cache-Control", "max-age=604800")
92 h.h.ServeHTTP(w, r)
93}
94
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040095func (s *AppManagerServer) Start() error {
gioaa0fcdb2024-06-10 22:19:25 +040096 r := mux.NewRouter()
gio1bf00802024-08-17 12:31:41 +040097 r.PathPrefix("/stat/").Handler(cachingHandler{http.FileServer(http.FS(statAssets))})
giocb34ad22024-07-11 08:01:13 +040098 r.HandleFunc("/api/networks", s.handleNetworks).Methods(http.MethodGet)
gioaa0fcdb2024-06-10 22:19:25 +040099 r.HandleFunc("/api/app-repo", s.handleAppRepo)
100 r.HandleFunc("/api/app/{slug}/install", s.handleAppInstall).Methods(http.MethodPost)
101 r.HandleFunc("/api/app/{slug}", s.handleApp).Methods(http.MethodGet)
102 r.HandleFunc("/api/instance/{slug}", s.handleInstance).Methods(http.MethodGet)
103 r.HandleFunc("/api/instance/{slug}/update", s.handleAppUpdate).Methods(http.MethodPost)
104 r.HandleFunc("/api/instance/{slug}/remove", s.handleAppRemove).Methods(http.MethodPost)
gioaa0fcdb2024-06-10 22:19:25 +0400105 r.HandleFunc("/app/{slug}", s.handleAppUI).Methods(http.MethodGet)
106 r.HandleFunc("/instance/{slug}", s.handleInstanceUI).Methods(http.MethodGet)
Davit Tabidze780a0d02024-08-05 20:53:26 +0400107 r.HandleFunc("/{pageType}", s.handleAppsList).Methods(http.MethodGet)
108 r.HandleFunc("/", s.handleAppsList).Methods(http.MethodGet)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400109 fmt.Printf("Starting HTTP server on port: %d\n", s.port)
gioaa0fcdb2024-06-10 22:19:25 +0400110 return http.ListenAndServe(fmt.Sprintf(":%d", s.port), r)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400111}
112
113type app struct {
gio3cdee592024-04-17 10:15:56 +0400114 Name string `json:"name"`
115 Icon template.HTML `json:"icon"`
116 ShortDescription string `json:"shortDescription"`
117 Slug string `json:"slug"`
118 Instances []installer.AppInstanceConfig `json:"instances,omitempty"`
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400119}
120
giocb34ad22024-07-11 08:01:13 +0400121func (s *AppManagerServer) handleNetworks(w http.ResponseWriter, r *http.Request) {
122 env, err := s.m.Config()
123 if err != nil {
124 http.Error(w, err.Error(), http.StatusInternalServerError)
125 return
126 }
127 networks, err := s.m.CreateNetworks(env)
128 if err != nil {
129 http.Error(w, err.Error(), http.StatusInternalServerError)
130 return
131 }
132 if err := json.NewEncoder(w).Encode(networks); err != nil {
133 http.Error(w, err.Error(), http.StatusInternalServerError)
134 return
135 }
136}
137
gioaa0fcdb2024-06-10 22:19:25 +0400138func (s *AppManagerServer) handleAppRepo(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400139 all, err := s.r.GetAll()
140 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400141 http.Error(w, err.Error(), http.StatusInternalServerError)
142 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400143 }
144 resp := make([]app, len(all))
145 for i, a := range all {
gio44f621b2024-04-29 09:44:38 +0400146 resp[i] = app{a.Name(), a.Icon(), a.Description(), a.Slug(), nil}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400147 }
gioaa0fcdb2024-06-10 22:19:25 +0400148 w.Header().Set("Content-Type", "application/json")
149 if err := json.NewEncoder(w).Encode(resp); err != nil {
150 http.Error(w, err.Error(), http.StatusInternalServerError)
151 return
152 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400153}
154
gioaa0fcdb2024-06-10 22:19:25 +0400155func (s *AppManagerServer) handleApp(w http.ResponseWriter, r *http.Request) {
156 slug, ok := mux.Vars(r)["slug"]
157 if !ok {
158 http.Error(w, "empty slug", http.StatusBadRequest)
159 return
160 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400161 a, err := s.r.Find(slug)
162 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400163 http.Error(w, err.Error(), http.StatusInternalServerError)
164 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400165 }
gio308105e2024-04-19 13:12:13 +0400166 instances, err := s.m.FindAllAppInstances(slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400167 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400168 http.Error(w, err.Error(), http.StatusInternalServerError)
169 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400170 }
gioaa0fcdb2024-06-10 22:19:25 +0400171 resp := app{a.Name(), a.Icon(), a.Description(), a.Slug(), instances}
172 w.Header().Set("Content-Type", "application/json")
173 if err := json.NewEncoder(w).Encode(resp); err != nil {
174 http.Error(w, err.Error(), http.StatusInternalServerError)
175 return
176 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400177}
178
gioaa0fcdb2024-06-10 22:19:25 +0400179func (s *AppManagerServer) handleInstance(w http.ResponseWriter, r *http.Request) {
180 slug, ok := mux.Vars(r)["slug"]
181 if !ok {
182 http.Error(w, "empty slug", http.StatusBadRequest)
183 return
184 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400185 instance, err := s.m.FindInstance(slug)
186 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400187 http.Error(w, err.Error(), http.StatusInternalServerError)
188 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400189 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400190 a, err := s.r.Find(instance.AppId)
191 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400192 http.Error(w, err.Error(), http.StatusInternalServerError)
193 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400194 }
gioaa0fcdb2024-06-10 22:19:25 +0400195 resp := app{a.Name(), a.Icon(), a.Description(), a.Slug(), []installer.AppInstanceConfig{*instance}}
196 w.Header().Set("Content-Type", "application/json")
197 if err := json.NewEncoder(w).Encode(resp); err != nil {
198 http.Error(w, err.Error(), http.StatusInternalServerError)
199 return
200 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400201}
202
gioaa0fcdb2024-06-10 22:19:25 +0400203func (s *AppManagerServer) handleAppInstall(w http.ResponseWriter, r *http.Request) {
204 slug, ok := mux.Vars(r)["slug"]
205 if !ok {
206 http.Error(w, "empty slug", http.StatusBadRequest)
207 return
208 }
209 contents, err := ioutil.ReadAll(r.Body)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400210 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400211 http.Error(w, err.Error(), http.StatusInternalServerError)
212 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400213 }
214 var values map[string]any
215 if err := json.Unmarshal(contents, &values); err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400216 http.Error(w, err.Error(), http.StatusInternalServerError)
217 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400218 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400219 log.Printf("Values: %+v\n", values)
gio3cdee592024-04-17 10:15:56 +0400220 a, err := installer.FindEnvApp(s.r, slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400221 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400222 http.Error(w, err.Error(), http.StatusInternalServerError)
223 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400224 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400225 log.Printf("Found application: %s\n", slug)
gio3cdee592024-04-17 10:15:56 +0400226 env, err := s.m.Config()
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400227 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400228 http.Error(w, err.Error(), http.StatusInternalServerError)
229 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400230 }
gio3cdee592024-04-17 10:15:56 +0400231 log.Printf("Configuration: %+v\n", env)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400232 suffixGen := installer.NewFixedLengthRandomSuffixGenerator(3)
gio3af43942024-04-16 08:13:50 +0400233 suffix, err := suffixGen.Generate()
234 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400235 http.Error(w, err.Error(), http.StatusInternalServerError)
236 return
gio3af43942024-04-16 08:13:50 +0400237 }
gio44f621b2024-04-29 09:44:38 +0400238 instanceId := a.Slug() + suffix
gio3cdee592024-04-17 10:15:56 +0400239 appDir := fmt.Sprintf("/apps/%s", instanceId)
240 namespace := fmt.Sprintf("%s%s%s", env.NamespacePrefix, a.Namespace(), suffix)
gio1cd65152024-08-16 08:18:49 +0400241 t := tasks.NewInstallTask(s.h, func() (installer.ReleaseResources, error) {
242 return s.m.Install(a, instanceId, appDir, namespace, values)
243 })
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400244 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
245 go s.reconciler.Reconcile(ctx)
gio778577f2024-04-29 09:44:38 +0400246 if _, ok := s.tasks[instanceId]; ok {
247 panic("MUST NOT REACH!")
248 }
gio1cd65152024-08-16 08:18:49 +0400249 s.tasks[instanceId] = t
250 s.ta[instanceId] = a
gio778577f2024-04-29 09:44:38 +0400251 t.OnDone(func(err error) {
252 delete(s.tasks, instanceId)
gio1cd65152024-08-16 08:18:49 +0400253 delete(s.ta, instanceId)
gio778577f2024-04-29 09:44:38 +0400254 })
gio778577f2024-04-29 09:44:38 +0400255 go t.Start()
gioaa0fcdb2024-06-10 22:19:25 +0400256 if _, err := fmt.Fprintf(w, "/instance/%s", instanceId); err != nil {
257 http.Error(w, err.Error(), http.StatusInternalServerError)
258 return
259 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400260}
261
gioaa0fcdb2024-06-10 22:19:25 +0400262func (s *AppManagerServer) handleAppUpdate(w http.ResponseWriter, r *http.Request) {
263 slug, ok := mux.Vars(r)["slug"]
264 if !ok {
265 http.Error(w, "empty slug", http.StatusBadRequest)
266 return
267 }
gioaa0fcdb2024-06-10 22:19:25 +0400268 contents, err := ioutil.ReadAll(r.Body)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400269 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400270 http.Error(w, err.Error(), http.StatusInternalServerError)
271 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400272 }
273 var values map[string]any
274 if err := json.Unmarshal(contents, &values); err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400275 http.Error(w, err.Error(), http.StatusInternalServerError)
276 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400277 }
gio778577f2024-04-29 09:44:38 +0400278 if _, ok := s.tasks[slug]; ok {
gioaa0fcdb2024-06-10 22:19:25 +0400279 http.Error(w, "Update already in progress", http.StatusBadRequest)
280 return
gio778577f2024-04-29 09:44:38 +0400281 }
giof8843412024-05-22 16:38:05 +0400282 rr, err := s.m.Update(slug, values)
gio778577f2024-04-29 09:44:38 +0400283 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400284 http.Error(w, err.Error(), http.StatusInternalServerError)
285 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400286 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400287 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
288 go s.reconciler.Reconcile(ctx)
gio778577f2024-04-29 09:44:38 +0400289 t := tasks.NewMonitorRelease(s.h, rr)
290 t.OnDone(func(err error) {
291 delete(s.tasks, slug)
292 })
293 s.tasks[slug] = t
294 go t.Start()
gioaa0fcdb2024-06-10 22:19:25 +0400295 if _, err := fmt.Fprintf(w, "/instance/%s", slug); err != nil {
296 http.Error(w, err.Error(), http.StatusInternalServerError)
297 return
298 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400299}
300
gioaa0fcdb2024-06-10 22:19:25 +0400301func (s *AppManagerServer) handleAppRemove(w http.ResponseWriter, r *http.Request) {
302 slug, ok := mux.Vars(r)["slug"]
303 if !ok {
304 http.Error(w, "empty slug", http.StatusBadRequest)
305 return
306 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400307 if err := s.m.Remove(slug); err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400308 http.Error(w, err.Error(), http.StatusInternalServerError)
309 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400310 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400311 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
312 go s.reconciler.Reconcile(ctx)
gioaa0fcdb2024-06-10 22:19:25 +0400313 if _, err := fmt.Fprint(w, "/"); err != nil {
314 http.Error(w, err.Error(), http.StatusInternalServerError)
315 return
316 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400317}
318
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400319type PageData struct {
Davit Tabidze780a0d02024-08-05 20:53:26 +0400320 Apps []app
321 CurrentPage string
322 SearchTarget string
323 SearchValue string
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400324}
325
Davit Tabidze780a0d02024-08-05 20:53:26 +0400326func (s *AppManagerServer) handleAppsList(w http.ResponseWriter, r *http.Request) {
327 pageType := mux.Vars(r)["pageType"]
328 if pageType == "" {
329 pageType = "all"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400330 }
Davit Tabidze780a0d02024-08-05 20:53:26 +0400331 searchQuery := r.FormValue("query")
332 apps, err := s.r.Filter(searchQuery)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400333 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400334 http.Error(w, err.Error(), http.StatusInternalServerError)
335 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400336 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400337 resp := make([]app, 0)
Davit Tabidze780a0d02024-08-05 20:53:26 +0400338 for _, a := range apps {
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400339 instances, err := s.m.FindAllAppInstances(a.Slug())
340 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400341 http.Error(w, err.Error(), http.StatusInternalServerError)
342 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400343 }
Davit Tabidze780a0d02024-08-05 20:53:26 +0400344 switch pageType {
345 case "installed":
346 if len(instances) != 0 {
347 resp = append(resp, app{a.Name(), a.Icon(), a.Description(), a.Slug(), instances})
348 }
349 case "not-installed":
350 if len(instances) == 0 {
351 resp = append(resp, app{a.Name(), a.Icon(), a.Description(), a.Slug(), nil})
352 }
353 default:
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400354 resp = append(resp, app{a.Name(), a.Icon(), a.Description(), a.Slug(), instances})
355 }
356 }
357 data := PageData{
Davit Tabidze780a0d02024-08-05 20:53:26 +0400358 Apps: resp,
359 CurrentPage: pageType,
360 SearchTarget: pageType,
361 SearchValue: searchQuery,
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400362 }
gioaa0fcdb2024-06-10 22:19:25 +0400363 if err := s.tmpl.index.Execute(w, data); err != nil {
364 http.Error(w, err.Error(), http.StatusInternalServerError)
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400365 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400366}
367
368type appPageData struct {
gio3cdee592024-04-17 10:15:56 +0400369 App installer.EnvApp
370 Instance *installer.AppInstanceConfig
371 Instances []installer.AppInstanceConfig
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400372 AvailableNetworks []installer.Network
gio778577f2024-04-29 09:44:38 +0400373 Task tasks.Task
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400374 CurrentPage string
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400375}
376
gioaa0fcdb2024-06-10 22:19:25 +0400377func (s *AppManagerServer) handleAppUI(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400378 global, err := s.m.Config()
379 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400380 http.Error(w, err.Error(), http.StatusInternalServerError)
381 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400382 }
gioaa0fcdb2024-06-10 22:19:25 +0400383 slug, ok := mux.Vars(r)["slug"]
384 if !ok {
385 http.Error(w, "empty slug", http.StatusBadRequest)
386 return
387 }
gio3cdee592024-04-17 10:15:56 +0400388 a, err := installer.FindEnvApp(s.r, slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400389 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400390 http.Error(w, err.Error(), http.StatusInternalServerError)
391 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400392 }
gio308105e2024-04-19 13:12:13 +0400393 instances, err := s.m.FindAllAppInstances(slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400394 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400395 http.Error(w, err.Error(), http.StatusInternalServerError)
396 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400397 }
giocb34ad22024-07-11 08:01:13 +0400398 networks, err := s.m.CreateNetworks(global)
399 if err != nil {
400 http.Error(w, err.Error(), http.StatusInternalServerError)
401 return
402 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400403 data := appPageData{
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400404 App: a,
405 Instances: instances,
giocb34ad22024-07-11 08:01:13 +0400406 AvailableNetworks: networks,
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400407 CurrentPage: a.Name(),
408 }
gioaa0fcdb2024-06-10 22:19:25 +0400409 if err := s.tmpl.app.Execute(w, data); err != nil {
410 http.Error(w, err.Error(), http.StatusInternalServerError)
411 return
412 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400413}
414
gioaa0fcdb2024-06-10 22:19:25 +0400415func (s *AppManagerServer) handleInstanceUI(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400416 global, err := s.m.Config()
417 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400418 http.Error(w, err.Error(), http.StatusInternalServerError)
419 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400420 }
gioaa0fcdb2024-06-10 22:19:25 +0400421 slug, ok := mux.Vars(r)["slug"]
422 if !ok {
423 http.Error(w, "empty slug", http.StatusBadRequest)
424 return
425 }
gio1cd65152024-08-16 08:18:49 +0400426 t, ok := s.tasks[slug]
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400427 instance, err := s.m.FindInstance(slug)
gio1cd65152024-08-16 08:18:49 +0400428 if err != nil && !ok {
gioaa0fcdb2024-06-10 22:19:25 +0400429 http.Error(w, err.Error(), http.StatusInternalServerError)
430 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400431 }
gio1cd65152024-08-16 08:18:49 +0400432 var a installer.EnvApp
433 if instance != nil {
434 a, err = s.m.GetInstanceApp(instance.Id)
435 if err != nil {
436 http.Error(w, err.Error(), http.StatusInternalServerError)
437 return
438 }
439 } else {
440 var ok bool
441 a, ok = s.ta[slug]
442 if !ok {
443 panic("MUST NOT REACH!")
444 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400445 }
gio44f621b2024-04-29 09:44:38 +0400446 instances, err := s.m.FindAllAppInstances(a.Slug())
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400447 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400448 http.Error(w, err.Error(), http.StatusInternalServerError)
449 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400450 }
giocb34ad22024-07-11 08:01:13 +0400451 networks, err := s.m.CreateNetworks(global)
452 if err != nil {
453 http.Error(w, err.Error(), http.StatusInternalServerError)
454 return
455 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400456 data := appPageData{
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400457 App: a,
gio778577f2024-04-29 09:44:38 +0400458 Instance: instance,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400459 Instances: instances,
giocb34ad22024-07-11 08:01:13 +0400460 AvailableNetworks: networks,
gio778577f2024-04-29 09:44:38 +0400461 Task: t,
gio1cd65152024-08-16 08:18:49 +0400462 CurrentPage: slug,
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400463 }
gioaa0fcdb2024-06-10 22:19:25 +0400464 if err := s.tmpl.app.Execute(w, data); err != nil {
465 http.Error(w, err.Error(), http.StatusInternalServerError)
466 return
467 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400468}