blob: c0a619f483b4b6c7350724e609c50e2320b3fd3f [file] [log] [blame]
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04001package installer
2
3import (
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +04004 "io/ioutil"
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +04005 "path/filepath"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04006
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +04007 "sigs.k8s.io/yaml"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04008)
9
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040010const appDir = "/apps"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040011const configFileName = "config.yaml"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040012const kustomizationFileName = "kustomization.yaml"
13
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040014type AppManager struct {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040015 repoIO RepoIO
16 nsCreator NamespaceCreator
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040017}
18
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040019func NewAppManager(repoIO RepoIO, nsCreator NamespaceCreator) (*AppManager, error) {
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040020 return &AppManager{
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040021 repoIO,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040022 nsCreator,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040023 }, nil
24}
25
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040026func (m *AppManager) Config() (Config, error) {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040027 return m.repoIO.ReadConfig()
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040028}
29
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040030func (m *AppManager) FindAllInstances(name string) ([]AppConfig, error) {
31 return m.repoIO.FindAllInstances(appDir, name)
32}
33
34func (m *AppManager) FindInstance(name string) (AppConfig, error) {
35 return m.repoIO.FindInstance(appDir, name)
36}
37
38func (m *AppManager) AppConfig(name string) (AppConfig, error) {
39 configF, err := m.repoIO.Reader(filepath.Join(appDir, name, configFileName))
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040040 if err != nil {
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040041 return AppConfig{}, err
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040042 }
43 defer configF.Close()
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040044 var cfg AppConfig
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040045 contents, err := ioutil.ReadAll(configF)
46 if err != nil {
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040047 return AppConfig{}, err
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040048 }
49 err = yaml.UnmarshalStrict(contents, &cfg)
50 return cfg, err
51}
52
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040053func (m *AppManager) Install(app App, ns NamespaceGenerator, suffixGen SuffixGenerator, config map[string]any) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040054 // if err := m.repoIO.Fetch(); err != nil {
55 // return err
56 // }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040057 suffix, err := suffixGen.Generate()
58 if err != nil {
59 return err
60 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040061 namespaces := make([]string, len(app.Namespaces))
62 for i, n := range app.Namespaces {
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040063 ns, err := ns.Generate(n)
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040064 if err != nil {
65 return err
66 }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040067 namespaces[i] = ns + suffix
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040068 }
69 for _, n := range namespaces {
70 if err := m.nsCreator.Create(n); err != nil {
71 return err
72 }
73 }
74 globalConfig, err := m.repoIO.ReadConfig()
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040075 if err != nil {
76 return err
77 }
78 all := map[string]any{
79 "Global": globalConfig.Values,
80 "Values": config,
81 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040082 if len(namespaces) > 0 {
83 all["Release"] = map[string]any{
84 "Namespace": namespaces[0],
85 }
86 }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040087 return m.repoIO.InstallApp(
88 app,
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040089 filepath.Join(appDir, app.Name+suffix),
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040090 all)
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040091}
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040092
93func (m *AppManager) Update(app App, instanceId string, config map[string]any) error {
94 // if err := m.repoIO.Fetch(); err != nil {
95 // return err
96 // }
97 globalConfig, err := m.repoIO.ReadConfig()
98 if err != nil {
99 return err
100 }
101 instanceDir := filepath.Join(appDir, instanceId)
102 instanceConfigPath := filepath.Join(instanceDir, configFileName)
103 appConfig, err := m.repoIO.ReadAppConfig(instanceConfigPath)
104 if err != nil {
105 return err
106 }
107 all := map[string]any{
108 "Global": globalConfig.Values,
109 "Values": config,
110 "Release": appConfig.Config["Release"],
111 }
112 return m.repoIO.InstallApp(app, instanceDir, all)
113}