From 11f56722f74f0f0979a2d40e5c9e262dce8ce635 Mon Sep 17 00:00:00 2001 From: admju Date: Mon, 2 Sep 2024 11:43:11 +0000 Subject: [PATCH] Uninstall --- src/Makefile | 4 +- src/cmd/args.go | 2 +- src/cmd/installCmd.go | 14 ++-- src/cmd/uninstallCmd.go | 37 +++++++---- src/helm/chart.go | 8 +++ src/install/common.go | 90 ++++++++++++++++++++++++++ src/install/install.go | 128 +++++++++---------------------------- src/install/uninstall.go | 69 +++++++++++++++++--- src/versionOc/versionOc.go | 23 ++++++- 9 files changed, 241 insertions(+), 134 deletions(-) create mode 100644 src/install/common.go diff --git a/src/Makefile b/src/Makefile index b744a41..e6e6f22 100644 --- a/src/Makefile +++ b/src/Makefile @@ -35,6 +35,9 @@ help: ${BIN_DIR}/${BIN_NAME}: ${SOURCES} $(OBJS) go build -o ${BIN_DIR}/${BIN_NAME} +get-deps: + @go mod tidy + build: ${BIN_DIR}/${BIN_NAME} run: $(OBJS) @@ -65,7 +68,6 @@ clean: @test ! -e go.sum || rm go.sum @test ! -e .oc-deploy.log || rm .oc-deploy.log @rm -rf workspace_* - @go mod tidy .PHONY: test test: diff --git a/src/cmd/args.go b/src/cmd/args.go index fed9fd3..ff7412f 100644 --- a/src/cmd/args.go +++ b/src/cmd/args.go @@ -38,7 +38,7 @@ func Execute() { Long: `Undeploy`, Args: cobra.MaximumNArgs(0), Run: func(cmd *cobra.Command, args []string) { - UninstallCmd(context, version) + UninstallCmd(context) }, Example: "oc-deploy uninstall --context ex1", } diff --git a/src/cmd/installCmd.go b/src/cmd/installCmd.go index dfcaf4b..750ed4a 100644 --- a/src/cmd/installCmd.go +++ b/src/cmd/installCmd.go @@ -4,7 +4,6 @@ import ( "fmt" log "oc-deploy/log_wrapper" - "oc-deploy/versionOc" "oc-deploy/install" ) @@ -13,20 +12,15 @@ func InstallCmd(context string, version string) { log.Log().Info().Msg(" << Contexte : " + context) - version, err := versionOc.Get(version) - log.Log().Info().Msg(" << Version : " + version) - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - workspace := fmt.Sprintf("workspace_%s", context) obj := install.InstallClass{Workspace: workspace, Version: version} - file, err := obj.New() + file, err := obj.NewInstall() if err != nil { log.Log().Fatal().Msg(" >> " + err.Error()) } - log.Log().Info().Msg(" >> Config : " + file ) + log.Log().Info().Msg(fmt.Sprintf(" << Version : %s", obj.Version)) + log.Log().Info().Msg(fmt.Sprintf(" >> Config : %s", file)) err = obj.Tools() if err != nil { @@ -43,7 +37,7 @@ func InstallCmd(context string, version string) { log.Log().Fatal().Msg(" >> " + err.Error()) } - err = obj.Charts() + err = obj.InstallCharts() if err != nil { log.Log().Fatal().Msg(" >> " + err.Error()) } diff --git a/src/cmd/uninstallCmd.go b/src/cmd/uninstallCmd.go index c59753f..c2b6d6a 100644 --- a/src/cmd/uninstallCmd.go +++ b/src/cmd/uninstallCmd.go @@ -1,34 +1,43 @@ package cmd import ( - // "fmt" + "fmt" // "strings" // "github.com/spf13/cobra" log "oc-deploy/log_wrapper" - "oc-deploy/versionOc" + // "oc-deploy/versionOc" "oc-deploy/install" ) -func UninstallCmd(context string, version string) { - log.Log().Info().Msg("Install >> ") +func UninstallCmd(context string) { + log.Log().Info().Msg("Unnstall >> ") log.Log().Info().Msg(" << Contexte : " + context) - version, err := versionOc.Get(version) - log.Log().Info().Msg(" << Version : " + version) + workspace := fmt.Sprintf("workspace_%s", context) + obj := install.InstallClass{Workspace: workspace} + + file, err := obj.NewUninstall() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + log.Log().Info().Msg(fmt.Sprintf(" << Version : %s", obj.Version)) + log.Log().Info().Msg(fmt.Sprintf(" >> Config : %s", file)) + + err = obj.Tools() if err != nil { log.Log().Fatal().Msg(" >> " + err.Error()) } - obj := install.UninstallClass{Workspace: "workspace_" + context, Version: version} - obj.New() - - // fic, err := obj.New() - // if err != nil { - // log.Log().Fatal().Msg(" >> " + err.Error()) - // } - // log.Log().Info().Msg(" >> Value : " + fic) + err = obj.K8s(context) + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + err = obj.UninstallCharts() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } } \ No newline at end of file diff --git a/src/helm/chart.go b/src/helm/chart.go index 2201209..8761581 100644 --- a/src/helm/chart.go +++ b/src/helm/chart.go @@ -88,6 +88,14 @@ func (this HelmChart) Uninstall() (string, error) { log.Log().Info().Msg(" >> Chart : " + this.Name) + existe, err := this.exists() + if err != nil { + return "", err + } + if ! existe { + return "Non présent", nil + } + msg := fmt.Sprintf("%s uninstall %s", bin, this.Name) log.Log().Debug().Msg(msg) diff --git a/src/install/common.go b/src/install/common.go new file mode 100644 index 0000000..737c196 --- /dev/null +++ b/src/install/common.go @@ -0,0 +1,90 @@ +package install + +import ( + "fmt" + "errors" + + log "oc-deploy/log_wrapper" + "oc-deploy/tool" + "oc-deploy/kubectl" +) + +func (this *InstallClass) Tools() (error) { + + var mem []tool.ToolClass + + for _, v := range this.tools { + + tool2 := tool.ToolClass{} + v.Bin = this.Workspace + err := tool2.New(v) + if err != nil { + return err + } + mem = append(mem,tool2) + } + + this.toolsBin = make(map[string]string) + + for _, p := range mem { + data := p.Obj.Get() + log.Log().Info().Msg(fmt.Sprintf(" >> Outils : %s", data.Name)) + err := p.Locate() + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) + return err + } + log.Log().Info().Msg(fmt.Sprintf(" << %s ", p.Path)) + version, err1 := p.Version() + if err1 != nil { + log.Log().Info().Msg(fmt.Sprintf(" << %s ", err1)) + return err1 + } + log.Log().Info().Msg(fmt.Sprintf(" << %s ", version)) + + this.toolsBin[data.Name] = p.Path + } + + return nil +} + +func (this *InstallClass) getToolBin(name string) (string, error) { + for key, value := range this.toolsBin { + if key == name { + return value, nil + } + } + + return "", errors.New("Error") +} + +func (this *InstallClass) K8s(context string) (error) { + bin_path, _ := this.getToolBin("kubectl") + + kube := kubectl.KubeContext{Bin: bin_path} + + err := kube.UseContext(context) + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) + return err + } + + currentcontext, namespace, server, err := kube.GetContext() + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) + return err + } + log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", currentcontext)) + + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", namespace)) + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", server)) + + err = kube.Check() + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", err)) + return err + } + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", "OK")) + + return nil +} diff --git a/src/install/install.go b/src/install/install.go index 4045607..c0838a3 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -2,98 +2,62 @@ package install import ( "fmt" - "errors" "sync" - // "time" + "os" log "oc-deploy/log_wrapper" "oc-deploy/utils" "oc-deploy/tool" - "oc-deploy/helm" "oc-deploy/chart" + "oc-deploy/helm" "oc-deploy/kubectl" + "oc-deploy/versionOc" ) type InstallClass struct { Version string Workspace string - versionFile string + // versionFile string tools []tool.ToolData toolsBin map[string]string charts []chart.ChartRepoData } -func (this *InstallClass) New() (string, error) { +func (this *InstallClass) NewInstall() (string, error) { // Extraction du fichier de version - log.Log().Debug().Msg("Téléchargement du fichier de version") - src := fmt.Sprintf("../offline/oc_%s.yml", this.Version) dst := fmt.Sprintf("%s/oc.yml", this.Workspace) - - err := utils.CopyFile(src, dst) - if err != nil { - return "", err + log.Log().Debug().Msg(fmt.Sprintf("Check du fichier de version : %s", dst)) + if _, err := os.Stat(dst); err == nil { + log.Log().Debug().Msg("Existe déjà") + version, err := versionOc.GetFromFile(dst) + if err != nil { + return "", err + } + this.Version = version + } else { + log.Log().Debug().Msg("Téléchargement du fichier de version") + version, err := versionOc.Get(this.Version) + if err != nil { + return "", err + } + src := fmt.Sprintf("../offline/oc_%s.yml", version) + err = utils.CopyFile(src, dst) + if err != nil { + return "", err + } } // Lecture du fichier de conf - this.versionFile = dst + // this.versionFile = dst - this.tools = tool.FromConfigFile(this.versionFile) - - this.charts = chart.FromConfigFile(this.versionFile) + this.tools = tool.FromConfigFile(dst) + this.charts = chart.FromConfigFile(dst) return dst, nil } -func (this *InstallClass) Tools() (error) { - - var mem []tool.ToolClass - - for _, v := range this.tools { - - tool2 := tool.ToolClass{} - v.Bin = this.Workspace - err := tool2.New(v) - if err != nil { - return err - } - mem = append(mem,tool2) - } - - this.toolsBin = make(map[string]string) - - for _, p := range mem { - data := p.Obj.Get() - log.Log().Info().Msg(fmt.Sprintf(" >> Outils : %s", data.Name)) - err := p.Locate() - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) - return err - } - log.Log().Info().Msg(fmt.Sprintf(" << %s ", p.Path)) - version, err1 := p.Version() - if err1 != nil { - log.Log().Info().Msg(fmt.Sprintf(" << %s ", err1)) - return err1 - } - log.Log().Info().Msg(fmt.Sprintf(" << %s ", version)) - - this.toolsBin[data.Name] = p.Path - } - - return nil -} - -func (this *InstallClass) getToolBin(name string) (string, error) { - for key, value := range this.toolsBin { - if key == name { - return value, nil - } - } - - return "", errors.New("Error") -} func (this *InstallClass) ChartRepo() (error) { @@ -116,38 +80,8 @@ func (this *InstallClass) ChartRepo() (error) { return nil } -func (this *InstallClass) K8s(context string) (error) { - bin_path, _ := this.getToolBin("kubectl") - kube := kubectl.KubeContext{Bin: bin_path} - - err := kube.UseContext(context) - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) - return err - } - - currentcontext, namespace, server, err := kube.GetContext() - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) - return err - } - log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", currentcontext)) - - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", namespace)) - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", server)) - - err = kube.Check() - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", err)) - return err - } - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", "OK")) - - return nil -} - -func (this *InstallClass) Charts() (error) { +func (this *InstallClass) InstallCharts() (error) { helm_bin, _ := this.getToolBin("helm") kubectl_bin, _ := this.getToolBin("kubectl") @@ -159,7 +93,7 @@ func (this *InstallClass) Charts() (error) { go func() { defer wg.Done() - this.worker(helm_bin, kubectl_bin, v1) + this.installChart(helm_bin, kubectl_bin, v1) } () } @@ -168,7 +102,7 @@ func (this *InstallClass) Charts() (error) { return nil } -func (this *InstallClass) worker(helm_bin string, kubectl_bin string, chart chart.ChartData) { +func (this *InstallClass) installChart(helm_bin string, kubectl_bin string, chart chart.ChartData) { log.Log().Info().Msg(fmt.Sprintf(" << Chart : %s ", chart.Name)) @@ -177,7 +111,7 @@ func (this *InstallClass) worker(helm_bin string, kubectl_bin string, chart char Chart: chart.Chart, Version: chart.Version, Workspace: this.Workspace, - Opts: chart.Opts, + Opts: chart.Opts, Values: chart.Values, FileValues: chart.FileValues} diff --git a/src/install/uninstall.go b/src/install/uninstall.go index d5f24f5..fabfd8f 100644 --- a/src/install/uninstall.go +++ b/src/install/uninstall.go @@ -1,18 +1,69 @@ package install import ( - // "fmt" - // "os" - // "io" - // "path/filepath" - // log "oc-deploy/log_wrapper" + "fmt" + "os" + "sync" + + log "oc-deploy/log_wrapper" + "oc-deploy/versionOc" + "oc-deploy/tool" + "oc-deploy/chart" + "oc-deploy/helm" ) -type UninstallClass struct { - Version string - Workspace string +func (this *InstallClass) NewUninstall() (string, error) { + dst := fmt.Sprintf("%s/oc.yml", this.Workspace) + + if _, err := os.Stat(dst); err != nil { + return dst, err + } + + version, err := versionOc.GetFromFile(dst) + if err != nil { + return "", err + } + + this.Version = version + + // Lecture du fichier de conf + this.tools = tool.FromConfigFile(dst) + this.charts = chart.FromConfigFile(dst) + + return dst, nil } -func (this UninstallClass) New() { +func (this *InstallClass) UninstallCharts() (error) { + helm_bin, _ := this.getToolBin("helm") + kubectl_bin, _ := this.getToolBin("kubectl") + var wg sync.WaitGroup + + for _, v := range this.charts { + for _, v1 := range v.Charts { + wg.Add(1) + + go func() { + defer wg.Done() + this.uninstallChart(helm_bin, kubectl_bin, v1) + } () + } + } + wg.Wait() + return nil } + +func (this *InstallClass) uninstallChart(helm_bin string, kubectl_bin string, chart chart.ChartData) { + + log.Log().Info().Msg(fmt.Sprintf(" << Chart : %s ", chart.Name)) + + helmchart := helm.HelmChart{Bin: helm_bin, + Name: chart.Name} + + res, err := helmchart.Uninstall() + if err != nil { + log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", helmchart.Name, "KO", err)) + return + } + log.Log().Info().Msg(fmt.Sprintf(" >> %s (%s)", helmchart.Name, res)) +} \ No newline at end of file diff --git a/src/versionOc/versionOc.go b/src/versionOc/versionOc.go index ae6ee3c..d72962c 100644 --- a/src/versionOc/versionOc.go +++ b/src/versionOc/versionOc.go @@ -14,6 +14,25 @@ type versionInput struct { Version string `yaml:"version"` } +func GetFromFile(fileversion string) (string, error) { + fin, err := os.Open(fileversion) + if err != nil { + return "", err + } + defer fin.Close() + + byteValue, err := ioutil.ReadAll(fin) + if err != nil { + return "", err + } + + var objmap versionInput + + yaml.Unmarshal(byteValue, &objmap) + + return objmap.Version, nil +} + // Get : Retourne la version func Get(version string) (string, error) { @@ -50,9 +69,9 @@ func readLatestFile() (string, error) { return "", err } - var objmap versionInput + var objmap versionInput - yaml.Unmarshal(byteValue, &objmap) + yaml.Unmarshal(byteValue, &objmap) return objmap.Version, nil } \ No newline at end of file