| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 1 | package tasks |
| 2 | |
| 3 | import ( |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame^] | 4 | "fmt" |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 5 | "net" |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame^] | 6 | "net/http" |
| 7 | "time" |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 8 | |
| 9 | "github.com/charmbracelet/keygen" |
| 10 | |
| 11 | "github.com/giolekva/pcloud/core/installer" |
| 12 | "github.com/giolekva/pcloud/core/installer/soft" |
| 13 | ) |
| 14 | |
| 15 | type state struct { |
| 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 |
| 24 | appsRepo installer.AppRepository[installer.App] |
| 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 | cd9e42c | 2023-12-13 09:49:44 +0400 | [diff] [blame] | 37 | type DNSZoneRef struct { |
| 38 | Name string |
| 39 | Namespace string |
| 40 | } |
| 41 | |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 42 | func NewCreateEnvTask( |
| 43 | env Env, |
| 44 | publicIPs []net.IP, |
| 45 | nsCreator installer.NamespaceCreator, |
| 46 | repo installer.RepoIO, |
| Giorgi Lekveishvili | cd9e42c | 2023-12-13 09:49:44 +0400 | [diff] [blame] | 47 | ) (Task, DNSZoneRef) { |
| Giorgi Lekveishvili | 77ee2dc | 2023-12-11 16:51:10 +0400 | [diff] [blame] | 48 | st := state{ |
| 49 | publicIPs: publicIPs, |
| 50 | nsCreator: nsCreator, |
| 51 | repo: repo, |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 52 | } |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame^] | 53 | t := newSequentialParentTask( |
| Giorgi Lekveishvili | 77ee2dc | 2023-12-11 16:51:10 +0400 | [diff] [blame] | 54 | "Create env", |
| Giorgi Lekveishvili | 378ea88 | 2023-12-12 13:59:18 +0400 | [diff] [blame] | 55 | append( |
| 56 | []Task{ |
| 57 | SetupConfigRepoTask(env, &st), |
| 58 | NewActivateEnvTask(env, &st), |
| 59 | SetupZoneTask(env, &st), |
| 60 | }, |
| 61 | SetupInfra(env, &st)..., |
| 62 | )..., |
| Giorgi Lekveishvili | 2df23db | 2023-12-14 07:55:22 +0400 | [diff] [blame^] | 63 | ) |
| 64 | done := make(chan struct{}) |
| 65 | t.OnDone(func(_ error) { |
| 66 | close(done) |
| 67 | }) |
| 68 | go reconcile(fmt.Sprintf("%s-flux", env.PCloudEnvName), done) |
| 69 | go reconcile(env.Name, done) |
| 70 | return t, DNSZoneRef{"dns-zone", env.Name} |
| 71 | } |
| 72 | |
| 73 | func reconcile(name string, quit chan struct{}) { |
| 74 | git := fmt.Sprintf("http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local/source/git/%s/%s/reconcile", name, name) |
| 75 | kust := fmt.Sprintf("http://fluxcd-reconciler.dodo-fluxcd-reconciler.svc.cluster.local/kustomization/%s/%s/reconcile", name, name) |
| 76 | for { |
| 77 | select { |
| 78 | case <-time.After(30 * time.Second): |
| 79 | http.Get(git) |
| 80 | http.Get(kust) |
| 81 | case <-quit: |
| 82 | return |
| 83 | } |
| 84 | } |
| Giorgi Lekveishvili | 46743d4 | 2023-12-10 15:47:23 +0400 | [diff] [blame] | 85 | } |