| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 1 | package tasks |
| 2 | |
| 3 | import ( |
| Giorgi Lekveishvili | d2f3dca | 2023-12-20 09:31:30 +0400 | [diff] [blame] | 4 | "context" |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 5 | "fmt" |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 6 | "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 | |
| 14 | type state struct { |
| gio | 3cdee59 | 2024-04-17 10:15:56 +0400 | [diff] [blame^] | 15 | 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 Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 26 | } |
| 27 | |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 28 | type Env struct { |
| gio | 3af4394 | 2024-04-16 08:13:50 +0400 | [diff] [blame] | 29 | PCloudEnvName string |
| 30 | Name string |
| 31 | ContactEmail string |
| 32 | Domain string |
| 33 | AdminPublicKey string |
| 34 | NamespacePrefix string |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 35 | } |
| 36 | |
| Giorgi Lekveishvili | ab7ff6e | 2024-03-29 13:11:30 +0400 | [diff] [blame] | 37 | type EnvInfoListener func(string) |
| 38 | |
| Giorgi Lekveishvili | cd9e42c | 2023-12-13 09:49:44 +0400 | [diff] [blame] | 39 | type DNSZoneRef struct { |
| 40 | Name string |
| 41 | Namespace string |
| 42 | } |
| 43 | |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 44 | func NewCreateEnvTask( |
| 45 | env Env, |
| 46 | publicIPs []net.IP, |
| Giorgi Lekveishvili | 9d5e3f5 | 2024-03-13 15:02:50 +0400 | [diff] [blame] | 47 | startIP net.IP, |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 48 | nsCreator installer.NamespaceCreator, |
| 49 | repo installer.RepoIO, |
| gio | 3cdee59 | 2024-04-17 10:15:56 +0400 | [diff] [blame^] | 50 | mgr *installer.InfraAppManager, |
| Giorgi Lekveishvili | ab7ff6e | 2024-03-29 13:11:30 +0400 | [diff] [blame] | 51 | infoListener EnvInfoListener, |
| Giorgi Lekveishvili | cd9e42c | 2023-12-13 09:49:44 +0400 | [diff] [blame] | 52 | ) (Task, DNSZoneRef) { |
| Giorgi Lekveishvili | 77ee2dc | 2023-12-11 16:51:10 +0400 | [diff] [blame] | 53 | st := state{ |
| gio | 3cdee59 | 2024-04-17 10:15:56 +0400 | [diff] [blame^] | 54 | infoListener: infoListener, |
| 55 | publicIPs: publicIPs, |
| 56 | nsCreator: nsCreator, |
| 57 | repo: repo, |
| 58 | infraAppManager: mgr, |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 59 | } |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 60 | t := newSequentialParentTask( |
| Giorgi Lekveishvili | 77ee2dc | 2023-12-11 16:51:10 +0400 | [diff] [blame] | 61 | "Create env", |
| Giorgi Lekveishvili | 5c1b06e | 2024-03-28 15:19:44 +0400 | [diff] [blame] | 62 | true, |
| 63 | SetupConfigRepoTask(env, &st), |
| 64 | SetupZoneTask(env, startIP, &st), |
| 65 | SetupInfra(env, startIP, &st), |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 66 | ) |
| Giorgi Lekveishvili | ab7ff6e | 2024-03-29 13:11:30 +0400 | [diff] [blame] | 67 | 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 Lekveishvili | d2f3dca | 2023-12-20 09:31:30 +0400 | [diff] [blame] | 70 | rctx, done := context.WithCancel(context.Background()) |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 71 | t.OnDone(func(_ error) { |
| Giorgi Lekveishvili | d2f3dca | 2023-12-20 09:31:30 +0400 | [diff] [blame] | 72 | done() |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 73 | }) |
| Giorgi Lekveishvili | d2f3dca | 2023-12-20 09:31:30 +0400 | [diff] [blame] | 74 | 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 Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 84 | return t, DNSZoneRef{"dns-zone", env.Name} |
| 85 | } |