blob: 82fb3d59ab85ad16d8bf44f81f28aafc610e23d2 [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()
gio1bf00802024-08-17 12:31:41 +0400134 r.PathPrefix("/stat/").Handler(cachingHandler{http.FileServer(http.FS(statAssets))})
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 }
giob4a3a192024-08-19 09:55:47 +0400283 _, err = s.repo.CommitAndPush("Generated new invitation")
284 return err
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400285}
286
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400287func extractRequest(r *http.Request) (createEnvReq, error) {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400288 var req createEnvReq
289 if err := func() error {
290 var err error
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400291 if err = r.ParseForm(); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400292 return err
293 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400294 if req.SecretToken, err = getFormValue(r.PostForm, "secret-token"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400295 return err
296 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400297 if req.Domain, err = getFormValue(r.PostForm, "domain"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400298 return err
299 }
gio7841f4f2024-07-26 19:53:49 +0400300 if req.PrivateNetworkSubdomain, err = getFormValue(r.PostForm, "private-network-subdomain"); err != nil {
301 return err
302 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400303 if req.ContactEmail, err = getFormValue(r.PostForm, "contact-email"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400304 return err
305 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400306 if req.AdminPublicKey, err = getFormValue(r.PostForm, "admin-public-key"); err != nil {
307 return err
308 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400309 return nil
310 }(); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400311 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400312 return createEnvReq{}, err
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400313 }
314 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400315 return req, nil
316}
317
318func (s *EnvServer) acceptInvitation(token string) error {
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400319 invitations, err := s.readInvitations()
320 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400321 return err
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400322 }
323 found := false
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400324 for i := range invitations {
325 if invitations[i].Token == token && invitations[i].Status == StatusActive {
326 invitations[i].Status = StatusAccepted
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400327 found = true
328 break
329 }
330 }
331 if !found {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400332 return fmt.Errorf("Invitation not found")
333 }
334 return s.writeInvitations(invitations)
335}
336
337func (s *EnvServer) createEnv(w http.ResponseWriter, r *http.Request) {
gio1591fa72024-05-24 18:01:58 +0400338 if err := s.repo.Pull(); err != nil {
339 http.Error(w, err.Error(), http.StatusInternalServerError)
340 return
341 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400342 req, err := extractRequest(r)
343 if err != nil {
344 http.Error(w, err.Error(), http.StatusInternalServerError)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400345 return
346 }
giof8843412024-05-22 16:38:05 +0400347 hf := installer.NewGitHelmFetcher()
348 lg := installer.NewInfraLocalChartGenerator()
349 mgr, err := installer.NewInfraAppManager(s.repo, s.nsCreator, hf, lg)
gio3cdee592024-04-17 10:15:56 +0400350 if err != nil {
351 http.Error(w, err.Error(), http.StatusInternalServerError)
352 return
353 }
354 var infra installer.InfraConfig
gioe72b54f2024-04-22 10:44:41 +0400355 if err := soft.ReadYaml(s.repo, "config.yaml", &infra); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400356 http.Error(w, err.Error(), http.StatusInternalServerError)
357 return
358 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400359 // if err := s.acceptInvitation(req.SecretToken); err != nil {
360 // http.Error(w, err.Error(), http.StatusInternalServerError)
361 // return
362 // }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400363 if name, err := s.nameGenerator.Generate(); err != nil {
364 http.Error(w, err.Error(), http.StatusInternalServerError)
365 return
366 } else {
367 req.Name = name
368 }
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400369 var cidrs installer.EnvCIDRs
gioe72b54f2024-04-22 10:44:41 +0400370 if err := soft.ReadYaml(s.repo, "env-cidrs.yaml", &cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400371 http.Error(w, err.Error(), http.StatusInternalServerError)
372 return
373 }
374 startIP, err := findNextStartIP(cidrs)
375 if err != nil {
376 http.Error(w, err.Error(), http.StatusInternalServerError)
377 return
378 }
379 cidrs = append(cidrs, installer.EnvCIDR{req.Name, startIP})
gioe72b54f2024-04-22 10:44:41 +0400380 if err := soft.WriteYaml(s.repo, "env-cidrs.yaml", cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400381 http.Error(w, err.Error(), http.StatusInternalServerError)
382 return
383 }
giob4a3a192024-08-19 09:55:47 +0400384 if _, err := s.repo.CommitAndPush(fmt.Sprintf("Allocate CIDR for %s", req.Name)); err != nil {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400385 http.Error(w, err.Error(), http.StatusInternalServerError)
386 return
387 }
gioe72b54f2024-04-22 10:44:41 +0400388 envNetwork, err := installer.NewEnvNetwork(startIP)
389 if err != nil {
390 http.Error(w, err.Error(), http.StatusInternalServerError)
391 return
392 }
gio7841f4f2024-07-26 19:53:49 +0400393 privateDomain := ""
394 if req.PrivateNetworkSubdomain != "" {
395 privateDomain = fmt.Sprintf("%s.%s", req.PrivateNetworkSubdomain, req.Domain)
396 }
gioe72b54f2024-04-22 10:44:41 +0400397 env := installer.EnvConfig{
398 Id: req.Name,
399 InfraName: infra.Name,
400 Domain: req.Domain,
gio7841f4f2024-07-26 19:53:49 +0400401 PrivateDomain: privateDomain,
gioe72b54f2024-04-22 10:44:41 +0400402 ContactEmail: req.ContactEmail,
403 AdminPublicKey: req.AdminPublicKey,
404 PublicIP: infra.PublicIP,
405 NameserverIP: infra.PublicIP,
406 NamespacePrefix: fmt.Sprintf("%s-", req.Name),
407 Network: envNetwork,
408 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400409 key := func() string {
410 for {
411 key, err := s.nameGenerator.Generate()
412 if err == nil {
413 return key
414 }
415 }
416 }()
417 infoUpdater := func(info string) {
418 s.envInfo[key] = template.HTML(markdown.ToHTML([]byte(info), nil, nil))
419 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400420 t, dns := tasks.NewCreateEnvTask(
gioe72b54f2024-04-22 10:44:41 +0400421 env,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400422 s.nsCreator,
giof8843412024-05-22 16:38:05 +0400423 s.jc,
424 s.hf,
gioe72b54f2024-04-22 10:44:41 +0400425 s.dnsFetcher,
426 s.httpClient,
427 s.dnsClient,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400428 s.repo,
gioe72b54f2024-04-22 10:44:41 +0400429 s.repoClient,
gio3cdee592024-04-17 10:15:56 +0400430 mgr,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400431 infoUpdater,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400432 )
giod9c398e2024-06-06 13:33:03 +0400433 if err := s.Tasks.Add(key, t); err != nil {
434 panic(err)
435 }
436
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400437 s.dns[key] = dns
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400438 go t.Start()
Giorgi Lekveishvilic85504d2023-12-20 19:29:47 +0400439 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400440}
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400441
442func findNextStartIP(cidrs installer.EnvCIDRs) (net.IP, error) {
443 m, err := netip.ParseAddr("10.0.0.0")
444 if err != nil {
445 return nil, err
446 }
447 for _, cidr := range cidrs {
448 i, err := netip.ParseAddr(cidr.IP.String())
449 if err != nil {
450 return nil, err
451 }
452 if i.Compare(m) > 0 {
453 m = i
454 }
455 }
456 sl := m.AsSlice()
457 sl[2]++
458 if sl[2] == 0b11111111 {
459 sl[2] = 0
460 sl[1]++
461 }
462 if sl[1] == 0b11111111 {
463 return nil, fmt.Errorf("Can not allocate")
464 }
465 ret, ok := netip.AddrFromSlice(sl)
466 if !ok {
467 return nil, fmt.Errorf("Must not reach")
468 }
469 return net.ParseIP(ret.String()), nil
470}