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"`
}
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

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

View File

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

View File

@ -27,8 +27,14 @@ func (this *InstallClass) NewUninstall() (string, error) {
this.Version = version
// Lecture du fichier de conf
this.tools = tool.FromConfigFile(dst)
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
}
@ -66,4 +72,4 @@ func (this *InstallClass) uninstallChart(helm_bin string, kubectl_bin string, ch
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

@ -36,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"
)
@ -21,7 +21,7 @@ func GetFromFile(fileversion string) (string, error) {
}
defer fin.Close()
byteValue, err := ioutil.ReadAll(fin)
byteValue, err := io.ReadAll(fin)
if err != nil {
return "", err
}
@ -64,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
}
@ -74,4 +74,4 @@ func readLatestFile() (string, error) {
yaml.Unmarshal(byteValue, &objmap)
return objmap.Version, nil
}
}