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.
This commit is contained in:
Lucas Bajolet
2024-02-02 10:41:22 -05:00
parent 14a2608aed
commit a08bb230d4
10 changed files with 29 additions and 18 deletions
+1 -1
View File
@@ -91,7 +91,7 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
return ret
}
diags := packerStarter.DetectPluginBinaries()
diags := packerStarter.DetectPluginBinaries(false)
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
+1 -1
View File
@@ -263,7 +263,7 @@ func TestInitCommand_Run(t *testing.T) {
Meta: tt.Meta,
}
if err := c.CoreConfig.Components.PluginConfig.Discover(); err != nil {
if err := c.CoreConfig.Components.PluginConfig.Discover(false); err != nil {
t.Fatalf("Failed to discover plugins: %s", err)
}
+1 -1
View File
@@ -147,7 +147,7 @@ func TestPluginsInstallCommand_Run(t *testing.T) {
Meta: tt.Meta,
}
if err := c.CoreConfig.Components.PluginConfig.Discover(); err != nil {
if err := c.CoreConfig.Components.PluginConfig.Discover(false); err != nil {
t.Fatalf("Failed to discover plugins: %s", err)
}
+1 -1
View File
@@ -65,7 +65,7 @@ func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int
return 0
}
diags := packerStarter.DetectPluginBinaries()
diags := packerStarter.DetectPluginBinaries(false)
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
+3 -2
View File
@@ -54,9 +54,9 @@ func (cfg *PackerConfig) PluginRequirements() (plugingetter.Requirements, hcl.Di
return reqs, diags
}
func (cfg *PackerConfig) DetectPluginBinaries() hcl.Diagnostics {
func (cfg *PackerConfig) DetectPluginBinaries(releaseOnly bool) hcl.Diagnostics {
// Do first pass to discover all the installed plugins
err := cfg.parser.PluginConfig.Discover()
err := cfg.parser.PluginConfig.Discover(releaseOnly)
if err != nil {
return (hcl.Diagnostics{}).Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
@@ -76,6 +76,7 @@ func (cfg *PackerConfig) DetectPluginBinaries() hcl.Diagnostics {
Checksummers: []plugingetter.Checksummer{
{Type: "sha256", Hash: sha256.New()},
},
ReleasesOnly: releaseOnly,
},
}
+2 -2
View File
@@ -135,10 +135,10 @@ func NewCore(c *CoreConfig) *Core {
// DetectPluginBinaries is used to load required plugins from the template,
// since it is unsupported in JSON, this is essentially a no-op.
func (c *Core) DetectPluginBinaries() hcl.Diagnostics {
func (c *Core) DetectPluginBinaries(releaseOnly bool) hcl.Diagnostics {
var diags hcl.Diagnostics
err := c.components.PluginConfig.Discover()
err := c.components.PluginConfig.Discover(releaseOnly)
if err != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
+10 -1
View File
@@ -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)
+2 -1
View File
@@ -49,7 +49,7 @@ var extractPluginBasename = regexp.MustCompile("^packer-plugin-([^_]+)")
// found plugins, in that order.
// Hence, the priority order is the reverse of the search order - i.e., the
// CWD has the highest priority.
func (c *PluginConfig) Discover() error {
func (c *PluginConfig) Discover(releasesOnly bool) error {
if c.Builders == nil {
c.Builders = MapOfBuilder{}
}
@@ -81,6 +81,7 @@ func (c *PluginConfig) Discover() error {
Checksummers: []plugingetter.Checksummer{
{Type: "sha256", Hash: sha256.New()},
},
ReleasesOnly: releasesOnly,
},
})
if err != nil {
+7 -7
View File
@@ -33,7 +33,7 @@ func TestDiscoverReturnsIfMagicCookieSet(t *testing.T) {
t.Setenv(pluginsdk.MagicCookieKey, pluginsdk.MagicCookieValue)
err := config.Discover()
err := config.Discover(false)
if err != nil {
t.Fatalf("Should not have errored: %s", err)
}
@@ -48,7 +48,7 @@ func TestMultiPlugin_describe(t *testing.T) {
pluginDir := os.Getenv("PACKER_PLUGIN_PATH")
defer os.RemoveAll(pluginDir)
c := PluginConfig{}
err := c.Discover()
err := c.Discover(false)
if err != nil {
t.Fatalf("error discovering plugins; %s", err.Error())
}
@@ -88,7 +88,7 @@ func TestMultiPlugin_describe_installed(t *testing.T) {
defer os.RemoveAll(pluginDir)
c := PluginConfig{}
err := c.Discover()
err := c.Discover(false)
if err != nil {
t.Fatalf("error discovering plugins; %s", err.Error())
}
@@ -151,7 +151,7 @@ func TestMultiPlugin_describe_installed_for_invalid(t *testing.T) {
defer os.RemoveAll(pluginDir)
c := PluginConfig{}
err := c.Discover()
err := c.Discover(false)
if err != nil {
t.Fatalf("error discovering plugins; %s", err.Error())
}
@@ -195,7 +195,7 @@ func TestMultiPlugin_defaultName(t *testing.T) {
defer os.RemoveAll(pluginDir)
c := PluginConfig{}
err := c.Discover()
err := c.Discover(false)
if err != nil {
t.Fatalf("error discovering plugins; %s ; mocks are %#v", err.Error(), defaultNameMock)
}
@@ -226,7 +226,7 @@ func TestMultiPlugin_IgnoreChecksumFile(t *testing.T) {
}
c := PluginConfig{}
err = c.Discover()
err = c.Discover(false)
if err != nil {
t.Fatalf("error discovering plugins; %s ; mocks are %#v", err.Error(), defaultNameMock)
}
@@ -244,7 +244,7 @@ func TestMultiPlugin_defaultName_each_plugin_type(t *testing.T) {
defer os.RemoveAll(pluginDir)
c := PluginConfig{}
err := c.Discover()
err := c.Discover(false)
if err != nil {
t.Fatal("Should not have error because pluginsdk.DEFAULT_NAME is used twice but only once per plugin type.")
}
+1 -1
View File
@@ -43,7 +43,7 @@ type InitializeOptions struct {
type PluginBinaryDetector interface {
// DetectPluginBinaries is used only for HCL2 templates, and loads required
// plugins if specified.
DetectPluginBinaries() hcl.Diagnostics
DetectPluginBinaries(releaseOnly bool) hcl.Diagnostics
}
// The Handler handles all Packer things. This interface reflects the Packer