This commit is contained in:
admju
2024-09-02 07:09:46 +00:00
parent 00d92b73f8
commit 4ae5926b01
40 changed files with 1711 additions and 0 deletions

17
src/tool/conf.go Normal file
View File

@@ -0,0 +1,17 @@
package tool
import (
"os"
"gopkg.in/yaml.v2"
)
type toolsData struct {
Tools []ToolData `yaml:"tools"`
}
func FromConfigFile(filename string) ([]ToolData) {
yamlFile, _ := os.ReadFile(filename)
var data toolsData
yaml.Unmarshal(yamlFile, &data)
return data.Tools
}

53
src/tool/helm.go Normal file
View File

@@ -0,0 +1,53 @@
package tool
import (
"fmt"
"os"
// log "oc-deploy/log_wrapper"
"oc-deploy/utils"
"oc-deploy/helm"
)
type HelmInstallData struct {
obj ToolData
tmp string
}
func (this HelmInstallData) Get() (ToolData) {
return this.obj
}
func (this HelmInstallData) Download() (error) {
bin_dir := this.obj.Bin
err2 := os.MkdirAll(bin_dir, os.ModePerm)
if err2 != nil {
fmt.Println(err2)
return err2
}
tmp_file := fmt.Sprintf("%s/oc-deploy-%s", this.tmp, this.obj.Name)
url := fmt.Sprintf(this.obj.Url, this.obj.Version)
err := utils.DownloadFromUrl(tmp_file, url, 0777)
if err != nil {
return err
}
r, _ := os.Open(tmp_file)
err1 := utils.ExtractTarGz(bin_dir, r)
os.Remove(tmp_file)
bin_file := fmt.Sprintf("%s/%s", bin_dir, this.obj.Name)
os.Chmod(bin_file, 0755)
return err1
}
///////////////
func (this HelmInstallData) Version(path string) (string, error) {
return helm.Version(path)
}

41
src/tool/kubectl.go Normal file
View File

@@ -0,0 +1,41 @@
package tool
import (
"fmt"
"os"
"path/filepath"
log "oc-deploy/log_wrapper"
"oc-deploy/utils"
"oc-deploy/kubectl"
)
type KubecltInstallData struct {
obj ToolData
}
func (this KubecltInstallData) Get() (ToolData) {
return this.obj
}
func (this KubecltInstallData) Download() (error) {
bin_dir := this.obj.Bin
bin := filepath.Join(bin_dir, this.obj.Name)
url := fmt.Sprintf(this.obj.Url, this.obj.Version)
log.Log().Debug().Msg(fmt.Sprintf("Téléchargement : %s, %s", bin, url))
os.MkdirAll(bin_dir, os.ModePerm)
err := utils.DownloadFromUrl(bin, url, 0777)
if err != nil {return err}
os.Chmod(bin, 0755)
return nil
}
///////////////
func (this KubecltInstallData) Version(path string) (string, error) {
return kubectl.Version(path)
}

80
src/tool/tool.go Normal file
View File

@@ -0,0 +1,80 @@
package tool
import (
"fmt"
"os"
"os/exec"
"errors"
"path/filepath"
)
type ToolData struct {
Name string `yaml:"name"`
Url string `yaml:"url"`
Version string `yaml:"version"`
Bin string
}
type ToolClass struct {
Obj Forme
Path string
}
type Forme interface {
Download() error
Version(string) (string, error)
Get() ToolData
}
//////////
func (this *ToolClass) New(data ToolData) (error) {
f, err := factory(data)
if err != nil {
return err
}
this.Obj = f
return nil
}
func (this *ToolClass) Locate() (error) {
obj := this.Obj
data := obj.Get()
path := filepath.Join(data.Bin, data.Name)
if _, err := os.Stat(path); err != nil {
path2, _ := exec.LookPath(data.Name)
if path2 != "" {
path = path2
} else {
err = obj.Download()
if err != nil {
return err
}
}
}
this.Path = path
return nil
}
func (this *ToolClass) Version() (string, error) {
obj := (this.Obj)
return obj.Version(this.Path)
}
//////////
func factory(data ToolData) (Forme, error) {
var f Forme
switch data.Name {
case "kubectl":
f = KubecltInstallData{obj: data}
case "helm":
f = HelmInstallData{obj: data, tmp: "/tmp"}
default:
return f, errors.New(fmt.Sprintf("Outil Inconnu : %s", data.Name))
}
return f, nil
}