diff --git a/src/cmd/args.go b/src/cmd/args.go index bacbd48..5d4f04c 100644 --- a/src/cmd/args.go +++ b/src/cmd/args.go @@ -55,13 +55,13 @@ func Execute() { Example: "oc-deploy generate --version 1.0 --context ex1", } - cmdInstall.Flags().StringVarP(&context, "context", "p", "opencloud", "Nom du projet") + cmdInstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") cmdInstall.Flags().StringVarP(&version, "version", "v", "latest", "Version") cmdInstall.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules, ...") - cmdUninstall.Flags().StringVarP(&context, "context", "p", "opencloud", "Nom du projet") + cmdUninstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") - cmdGenerate.Flags().StringVarP(&context, "context", "p", "opencloud", "Nom du projet") + cmdGenerate.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") cmdGenerate.Flags().StringVarP(&version, "version", "v", "latest", "Version") rootCmd.AddCommand(cmdInstall) diff --git a/src/cmd/installCmd.go b/src/cmd/installCmd.go index 72e0e87..b910c3c 100644 --- a/src/cmd/installCmd.go +++ b/src/cmd/installCmd.go @@ -30,6 +30,8 @@ func InstallCmd(context string, version string, modules []string) { log.Log().Fatal().Msg(" >> " + err.Error()) } + obj.SetCommands() + err = obj.ChartRepo() if err != nil { log.Log().Fatal().Msg(" >> " + err.Error()) diff --git a/src/helm/command.go b/src/helm/command.go index 0f3fe96..5fad825 100644 --- a/src/helm/command.go +++ b/src/helm/command.go @@ -1,38 +1,16 @@ package helm import ( - "fmt" - "strings" - "errors" - "os/exec" - - log "oc-deploy/log_wrapper" ) type HelmCommandInterface interface { - Status(string) (string, error) + Status(string) (string, error) + AddRepository(HelmRepo) (string, error) } type HelmCommandData struct { - bin string - // name string + Bin string } -type RealHelmCommandStatus HelmCommandData +type RealHelmCommand HelmCommandData -func (this RealHelmCommandStatus) Status(name string) (string, error) { - - msg := fmt.Sprintf("%s status %s --show-resources -o json", this.bin, 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 { - log.Log().Debug().Msg(string(stdout)) - return "", errors.New(string(stdout)) - } - - return string(stdout), nil -} diff --git a/src/helm/repo.go b/src/helm/repo.go index a5a9ba7..42b7443 100644 --- a/src/helm/repo.go +++ b/src/helm/repo.go @@ -4,30 +4,39 @@ import ( "fmt" "strings" "os/exec" + "encoding/json" log "oc-deploy/log_wrapper" + "oc-deploy/utils" ) 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) { +func (this RealHelmCommand) AddRepository(repo HelmRepo) (string, error) { + helm_bin := this.Bin force_update := "--force-update=false" - if this.ForceUpdate { + if repo.ForceUpdate { force_update = "--force-update=true" + } else { + list, _ := this.ListRepository() + if utils.StringInSlice(repo.Name, list) { + return "Existe déjà", nil + } } - msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, this.Name, this.Repository, force_update, this.Opts) + msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, repo.Name, repo.Repository, force_update, repo.Opts) log.Log().Debug().Msg(msg) - cmd := exec.Command(helm_bin, "repo", "add", this.Name, this.Repository, force_update) + cmd_args := strings.Split(msg, " ") + + cmd := exec.Command(cmd_args[0], cmd_args[1:]...) stdout, err := cmd.CombinedOutput() res := string(stdout) @@ -36,14 +45,48 @@ func (this HelmRepo) AddRepository() (string, error) { return res, err } +type parseList struct { + Name string `json:"name"` +} + +func (this RealHelmCommand) ListRepository() ([]string, error) { + + helm_bin := this.Bin + res := make([]string, 0, 0) + + msg := fmt.Sprintf("%s repo list -o json", helm_bin) + 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 res, err + } + + var objmap []parseList + + err = json.Unmarshal([]byte(stdout), &objmap) + if err != nil { + return res, err + } + + for _, ele := range objmap { + res = append(res, ele.Name) + } + + return res, err +} + // helm repo remove [NAME] -func (this HelmRepo) RemoveRepository() (string, error) { +func (this RealHelmCommand) RemoveRepository(repo HelmRepo) (string, error) { helm_bin := this.Bin - msg := fmt.Sprintf("%s repo remove %s", helm_bin, this.Name) + msg := fmt.Sprintf("%s repo remove %s", helm_bin, repo.Name) log.Log().Debug().Msg(msg) - cmd := exec.Command(helm_bin, "repo", "remove", this.Name) + cmd := exec.Command(helm_bin, "repo", "remove", repo.Name) stdout, err := cmd.CombinedOutput() res := string(stdout) diff --git a/src/helm/ressources.go b/src/helm/ressources.go index f7becb3..dde5b33 100644 --- a/src/helm/ressources.go +++ b/src/helm/ressources.go @@ -11,7 +11,7 @@ type HelmStatus struct { } func (this *HelmStatus) New(bin string) { - this.command = RealHelmCommandStatus{bin: bin} + this.command = RealHelmCommand{Bin: bin} } func (this *HelmStatus) NewMock(mock HelmCommandInterface) { diff --git a/src/helm/status.go b/src/helm/status.go new file mode 100644 index 0000000..5a7a991 --- /dev/null +++ b/src/helm/status.go @@ -0,0 +1,30 @@ +package helm + +import ( + "fmt" + "strings" + "errors" + "os/exec" + + log "oc-deploy/log_wrapper" +) + +func (this RealHelmCommand) Status(name string) (string, error) { + + helm_bin := this.Bin + + msg := fmt.Sprintf("%s status %s --show-resources -o json", helm_bin, 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 { + log.Log().Debug().Msg(string(stdout)) + return "", errors.New(string(stdout)) + } + + return string(stdout), nil +} + diff --git a/src/install/common.go b/src/install/common.go index 5ec3694..67ba0f5 100644 --- a/src/install/common.go +++ b/src/install/common.go @@ -7,6 +7,7 @@ import ( log "oc-deploy/log_wrapper" "oc-deploy/tool" "oc-deploy/kubectl" + "oc-deploy/helm" ) func (this *InstallClass) Tools() (error) { @@ -48,6 +49,18 @@ func (this *InstallClass) Tools() (error) { return nil } +func (this *InstallClass) SetCommands() { + helm_bin, _ := this.getToolBin("helm") + this.commandHelm = helm.RealHelmCommand{Bin: helm_bin} + + // kubectl_bin, _ := this.getToolBin("kubectl") + // this.commandKubectl = helm.RealHelmCommand{Bin: kubectl_bin} +} + +func (this *InstallClass) SetCommandsMock(mock helm.HelmCommandInterface) { + this.commandHelm = mock +} + func (this *InstallClass) getToolBin(name string) (string, error) { for key, value := range this.toolsBin { if key == name { diff --git a/src/install/install.go b/src/install/install.go index e2f35e2..a280f61 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -21,6 +21,8 @@ type InstallClass struct { tools []tool.ToolData toolsBin map[string]string charts []chart.ChartRepoData + + commandHelm helm.HelmCommandInterface } func (this *InstallClass) NewInstall() (string, error) { @@ -61,22 +63,22 @@ func (this *InstallClass) NewInstall() (string, error) { return dst, err } + bin_path, _ := this.getToolBin("helm") + fmt.Println("Install 67 bin_path", bin_path) + return dst, nil } func (this *InstallClass) ChartRepo() (error) { - bin_path, _ := this.getToolBin("helm") - for _, v := range this.charts { 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() + repo := helm.HelmRepo{Name: v.Repository.Name, + Repository: v.Repository.Url, + ForceUpdate: v.Repository.ForceUpdate} + res, err := this.commandHelm.AddRepository(repo) if err != nil { log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) return err diff --git a/src/utils/slice.go b/src/utils/slice.go new file mode 100644 index 0000000..4eb462e --- /dev/null +++ b/src/utils/slice.go @@ -0,0 +1,10 @@ +package utils + +func StringInSlice(a string, list []string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +}