Uninstall

This commit is contained in:
admju 2024-09-02 11:43:11 +00:00
parent 4ae5926b01
commit 11f56722f7
9 changed files with 241 additions and 134 deletions

View File

@ -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:

View File

@ -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",
}

View File

@ -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())
}

View File

@ -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())
}
}

View File

@ -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)

90
src/install/common.go Normal file
View File

@ -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
}

View File

@ -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}

View File

@ -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))
}

View File

@ -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
}