2024-09-02 09:09:46 +02:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2024-09-02 13:43:11 +02:00
|
|
|
"os"
|
2024-09-02 09:09:46 +02:00
|
|
|
|
|
|
|
log "oc-deploy/log_wrapper"
|
|
|
|
"oc-deploy/utils"
|
|
|
|
"oc-deploy/tool"
|
|
|
|
"oc-deploy/chart"
|
2024-09-02 13:43:11 +02:00
|
|
|
"oc-deploy/helm"
|
2024-09-02 09:09:46 +02:00
|
|
|
"oc-deploy/kubectl"
|
2024-09-02 13:43:11 +02:00
|
|
|
"oc-deploy/versionOc"
|
2024-09-02 09:09:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type InstallClass struct {
|
|
|
|
Version string
|
|
|
|
Workspace string
|
|
|
|
|
2024-09-02 13:43:11 +02:00
|
|
|
// versionFile string
|
2024-09-02 09:09:46 +02:00
|
|
|
tools []tool.ToolData
|
|
|
|
toolsBin map[string]string
|
|
|
|
charts []chart.ChartRepoData
|
|
|
|
}
|
|
|
|
|
2024-09-02 13:43:11 +02:00
|
|
|
func (this *InstallClass) NewInstall() (string, error) {
|
2024-09-02 09:09:46 +02:00
|
|
|
|
|
|
|
// Extraction du fichier de version
|
|
|
|
dst := fmt.Sprintf("%s/oc.yml", this.Workspace)
|
2024-09-02 13:43:11 +02:00
|
|
|
log.Log().Debug().Msg(fmt.Sprintf("Check du fichier de version : %s", dst))
|
|
|
|
if _, err := os.Stat(dst); err == nil {
|
|
|
|
log.Log().Debug().Msg("Existe déjà")
|
|
|
|
version, err := versionOc.GetFromFile(dst)
|
2024-09-02 09:09:46 +02:00
|
|
|
if err != nil {
|
2024-09-02 13:43:11 +02:00
|
|
|
return "", err
|
2024-09-02 09:09:46 +02:00
|
|
|
}
|
2024-09-02 13:43:11 +02:00
|
|
|
this.Version = version
|
|
|
|
} else {
|
|
|
|
log.Log().Debug().Msg("Téléchargement du fichier de version")
|
|
|
|
version, err := versionOc.Get(this.Version)
|
2024-09-02 09:09:46 +02:00
|
|
|
if err != nil {
|
2024-09-02 13:43:11 +02:00
|
|
|
return "", err
|
2024-09-02 09:09:46 +02:00
|
|
|
}
|
2024-09-02 13:43:11 +02:00
|
|
|
src := fmt.Sprintf("../offline/oc_%s.yml", version)
|
|
|
|
err = utils.CopyFile(src, dst)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2024-09-02 09:09:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 13:43:11 +02:00
|
|
|
// Lecture du fichier de conf
|
|
|
|
// this.versionFile = dst
|
2024-09-02 09:09:46 +02:00
|
|
|
|
2024-09-03 15:18:20 +02:00
|
|
|
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
|
|
|
|
}
|
2024-09-02 09:09:46 +02:00
|
|
|
|
2024-09-02 13:43:11 +02:00
|
|
|
return dst, nil
|
2024-09-02 09:09:46 +02:00
|
|
|
}
|
|
|
|
|
2024-09-02 13:43:11 +02:00
|
|
|
|
2024-09-02 09:09:46 +02:00
|
|
|
func (this *InstallClass) ChartRepo() (error) {
|
|
|
|
|
|
|
|
bin_path, _ := this.getToolBin("helm")
|
|
|
|
|
|
|
|
for _, v := range this.charts {
|
2024-09-03 17:18:11 +02:00
|
|
|
if v.Repository.Name != "" {
|
|
|
|
log.Log().Info().Msg(fmt.Sprintf(" >> Helm Repo : %s", v.Repository.Name))
|
|
|
|
repo := helm.HelmRepo{Bin: bin_path,
|
|
|
|
Name: v.Repository.Name,
|
|
|
|
Repository: v.Repository.Url,
|
|
|
|
ForceUpdate: v.Repository.ForceUpdate}
|
|
|
|
res, err := repo.AddRepository()
|
|
|
|
if err != nil {
|
|
|
|
log.Log().Info().Msg(fmt.Sprintf(" << %s ", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Log().Info().Msg(fmt.Sprintf(" << %s ", res))
|
2024-09-02 09:09:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-02 13:43:11 +02:00
|
|
|
func (this *InstallClass) InstallCharts() (error) {
|
2024-09-02 09:09:46 +02:00
|
|
|
helm_bin, _ := this.getToolBin("helm")
|
|
|
|
kubectl_bin, _ := this.getToolBin("kubectl")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for _, v := range this.charts {
|
|
|
|
for _, v1 := range v.Charts {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2024-09-02 13:43:11 +02:00
|
|
|
this.installChart(helm_bin, kubectl_bin, v1)
|
2024-09-02 09:09:46 +02:00
|
|
|
} ()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-02 13:43:11 +02:00
|
|
|
func (this *InstallClass) installChart(helm_bin string, kubectl_bin string, chart chart.ChartData) {
|
2024-09-02 09:09:46 +02:00
|
|
|
|
|
|
|
log.Log().Info().Msg(fmt.Sprintf(" << Chart : %s ", chart.Name))
|
|
|
|
|
|
|
|
helmchart := helm.HelmChart{Bin: helm_bin,
|
|
|
|
Name: chart.Name,
|
|
|
|
Chart: chart.Chart,
|
2024-09-03 17:18:11 +02:00
|
|
|
Url: chart.Url,
|
2024-09-02 09:09:46 +02:00
|
|
|
Version: chart.Version,
|
|
|
|
Workspace: this.Workspace,
|
2024-09-02 13:43:11 +02:00
|
|
|
Opts: chart.Opts,
|
2024-09-02 09:09:46 +02:00
|
|
|
Values: chart.Values,
|
|
|
|
FileValues: chart.FileValues}
|
|
|
|
|
|
|
|
obj := kubectl.KubeObject{Bin: kubectl_bin,
|
|
|
|
Name: chart.Name}
|
|
|
|
|
|
|
|
res, err := helmchart.Install()
|
|
|
|
if err != nil {
|
2024-09-03 17:18:11 +02:00
|
|
|
log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", helmchart.Name, "KO", err))
|
2024-09-02 09:09:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Log().Info().Msg(fmt.Sprintf(" >> %s (%s)", helmchart.Name, res))
|
|
|
|
|
|
|
|
err = obj.Wait()
|
|
|
|
if err != nil {
|
|
|
|
log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", chart.Name, "KO", err))
|
|
|
|
} else {
|
|
|
|
log.Log().Info().Msg(fmt.Sprintf(" >> %s %s", chart.Name, "OK"))
|
|
|
|
}
|
|
|
|
}
|