blob: 1d1eaba27c7914938626a29266db9685f09db1c7 [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 Lekveishviliab7ff6e2024-03-29 13:11:30 +040015 infoListener EnvInfoListener
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
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040024 appsRepo installer.AppRepository
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040025 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 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,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040050 infoListener EnvInfoListener,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040051) (Task, DNSZoneRef) {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040052 st := state{
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040053 infoListener: infoListener,
54 publicIPs: publicIPs,
55 nsCreator: nsCreator,
56 repo: repo,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040057 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040058 t := newSequentialParentTask(
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040059 "Create env",
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040060 true,
61 SetupConfigRepoTask(env, &st),
62 SetupZoneTask(env, startIP, &st),
63 SetupInfra(env, startIP, &st),
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040064 )
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040065 t.afterDone = func() {
66 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))
67 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040068 rctx, done := context.WithCancel(context.Background())
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040069 t.OnDone(func(_ error) {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040070 done()
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040071 })
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040072 pr := NewFluxcdReconciler( // TODO(gio): make reconciler address a flag
73 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
74 fmt.Sprintf("%s-flux", env.PCloudEnvName),
75 )
76 er := NewFluxcdReconciler(
77 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
78 env.Name,
79 )
80 go pr.Reconcile(rctx)
81 go er.Reconcile(rctx)
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040082 return t, DNSZoneRef{"dns-zone", env.Name}
83}