blob: f5cc0c79a8b95558ac724c54a0f8bc3bae06c071 [file] [log] [blame]
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04001package tasks
2
3import (
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +04004 "fmt"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04005 "net"
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +04006 "net/http"
7 "time"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04008
9 "github.com/charmbracelet/keygen"
10
11 "github.com/giolekva/pcloud/core/installer"
12 "github.com/giolekva/pcloud/core/installer/soft"
13)
14
15type state struct {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040016 publicIPs []net.IP
17 nsCreator installer.NamespaceCreator
18 repo installer.RepoIO
19 ssAdminKeys *keygen.KeyPair
20 ssClient *soft.Client
21 fluxUserName string
22 keys *keygen.KeyPair
23 appManager *installer.AppManager
24 appsRepo installer.AppRepository[installer.App]
25 nsGen installer.NamespaceGenerator
26 emptySuffixGen installer.SuffixGenerator
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040027}
28
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040029type Env struct {
30 PCloudEnvName string
31 Name string
32 ContactEmail string
33 Domain string
34 AdminPublicKey string
35}
36
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040037type DNSZoneRef struct {
38 Name string
39 Namespace string
40}
41
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040042func NewCreateEnvTask(
43 env Env,
44 publicIPs []net.IP,
45 nsCreator installer.NamespaceCreator,
46 repo installer.RepoIO,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040047) (Task, DNSZoneRef) {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040048 st := state{
49 publicIPs: publicIPs,
50 nsCreator: nsCreator,
51 repo: repo,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040052 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040053 t := newSequentialParentTask(
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040054 "Create env",
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040055 append(
56 []Task{
57 SetupConfigRepoTask(env, &st),
58 NewActivateEnvTask(env, &st),
59 SetupZoneTask(env, &st),
60 },
61 SetupInfra(env, &st)...,
62 )...,
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040063 )
64 done := make(chan struct{})
65 t.OnDone(func(_ error) {
66 close(done)
67 })
68 go reconcile(fmt.Sprintf("%s-flux", env.PCloudEnvName), done)
69 go reconcile(env.Name, done)
70 return t, DNSZoneRef{"dns-zone", env.Name}
71}
72
73func reconcile(name string, quit chan struct{}) {
74 git := fmt.Sprintf("http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local/source/git/%s/%s/reconcile", name, name)
75 kust := fmt.Sprintf("http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local/kustomization/%s/%s/reconcile", name, name)
76 for {
77 select {
78 case <-time.After(30 * time.Second):
79 http.Get(git)
80 http.Get(kust)
81 case <-quit:
82 return
83 }
84 }
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040085}