blob: eb537da319ceec729b034c94df2ccc42d4198ae6 [file] [log] [blame]
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04001package tasks
2
3import (
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +04004 "context"
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +04005 "fmt"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04006 "net"
7
8 "github.com/charmbracelet/keygen"
9
10 "github.com/giolekva/pcloud/core/installer"
11 "github.com/giolekva/pcloud/core/installer/soft"
12)
13
14type state struct {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040015 publicIPs []net.IP
16 nsCreator installer.NamespaceCreator
17 repo installer.RepoIO
18 ssAdminKeys *keygen.KeyPair
19 ssClient *soft.Client
20 fluxUserName string
21 keys *keygen.KeyPair
22 appManager *installer.AppManager
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040023 appsRepo installer.AppRepository
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040024 nsGen installer.NamespaceGenerator
25 emptySuffixGen installer.SuffixGenerator
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040026}
27
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040028type Env struct {
29 PCloudEnvName string
30 Name string
31 ContactEmail string
32 Domain string
33 AdminPublicKey string
34}
35
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040036type DNSZoneRef struct {
37 Name string
38 Namespace string
39}
40
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040041func NewCreateEnvTask(
42 env Env,
43 publicIPs []net.IP,
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +040044 startIP net.IP,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040045 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),
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +040059 SetupZoneTask(env, startIP, &st),
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040060 },
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +040061 SetupInfra(env, startIP, &st)...,
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040062 )...,
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040063 )
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040064 rctx, done := context.WithCancel(context.Background())
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040065 t.OnDone(func(_ error) {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040066 done()
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040067 })
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040068 pr := NewFluxcdReconciler( // TODO(gio): make reconciler address a flag
69 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
70 fmt.Sprintf("%s-flux", env.PCloudEnvName),
71 )
72 er := NewFluxcdReconciler(
73 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
74 env.Name,
75 )
76 go pr.Reconcile(rctx)
77 go er.Reconcile(rctx)
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040078 return t, DNSZoneRef{"dns-zone", env.Name}
79}