blob: 20528a3e95527647501b1457f7b964d32cecb153 [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
7 "github.com/charmbracelet/keygen"
8
9 "github.com/giolekva/pcloud/core/installer"
gioe72b54f2024-04-22 10:44:41 +040010 "github.com/giolekva/pcloud/core/installer/dns"
11 "github.com/giolekva/pcloud/core/installer/http"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040012 "github.com/giolekva/pcloud/core/installer/soft"
13)
14
15type state struct {
gio3cdee592024-04-17 10:15:56 +040016 infoListener EnvInfoListener
gio3cdee592024-04-17 10:15:56 +040017 nsCreator installer.NamespaceCreator
giof8843412024-05-22 16:38:05 +040018 jc installer.JobCreator
19 hf installer.HelmFetcher
gioe72b54f2024-04-22 10:44:41 +040020 dnsFetcher installer.ZoneStatusFetcher
21 httpClient http.Client
22 dnsClient dns.Client
23 repo soft.RepoIO
24 repoClient soft.ClientGetter
gio3cdee592024-04-17 10:15:56 +040025 ssAdminKeys *keygen.KeyPair
gioe72b54f2024-04-22 10:44:41 +040026 ssClient soft.Client
gio3cdee592024-04-17 10:15:56 +040027 fluxUserName string
28 keys *keygen.KeyPair
29 appManager *installer.AppManager
30 appsRepo installer.AppRepository
31 infraAppManager *installer.InfraAppManager
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040032}
33
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040034type EnvInfoListener func(string)
35
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040036func NewCreateEnvTask(
gioe72b54f2024-04-22 10:44:41 +040037 env installer.EnvConfig,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040038 nsCreator installer.NamespaceCreator,
giof8843412024-05-22 16:38:05 +040039 jc installer.JobCreator,
40 hf installer.HelmFetcher,
gioe72b54f2024-04-22 10:44:41 +040041 dnsFetcher installer.ZoneStatusFetcher,
42 httpClient http.Client,
43 dnsClient dns.Client,
44 repo soft.RepoIO,
45 repoClient soft.ClientGetter,
gio3cdee592024-04-17 10:15:56 +040046 mgr *installer.InfraAppManager,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040047 infoListener EnvInfoListener,
gioe72b54f2024-04-22 10:44:41 +040048) (Task, installer.EnvDNS) {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040049 st := state{
gio3cdee592024-04-17 10:15:56 +040050 infoListener: infoListener,
gio3cdee592024-04-17 10:15:56 +040051 nsCreator: nsCreator,
giof8843412024-05-22 16:38:05 +040052 jc: jc,
53 hf: hf,
gioe72b54f2024-04-22 10:44:41 +040054 dnsFetcher: dnsFetcher,
55 httpClient: httpClient,
56 dnsClient: dnsClient,
gio3cdee592024-04-17 10:15:56 +040057 repo: repo,
gioe72b54f2024-04-22 10:44:41 +040058 repoClient: repoClient,
gio3cdee592024-04-17 10:15:56 +040059 infraAppManager: mgr,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040060 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040061 t := newSequentialParentTask(
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040062 "Create env",
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040063 true,
64 SetupConfigRepoTask(env, &st),
gioe72b54f2024-04-22 10:44:41 +040065 SetupZoneTask(env, mgr, &st),
66 SetupInfra(env, &st),
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040067 )
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040068 t.afterDone = func() {
69 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))
70 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040071 rctx, done := context.WithCancel(context.Background())
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040072 t.OnDone(func(_ error) {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040073 done()
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040074 })
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040075 pr := NewFluxcdReconciler( // TODO(gio): make reconciler address a flag
76 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
gioe72b54f2024-04-22 10:44:41 +040077 fmt.Sprintf("%s-flux", env.InfraName),
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040078 )
79 er := NewFluxcdReconciler(
80 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
gioe72b54f2024-04-22 10:44:41 +040081 env.Id,
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040082 )
83 go pr.Reconcile(rctx)
84 go er.Reconcile(rctx)
gioe72b54f2024-04-22 10:44:41 +040085 return t, installer.EnvDNS{
86 Zone: env.Domain,
87 Address: fmt.Sprintf("http://dns-api.%sdns.svc.cluster.local/records-to-publish", env.NamespacePrefix),
88 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040089}