blob: a38e5b1a0fc862ac424b2f0ba674632e85fd7a61 [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
gioe72b54f2024-04-22 10:44:41 +040018 dnsFetcher installer.ZoneStatusFetcher
19 httpClient http.Client
20 dnsClient dns.Client
21 repo soft.RepoIO
22 repoClient soft.ClientGetter
gio3cdee592024-04-17 10:15:56 +040023 ssAdminKeys *keygen.KeyPair
gioe72b54f2024-04-22 10:44:41 +040024 ssClient soft.Client
gio3cdee592024-04-17 10:15:56 +040025 fluxUserName string
26 keys *keygen.KeyPair
27 appManager *installer.AppManager
28 appsRepo installer.AppRepository
29 infraAppManager *installer.InfraAppManager
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040030}
31
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040032type EnvInfoListener func(string)
33
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040034func NewCreateEnvTask(
gioe72b54f2024-04-22 10:44:41 +040035 env installer.EnvConfig,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040036 nsCreator installer.NamespaceCreator,
gioe72b54f2024-04-22 10:44:41 +040037 dnsFetcher installer.ZoneStatusFetcher,
38 httpClient http.Client,
39 dnsClient dns.Client,
40 repo soft.RepoIO,
41 repoClient soft.ClientGetter,
gio3cdee592024-04-17 10:15:56 +040042 mgr *installer.InfraAppManager,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040043 infoListener EnvInfoListener,
gioe72b54f2024-04-22 10:44:41 +040044) (Task, installer.EnvDNS) {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040045 st := state{
gio3cdee592024-04-17 10:15:56 +040046 infoListener: infoListener,
gio3cdee592024-04-17 10:15:56 +040047 nsCreator: nsCreator,
gioe72b54f2024-04-22 10:44:41 +040048 dnsFetcher: dnsFetcher,
49 httpClient: httpClient,
50 dnsClient: dnsClient,
gio3cdee592024-04-17 10:15:56 +040051 repo: repo,
gioe72b54f2024-04-22 10:44:41 +040052 repoClient: repoClient,
gio3cdee592024-04-17 10:15:56 +040053 infraAppManager: mgr,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040054 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040055 t := newSequentialParentTask(
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040056 "Create env",
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040057 true,
58 SetupConfigRepoTask(env, &st),
gioe72b54f2024-04-22 10:44:41 +040059 SetupZoneTask(env, mgr, &st),
60 SetupInfra(env, &st),
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040061 )
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040062 t.afterDone = func() {
63 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))
64 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040065 rctx, done := context.WithCancel(context.Background())
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040066 t.OnDone(func(_ error) {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040067 done()
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040068 })
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040069 pr := NewFluxcdReconciler( // TODO(gio): make reconciler address a flag
70 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
gioe72b54f2024-04-22 10:44:41 +040071 fmt.Sprintf("%s-flux", env.InfraName),
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040072 )
73 er := NewFluxcdReconciler(
74 "http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local",
gioe72b54f2024-04-22 10:44:41 +040075 env.Id,
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040076 )
77 go pr.Reconcile(rctx)
78 go er.Reconcile(rctx)
gioe72b54f2024-04-22 10:44:41 +040079 return t, installer.EnvDNS{
80 Zone: env.Domain,
81 Address: fmt.Sprintf("http://dns-api.%sdns.svc.cluster.local/records-to-publish", env.NamespacePrefix),
82 }
Giorgi Lekveishvili2df23db2023-12-14 07:55:22 +040083}