From 03961773331cd564089661e99f3ae4800ea4ae89 Mon Sep 17 00:00:00 2001 From: Gene Liverman Date: Tue, 7 May 2024 11:05:36 -0400 Subject: [PATCH 1/3] Allow legacy --puppetfile flag to be used This allows for utilizing the current version of webhook-go in environments saddled with legacy versions of r10k that don't yet have the new --modules flag. --- README.md | 6 ++++++ api/environment.go | 6 +++++- config/config.go | 18 ++++++++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8ca7245..7e5e87b 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,12 @@ Type: bool Description: Deploy modules in environments. Default: `true` +### `use_legacy_puppetfile_flag` + +Type: bool +Description: Use the legacy `--puppetfile` flag instead of `--modules`. This should only be used when your version of r10k doesn't support the newer flag. +Default: `false` + ### `generate_types` Type: bool diff --git a/api/environment.go b/api/environment.go index 5e69e39..79364a2 100644 --- a/api/environment.go +++ b/api/environment.go @@ -63,7 +63,11 @@ func (e EnvironmentController) DeployEnvironment(c *gin.Context) { cmd = append(cmd, "--generate-types") } if conf.R10k.DeployModules { - cmd = append(cmd, "--modules") + if conf.R10k.UseLegacyPuppetfileFlag { + cmd = append(cmd, "--puppetfile") + } else { + cmd = append(cmd, "--modules") + } } // Pass the command to the execute function and act on the result and any error diff --git a/config/config.go b/config/config.go index 1e1adee..640575e 100644 --- a/config/config.go +++ b/config/config.go @@ -35,14 +35,15 @@ type Config struct { ServerUri string `mapstructure:"server_uri"` } `mapstructure:"chatops"` R10k struct { - CommandPath string `mapstructure:"command_path"` - ConfigPath string `mapstructure:"config_path"` - DefaultBranch string `mapstructure:"default_branch"` - Prefix string `mapstructure:"prefix"` - AllowUppercase bool `mapstructure:"allow_uppercase"` - Verbose bool `mapstructure:"verbose"` - DeployModules bool `mapstructure:"deploy_modules"` - GenerateTypes bool `mapstructure:"generate_types"` + CommandPath string `mapstructure:"command_path"` + ConfigPath string `mapstructure:"config_path"` + DefaultBranch string `mapstructure:"default_branch"` + Prefix string `mapstructure:"prefix"` + AllowUppercase bool `mapstructure:"allow_uppercase"` + Verbose bool `mapstructure:"verbose"` + DeployModules bool `mapstructure:"deploy_modules"` + UseLegacyPuppetfileFlag bool `mapstructure:"use_legacy_puppetfile_flag"` + GenerateTypes bool `mapstructure:"generate_types"` } `mapstructure:"r10k"` } @@ -88,6 +89,7 @@ func setDefaults(v *viper.Viper) *viper.Viper { v.SetDefault("r10k.verbose", true) v.SetDefault("r10k.deploy_modules", true) v.SetDefault("r10k.generate_types", true) + v.SetDefault("r10k.use_legacy_puppetfile_flag", false) return v } From 661e497bedeebb31225bd88180527b50cc2500d0 Mon Sep 17 00:00:00 2001 From: Gene Liverman Date: Wed, 1 May 2024 11:11:36 -0400 Subject: [PATCH 2/3] Use provided r10k command_path Prior to this, the Config struct had a setting under the R10k struct called CommandPath that could be set in the config file, but was ignored as both places that would use it were hard coded to instead use the string `r10k`. This resulted in the application looking in the path for the r10k binary. A default is set in config.go also, but that seems to have also been ignored. This fixes #152 --- README.md | 6 ++++++ api/environment.go | 2 +- api/module.go | 2 +- lib/helpers/r10k-command.go | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 lib/helpers/r10k-command.go diff --git a/README.md b/README.md index 7e5e87b..78b4841 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,12 @@ Type: bool Description: Run `puppet generate types` after updating an environment Default: `true` +### `command_path` + +Type: `string` +Description: Allow overriding the default path to r10k. +Default: `/opt/puppetlabs/puppetserver/bin/r10k` + ## Usage Webhook API provides following paths diff --git a/api/environment.go b/api/environment.go index 79364a2..e40dba7 100644 --- a/api/environment.go +++ b/api/environment.go @@ -24,7 +24,7 @@ func (e EnvironmentController) DeployEnvironment(c *gin.Context) { var branch string // Set the base r10k command into a slice of strings - cmd := []string{"r10k", "deploy", "environment"} + cmd := []string{h.GetR10kCommand(), "deploy", "environment"} // Get the configuration conf := config.GetConfig() diff --git a/api/module.go b/api/module.go index 34228a0..436690d 100644 --- a/api/module.go +++ b/api/module.go @@ -24,7 +24,7 @@ func (m ModuleController) DeployModule(c *gin.Context) { var h helpers.Helper // Set the base r10k command into a string slice - cmd := []string{"r10k", "deploy", "module"} + cmd := []string{h.GetR10kCommand(), "deploy", "module"} // Get the configuration conf := config.GetConfig() diff --git a/lib/helpers/r10k-command.go b/lib/helpers/r10k-command.go new file mode 100644 index 0000000..5a62943 --- /dev/null +++ b/lib/helpers/r10k-command.go @@ -0,0 +1,14 @@ +package helpers + +import "github.com/voxpupuli/webhook-go/config" + +const Command = "r10k" + +func (h *Helper) GetR10kCommand() string { + conf := config.GetConfig().R10k + commandPath := conf.CommandPath + if commandPath == "" { + return Command + } + return commandPath +} From c1bfe0953c1445d27119148d6a29c5aa6bbde585 Mon Sep 17 00:00:00 2001 From: Tino Mueller Date: Sun, 14 Apr 2024 11:42:46 +0200 Subject: [PATCH 3/3] make module_name option optional --- api/module.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/api/module.go b/api/module.go index 436690d..26b9c70 100644 --- a/api/module.go +++ b/api/module.go @@ -58,13 +58,13 @@ func (m ModuleController) DeployModule(c *gin.Context) { module := data.ModuleName overrideModule := c.Query("module_name") // Restrictions to Puppet module names are: 1) begin with lowercase letter, 2) contain lowercase, digits or underscores - match, _ := regexp.MatchString("^[a-z][a-z0-9_]*$", overrideModule) - if !match { - c.JSON(http.StatusInternalServerError, gin.H{"message": "Invalid module name"}) - c.Abort() - return - } if overrideModule != "" { + match, _ := regexp.MatchString("^[a-z][a-z0-9_]*$", overrideModule) + if !match { + c.JSON(http.StatusInternalServerError, gin.H{"message": "Invalid module name"}) + c.Abort() + return + } module = overrideModule }