Skip to content

Commit

Permalink
Remove functions for detecting plugin usage
Browse files Browse the repository at this point in the history
With the removal of vendored_plugins we are no longer interested in tacking bundled plugins usage.
For plugins such as file, null, or anything bultin into Packer we don't track because there is no
way to install them outside of Packer, for now.
  • Loading branch information
nywilken committed Oct 24, 2023
1 parent c65bb2f commit e8d5436
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 233 deletions.
2 changes: 0 additions & 2 deletions command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
}

diags = packerStarter.Initialize(packer.InitializeOptions{})
bundledDiags := c.DetectBundledPlugins(packerStarter)
diags = append(bundledDiags, diags...)
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
Expand Down
117 changes: 0 additions & 117 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template"
Expand Down Expand Up @@ -169,84 +168,6 @@ func (m *Meta) GetConfigFromJSON(cla *MetaArgs) (packer.Handler, int) {
return core, ret
}

func (m *Meta) DetectBundledPlugins(handler packer.Handler) hcl.Diagnostics {
var plugins []string

switch h := handler.(type) {
case *packer.Core:
plugins = m.detectBundledPluginsJSON(h)
case *hcl2template.PackerConfig:
plugins = m.detectBundledPluginsHCL2(handler.(*hcl2template.PackerConfig))
}

if len(plugins) == 0 {
return nil
}

packer.CheckpointReporter.SetBundledUsage()

buf := &strings.Builder{}
buf.WriteString("This template relies on the use of plugins bundled into the Packer binary.\n")
buf.WriteString("The practice of bundling external plugins into Packer will be removed in an upcoming version.\n\n")
switch h := handler.(type) {
case *packer.Core:
buf.WriteString("To remove this warning and ensure builds keep working you can install these external plugins with the 'packer plugins install' command\n\n")

for _, plugin := range plugins {
fmt.Fprintf(buf, "* packer plugins install %s\n", plugin)
}

buf.WriteString("\nAlternatively, if you upgrade your templates to HCL2, you can use 'packer init' with a 'required_plugins' block to automatically install external plugins.\n\n")
fmt.Fprintf(buf, "You can try HCL2 by running 'packer hcl2_upgrade %s'", h.Template.Path)
case *hcl2template.PackerConfig:
buf.WriteString("To remove this warning, add the following section to your template:\n")
buf.WriteString(m.fixRequiredPlugins(h))
buf.WriteString("\nThen run 'packer init' to manage installation of the plugins")
}

return hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Bundled plugins used",
Detail: buf.String(),
},
}
}

func (m *Meta) detectBundledPluginsJSON(core *packer.Core) []string {
bundledPlugins := map[string]struct{}{}

tmpl := core.Template
if tmpl == nil {
panic("No template parsed. This is a Packer bug which should be reported, please open an issue on the project's issue tracker.")
}

for _, b := range tmpl.Builders {
builderType := fmt.Sprintf("packer-builder-%s", b.Type)
if bundledStatus[builderType] {
bundledPlugins[builderType] = struct{}{}
}
}

for _, p := range tmpl.Provisioners {
provisionerType := fmt.Sprintf("packer-provisioner-%s", p.Type)
if bundledStatus[provisionerType] {
bundledPlugins[provisionerType] = struct{}{}
}
}

for _, pps := range tmpl.PostProcessors {
for _, pp := range pps {
postProcessorType := fmt.Sprintf("packer-post-processor-%s", pp.Type)
if bundledStatus[postProcessorType] {
bundledPlugins[postProcessorType] = struct{}{}
}
}
}

return compileBundledPluginList(bundledPlugins)
}

var knownPluginPrefixes = map[string]string{
"amazon": "github.com/hashicorp/amazon",
"ansible": "github.com/hashicorp/ansible",
Expand Down Expand Up @@ -306,44 +227,6 @@ func (m *Meta) fixRequiredPlugins(config *hcl2template.PackerConfig) string {
return generateRequiredPluginsBlock(retPlugins)
}

func (m *Meta) detectBundledPluginsHCL2(config *hcl2template.PackerConfig) []string {
bundledPlugins := map[string]struct{}{}

for _, b := range config.Builds {
for _, src := range b.Sources {
builderType := fmt.Sprintf("packer-builder-%s", src.Type)
if bundledStatus[builderType] {
bundledPlugins[builderType] = struct{}{}
}
}

for _, p := range b.ProvisionerBlocks {
provisionerType := fmt.Sprintf("packer-provisioner-%s", p.PType)
if bundledStatus[provisionerType] {
bundledPlugins[provisionerType] = struct{}{}
}
}

for _, pps := range b.PostProcessorsLists {
for _, pp := range pps {
postProcessorType := fmt.Sprintf("packer-post-processor-%s", pp.PType)
if bundledStatus[postProcessorType] {
bundledPlugins[postProcessorType] = struct{}{}
}
}
}
}

for _, ds := range config.Datasources {
dsType := fmt.Sprintf("packer-datasource-%s", ds.Type)
if bundledStatus[dsType] {
bundledPlugins[dsType] = struct{}{}
}
}

return compileBundledPluginList(bundledPlugins)
}

func generateRequiredPluginsBlock(plugins []string) string {
if len(plugins) == 0 {
return ""
Expand Down
2 changes: 0 additions & 2 deletions command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int
diags = packerStarter.Initialize(packer.InitializeOptions{
SkipDatasourcesExecution: !cla.EvaluateDatasources,
})
bundledDiags := c.DetectBundledPlugins(packerStarter)
diags = append(bundledDiags, diags...)
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
Expand Down
108 changes: 0 additions & 108 deletions command/vendored_plugins.go

This file was deleted.

4 changes: 0 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ func (c *config) discoverInternalComponents() error {
for builder := range command.Builders {
builder := builder
if !c.Plugins.Builders.Has(builder) {
command.TrackBundledPlugin(fmt.Sprintf("packer-builder-%s", builder))
bin := fmt.Sprintf("%s%splugin%spacker-builder-%s",
packerPath, PACKERSPACE, PACKERSPACE, builder)
c.Plugins.Builders.Set(builder, func() (packersdk.Builder, error) {
Expand All @@ -165,7 +164,6 @@ func (c *config) discoverInternalComponents() error {
for provisioner := range command.Provisioners {
provisioner := provisioner
if !c.Plugins.Provisioners.Has(provisioner) {
command.TrackBundledPlugin(fmt.Sprintf("packer-provisioner-%s", provisioner))
bin := fmt.Sprintf("%s%splugin%spacker-provisioner-%s",
packerPath, PACKERSPACE, PACKERSPACE, provisioner)
c.Plugins.Provisioners.Set(provisioner, func() (packersdk.Provisioner, error) {
Expand All @@ -177,7 +175,6 @@ func (c *config) discoverInternalComponents() error {
for postProcessor := range command.PostProcessors {
postProcessor := postProcessor
if !c.Plugins.PostProcessors.Has(postProcessor) {
command.TrackBundledPlugin(fmt.Sprintf("packer-post-processor-%s", postProcessor))
bin := fmt.Sprintf("%s%splugin%spacker-post-processor-%s",
packerPath, PACKERSPACE, PACKERSPACE, postProcessor)
c.Plugins.PostProcessors.Set(postProcessor, func() (packersdk.PostProcessor, error) {
Expand All @@ -189,7 +186,6 @@ func (c *config) discoverInternalComponents() error {
for dataSource := range command.Datasources {
dataSource := dataSource
if !c.Plugins.DataSources.Has(dataSource) {
command.TrackBundledPlugin(fmt.Sprintf("packer-datasource-%s", dataSource))
bin := fmt.Sprintf("%s%splugin%spacker-datasource-%s",
packerPath, PACKERSPACE, PACKERSPACE, dataSource)
c.Plugins.DataSources.Set(dataSource, func() (packersdk.Datasource, error) {
Expand Down

0 comments on commit e8d5436

Please sign in to comment.