blob: 9bd0a34c14b7f7dd7944466eec00670548aab2ef [file] [log] [blame]
gio59946282024-10-07 12:55:51 +04001package env
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04002
3import (
gio69b84432024-10-06 17:46:05 +04004 "context"
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +04005 "embed"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04006 "encoding/json"
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +04007 "errors"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04008 "fmt"
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +04009 "html/template"
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040010 "io"
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +040011 "io/fs"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040012 "net"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040013 "net/http"
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +040014 "net/netip"
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +040015 "strings"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040016
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040017 "github.com/gomarkdown/markdown"
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040018 "github.com/gorilla/mux"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040019
20 "github.com/giolekva/pcloud/core/installer"
gioe72b54f2024-04-22 10:44:41 +040021 "github.com/giolekva/pcloud/core/installer/dns"
22 phttp "github.com/giolekva/pcloud/core/installer/http"
gio59946282024-10-07 12:55:51 +040023 "github.com/giolekva/pcloud/core/installer/server"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040024 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040025 "github.com/giolekva/pcloud/core/installer/tasks"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040026)
27
gio59946282024-10-07 12:55:51 +040028//go:embed templates/*
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040029var tmpls embed.FS
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040030
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040031var tmplsParsed templates
32
gio59946282024-10-07 12:55:51 +040033//go:embed static/*
34var staticAssets embed.FS
35
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040036func init() {
37 if t, err := parseTemplates(tmpls); err != nil {
38 panic(err)
39 } else {
40 tmplsParsed = t
41 }
42}
43
44type templates struct {
45 form *template.Template
46 status *template.Template
47}
48
49func parseTemplates(fs embed.FS) (templates, error) {
gio59946282024-10-07 12:55:51 +040050 base, err := template.ParseFS(fs, "templates/base.html")
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040051 if err != nil {
52 return templates{}, err
53 }
54 parse := func(path string) (*template.Template, error) {
55 if b, err := base.Clone(); err != nil {
56 return nil, err
57 } else {
58 return b.ParseFS(fs, path)
59 }
60 }
gio59946282024-10-07 12:55:51 +040061 form, err := parse("templates/form.html")
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040062 if err != nil {
63 return templates{}, err
64 }
gio59946282024-10-07 12:55:51 +040065 status, err := parse("templates/status.html")
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040066 if err != nil {
67 return templates{}, err
68 }
69 return templates{form, status}, nil
70}
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040071
72type Status string
73
74const (
75 StatusActive Status = "ACTIVE"
76 StatusAccepted Status = "ACCEPTED"
77)
78
79// TODO(giolekva): add CreatedAt and ValidUntil
80type invitation struct {
81 Token string `json:"token"`
82 Status Status `json:"status"`
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040083}
84
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040085type EnvServer struct {
gio69b84432024-10-06 17:46:05 +040086 srv *http.Server
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040087 port int
gioe72b54f2024-04-22 10:44:41 +040088 ss soft.Client
89 repo soft.RepoIO
90 repoClient soft.ClientGetter
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040091 nsCreator installer.NamespaceCreator
giof8843412024-05-22 16:38:05 +040092 jc installer.JobCreator
93 hf installer.HelmFetcher
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040094 dnsFetcher installer.ZoneStatusFetcher
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040095 nameGenerator installer.NameGenerator
gioe72b54f2024-04-22 10:44:41 +040096 httpClient phttp.Client
97 dnsClient dns.Client
giod9c398e2024-06-06 13:33:03 +040098 Tasks tasks.TaskManager
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040099 envInfo map[string]template.HTML
gioe72b54f2024-04-22 10:44:41 +0400100 dns map[string]installer.EnvDNS
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400101 dnsPublished map[string]struct{}
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400102}
103
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400104func NewEnvServer(
105 port int,
gioe72b54f2024-04-22 10:44:41 +0400106 ss soft.Client,
107 repo soft.RepoIO,
108 repoClient soft.ClientGetter,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400109 nsCreator installer.NamespaceCreator,
giof8843412024-05-22 16:38:05 +0400110 jc installer.JobCreator,
111 hf installer.HelmFetcher,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400112 dnsFetcher installer.ZoneStatusFetcher,
113 nameGenerator installer.NameGenerator,
gioe72b54f2024-04-22 10:44:41 +0400114 httpClient phttp.Client,
115 dnsClient dns.Client,
giod9c398e2024-06-06 13:33:03 +0400116 tm tasks.TaskManager,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400117) *EnvServer {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400118 return &EnvServer{
gio69b84432024-10-06 17:46:05 +0400119 nil,
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400120 port,
121 ss,
122 repo,
gioe72b54f2024-04-22 10:44:41 +0400123 repoClient,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400124 nsCreator,
giof8843412024-05-22 16:38:05 +0400125 jc,
126 hf,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400127 dnsFetcher,
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400128 nameGenerator,
gioe72b54f2024-04-22 10:44:41 +0400129 httpClient,
130 dnsClient,
giod9c398e2024-06-06 13:33:03 +0400131 tm,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400132 make(map[string]template.HTML),
gioe72b54f2024-04-22 10:44:41 +0400133 make(map[string]installer.EnvDNS),
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400134 make(map[string]struct{}),
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400135 }
136}
137
gio69b84432024-10-06 17:46:05 +0400138func (s *EnvServer) Start() error {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400139 r := mux.NewRouter()
gio59946282024-10-07 12:55:51 +0400140 r.PathPrefix("/static/").Handler(server.NewCachingHandler(http.FileServer(http.FS(staticAssets))))
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400141 r.Path("/env/{key}").Methods("GET").HandlerFunc(s.monitorTask)
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400142 r.Path("/env/{key}").Methods("POST").HandlerFunc(s.publishDNSRecords)
Giorgi Lekveishvili123a3672023-12-04 13:01:29 +0400143 r.Path("/").Methods("GET").HandlerFunc(s.createEnvForm)
144 r.Path("/").Methods("POST").HandlerFunc(s.createEnv)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400145 r.Path("/create-invitation").Methods("GET").HandlerFunc(s.createInvitation)
gio69b84432024-10-06 17:46:05 +0400146 srv := &http.Server{
147 Addr: fmt.Sprintf(":%d", s.port),
148 Handler: r,
149 }
150 s.srv = srv
151 if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
152 return err
153 }
154 return nil
155}
156
157func (s *EnvServer) Stop() error {
158 return s.srv.Shutdown(context.Background())
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400159}
160
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400161func (s *EnvServer) monitorTask(w http.ResponseWriter, r *http.Request) {
162 vars := mux.Vars(r)
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400163 key, ok := vars["key"]
164 if !ok {
165 http.Error(w, "Task key not provided", http.StatusBadRequest)
166 return
167 }
giod9c398e2024-06-06 13:33:03 +0400168 t, err := s.Tasks.Get(key)
169 if err != nil {
170 http.Error(w, err.Error(), http.StatusBadRequest)
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400171 return
172 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400173 dnsRecords := ""
174 if _, ok := s.dnsPublished[key]; !ok {
175 dnsRef, ok := s.dns[key]
176 if !ok {
177 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
178 return
179 }
gioe72b54f2024-04-22 10:44:41 +0400180 if records, err := s.dnsFetcher.Fetch(dnsRef.Address); err == nil {
181 dnsRecords = records
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400182 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400183 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400184 data := map[string]any{
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400185 "Root": t,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400186 "EnvInfo": s.envInfo[key],
187 "DNSRecords": dnsRecords,
188 }
189 if err := tmplsParsed.status.Execute(w, data); err != nil {
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400190 http.Error(w, err.Error(), http.StatusInternalServerError)
191 return
192 }
193}
194
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400195func (s *EnvServer) publishDNSRecords(w http.ResponseWriter, r *http.Request) {
196 vars := mux.Vars(r)
197 key, ok := vars["key"]
198 if !ok {
199 http.Error(w, "Task key not provided", http.StatusBadRequest)
200 return
201 }
202 dnsRef, ok := s.dns[key]
203 if !ok {
204 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
205 return
206 }
gioe72b54f2024-04-22 10:44:41 +0400207 records, err := s.dnsFetcher.Fetch(dnsRef.Address)
208 if err != nil {
209 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
210 return
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400211 }
212 r.ParseForm()
gio59946282024-10-07 12:55:51 +0400213 if apiToken, err := server.GetFormValue(r.PostForm, "api-token"); err != nil {
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400214 http.Error(w, err.Error(), http.StatusBadRequest)
215 return
216 } else {
217 p := NewGandiUpdater(apiToken)
gioe72b54f2024-04-22 10:44:41 +0400218 zone := strings.Join(strings.Split(dnsRef.Zone, ".")[1:], ".") // TODO(gio): this is not gonna work with no subdomain case
219 if err := p.Update(zone, strings.Split(records, "\n")); err != nil {
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400220 http.Error(w, err.Error(), http.StatusInternalServerError)
221 return
222 }
223 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400224 s.envInfo[key] = "Successfully published DNS records, waiting to propagate."
225 s.dnsPublished[key] = struct{}{}
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400226 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400227}
228
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400229func (s *EnvServer) createEnvForm(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400230 if err := tmplsParsed.form.Execute(w, nil); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400231 http.Error(w, err.Error(), http.StatusInternalServerError)
232 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400233}
234
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400235func (s *EnvServer) createInvitation(w http.ResponseWriter, r *http.Request) {
236 invitations, err := s.readInvitations()
237 if err != nil {
238 http.Error(w, err.Error(), http.StatusInternalServerError)
239 return
240 }
241 token, err := installer.NewFixedLengthRandomNameGenerator(100).Generate() // TODO(giolekva): use cryptographic tokens
242 if err != nil {
243 http.Error(w, err.Error(), http.StatusInternalServerError)
244 return
245
246 }
247 invitations = append(invitations, invitation{token, StatusActive})
248 if err := s.writeInvitations(invitations); err != nil {
249 http.Error(w, err.Error(), http.StatusInternalServerError)
250 return
251 }
252 if _, err := w.Write([]byte("OK")); err != nil {
253 http.Error(w, err.Error(), http.StatusInternalServerError)
254 return
255 }
256}
257
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400258type createEnvReq struct {
gio7841f4f2024-07-26 19:53:49 +0400259 Name string
260 ContactEmail string `json:"contactEmail"`
261 Domain string `json:"domain"`
262 PrivateNetworkSubdomain string `json:"privateNetworkSubdomain"`
263 AdminPublicKey string `json:"adminPublicKey"`
264 SecretToken string `json:"secretToken"`
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400265}
266
267func (s *EnvServer) readInvitations() ([]invitation, error) {
268 r, err := s.repo.Reader("invitations")
269 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400270 if errors.Is(err, fs.ErrNotExist) {
271 return make([]invitation, 0), nil
272 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400273 return nil, err
274 }
275 defer r.Close()
276 dec := json.NewDecoder(r)
277 invitations := make([]invitation, 0)
278 for {
279 var i invitation
280 if err := dec.Decode(&i); err == io.EOF {
281 break
282 }
283 invitations = append(invitations, i)
284 }
285 return invitations, nil
286}
287
288func (s *EnvServer) writeInvitations(invitations []invitation) error {
289 w, err := s.repo.Writer("invitations")
290 if err != nil {
291 return err
292 }
293 defer w.Close()
294 enc := json.NewEncoder(w)
295 for _, i := range invitations {
296 if err := enc.Encode(i); err != nil {
297 return err
298 }
299 }
giob4a3a192024-08-19 09:55:47 +0400300 _, err = s.repo.CommitAndPush("Generated new invitation")
301 return err
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400302}
303
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400304func extractRequest(r *http.Request) (createEnvReq, error) {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400305 var req createEnvReq
306 if err := func() error {
307 var err error
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400308 if err = r.ParseForm(); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400309 return err
310 }
gio59946282024-10-07 12:55:51 +0400311 if req.SecretToken, err = server.GetFormValue(r.PostForm, "secret-token"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400312 return err
313 }
gio59946282024-10-07 12:55:51 +0400314 if req.Domain, err = server.GetFormValue(r.PostForm, "domain"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400315 return err
316 }
gio59946282024-10-07 12:55:51 +0400317 if req.PrivateNetworkSubdomain, err = server.GetFormValue(r.PostForm, "private-network-subdomain"); err != nil {
gio7841f4f2024-07-26 19:53:49 +0400318 return err
319 }
gio59946282024-10-07 12:55:51 +0400320 if req.ContactEmail, err = server.GetFormValue(r.PostForm, "contact-email"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400321 return err
322 }
gio59946282024-10-07 12:55:51 +0400323 if req.AdminPublicKey, err = server.GetFormValue(r.PostForm, "admin-public-key"); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400324 return err
325 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400326 return nil
327 }(); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400328 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400329 return createEnvReq{}, err
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400330 }
331 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400332 return req, nil
333}
334
335func (s *EnvServer) acceptInvitation(token string) error {
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400336 invitations, err := s.readInvitations()
337 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400338 return err
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400339 }
340 found := false
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400341 for i := range invitations {
342 if invitations[i].Token == token && invitations[i].Status == StatusActive {
343 invitations[i].Status = StatusAccepted
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400344 found = true
345 break
346 }
347 }
348 if !found {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400349 return fmt.Errorf("Invitation not found")
350 }
351 return s.writeInvitations(invitations)
352}
353
354func (s *EnvServer) createEnv(w http.ResponseWriter, r *http.Request) {
gio1591fa72024-05-24 18:01:58 +0400355 if err := s.repo.Pull(); err != nil {
356 http.Error(w, err.Error(), http.StatusInternalServerError)
357 return
358 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400359 req, err := extractRequest(r)
360 if err != nil {
361 http.Error(w, err.Error(), http.StatusInternalServerError)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400362 return
363 }
giof8843412024-05-22 16:38:05 +0400364 hf := installer.NewGitHelmFetcher()
365 lg := installer.NewInfraLocalChartGenerator()
366 mgr, err := installer.NewInfraAppManager(s.repo, s.nsCreator, hf, lg)
gio3cdee592024-04-17 10:15:56 +0400367 if err != nil {
368 http.Error(w, err.Error(), http.StatusInternalServerError)
369 return
370 }
371 var infra installer.InfraConfig
gioe72b54f2024-04-22 10:44:41 +0400372 if err := soft.ReadYaml(s.repo, "config.yaml", &infra); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400373 http.Error(w, err.Error(), http.StatusInternalServerError)
374 return
375 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400376 // if err := s.acceptInvitation(req.SecretToken); err != nil {
377 // http.Error(w, err.Error(), http.StatusInternalServerError)
378 // return
379 // }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400380 if name, err := s.nameGenerator.Generate(); err != nil {
381 http.Error(w, err.Error(), http.StatusInternalServerError)
382 return
383 } else {
384 req.Name = name
385 }
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400386 var cidrs installer.EnvCIDRs
gioe72b54f2024-04-22 10:44:41 +0400387 if err := soft.ReadYaml(s.repo, "env-cidrs.yaml", &cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400388 http.Error(w, err.Error(), http.StatusInternalServerError)
389 return
390 }
391 startIP, err := findNextStartIP(cidrs)
392 if err != nil {
393 http.Error(w, err.Error(), http.StatusInternalServerError)
394 return
395 }
396 cidrs = append(cidrs, installer.EnvCIDR{req.Name, startIP})
gioe72b54f2024-04-22 10:44:41 +0400397 if err := soft.WriteYaml(s.repo, "env-cidrs.yaml", cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400398 http.Error(w, err.Error(), http.StatusInternalServerError)
399 return
400 }
giob4a3a192024-08-19 09:55:47 +0400401 if _, err := s.repo.CommitAndPush(fmt.Sprintf("Allocate CIDR for %s", req.Name)); err != nil {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400402 http.Error(w, err.Error(), http.StatusInternalServerError)
403 return
404 }
gioe72b54f2024-04-22 10:44:41 +0400405 envNetwork, err := installer.NewEnvNetwork(startIP)
406 if err != nil {
407 http.Error(w, err.Error(), http.StatusInternalServerError)
408 return
409 }
gio7841f4f2024-07-26 19:53:49 +0400410 privateDomain := ""
411 if req.PrivateNetworkSubdomain != "" {
412 privateDomain = fmt.Sprintf("%s.%s", req.PrivateNetworkSubdomain, req.Domain)
413 }
gioe72b54f2024-04-22 10:44:41 +0400414 env := installer.EnvConfig{
415 Id: req.Name,
416 InfraName: infra.Name,
417 Domain: req.Domain,
gio7841f4f2024-07-26 19:53:49 +0400418 PrivateDomain: privateDomain,
gioe72b54f2024-04-22 10:44:41 +0400419 ContactEmail: req.ContactEmail,
420 AdminPublicKey: req.AdminPublicKey,
421 PublicIP: infra.PublicIP,
422 NameserverIP: infra.PublicIP,
423 NamespacePrefix: fmt.Sprintf("%s-", req.Name),
424 Network: envNetwork,
425 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400426 key := func() string {
427 for {
428 key, err := s.nameGenerator.Generate()
429 if err == nil {
430 return key
431 }
432 }
433 }()
434 infoUpdater := func(info string) {
435 s.envInfo[key] = template.HTML(markdown.ToHTML([]byte(info), nil, nil))
436 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400437 t, dns := tasks.NewCreateEnvTask(
gioe72b54f2024-04-22 10:44:41 +0400438 env,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400439 s.nsCreator,
giof8843412024-05-22 16:38:05 +0400440 s.jc,
441 s.hf,
gioe72b54f2024-04-22 10:44:41 +0400442 s.dnsFetcher,
443 s.httpClient,
444 s.dnsClient,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400445 s.repo,
gioe72b54f2024-04-22 10:44:41 +0400446 s.repoClient,
gio3cdee592024-04-17 10:15:56 +0400447 mgr,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400448 infoUpdater,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400449 )
giod9c398e2024-06-06 13:33:03 +0400450 if err := s.Tasks.Add(key, t); err != nil {
451 panic(err)
452 }
453
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400454 s.dns[key] = dns
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400455 go t.Start()
Giorgi Lekveishvilic85504d2023-12-20 19:29:47 +0400456 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400457}
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400458
459func findNextStartIP(cidrs installer.EnvCIDRs) (net.IP, error) {
460 m, err := netip.ParseAddr("10.0.0.0")
461 if err != nil {
462 return nil, err
463 }
464 for _, cidr := range cidrs {
465 i, err := netip.ParseAddr(cidr.IP.String())
466 if err != nil {
467 return nil, err
468 }
469 if i.Compare(m) > 0 {
470 m = i
471 }
472 }
473 sl := m.AsSlice()
474 sl[2]++
475 if sl[2] == 0b11111111 {
476 sl[2] = 0
477 sl[1]++
478 }
479 if sl[1] == 0b11111111 {
480 return nil, fmt.Errorf("Can not allocate")
481 }
482 ret, ok := netip.AddrFromSlice(sl)
483 if !ok {
484 return nil, fmt.Errorf("Must not reach")
485 }
486 return net.ParseIP(ret.String()), nil
487}