blob: 4c084cd628c4830a7ec907229423b4f6448f76c7 [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 {
gio7841f4f2024-07-26 19:53:49 +0400242 Name string
243 ContactEmail string `json:"contactEmail"`
244 Domain string `json:"domain"`
245 PrivateNetworkSubdomain string `json:"privateNetworkSubdomain"`
246 AdminPublicKey string `json:"adminPublicKey"`
247 SecretToken string `json:"secretToken"`
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400248}
249
250func (s *EnvServer) readInvitations() ([]invitation, error) {
251 r, err := s.repo.Reader("invitations")
252 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400253 if errors.Is(err, fs.ErrNotExist) {
254 return make([]invitation, 0), nil
255 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400256 return nil, err
257 }
258 defer r.Close()
259 dec := json.NewDecoder(r)
260 invitations := make([]invitation, 0)
261 for {
262 var i invitation
263 if err := dec.Decode(&i); err == io.EOF {
264 break
265 }
266 invitations = append(invitations, i)
267 }
268 return invitations, nil
269}
270
271func (s *EnvServer) writeInvitations(invitations []invitation) error {
272 w, err := s.repo.Writer("invitations")
273 if err != nil {
274 return err
275 }
276 defer w.Close()
277 enc := json.NewEncoder(w)
278 for _, i := range invitations {
279 if err := enc.Encode(i); err != nil {
280 return err
281 }
282 }
283 return s.repo.CommitAndPush("Generated new invitation")
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400284}
285
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400286func extractRequest(r *http.Request) (createEnvReq, error) {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400287 var req createEnvReq
288 if err := func() error {
289 var err error
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400290 if err = r.ParseForm(); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400291 return err
292 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400293 if req.SecretToken, err = getFormValue(r.PostForm, "secret-token"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400294 return err
295 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400296 if req.Domain, err = getFormValue(r.PostForm, "domain"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400297 return err
298 }
gio7841f4f2024-07-26 19:53:49 +0400299 if req.PrivateNetworkSubdomain, err = getFormValue(r.PostForm, "private-network-subdomain"); err != nil {
300 return err
301 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400302 if req.ContactEmail, err = getFormValue(r.PostForm, "contact-email"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400303 return err
304 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400305 if req.AdminPublicKey, err = getFormValue(r.PostForm, "admin-public-key"); err != nil {
306 return err
307 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400308 return nil
309 }(); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400310 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400311 return createEnvReq{}, err
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400312 }
313 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400314 return req, nil
315}
316
317func (s *EnvServer) acceptInvitation(token string) error {
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400318 invitations, err := s.readInvitations()
319 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400320 return err
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400321 }
322 found := false
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400323 for i := range invitations {
324 if invitations[i].Token == token && invitations[i].Status == StatusActive {
325 invitations[i].Status = StatusAccepted
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400326 found = true
327 break
328 }
329 }
330 if !found {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400331 return fmt.Errorf("Invitation not found")
332 }
333 return s.writeInvitations(invitations)
334}
335
336func (s *EnvServer) createEnv(w http.ResponseWriter, r *http.Request) {
gio1591fa72024-05-24 18:01:58 +0400337 if err := s.repo.Pull(); err != nil {
338 http.Error(w, err.Error(), http.StatusInternalServerError)
339 return
340 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400341 req, err := extractRequest(r)
342 if err != nil {
343 http.Error(w, err.Error(), http.StatusInternalServerError)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400344 return
345 }
giof8843412024-05-22 16:38:05 +0400346 hf := installer.NewGitHelmFetcher()
347 lg := installer.NewInfraLocalChartGenerator()
348 mgr, err := installer.NewInfraAppManager(s.repo, s.nsCreator, hf, lg)
gio3cdee592024-04-17 10:15:56 +0400349 if err != nil {
350 http.Error(w, err.Error(), http.StatusInternalServerError)
351 return
352 }
353 var infra installer.InfraConfig
gioe72b54f2024-04-22 10:44:41 +0400354 if err := soft.ReadYaml(s.repo, "config.yaml", &infra); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400355 http.Error(w, err.Error(), http.StatusInternalServerError)
356 return
357 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400358 // if err := s.acceptInvitation(req.SecretToken); err != nil {
359 // http.Error(w, err.Error(), http.StatusInternalServerError)
360 // return
361 // }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400362 if name, err := s.nameGenerator.Generate(); err != nil {
363 http.Error(w, err.Error(), http.StatusInternalServerError)
364 return
365 } else {
366 req.Name = name
367 }
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400368 var cidrs installer.EnvCIDRs
gioe72b54f2024-04-22 10:44:41 +0400369 if err := soft.ReadYaml(s.repo, "env-cidrs.yaml", &cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400370 http.Error(w, err.Error(), http.StatusInternalServerError)
371 return
372 }
373 startIP, err := findNextStartIP(cidrs)
374 if err != nil {
375 http.Error(w, err.Error(), http.StatusInternalServerError)
376 return
377 }
378 cidrs = append(cidrs, installer.EnvCIDR{req.Name, startIP})
gioe72b54f2024-04-22 10:44:41 +0400379 if err := soft.WriteYaml(s.repo, "env-cidrs.yaml", cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400380 http.Error(w, err.Error(), http.StatusInternalServerError)
381 return
382 }
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400383 if err := s.repo.CommitAndPush(fmt.Sprintf("Allocate CIDR for %s", req.Name)); err != nil {
384 http.Error(w, err.Error(), http.StatusInternalServerError)
385 return
386 }
gioe72b54f2024-04-22 10:44:41 +0400387 envNetwork, err := installer.NewEnvNetwork(startIP)
388 if err != nil {
389 http.Error(w, err.Error(), http.StatusInternalServerError)
390 return
391 }
gio7841f4f2024-07-26 19:53:49 +0400392 privateDomain := ""
393 if req.PrivateNetworkSubdomain != "" {
394 privateDomain = fmt.Sprintf("%s.%s", req.PrivateNetworkSubdomain, req.Domain)
395 }
gioe72b54f2024-04-22 10:44:41 +0400396 env := installer.EnvConfig{
397 Id: req.Name,
398 InfraName: infra.Name,
399 Domain: req.Domain,
gio7841f4f2024-07-26 19:53:49 +0400400 PrivateDomain: privateDomain,
gioe72b54f2024-04-22 10:44:41 +0400401 ContactEmail: req.ContactEmail,
402 AdminPublicKey: req.AdminPublicKey,
403 PublicIP: infra.PublicIP,
404 NameserverIP: infra.PublicIP,
405 NamespacePrefix: fmt.Sprintf("%s-", req.Name),
406 Network: envNetwork,
407 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400408 key := func() string {
409 for {
410 key, err := s.nameGenerator.Generate()
411 if err == nil {
412 return key
413 }
414 }
415 }()
416 infoUpdater := func(info string) {
417 s.envInfo[key] = template.HTML(markdown.ToHTML([]byte(info), nil, nil))
418 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400419 t, dns := tasks.NewCreateEnvTask(
gioe72b54f2024-04-22 10:44:41 +0400420 env,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400421 s.nsCreator,
giof8843412024-05-22 16:38:05 +0400422 s.jc,
423 s.hf,
gioe72b54f2024-04-22 10:44:41 +0400424 s.dnsFetcher,
425 s.httpClient,
426 s.dnsClient,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400427 s.repo,
gioe72b54f2024-04-22 10:44:41 +0400428 s.repoClient,
gio3cdee592024-04-17 10:15:56 +0400429 mgr,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400430 infoUpdater,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400431 )
giod9c398e2024-06-06 13:33:03 +0400432 if err := s.Tasks.Add(key, t); err != nil {
433 panic(err)
434 }
435
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400436 s.dns[key] = dns
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400437 go t.Start()
Giorgi Lekveishvilic85504d2023-12-20 19:29:47 +0400438 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400439}
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400440
441func findNextStartIP(cidrs installer.EnvCIDRs) (net.IP, error) {
442 m, err := netip.ParseAddr("10.0.0.0")
443 if err != nil {
444 return nil, err
445 }
446 for _, cidr := range cidrs {
447 i, err := netip.ParseAddr(cidr.IP.String())
448 if err != nil {
449 return nil, err
450 }
451 if i.Compare(m) > 0 {
452 m = i
453 }
454 }
455 sl := m.AsSlice()
456 sl[2]++
457 if sl[2] == 0b11111111 {
458 sl[2] = 0
459 sl[1]++
460 }
461 if sl[1] == 0b11111111 {
462 return nil, fmt.Errorf("Can not allocate")
463 }
464 ret, ok := netip.AddrFromSlice(sl)
465 if !ok {
466 return nil, fmt.Errorf("Must not reach")
467 }
468 return net.ParseIP(ret.String()), nil
469}