blob: 7eaf0daf5899f1b260e214b15e6e2b03cabbf9a0 [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 {
gio3af43942024-04-16 08:13:50 +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
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040025}
26
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040027type Env struct {
gio3af43942024-04-16 08:13:50 +040028 PCloudEnvName string
29 Name string
30 ContactEmail string
31 Domain string
32 AdminPublicKey string
33 NamespacePrefix string
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040034}
35
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040036type EnvInfoListener func(string)
37
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040038type DNSZoneRef struct {
39 Name string
40 Namespace string
41}
42
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040043func NewCreateEnvTask(
44 env Env,
45 publicIPs []net.IP,
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +040046 startIP net.IP,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040047 nsCreator installer.NamespaceCreator,
48 repo installer.RepoIO,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040049 infoListener EnvInfoListener,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040050) (Task, DNSZoneRef) {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040051 st := state{
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040052 infoListener: infoListener,
53 publicIPs: publicIPs,
54 nsCreator: nsCreator,
55 repo: repo,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040056 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040057 t := newSequentialParentTask(
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040058 "Create env",
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040059 true,
60 SetupConfigRepoTask(env, &st),
61 SetupZoneTask(env, startIP, &st),
62 SetupInfra(env, startIP, &st),
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040063 )
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040064 t.afterDone = func() {
65 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))
66 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040067 rctx, done := context.WithCancel(context.Background())
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040068 t.OnDone(func(_ error) {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040069 done()
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040070 })
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040071 pr := NewFluxcdReconciler( // TODO(gio): make reconciler address a flag
72 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
73 fmt.Sprintf("%s-flux", env.PCloudEnvName),
74 )
75 er := NewFluxcdReconciler(
76 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
77 env.Name,
78 )
79 go pr.Reconcile(rctx)
80 go er.Reconcile(rctx)
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040081 return t, DNSZoneRef{"dns-zone", env.Name}
82}