| 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 { |
| Giorgi Lekveishvili | ab7ff6e | 2024-03-29 13:11:30 +0400 | [diff] [blame] | 15 | infoListener EnvInfoListener |
| Giorgi Lekveishvili | 378ea88 | 2023-12-12 13:59:18 +0400 | [diff] [blame] | 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 |
| Giorgi Lekveishvili | 08af67a | 2024-01-18 08:53:05 +0400 | [diff] [blame] | 24 | appsRepo installer.AppRepository |
| Giorgi Lekveishvili | 378ea88 | 2023-12-12 13:59:18 +0400 | [diff] [blame] | 25 | nsGen installer.NamespaceGenerator |
| 26 | emptySuffixGen installer.SuffixGenerator |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 27 | } |
| 28 | |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 29 | type Env struct { |
| 30 | PCloudEnvName string |
| 31 | Name string |
| 32 | ContactEmail string |
| 33 | Domain string |
| 34 | AdminPublicKey string |
| 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, |
| Giorgi Lekveishvili | ab7ff6e | 2024-03-29 13:11:30 +0400 | [diff] [blame] | 50 | infoListener EnvInfoListener, |
| Giorgi Lekveishvili | cd9e42c | 2023-12-13 09:49:44 +0400 | [diff] [blame] | 51 | ) (Task, DNSZoneRef) { |
| Giorgi Lekveishvili | 77ee2dc | 2023-12-11 16:51:10 +0400 | [diff] [blame] | 52 | st := state{ |
| Giorgi Lekveishvili | ab7ff6e | 2024-03-29 13:11:30 +0400 | [diff] [blame] | 53 | infoListener: infoListener, |
| 54 | publicIPs: publicIPs, |
| 55 | nsCreator: nsCreator, |
| 56 | repo: repo, |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 57 | } |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 58 | t := newSequentialParentTask( |
| Giorgi Lekveishvili | 77ee2dc | 2023-12-11 16:51:10 +0400 | [diff] [blame] | 59 | "Create env", |
| Giorgi Lekveishvili | 5c1b06e | 2024-03-28 15:19:44 +0400 | [diff] [blame] | 60 | true, |
| 61 | SetupConfigRepoTask(env, &st), |
| 62 | SetupZoneTask(env, startIP, &st), |
| 63 | SetupInfra(env, startIP, &st), |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 64 | ) |
| Giorgi Lekveishvili | ab7ff6e | 2024-03-29 13:11:30 +0400 | [diff] [blame] | 65 | 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 Lekveishvili | d2f3dca | 2023-12-20 09:31:30 +0400 | [diff] [blame] | 68 | rctx, done := context.WithCancel(context.Background()) |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 69 | t.OnDone(func(_ error) { |
| Giorgi Lekveishvili | d2f3dca | 2023-12-20 09:31:30 +0400 | [diff] [blame] | 70 | done() |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 71 | }) |
| Giorgi Lekveishvili | d2f3dca | 2023-12-20 09:31:30 +0400 | [diff] [blame] | 72 | 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 Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame] | 82 | return t, DNSZoneRef{"dns-zone", env.Name} |
| 83 | } |