From 9f7e6ca52b14152bff6169f6b370a6454b79f436 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 2 Feb 2024 10:34:52 -0500 Subject: [PATCH] plugins: add a release only flag to Discover Since we now support loading pre-releases, we also want Packer to be able to ignore them by user demand, so we put in place the infrastructure to modulate this. --- hcl2template/plugin.go | 1 + packer/plugin-getter/plugins.go | 11 ++++++++++- packer/plugin.go | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/hcl2template/plugin.go b/hcl2template/plugin.go index 6a66aa610..f03e03292 100644 --- a/hcl2template/plugin.go +++ b/hcl2template/plugin.go @@ -76,6 +76,7 @@ func (cfg *PackerConfig) DetectPluginBinaries() hcl.Diagnostics { Checksummers: []plugingetter.Checksummer{ {Type: "sha256", Hash: sha256.New()}, }, + ReleasesOnly: cfg.parser.PluginConfig.ReleasesOnly, }, } diff --git a/packer/plugin-getter/plugins.go b/packer/plugin-getter/plugins.go index fae1809e0..3271a868d 100644 --- a/packer/plugin-getter/plugins.go +++ b/packer/plugin-getter/plugins.go @@ -60,6 +60,10 @@ type BinaryInstallationOptions struct { Ext string Checksummers []Checksummer + + // ReleasesOnly may be set by commands like validate or build, and + // forces Packer to not consider plugin pre-releases. + ReleasesOnly bool } type ListInstallationsOptions struct { @@ -154,13 +158,18 @@ func (pr Requirement) ListInstallations(opts ListInstallationsOptions) (InstallL // versionsStr now looks like v1.2.3_x5.1 or amazon_v1.2.3_x5.1 parts := strings.SplitN(versionsStr, "_", 2) pluginVersionStr, protocolVerionStr := parts[0], parts[1] - _, err = version.NewVersion(pluginVersionStr) + ver, err := version.NewVersion(pluginVersionStr) if err != nil { // could not be parsed, ignoring the file log.Printf("found %q with an incorrect %q version, ignoring it. %v", path, pluginVersionStr, err) continue } + if ver.Prerelease() != "" && opts.ReleasesOnly { + log.Printf("ignoring pre-release plugin %q", path) + continue + } + matches := pluginVersionRegex.FindStringSubmatch(pluginVersionStr) if matches == nil { log.Printf("invalid version found: %q, ignoring", pluginVersionStr) diff --git a/packer/plugin.go b/packer/plugin.go index c6d4c1259..d2bf0e200 100644 --- a/packer/plugin.go +++ b/packer/plugin.go @@ -34,6 +34,7 @@ type PluginConfig struct { Provisioners ProvisionerSet PostProcessors PostProcessorSet DataSources DatasourceSet + ReleasesOnly bool } // PACKERSPACE is used to represent the spaces that separate args for a command @@ -81,6 +82,7 @@ func (c *PluginConfig) Discover() error { Checksummers: []plugingetter.Checksummer{ {Type: "sha256", Hash: sha256.New()}, }, + ReleasesOnly: c.ReleasesOnly, }, }) if err != nil {