blob: dafbaf7f188330c184eec34ba85fbfbd395da927 [file] [log] [blame]
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +04001package tasks
2
3import (
4 "bytes"
5 "embed"
6 "encoding/base64"
7 "fmt"
8 "path"
9 "strings"
10 "text/template"
gio3af43942024-04-16 08:13:50 +040011
12 "github.com/giolekva/pcloud/core/installer"
gioe72b54f2024-04-22 10:44:41 +040013 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040014)
15
16//go:embed env-tmpl
17var filesTmpls embed.FS
18
gioe72b54f2024-04-22 10:44:41 +040019func NewActivateEnvTask(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040020 return newSequentialParentTask(
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040021 "Activate GitOps",
22 false,
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040023 AddNewEnvTask(env, st),
24 // TODO(gio): sync dodo-flux
25 )
26}
27
gioe72b54f2024-04-22 10:44:41 +040028func AddNewEnvTask(env installer.EnvConfig, st *state) Task {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040029 t := newLeafTask("Commit initial configuration", func() error {
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040030 ssPublicKeys, err := st.ssClient.GetPublicKeys()
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040031 if err != nil {
32 return err
33 }
gioe72b54f2024-04-22 10:44:41 +040034 repoHost := strings.Split(st.ssClient.Address(), ":")[0]
giob4a3a192024-08-19 09:55:47 +040035 _, err = st.repo.Do(func(r soft.RepoFS) (string, error) {
gioe72b54f2024-04-22 10:44:41 +040036 kust, err := soft.ReadKustomization(r, "environments/kustomization.yaml")
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040037 if err != nil {
gio3af43942024-04-16 08:13:50 +040038 return "", err
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040039 }
gioe72b54f2024-04-22 10:44:41 +040040 kust.AddResources(env.Id)
gio3af43942024-04-16 08:13:50 +040041 tmpls, err := template.ParseFS(filesTmpls, "env-tmpl/*.yaml")
42 if err != nil {
43 return "", err
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040044 }
gio3af43942024-04-16 08:13:50 +040045 var knownHosts bytes.Buffer
46 for _, key := range ssPublicKeys {
47 fmt.Fprintf(&knownHosts, "%s %s\n", repoHost, key)
48 }
49 for _, tmpl := range tmpls.Templates() { // TODO(gio): migrate to cue
gioe72b54f2024-04-22 10:44:41 +040050 dstPath := path.Join("environments", env.Id, tmpl.Name())
gio3af43942024-04-16 08:13:50 +040051 dst, err := r.Writer(dstPath)
52 if err != nil {
53 return "", err
54 }
55 defer dst.Close()
56 if err := tmpl.Execute(dst, map[string]string{
gioe72b54f2024-04-22 10:44:41 +040057 "Name": env.Id,
gio3af43942024-04-16 08:13:50 +040058 "PrivateKey": base64.StdEncoding.EncodeToString(st.keys.RawPrivateKey()),
59 "PublicKey": base64.StdEncoding.EncodeToString(st.keys.RawAuthorizedKey()),
60 "RepoHost": repoHost,
61 "RepoName": "config",
62 "KnownHosts": base64.StdEncoding.EncodeToString(knownHosts.Bytes()),
63 }); err != nil {
64 return "", err
65 }
66 }
gioe72b54f2024-04-22 10:44:41 +040067 if err := soft.WriteYaml(r, "environments/kustomization.yaml", kust); err != nil {
gio3af43942024-04-16 08:13:50 +040068 return "", err
69 }
gioe72b54f2024-04-22 10:44:41 +040070 return fmt.Sprintf("%s: initialize environment", env.Id), nil
gio3af43942024-04-16 08:13:50 +040071 })
giob4a3a192024-08-19 09:55:47 +040072 return err
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +040073 })
74 return &t
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040075}