blob: 949cbe09ffd82ccc3c6fb95a42c62eb47c0ae85c [file] [log] [blame]
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04001package welcome
2
3import (
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +04004 "embed"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04005 "encoding/json"
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +04006 "errors"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04007 "fmt"
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +04008 "html/template"
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +04009 "io"
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +040010 "io/fs"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040011 "log"
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"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040023 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040024 "github.com/giolekva/pcloud/core/installer/tasks"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040025)
26
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040027//go:embed env-manager-tmpl/*
28var tmpls embed.FS
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040029
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040030var tmplsParsed templates
31
32func init() {
33 if t, err := parseTemplates(tmpls); err != nil {
34 panic(err)
35 } else {
36 tmplsParsed = t
37 }
38}
39
40type templates struct {
41 form *template.Template
42 status *template.Template
43}
44
45func parseTemplates(fs embed.FS) (templates, error) {
46 base, err := template.ParseFS(fs, "env-manager-tmpl/base.html")
47 if err != nil {
48 return templates{}, err
49 }
50 parse := func(path string) (*template.Template, error) {
51 if b, err := base.Clone(); err != nil {
52 return nil, err
53 } else {
54 return b.ParseFS(fs, path)
55 }
56 }
57 form, err := parse("env-manager-tmpl/form.html")
58 if err != nil {
59 return templates{}, err
60 }
61 status, err := parse("env-manager-tmpl/status.html")
62 if err != nil {
63 return templates{}, err
64 }
65 return templates{form, status}, nil
66}
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040067
68type Status string
69
70const (
71 StatusActive Status = "ACTIVE"
72 StatusAccepted Status = "ACCEPTED"
73)
74
75// TODO(giolekva): add CreatedAt and ValidUntil
76type invitation struct {
77 Token string `json:"token"`
78 Status Status `json:"status"`
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040079}
80
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040081type EnvServer struct {
82 port int
gioe72b54f2024-04-22 10:44:41 +040083 ss soft.Client
84 repo soft.RepoIO
85 repoClient soft.ClientGetter
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040086 nsCreator installer.NamespaceCreator
giof8843412024-05-22 16:38:05 +040087 jc installer.JobCreator
88 hf installer.HelmFetcher
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040089 dnsFetcher installer.ZoneStatusFetcher
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040090 nameGenerator installer.NameGenerator
gioe72b54f2024-04-22 10:44:41 +040091 httpClient phttp.Client
92 dnsClient dns.Client
giod9c398e2024-06-06 13:33:03 +040093 Tasks tasks.TaskManager
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040094 envInfo map[string]template.HTML
gioe72b54f2024-04-22 10:44:41 +040095 dns map[string]installer.EnvDNS
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040096 dnsPublished map[string]struct{}
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040097}
98
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040099func NewEnvServer(
100 port int,
gioe72b54f2024-04-22 10:44:41 +0400101 ss soft.Client,
102 repo soft.RepoIO,
103 repoClient soft.ClientGetter,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400104 nsCreator installer.NamespaceCreator,
giof8843412024-05-22 16:38:05 +0400105 jc installer.JobCreator,
106 hf installer.HelmFetcher,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400107 dnsFetcher installer.ZoneStatusFetcher,
108 nameGenerator installer.NameGenerator,
gioe72b54f2024-04-22 10:44:41 +0400109 httpClient phttp.Client,
110 dnsClient dns.Client,
giod9c398e2024-06-06 13:33:03 +0400111 tm tasks.TaskManager,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400112) *EnvServer {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400113 return &EnvServer{
114 port,
115 ss,
116 repo,
gioe72b54f2024-04-22 10:44:41 +0400117 repoClient,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400118 nsCreator,
giof8843412024-05-22 16:38:05 +0400119 jc,
120 hf,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400121 dnsFetcher,
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400122 nameGenerator,
gioe72b54f2024-04-22 10:44:41 +0400123 httpClient,
124 dnsClient,
giod9c398e2024-06-06 13:33:03 +0400125 tm,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400126 make(map[string]template.HTML),
gioe72b54f2024-04-22 10:44:41 +0400127 make(map[string]installer.EnvDNS),
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400128 make(map[string]struct{}),
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400129 }
130}
131
132func (s *EnvServer) Start() {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400133 r := mux.NewRouter()
gio09f8efa2024-06-10 22:35:24 +0400134 r.PathPrefix("/static/").Handler(cachingHandler{http.FileServer(http.FS(staticAssets))})
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400135 r.Path("/env/{key}").Methods("GET").HandlerFunc(s.monitorTask)
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400136 r.Path("/env/{key}").Methods("POST").HandlerFunc(s.publishDNSRecords)
Giorgi Lekveishvili123a3672023-12-04 13:01:29 +0400137 r.Path("/").Methods("GET").HandlerFunc(s.createEnvForm)
138 r.Path("/").Methods("POST").HandlerFunc(s.createEnv)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400139 r.Path("/create-invitation").Methods("GET").HandlerFunc(s.createInvitation)
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400140 http.Handle("/", r)
141 log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", s.port), nil))
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400142}
143
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400144func (s *EnvServer) monitorTask(w http.ResponseWriter, r *http.Request) {
145 vars := mux.Vars(r)
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400146 key, ok := vars["key"]
147 if !ok {
148 http.Error(w, "Task key not provided", http.StatusBadRequest)
149 return
150 }
giod9c398e2024-06-06 13:33:03 +0400151 t, err := s.Tasks.Get(key)
152 if err != nil {
153 http.Error(w, err.Error(), http.StatusBadRequest)
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400154 return
155 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400156 dnsRecords := ""
157 if _, ok := s.dnsPublished[key]; !ok {
158 dnsRef, ok := s.dns[key]
159 if !ok {
160 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
161 return
162 }
gioe72b54f2024-04-22 10:44:41 +0400163 if records, err := s.dnsFetcher.Fetch(dnsRef.Address); err == nil {
164 dnsRecords = records
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400165 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400166 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400167 data := map[string]any{
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400168 "Root": t,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400169 "EnvInfo": s.envInfo[key],
170 "DNSRecords": dnsRecords,
171 }
172 if err := tmplsParsed.status.Execute(w, data); err != nil {
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400173 http.Error(w, err.Error(), http.StatusInternalServerError)
174 return
175 }
176}
177
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400178func (s *EnvServer) publishDNSRecords(w http.ResponseWriter, r *http.Request) {
179 vars := mux.Vars(r)
180 key, ok := vars["key"]
181 if !ok {
182 http.Error(w, "Task key not provided", http.StatusBadRequest)
183 return
184 }
185 dnsRef, ok := s.dns[key]
186 if !ok {
187 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
188 return
189 }
gioe72b54f2024-04-22 10:44:41 +0400190 records, err := s.dnsFetcher.Fetch(dnsRef.Address)
191 if err != nil {
192 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
193 return
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400194 }
195 r.ParseForm()
196 if apiToken, err := getFormValue(r.PostForm, "api-token"); err != nil {
197 http.Error(w, err.Error(), http.StatusBadRequest)
198 return
199 } else {
200 p := NewGandiUpdater(apiToken)
gioe72b54f2024-04-22 10:44:41 +0400201 zone := strings.Join(strings.Split(dnsRef.Zone, ".")[1:], ".") // TODO(gio): this is not gonna work with no subdomain case
202 if err := p.Update(zone, strings.Split(records, "\n")); err != nil {
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400203 http.Error(w, err.Error(), http.StatusInternalServerError)
204 return
205 }
206 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400207 s.envInfo[key] = "Successfully published DNS records, waiting to propagate."
208 s.dnsPublished[key] = struct{}{}
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400209 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400210}
211
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400212func (s *EnvServer) createEnvForm(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400213 if err := tmplsParsed.form.Execute(w, nil); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400214 http.Error(w, err.Error(), http.StatusInternalServerError)
215 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400216}
217
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400218func (s *EnvServer) createInvitation(w http.ResponseWriter, r *http.Request) {
219 invitations, err := s.readInvitations()
220 if err != nil {
221 http.Error(w, err.Error(), http.StatusInternalServerError)
222 return
223 }
224 token, err := installer.NewFixedLengthRandomNameGenerator(100).Generate() // TODO(giolekva): use cryptographic tokens
225 if err != nil {
226 http.Error(w, err.Error(), http.StatusInternalServerError)
227 return
228
229 }
230 invitations = append(invitations, invitation{token, StatusActive})
231 if err := s.writeInvitations(invitations); err != nil {
232 http.Error(w, err.Error(), http.StatusInternalServerError)
233 return
234 }
235 if _, err := w.Write([]byte("OK")); err != nil {
236 http.Error(w, err.Error(), http.StatusInternalServerError)
237 return
238 }
239}
240
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400241type createEnvReq struct {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400242 Name string
243 ContactEmail string `json:"contactEmail"`
244 Domain string `json:"domain"`
245 AdminPublicKey string `json:"adminPublicKey"`
246 SecretToken string `json:"secretToken"`
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400247}
248
249func (s *EnvServer) readInvitations() ([]invitation, error) {
250 r, err := s.repo.Reader("invitations")
251 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400252 if errors.Is(err, fs.ErrNotExist) {
253 return make([]invitation, 0), nil
254 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400255 return nil, err
256 }
257 defer r.Close()
258 dec := json.NewDecoder(r)
259 invitations := make([]invitation, 0)
260 for {
261 var i invitation
262 if err := dec.Decode(&i); err == io.EOF {
263 break
264 }
265 invitations = append(invitations, i)
266 }
267 return invitations, nil
268}
269
270func (s *EnvServer) writeInvitations(invitations []invitation) error {
271 w, err := s.repo.Writer("invitations")
272 if err != nil {
273 return err
274 }
275 defer w.Close()
276 enc := json.NewEncoder(w)
277 for _, i := range invitations {
278 if err := enc.Encode(i); err != nil {
279 return err
280 }
281 }
282 return s.repo.CommitAndPush("Generated new invitation")
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400283}
284
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400285func extractRequest(r *http.Request) (createEnvReq, error) {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400286 var req createEnvReq
287 if err := func() error {
288 var err error
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400289 if err = r.ParseForm(); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400290 return err
291 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400292 if req.SecretToken, err = getFormValue(r.PostForm, "secret-token"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400293 return err
294 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400295 if req.Domain, err = getFormValue(r.PostForm, "domain"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400296 return err
297 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400298 if req.ContactEmail, err = getFormValue(r.PostForm, "contact-email"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400299 return err
300 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400301 if req.AdminPublicKey, err = getFormValue(r.PostForm, "admin-public-key"); err != nil {
302 return err
303 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400304 return nil
305 }(); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400306 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400307 return createEnvReq{}, err
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400308 }
309 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400310 return req, nil
311}
312
313func (s *EnvServer) acceptInvitation(token string) error {
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400314 invitations, err := s.readInvitations()
315 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400316 return err
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400317 }
318 found := false
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400319 for i := range invitations {
320 if invitations[i].Token == token && invitations[i].Status == StatusActive {
321 invitations[i].Status = StatusAccepted
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400322 found = true
323 break
324 }
325 }
326 if !found {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400327 return fmt.Errorf("Invitation not found")
328 }
329 return s.writeInvitations(invitations)
330}
331
332func (s *EnvServer) createEnv(w http.ResponseWriter, r *http.Request) {
gio1591fa72024-05-24 18:01:58 +0400333 if err := s.repo.Pull(); err != nil {
334 http.Error(w, err.Error(), http.StatusInternalServerError)
335 return
336 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400337 req, err := extractRequest(r)
338 if err != nil {
339 http.Error(w, err.Error(), http.StatusInternalServerError)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400340 return
341 }
giof8843412024-05-22 16:38:05 +0400342 hf := installer.NewGitHelmFetcher()
343 lg := installer.NewInfraLocalChartGenerator()
344 mgr, err := installer.NewInfraAppManager(s.repo, s.nsCreator, hf, lg)
gio3cdee592024-04-17 10:15:56 +0400345 if err != nil {
346 http.Error(w, err.Error(), http.StatusInternalServerError)
347 return
348 }
349 var infra installer.InfraConfig
gioe72b54f2024-04-22 10:44:41 +0400350 if err := soft.ReadYaml(s.repo, "config.yaml", &infra); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400351 http.Error(w, err.Error(), http.StatusInternalServerError)
352 return
353 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400354 // if err := s.acceptInvitation(req.SecretToken); err != nil {
355 // http.Error(w, err.Error(), http.StatusInternalServerError)
356 // return
357 // }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400358 if name, err := s.nameGenerator.Generate(); err != nil {
359 http.Error(w, err.Error(), http.StatusInternalServerError)
360 return
361 } else {
362 req.Name = name
363 }
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400364 var cidrs installer.EnvCIDRs
gioe72b54f2024-04-22 10:44:41 +0400365 if err := soft.ReadYaml(s.repo, "env-cidrs.yaml", &cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400366 http.Error(w, err.Error(), http.StatusInternalServerError)
367 return
368 }
369 startIP, err := findNextStartIP(cidrs)
370 if err != nil {
371 http.Error(w, err.Error(), http.StatusInternalServerError)
372 return
373 }
374 cidrs = append(cidrs, installer.EnvCIDR{req.Name, startIP})
gioe72b54f2024-04-22 10:44:41 +0400375 if err := soft.WriteYaml(s.repo, "env-cidrs.yaml", cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400376 http.Error(w, err.Error(), http.StatusInternalServerError)
377 return
378 }
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400379 if err := s.repo.CommitAndPush(fmt.Sprintf("Allocate CIDR for %s", req.Name)); err != nil {
380 http.Error(w, err.Error(), http.StatusInternalServerError)
381 return
382 }
gioe72b54f2024-04-22 10:44:41 +0400383 envNetwork, err := installer.NewEnvNetwork(startIP)
384 if err != nil {
385 http.Error(w, err.Error(), http.StatusInternalServerError)
386 return
387 }
388 env := installer.EnvConfig{
389 Id: req.Name,
390 InfraName: infra.Name,
391 Domain: req.Domain,
392 PrivateDomain: fmt.Sprintf("p.%s", req.Domain),
393 ContactEmail: req.ContactEmail,
394 AdminPublicKey: req.AdminPublicKey,
395 PublicIP: infra.PublicIP,
396 NameserverIP: infra.PublicIP,
397 NamespacePrefix: fmt.Sprintf("%s-", req.Name),
398 Network: envNetwork,
399 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400400 key := func() string {
401 for {
402 key, err := s.nameGenerator.Generate()
403 if err == nil {
404 return key
405 }
406 }
407 }()
408 infoUpdater := func(info string) {
409 s.envInfo[key] = template.HTML(markdown.ToHTML([]byte(info), nil, nil))
410 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400411 t, dns := tasks.NewCreateEnvTask(
gioe72b54f2024-04-22 10:44:41 +0400412 env,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400413 s.nsCreator,
giof8843412024-05-22 16:38:05 +0400414 s.jc,
415 s.hf,
gioe72b54f2024-04-22 10:44:41 +0400416 s.dnsFetcher,
417 s.httpClient,
418 s.dnsClient,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400419 s.repo,
gioe72b54f2024-04-22 10:44:41 +0400420 s.repoClient,
gio3cdee592024-04-17 10:15:56 +0400421 mgr,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400422 infoUpdater,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400423 )
giod9c398e2024-06-06 13:33:03 +0400424 if err := s.Tasks.Add(key, t); err != nil {
425 panic(err)
426 }
427
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400428 s.dns[key] = dns
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400429 go t.Start()
Giorgi Lekveishvilic85504d2023-12-20 19:29:47 +0400430 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400431}
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400432
433func findNextStartIP(cidrs installer.EnvCIDRs) (net.IP, error) {
434 m, err := netip.ParseAddr("10.0.0.0")
435 if err != nil {
436 return nil, err
437 }
438 for _, cidr := range cidrs {
439 i, err := netip.ParseAddr(cidr.IP.String())
440 if err != nil {
441 return nil, err
442 }
443 if i.Compare(m) > 0 {
444 m = i
445 }
446 }
447 sl := m.AsSlice()
448 sl[2]++
449 if sl[2] == 0b11111111 {
450 sl[2] = 0
451 sl[1]++
452 }
453 if sl[1] == 0b11111111 {
454 return nil, fmt.Errorf("Can not allocate")
455 }
456 ret, ok := netip.AddrFromSlice(sl)
457 if !ok {
458 return nil, fmt.Errorf("Must not reach")
459 }
460 return net.ParseIP(ret.String()), nil
461}