blob: 68fbe57586f62686cf14abd92734aa5b2c34d313 [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 {
gio3cdee592024-04-17 10:15:56 +040015 infoListener EnvInfoListener
16 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
25 infraAppManager *installer.InfraAppManager
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040026}
27
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040028type Env struct {
gio3af43942024-04-16 08:13:50 +040029 PCloudEnvName string
30 Name string
31 ContactEmail string
32 Domain string
33 AdminPublicKey string
34 NamespacePrefix string
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040035}
36
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040037type EnvInfoListener func(string)
38
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040039type DNSZoneRef struct {
40 Name string
41 Namespace string
42}
43
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040044func NewCreateEnvTask(
45 env Env,
46 publicIPs []net.IP,
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +040047 startIP net.IP,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040048 nsCreator installer.NamespaceCreator,
49 repo installer.RepoIO,
gio3cdee592024-04-17 10:15:56 +040050 mgr *installer.InfraAppManager,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040051 infoListener EnvInfoListener,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040052) (Task, DNSZoneRef) {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040053 st := state{
gio3cdee592024-04-17 10:15:56 +040054 infoListener: infoListener,
55 publicIPs: publicIPs,
56 nsCreator: nsCreator,
57 repo: repo,
58 infraAppManager: mgr,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040059 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040060 t := newSequentialParentTask(
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040061 "Create env",
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040062 true,
63 SetupConfigRepoTask(env, &st),
64 SetupZoneTask(env, startIP, &st),
65 SetupInfra(env, startIP, &st),
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040066 )
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040067 t.afterDone = func() {
68 infoListener(fmt.Sprintf("dodo environment for %s has been provisioned successfully. Visit [https://welcome.%s](https://welcome.%s) to create administrative account and log into the system.", env.Domain, env.Domain, env.Domain))
69 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040070 rctx, done := context.WithCancel(context.Background())
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040071 t.OnDone(func(_ error) {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040072 done()
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040073 })
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040074 pr := NewFluxcdReconciler( // TODO(gio): make reconciler address a flag
75 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
76 fmt.Sprintf("%s-flux", env.PCloudEnvName),
77 )
78 er := NewFluxcdReconciler(
79 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
80 env.Name,
81 )
82 go pr.Reconcile(rctx)
83 go er.Reconcile(rctx)
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040084 return t, DNSZoneRef{"dns-zone", env.Name}
85}