subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 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" |
subhash kumar singh | c39ef83 | 2022-11-18 12:05:59 +0000 | [diff] [blame] | 27 | "strings" |
subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 28 | |
| 29 | "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" |
| 30 | "helm.sh/helm/v3/pkg/action" |
| 31 | "helm.sh/helm/v3/pkg/chart/loader" |
| 32 | "helm.sh/helm/v3/pkg/cli" |
| 33 | ) |
| 34 | |
| 35 | type DeploymentManager struct { |
| 36 | settings *cli.EnvSettings |
| 37 | } |
| 38 | |
| 39 | const ( |
| 40 | HELM_DRIVER = "HELM_DRIVER" |
| 41 | CHART_NAME_FORMAT = "chart-%s-%s.tgz" |
subhash kumar singh | b0e549a | 2022-11-17 08:51:11 +0000 | [diff] [blame] | 42 | RELESE_NAME_FORMAT = "ricdms-%s-rel-%s" |
subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | func NewDeploymentManager() IDeploy { |
subhash kumar singh | c39ef83 | 2022-11-18 12:05:59 +0000 | [diff] [blame] | 46 | return &DeploymentManager{ |
| 47 | settings: cli.New(), |
| 48 | } |
subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 49 | } |
| 50 | |
subhash kumar singh | b0e549a | 2022-11-17 08:51:11 +0000 | [diff] [blame] | 51 | func (d *DeploymentManager) install(chartPath, appName, version, namesapce string) error { |
subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 52 | conf := action.Configuration{} |
subhash kumar singh | cd908f5 | 2022-12-03 18:36:58 +0000 | [diff] [blame^] | 53 | err := conf.Init(d.settings.RESTClientGetter(), namesapce, os.Getenv(HELM_DRIVER), ricdms.Logger.Debug) |
subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 54 | |
| 55 | if err != nil { |
| 56 | ricdms.Logger.Error("not able to prepare install configuration: %v", err) |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | install := action.NewInstall(&conf) |
subhash kumar singh | c39ef83 | 2022-11-18 12:05:59 +0000 | [diff] [blame] | 61 | install.ReleaseName = fmt.Sprintf(RELESE_NAME_FORMAT, appName, strings.ReplaceAll(version, ".", "")) |
subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 62 | install.Namespace = namesapce |
| 63 | |
| 64 | cp, err := install.ChartPathOptions.LocateChart(chartPath, d.settings) |
| 65 | if err != nil { |
| 66 | ricdms.Logger.Error("Not able to locate charts on: %s", chartPath) |
| 67 | } |
| 68 | |
| 69 | chart, err := loader.Load(cp) |
| 70 | if err != nil { |
| 71 | ricdms.Logger.Error("Not able to load charts : %v", err) |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | release, err := install.Run(chart, map[string]interface{}{}) |
| 76 | if err != nil { |
| 77 | ricdms.Logger.Error("Not able to install the xApp : %v", err) |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | ricdms.Logger.Info("chart is installed with following details : %v", release) |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func (d *DeploymentManager) writeToFile(reader io.ReadCloser, appname, version string) error { |
| 86 | if reader != nil { |
| 87 | outfile, err := os.Create(fmt.Sprintf(CHART_NAME_FORMAT, appname, version)) |
| 88 | |
| 89 | if err != nil { |
| 90 | ricdms.Logger.Error("outfile can't be created") |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | defer outfile.Close() |
| 95 | |
| 96 | _, err = io.Copy(outfile, reader) |
| 97 | if err != nil { |
| 98 | ricdms.Logger.Error("Error while creating chart tar: %v", err) |
| 99 | } |
| 100 | return nil |
| 101 | } |
| 102 | return fmt.Errorf("reader is nil") |
| 103 | } |
| 104 | |
| 105 | func (d *DeploymentManager) Deploy(reader io.ReadCloser, appname, version, namespace string) error { |
| 106 | err := d.writeToFile(reader, appname, version) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
subhash kumar singh | b0e549a | 2022-11-17 08:51:11 +0000 | [diff] [blame] | 111 | err = d.install(fmt.Sprintf(CHART_NAME_FORMAT, appname, version), appname, version, namespace) |
subhash kumar singh | f95c1d3 | 2022-11-10 09:57:57 +0000 | [diff] [blame] | 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | return nil |
| 116 | } |
subhash kumar singh | c39ef83 | 2022-11-18 12:05:59 +0000 | [diff] [blame] | 117 | |
| 118 | func (d *DeploymentManager) Uninstall(appname, version, namespace string) error { |
| 119 | conf := action.Configuration{} |
| 120 | |
| 121 | err := conf.Init(d.settings.RESTClientGetter(), namespace, os.Getenv(HELM_DRIVER), ricdms.Logger.Debug) |
| 122 | if err != nil { |
| 123 | ricdms.Logger.Error("Not able to prepare uninstall configuration: %v", err) |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | uninstall := action.NewUninstall(&conf) |
| 128 | uninstall.Wait = true |
| 129 | |
| 130 | resp, err := uninstall.Run(fmt.Sprintf(RELESE_NAME_FORMAT, appname, strings.ReplaceAll(version, ".", ""))) |
| 131 | if err != nil { |
| 132 | ricdms.Logger.Error("Error while uninstalling deployment: %v", err) |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | ricdms.Logger.Info("deployment uninstallation comlete : %", resp.Info) |
| 137 | return nil |
| 138 | } |