Include version for release
[ric-plt/ricdms.git] / pkg / deploy / deployment_manager.go
1 //==================================================================================
2 //  Copyright (c) 2022 Samsung
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "License");
5 //   you may not use this file except in compliance with the License.
6 //   You may obtain a copy of the License at
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //   Unless required by applicable law or agreed to in writing, software
11 //   distributed under the License is distributed on an "AS IS" BASIS,
12 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //   See the License for the specific language governing permissions and
14 //   limitations under the License.
15 //
16 //   This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //   platform project (RICP).
18 //==================================================================================
19 //
20
21 package deploy
22
23 import (
24         "fmt"
25         "io"
26         "os"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
29         "helm.sh/helm/v3/pkg/action"
30         "helm.sh/helm/v3/pkg/chart/loader"
31         "helm.sh/helm/v3/pkg/cli"
32 )
33
34 type DeploymentManager struct {
35         settings *cli.EnvSettings
36 }
37
38 const (
39         HELM_DRIVER        = "HELM_DRIVER"
40         CHART_NAME_FORMAT  = "chart-%s-%s.tgz"
41         RELESE_NAME_FORMAT = "ricdms-%s-rel-%s"
42 )
43
44 func NewDeploymentManager() IDeploy {
45         return &DeploymentManager{}
46 }
47
48 func (d *DeploymentManager) install(chartPath, appName, version, namesapce string) error {
49         conf := action.Configuration{}
50         err := conf.Init(d.settings.RESTClientGetter(), "namespace", os.Getenv(HELM_DRIVER), ricdms.Logger.Debug)
51
52         if err != nil {
53                 ricdms.Logger.Error("not able to prepare install configuration: %v", err)
54                 return err
55         }
56
57         install := action.NewInstall(&conf)
58         install.ReleaseName = fmt.Sprintf(RELESE_NAME_FORMAT, appName, version)
59         install.Namespace = namesapce
60
61         cp, err := install.ChartPathOptions.LocateChart(chartPath, d.settings)
62         if err != nil {
63                 ricdms.Logger.Error("Not able to locate charts on: %s", chartPath)
64         }
65
66         chart, err := loader.Load(cp)
67         if err != nil {
68                 ricdms.Logger.Error("Not able to load charts : %v", err)
69                 return err
70         }
71
72         release, err := install.Run(chart, map[string]interface{}{})
73         if err != nil {
74                 ricdms.Logger.Error("Not able to install the xApp : %v", err)
75                 return err
76         }
77
78         ricdms.Logger.Info("chart is installed with following details : %v", release)
79         return nil
80 }
81
82 func (d *DeploymentManager) writeToFile(reader io.ReadCloser, appname, version string) error {
83         if reader != nil {
84                 outfile, err := os.Create(fmt.Sprintf(CHART_NAME_FORMAT, appname, version))
85
86                 if err != nil {
87                         ricdms.Logger.Error("outfile can't be created")
88                         return err
89                 }
90
91                 defer outfile.Close()
92
93                 _, err = io.Copy(outfile, reader)
94                 if err != nil {
95                         ricdms.Logger.Error("Error while creating chart tar: %v", err)
96                 }
97                 return nil
98         }
99         return fmt.Errorf("reader is nil")
100 }
101
102 func (d *DeploymentManager) Deploy(reader io.ReadCloser, appname, version, namespace string) error {
103         err := d.writeToFile(reader, appname, version)
104         if err != nil {
105                 return err
106         }
107
108         err = d.install(fmt.Sprintf(CHART_NAME_FORMAT, appname, version), appname, version, namespace)
109         if err != nil {
110                 return err
111         }
112         return nil
113 }