oc-deploy/src/helm/chart.go

159 lines
3.5 KiB
Go
Raw Normal View History

2024-09-02 13:44:44 +02:00
package helm
import (
2024-09-03 15:18:20 +02:00
"fmt"
2024-09-06 16:08:05 +02:00
"strconv"
2024-09-03 15:18:20 +02:00
"os"
"os/exec"
"strings"
"errors"
"path/filepath"
"encoding/json"
2024-09-02 13:44:44 +02:00
log "oc-deploy/log_wrapper"
)
type HelmChart struct {
2024-09-03 15:18:20 +02:00
Bin string
2024-09-02 13:44:44 +02:00
Name string
2024-09-03 15:18:20 +02:00
Chart string
Version string
2024-09-03 17:18:11 +02:00
Url string
2024-09-03 15:18:20 +02:00
Workspace string
Opts string
Values string
FileValues string
2024-09-02 13:44:44 +02:00
}
type installInfoOutput struct {
Description string `json:"description"`
2024-09-03 15:18:20 +02:00
Notes string `json:"notes"`
Status string `json:"status"`
2024-09-02 13:44:44 +02:00
}
type installOutput struct {
Info installInfoOutput `json:"info"`
}
func (this HelmChart) Install() (string, error) {
2024-09-03 15:18:20 +02:00
bin := this.Bin
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
existe, err := this.exists()
if err != nil {
return "", err
}
if existe {
return "Existe déjà", nil
}
2024-09-02 13:44:44 +02:00
2024-09-03 17:18:11 +02:00
ficChart := this.Chart
// Recherche locale
if _, err := os.Stat(ficChart); err != nil {
} else {
// Recherche voa le Workspace
ficChart := filepath.Join(this.Workspace, this.Chart)
if _, err := os.Stat(ficChart); err == nil {
} else {
if this.Url != "" {
fmt.Println("============ 52 Télechargement", this.Url)
}
}
}
msg := fmt.Sprintf("%s install %s %s %s --output json", bin, this.Name, ficChart, this.Opts)
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
if this.Version != "" {
msg = fmt.Sprintf("%s --version %s", msg, this.Version)
}
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
if this.FileValues != "" {
2024-09-02 13:44:44 +02:00
fic := filepath.Join(this.Workspace, this.FileValues)
2024-09-03 15:18:20 +02:00
if _, err := os.Stat(fic); err != nil {
log.Log().Warn().Msg(fic)
} else {
msg = fmt.Sprintf("%s --values %s", msg, fic)
}
}
2024-09-02 13:44:44 +02:00
msg = strings.Replace(msg, " ", " ", -1)
2024-09-03 15:18:20 +02:00
log.Log().Debug().Msg(msg)
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
cmd_args := strings.Split(msg, " ")
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
if err != nil {
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return "", errors.New(res)
}
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
var objmap installOutput
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
err = json.Unmarshal(stdout, &objmap)
if err != nil {
return "", err
}
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
res := objmap.Info.Status
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
return res, nil
2024-09-02 13:44:44 +02:00
}
func (this HelmChart) Uninstall() (string, error) {
2024-09-03 15:18:20 +02:00
bin := this.Bin
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
log.Log().Info().Msg(" >> Chart : " + this.Name)
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
existe, err := this.exists()
if err != nil {
return "", err
}
2024-09-02 13:44:44 +02:00
if ! existe {
return "Non présent", nil
}
2024-09-03 15:18:20 +02:00
msg := fmt.Sprintf("%s uninstall %s", bin, this.Name)
log.Log().Debug().Msg(msg)
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
cmd := exec.Command(bin, "uninstall", this.Name)
stdout, err := cmd.CombinedOutput()
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
return string(stdout), err
2024-09-02 13:44:44 +02:00
}
// ../bin/helm list --filter phpmyadminm --short
func (this HelmChart) exists() (bool, error) {
2024-09-03 15:18:20 +02:00
bin := this.Bin
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.Name)
log.Log().Debug().Msg(msg)
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
cmd_args := strings.Split(msg, " ")
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
if err != nil {
2024-09-06 16:08:05 +02:00
log.Log().Debug().Msg(string(stdout))
2024-09-03 15:18:20 +02:00
return false, errors.New(string(stdout))
}
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
log.Log().Debug().Msg(string(stdout))
2024-09-06 16:08:05 +02:00
log.Log().Debug().Msg(strconv.FormatBool(res != ""))
2024-09-02 13:44:44 +02:00
2024-09-03 15:18:20 +02:00
return res != "", nil
2024-09-02 13:44:44 +02:00
}
2024-09-06 16:08:05 +02:00
func (this HelmChart) GetRessources() (map[string]string, error) {
hs := HelmStatus{Name: this.Name}
hs.New(this.Bin)
data, _ := hs.getRessources()
return data, nil
}