Compare commits

...

4 Commits

Author SHA1 Message Date
admju
11a4d5cc90 lint 2024-09-03 13:18:20 +00:00
admju
26404e5892 lint 2024-09-03 11:46:45 +00:00
admju
9cf954776f format Unix 2024-09-02 11:44:44 +00:00
admju
11f56722f7 Uninstall 2024-09-02 11:43:39 +00:00
31 changed files with 1242 additions and 1111 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

@ -26,9 +26,12 @@ type chartsRepoData struct {
Charts []ChartRepoData `yaml:"opencloud"`
}
func FromConfigFile(filename string) ([]ChartRepoData) {
func FromConfigFile(filename string) ([]ChartRepoData, error) {
yamlFile, _ := os.ReadFile(filename)
var data chartsRepoData
yaml.Unmarshal(yamlFile, &data)
return data.Charts
err := yaml.Unmarshal(yamlFile, &data)
if err != nil {
return data.Charts, err
}
return data.Charts, nil
}

View File

@ -14,7 +14,7 @@ func TestReadConfChart(t *testing.T){
assert.FileExists(t, src, "FromConfigFile error")
data := FromConfigFile(src)
data, _ := FromConfigFile(src)
assert.Equal(t, data[0].Name, "bitnami", "FromConfigFile error")
assert.Equal(t, data[0].Repository, "https://charts.bitnami.com/bitnami", "FromConfigFile error")

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

@ -8,7 +8,6 @@ import (
type GenerateClass struct {
Version string
Workspace string
url string
}
func (this GenerateClass) New() (string, error) {

View File

@ -76,7 +76,10 @@ func (this HelmChart) Install() (string, error) {
var objmap installOutput
json.Unmarshal(stdout, &objmap)
err = json.Unmarshal(stdout, &objmap)
if err != nil {
return "", err
}
res := objmap.Info.Status
@ -88,6 +91,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,69 @@ 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)
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)
var err error
this.tools, err = tool.FromConfigFile(dst)
if err != nil {
return dst, err
}
this.charts, _ = chart.FromConfigFile(dst)
if err != nil {
return dst, err
}
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 +87,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 +100,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 +109,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))

View File

@ -1,18 +1,75 @@
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, err = tool.FromConfigFile(dst)
if err != nil {
return dst, err
}
this.charts, _ = chart.FromConfigFile(dst)
if err != nil {
return dst, err
}
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

@ -60,7 +60,10 @@ func (this KubeContext) GetContext() (string, string, string, error) {
var objmap kubeConfig
json.Unmarshal(stdout, &objmap)
err := json.Unmarshal(stdout, &objmap)
if err != nil {
return "", "", "", err
}
currentContext := objmap.CurrentContext
currentCluster := ""

View File

@ -9,9 +9,12 @@ type toolsData struct {
Tools []ToolData `yaml:"tools"`
}
func FromConfigFile(filename string) ([]ToolData) {
func FromConfigFile(filename string) ([]ToolData, error) {
yamlFile, _ := os.ReadFile(filename)
var data toolsData
yaml.Unmarshal(yamlFile, &data)
return data.Tools
err := yaml.Unmarshal(yamlFile, &data)
if err != nil {
return data.Tools, nil
}
return data.Tools, nil
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"os"
// log "oc-deploy/log_wrapper"
"oc-deploy/utils"
"oc-deploy/helm"
)
@ -37,14 +36,16 @@ func (this HelmInstallData) Download() (error) {
r, _ := os.Open(tmp_file)
err1 := utils.ExtractTarGz(bin_dir, r)
if err1 != nil {return err1}
os.Remove(tmp_file)
bin_file := fmt.Sprintf("%s/%s", bin_dir, this.obj.Name)
os.Chmod(bin_file, 0755)
errChmod := os.Chmod(bin_file, 0755)
if errChmod != nil {return errChmod}
return err1
return nil
}
///////////////

View File

@ -4,7 +4,6 @@ import (
"fmt"
"os"
"os/exec"
"errors"
"path/filepath"
)
@ -73,7 +72,7 @@ func factory(data ToolData) (Forme, error) {
case "helm":
f = HelmInstallData{obj: data, tmp: "/tmp"}
default:
return f, errors.New(fmt.Sprintf("Outil Inconnu : %s", data.Name))
return f, fmt.Errorf("Outil Inconnu : %s", data.Name)
}
return f, nil

View File

@ -43,7 +43,7 @@ func ExtractTarGz(dest string, gzipStream io.Reader) error {
tarReader := tar.NewReader(uncompressedStream)
for true {
for {
header, err := tarReader.Next()
if err == io.EOF {

View File

@ -5,7 +5,7 @@ import (
"fmt"
"os"
"errors"
"io/ioutil"
"io"
"gopkg.in/yaml.v2"
log "oc-deploy/log_wrapper"
)
@ -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 := io.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) {
@ -45,7 +64,7 @@ func readLatestFile() (string, error) {
}
defer fin.Close()
byteValue, err := ioutil.ReadAll(fin)
byteValue, err := io.ReadAll(fin)
if err != nil {
return "", err
}