init bin
This commit is contained in:
121
src/helm/chart.go
Normal file
121
src/helm/chart.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"encoding/json"
|
||||
log "oc-deploy/log_wrapper"
|
||||
)
|
||||
|
||||
type HelmChart struct {
|
||||
Bin string
|
||||
Name 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"`
|
||||
}
|
||||
|
||||
type installOutput struct {
|
||||
Info installInfoOutput `json:"info"`
|
||||
}
|
||||
|
||||
func (this HelmChart) Install() (string, error) {
|
||||
bin := this.Bin
|
||||
|
||||
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)
|
||||
|
||||
if this.Version != "" {
|
||||
msg = fmt.Sprintf("%s --version %s", msg, this.Version)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
msg = strings.Replace(msg, " ", " ", -1)
|
||||
|
||||
log.Log().Debug().Msg(msg)
|
||||
|
||||
cmd_args := strings.Split(msg, " ")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
var objmap installOutput
|
||||
|
||||
json.Unmarshal(stdout, &objmap)
|
||||
|
||||
res := objmap.Info.Status
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (this HelmChart) Uninstall() (string, error) {
|
||||
bin := this.Bin
|
||||
|
||||
log.Log().Info().Msg(" >> Chart : " + this.Name)
|
||||
|
||||
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()
|
||||
|
||||
return string(stdout), err
|
||||
}
|
||||
|
||||
// ../bin/helm list --filter phpmyadminm --short
|
||||
func (this HelmChart) exists() (bool, error) {
|
||||
bin := this.Bin
|
||||
|
||||
msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.Name)
|
||||
log.Log().Debug().Msg(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))
|
||||
}
|
||||
|
||||
res := string(stdout)
|
||||
res = strings.TrimSuffix(res, "\n")
|
||||
|
||||
log.Log().Debug().Msg(string(stdout))
|
||||
|
||||
return res != "", nil
|
||||
}
|
||||
24
src/helm/main_test.go
Normal file
24
src/helm/main_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var TEST_DEST_DIR = "../wrk_helm"
|
||||
var TEST_SRC_DIR = filepath.Join("../../test", "helm")
|
||||
var TEST_BIN_DIR = filepath.Join("../../test", "bin")
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
folderPath := TEST_DEST_DIR
|
||||
|
||||
os.RemoveAll(folderPath)
|
||||
os.MkdirAll(folderPath, os.ModePerm)
|
||||
|
||||
// call flag.Parse() here if TestMain uses flags
|
||||
exitCode := m.Run()
|
||||
|
||||
os.RemoveAll(folderPath)
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
53
src/helm/repo.go
Normal file
53
src/helm/repo.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"os/exec"
|
||||
|
||||
log "oc-deploy/log_wrapper"
|
||||
)
|
||||
|
||||
type HelmRepo struct {
|
||||
Bin string // Chemin vers le binaire
|
||||
Name string
|
||||
Repository string // Url du dépôt
|
||||
ForceUpdate bool
|
||||
Opts string
|
||||
}
|
||||
|
||||
func (this HelmRepo) AddRepository() (string, error) {
|
||||
helm_bin := this.Bin
|
||||
|
||||
force_update := "--force-update=false"
|
||||
if this.ForceUpdate {
|
||||
force_update = "--force-update=true"
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, this.Name, this.Repository, force_update, this.Opts)
|
||||
log.Log().Debug().Msg(msg)
|
||||
|
||||
cmd := exec.Command(helm_bin, "repo", "add", this.Name, this.Repository, force_update)
|
||||
stdout, err := cmd.CombinedOutput()
|
||||
|
||||
res := string(stdout)
|
||||
res = strings.TrimSuffix(res, "\n")
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
// helm repo remove [NAME]
|
||||
func (this HelmRepo) RemoveRepository() (string, error) {
|
||||
helm_bin := this.Bin
|
||||
|
||||
msg := fmt.Sprintf("%s repo remove %s", helm_bin, this.Name)
|
||||
log.Log().Debug().Msg(msg)
|
||||
|
||||
cmd := exec.Command(helm_bin, "repo", "remove", this.Name)
|
||||
stdout, err := cmd.CombinedOutput()
|
||||
|
||||
res := string(stdout)
|
||||
res = strings.TrimSuffix(res, "\n")
|
||||
|
||||
return res, err
|
||||
}
|
||||
23
src/helm/repo_test.go
Normal file
23
src/helm/repo_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
// "os"
|
||||
// "path/filepath"
|
||||
|
||||
"testing"
|
||||
|
||||
// "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHelmRepoAdd(t *testing.T){
|
||||
// fmt.Println(" TestVersion ", TEST_BIN_DIR)
|
||||
|
||||
// bin := filepath.Join(TEST_BIN_DIR, "helm")
|
||||
// os.Chmod(bin, 0700)
|
||||
// assert.FileExists(t, bin, "TestHelmVersion error")
|
||||
|
||||
// version, err := Version(bin)
|
||||
|
||||
// assert.Nilf(t, err, "error message %s", bin)
|
||||
// assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error")
|
||||
}
|
||||
20
src/helm/version.go
Normal file
20
src/helm/version.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Version(path string) (string, error) {
|
||||
|
||||
cmd := exec.Command(path, "version", "--short")
|
||||
stdout, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
res := string(stdout)
|
||||
res = strings.TrimSuffix(res, "\n")
|
||||
return res, nil
|
||||
}
|
||||
22
src/helm/version_test.go
Normal file
22
src/helm/version_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHelmVersion(t *testing.T){
|
||||
|
||||
bin := filepath.Join(TEST_BIN_DIR, "helm")
|
||||
os.Chmod(bin, 0700)
|
||||
assert.FileExists(t, bin, "TestHelmVersion error")
|
||||
|
||||
version, err := Version(bin)
|
||||
|
||||
assert.Nilf(t, err, "error message %s", bin)
|
||||
assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error")
|
||||
}
|
||||
Reference in New Issue
Block a user