This commit is contained in:
admju 2024-09-03 13:18:20 +00:00
parent 26404e5892
commit 11a4d5cc90
12 changed files with 121 additions and 96 deletions

View File

@ -26,9 +26,12 @@ type chartsRepoData struct {
Charts []ChartRepoData `yaml:"opencloud"` Charts []ChartRepoData `yaml:"opencloud"`
} }
func FromConfigFile(filename string) ([]ChartRepoData) { func FromConfigFile(filename string) ([]ChartRepoData, error) {
yamlFile, _ := os.ReadFile(filename) yamlFile, _ := os.ReadFile(filename)
var data chartsRepoData var data chartsRepoData
yaml.Unmarshal(yamlFile, &data) err := yaml.Unmarshal(yamlFile, &data)
return data.Charts 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") 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].Name, "bitnami", "FromConfigFile error")
assert.Equal(t, data[0].Repository, "https://charts.bitnami.com/bitnami", "FromConfigFile error") assert.Equal(t, data[0].Repository, "https://charts.bitnami.com/bitnami", "FromConfigFile error")

View File

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

View File

@ -1,31 +1,31 @@
package helm package helm
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"errors" "errors"
"path/filepath" "path/filepath"
"encoding/json" "encoding/json"
log "oc-deploy/log_wrapper" log "oc-deploy/log_wrapper"
) )
type HelmChart struct { type HelmChart struct {
Bin string Bin string
Name string Name string
Chart string Chart string
Version string Version string
Workspace string Workspace string
Opts string Opts string
Values string Values string
FileValues string FileValues string
} }
type installInfoOutput struct { type installInfoOutput struct {
Description string `json:"description"` Description string `json:"description"`
Notes string `json:"notes"` Notes string `json:"notes"`
Status string `json:"status"` Status string `json:"status"`
} }
type installOutput struct { type installOutput struct {
@ -33,97 +33,100 @@ type installOutput struct {
} }
func (this HelmChart) Install() (string, error) { func (this HelmChart) Install() (string, error) {
bin := this.Bin bin := this.Bin
existe, err := this.exists() existe, err := this.exists()
if err != nil { if err != nil {
return "", err return "", err
} }
if existe { if existe {
return "Existe déjà", nil return "Existe déjà", nil
} }
msg := fmt.Sprintf("%s install %s %s %s --output json", bin, this.Name, this.Chart, this.Opts) msg := fmt.Sprintf("%s install %s %s %s --output json", bin, this.Name, this.Chart, this.Opts)
if this.Version != "" { if this.Version != "" {
msg = fmt.Sprintf("%s --version %s", msg, this.Version) msg = fmt.Sprintf("%s --version %s", msg, this.Version)
} }
if this.FileValues != "" { if this.FileValues != "" {
fic := filepath.Join(this.Workspace, this.FileValues) fic := filepath.Join(this.Workspace, this.FileValues)
if _, err := os.Stat(fic); err != nil { if _, err := os.Stat(fic); err != nil {
log.Log().Warn().Msg(fic) log.Log().Warn().Msg(fic)
} else { } else {
msg = fmt.Sprintf("%s --values %s", msg, fic) msg = fmt.Sprintf("%s --values %s", msg, fic)
} }
} }
msg = strings.Replace(msg, " ", " ", -1) msg = strings.Replace(msg, " ", " ", -1)
log.Log().Debug().Msg(msg) log.Log().Debug().Msg(msg)
cmd_args := strings.Split(msg, " ") cmd_args := strings.Split(msg, " ")
cmd := exec.Command(cmd_args[0], cmd_args[1:]...) cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
res := string(stdout) res := string(stdout)
res = strings.TrimSuffix(res, "\n") res = strings.TrimSuffix(res, "\n")
return "", errors.New(res) return "", errors.New(res)
} }
var objmap installOutput var objmap installOutput
json.Unmarshal(stdout, &objmap) err = json.Unmarshal(stdout, &objmap)
if err != nil {
return "", err
}
res := objmap.Info.Status res := objmap.Info.Status
return res, nil return res, nil
} }
func (this HelmChart) Uninstall() (string, error) { func (this HelmChart) Uninstall() (string, error) {
bin := this.Bin bin := this.Bin
log.Log().Info().Msg(" >> Chart : " + this.Name) log.Log().Info().Msg(" >> Chart : " + this.Name)
existe, err := this.exists() existe, err := this.exists()
if err != nil { if err != nil {
return "", err return "", err
} }
if ! existe { if ! existe {
return "Non présent", nil return "Non présent", nil
} }
msg := fmt.Sprintf("%s uninstall %s", bin, this.Name) msg := fmt.Sprintf("%s uninstall %s", bin, this.Name)
log.Log().Debug().Msg(msg) log.Log().Debug().Msg(msg)
cmd := exec.Command(bin, "uninstall", this.Name) cmd := exec.Command(bin, "uninstall", this.Name)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
return string(stdout), err return string(stdout), err
} }
// ../bin/helm list --filter phpmyadminm --short // ../bin/helm list --filter phpmyadminm --short
func (this HelmChart) exists() (bool, error) { func (this HelmChart) exists() (bool, error) {
bin := this.Bin bin := this.Bin
msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.Name) msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.Name)
log.Log().Debug().Msg(msg) log.Log().Debug().Msg(msg)
cmd_args := strings.Split(msg, " ") cmd_args := strings.Split(msg, " ")
cmd := exec.Command(cmd_args[0], cmd_args[1:]...) cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return false, errors.New(string(stdout)) return false, errors.New(string(stdout))
} }
res := string(stdout) res := string(stdout)
res = strings.TrimSuffix(res, "\n") res = strings.TrimSuffix(res, "\n")
log.Log().Debug().Msg(string(stdout)) log.Log().Debug().Msg(string(stdout))
return res != "", nil return res != "", nil
} }

View File

@ -52,8 +52,15 @@ func (this *InstallClass) NewInstall() (string, error) {
// Lecture du fichier de conf // Lecture du fichier de conf
// this.versionFile = dst // this.versionFile = dst
this.tools = tool.FromConfigFile(dst) var err error
this.charts = chart.FromConfigFile(dst) 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 return dst, nil
} }

View File

@ -27,8 +27,14 @@ func (this *InstallClass) NewUninstall() (string, error) {
this.Version = version this.Version = version
// Lecture du fichier de conf // Lecture du fichier de conf
this.tools = tool.FromConfigFile(dst) this.tools, err = tool.FromConfigFile(dst)
this.charts = chart.FromConfigFile(dst) if err != nil {
return dst, err
}
this.charts, _ = chart.FromConfigFile(dst)
if err != nil {
return dst, err
}
return dst, nil return dst, nil
} }

View File

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

View File

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

View File

@ -36,14 +36,16 @@ func (this HelmInstallData) Download() (error) {
r, _ := os.Open(tmp_file) r, _ := os.Open(tmp_file)
err1 := utils.ExtractTarGz(bin_dir, r) err1 := utils.ExtractTarGz(bin_dir, r)
if err1 != nil {return err1}
os.Remove(tmp_file) os.Remove(tmp_file)
bin_file := fmt.Sprintf("%s/%s", bin_dir, this.obj.Name) 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" "fmt"
"os" "os"
"os/exec" "os/exec"
"errors"
"path/filepath" "path/filepath"
) )
@ -73,7 +72,7 @@ func factory(data ToolData) (Forme, error) {
case "helm": case "helm":
f = HelmInstallData{obj: data, tmp: "/tmp"} f = HelmInstallData{obj: data, tmp: "/tmp"}
default: default:
return f, errors.New(fmt.Sprintf("Outil Inconnu : %s", data.Name)) return f, fmt.Errorf("Outil Inconnu : %s", data.Name)
} }
return f, nil return f, nil

View File

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

View File

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