From e8d5436228567492bc05a2b00705da79d661ee6d Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Thu, 19 Oct 2023 10:58:39 -0400 Subject: [PATCH] Remove functions for detecting plugin usage 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. --- command/build.go | 2 - command/meta.go | 117 ------------------------------------ command/validate.go | 2 - command/vendored_plugins.go | 108 --------------------------------- config.go | 4 -- 5 files changed, 233 deletions(-) delete mode 100644 command/vendored_plugins.go diff --git a/command/build.go b/command/build.go index 94361807407..c9f7a537d1c 100644 --- a/command/build.go +++ b/command/build.go @@ -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 diff --git a/command/meta.go b/command/meta.go index 455478e2188..0e464ccc36d 100644 --- a/command/meta.go +++ b/command/meta.go @@ -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" @@ -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", @@ -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 "" diff --git a/command/validate.go b/command/validate.go index 5e40a5f9530..9747dabf95c 100644 --- a/command/validate.go +++ b/command/validate.go @@ -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 diff --git a/command/vendored_plugins.go b/command/vendored_plugins.go deleted file mode 100644 index 5853e34cac4..00000000000 --- a/command/vendored_plugins.go +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: BUSL-1.1 - -package command - -import ( - "log" - - packersdk "github.com/hashicorp/packer-plugin-sdk/packer" - // Previously core-bundled components, split into their own plugins but - // still vendored with Packer for now. Importing as library instead of - // forcing use of packer init. -) - -// VendoredDatasources are datasource components that were once bundled with the -// Packer core, but are now being imported from their counterpart plugin repos -var VendoredDatasources = map[string]packersdk.Datasource{} - -// VendoredBuilders are builder components that were once bundled with the -// Packer core, but are now being imported from their counterpart plugin repos -var VendoredBuilders = map[string]packersdk.Builder{} - -// VendoredProvisioners are provisioner components that were once bundled with the -// Packer core, but are now being imported from their counterpart plugin repos -var VendoredProvisioners = map[string]packersdk.Provisioner{} - -// VendoredPostProcessors are post-processor components that were once bundled with the -// Packer core, but are now being imported from their counterpart plugin repos -var VendoredPostProcessors = map[string]packersdk.PostProcessor{} - -// bundledStatus is used to know if one of the bundled components is loaded from -// an external plugin, or from the bundled plugins. -// -// We keep track of this to produce a warning if a user relies on one -// such plugin, as they will be removed in a later version of Packer. -var bundledStatus = map[string]bool{} - -// TrackBundledPlugin marks a component as loaded from Packer's bundled plugins -// instead of from an externally loaded plugin. -// -// NOTE: `pluginName' must be in the format `packer--' -func TrackBundledPlugin(pluginName string) { - _, exists := bundledStatus[pluginName] - if !exists { - return - } - - bundledStatus[pluginName] = true -} - -var componentPluginMap = map[string]string{} - -// compileBundledPluginList returns a list of plugins to import in a config -// -// This only works on bundled plugins and serves as a way to inform users that -// they should not rely on a bundled plugin anymore, but give them recommendations -// on how to manage those plugins instead. -func compileBundledPluginList(componentMap map[string]struct{}) []string { - plugins := map[string]struct{}{} - for component := range componentMap { - plugin, ok := componentPluginMap[component] - if !ok { - log.Printf("Unknown bundled plugin component: %q", component) - continue - } - - plugins[plugin] = struct{}{} - } - - pluginList := make([]string, 0, len(plugins)) - for plugin := range plugins { - pluginList = append(pluginList, plugin) - } - - return pluginList -} - -// Upon init lets load up any plugins that were vendored manually into the default -// set of plugins. -func init() { - for k, v := range VendoredDatasources { - if _, ok := Datasources[k]; ok { - continue - } - Datasources[k] = v - } - - for k, v := range VendoredBuilders { - if _, ok := Builders[k]; ok { - continue - } - Builders[k] = v - } - - for k, v := range VendoredProvisioners { - if _, ok := Provisioners[k]; ok { - continue - } - Provisioners[k] = v - } - - for k, v := range VendoredPostProcessors { - if _, ok := PostProcessors[k]; ok { - continue - } - PostProcessors[k] = v - } -} diff --git a/config.go b/config.go index 6688d6a74a5..d4dab230e86 100644 --- a/config.go +++ b/config.go @@ -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) { @@ -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) { @@ -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) { @@ -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) {