From 5576bb5caa0c4ca68b04d6870e78e1e43e4e1ca9 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 2 Dec 2020 12:12:47 -0800 Subject: [PATCH] move packer config constants next to the packer config --- .../ebs/acceptance/builder_acceptance.go | 5 +- builder/hyperv/iso/builder_test.go | 6 +- builder/hyperv/vmcx/builder_test.go | 6 +- builder/parallels/iso/builder_test.go | 4 +- builder/qemu/config_test.go | 4 +- .../iso/acceptance/builder_acceptance.go | 5 +- builder/virtualbox/iso/builder_test.go | 4 +- builder/vmware/iso/builder_test.go | 4 +- builder/vmware/iso/step_create_vmx_test.go | 6 +- command/build_parallel_test.go | 4 +- command/build_test.go | 6 +- command/build_timeout_test.go | 4 +- command/exec_test.go | 6 +- config.go | 17 ++- config_test.go | 116 ++++++++++++++++-- hcl2template/common_test.go | 6 +- helper/tests/acc/provisioners.go | 6 +- helper/tests/core.go | 6 +- packer-plugin-sdk/common/packer_config.go | 37 ++++++ {packer => packer-plugin-sdk/packer}/maps.go | 14 +-- packer/build.go | 54 ++------ packer/build_test.go | 21 ++-- packer/core_test.go | 2 +- packer/testing.go | 8 +- provisioner/chef-solo/provisioner_test.go | 4 +- .../powershell/provisioner_acc_test.go | 5 +- provisioner/powershell/provisioner_test.go | 4 +- .../shell-local/provisioner_acc_test.go | 5 +- provisioner/shell/provisioner_acc_test.go | 5 +- 29 files changed, 234 insertions(+), 140 deletions(-) rename {packer => packer-plugin-sdk/packer}/maps.go (67%) diff --git a/builder/amazon/ebs/acceptance/builder_acceptance.go b/builder/amazon/ebs/acceptance/builder_acceptance.go index 5956e5c9a..67097fbc3 100644 --- a/builder/amazon/ebs/acceptance/builder_acceptance.go +++ b/builder/amazon/ebs/acceptance/builder_acceptance.go @@ -11,7 +11,6 @@ import ( amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" testshelper "github.com/hashicorp/packer/helper/tests" @@ -55,8 +54,8 @@ func (s *AmazonEBSAccTest) CleanUp() error { return helper.CleanUpAmi() } -func (s *AmazonEBSAccTest) GetBuilderStore() packer.MapOfBuilder { - return packer.MapOfBuilder{ +func (s *AmazonEBSAccTest) GetBuilderStore() packersdk.MapOfBuilder { + return packersdk.MapOfBuilder{ "amazon-ebs": func() (packersdk.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, } } diff --git a/builder/hyperv/iso/builder_test.go b/builder/hyperv/iso/builder_test.go index 1cdf5cd34..b1a18f3a8 100644 --- a/builder/hyperv/iso/builder_test.go +++ b/builder/hyperv/iso/builder_test.go @@ -10,7 +10,7 @@ import ( "testing" hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer-plugin-sdk/common" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -26,7 +26,7 @@ func testConfig() map[string]interface{} { "disk_block_size": 1, "guest_additions_mode": "none", "disk_additional_size": "50000,40000,30000", - packer.BuildNameConfigKey: "foo", + common.BuildNameConfigKey: "foo", } } @@ -591,7 +591,7 @@ func TestUserVariablesInBootCommand(t *testing.T) { var b Builder config := testConfig() - config[packer.UserVariablesConfigKey] = map[string]string{"test-variable": "test"} + config[common.UserVariablesConfigKey] = map[string]string{"test-variable": "test"} config["boot_command"] = []string{"blah {{user `test-variable`}} blah"} _, warns, err := b.Prepare(config) diff --git a/builder/hyperv/vmcx/builder_test.go b/builder/hyperv/vmcx/builder_test.go index edbf9a988..4daec3d7d 100644 --- a/builder/hyperv/vmcx/builder_test.go +++ b/builder/hyperv/vmcx/builder_test.go @@ -10,7 +10,7 @@ import ( "os" hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer-plugin-sdk/common" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -25,7 +25,7 @@ func testConfig() map[string]interface{} { "memory": 64, "guest_additions_mode": "none", "clone_from_vmcx_path": "generated", - packer.BuildNameConfigKey: "foo", + common.BuildNameConfigKey: "foo", } } @@ -483,7 +483,7 @@ func TestUserVariablesInBootCommand(t *testing.T) { defer os.RemoveAll(td) config["clone_from_vmcx_path"] = td - config[packer.UserVariablesConfigKey] = map[string]string{"test-variable": "test"} + config[common.UserVariablesConfigKey] = map[string]string{"test-variable": "test"} config["boot_command"] = []string{"blah {{user `test-variable`}} blah"} _, warns, err := b.Prepare(config) diff --git a/builder/parallels/iso/builder_test.go b/builder/parallels/iso/builder_test.go index b56e57e7d..b29b7db6c 100644 --- a/builder/parallels/iso/builder_test.go +++ b/builder/parallels/iso/builder_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -17,7 +17,7 @@ func testConfig() map[string]interface{} { "ssh_username": "foo", "parallels_tools_flavor": "lin", - packer.BuildNameConfigKey: "foo", + common.BuildNameConfigKey: "foo", } } diff --git a/builder/qemu/config_test.go b/builder/qemu/config_test.go index e74c7e336..9b10e9cdf 100644 --- a/builder/qemu/config_test.go +++ b/builder/qemu/config_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/stretchr/testify/assert" ) @@ -49,7 +49,7 @@ func testConfig() map[string]interface{} { "iso_checksum": "md5:0B0F137F17AC10944716020B018F8126", "iso_url": "http://www.google.com/", "ssh_username": "foo", - packer.BuildNameConfigKey: "foo", + common.BuildNameConfigKey: "foo", } } diff --git a/builder/virtualbox/iso/acceptance/builder_acceptance.go b/builder/virtualbox/iso/acceptance/builder_acceptance.go index 08bc889db..e394ca6c4 100644 --- a/builder/virtualbox/iso/acceptance/builder_acceptance.go +++ b/builder/virtualbox/iso/acceptance/builder_acceptance.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/packer/builder/virtualbox/iso" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" testshelper "github.com/hashicorp/packer/helper/tests" @@ -40,8 +39,8 @@ func (v *VirtualBoxISOAccTest) CleanUp() error { return nil } -func (v *VirtualBoxISOAccTest) GetBuilderStore() packer.MapOfBuilder { - return packer.MapOfBuilder{ +func (v *VirtualBoxISOAccTest) GetBuilderStore() packersdk.MapOfBuilder { + return packersdk.MapOfBuilder{ "virtualbox-iso": func() (packersdk.Builder, error) { return &iso.Builder{}, nil }, } } diff --git a/builder/virtualbox/iso/builder_test.go b/builder/virtualbox/iso/builder_test.go index 982cf784b..fceadf05e 100644 --- a/builder/virtualbox/iso/builder_test.go +++ b/builder/virtualbox/iso/builder_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/hashicorp/packer/builder/virtualbox/common" - "github.com/hashicorp/packer/packer" + packercommon "github.com/hashicorp/packer/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -17,7 +17,7 @@ func testConfig() map[string]interface{} { "shutdown_command": "yes", "ssh_username": "foo", - packer.BuildNameConfigKey: "foo", + packercommon.BuildNameConfigKey: "foo", } } diff --git a/builder/vmware/iso/builder_test.go b/builder/vmware/iso/builder_test.go index 05e1e223d..1fb06f673 100644 --- a/builder/vmware/iso/builder_test.go +++ b/builder/vmware/iso/builder_test.go @@ -7,7 +7,7 @@ import ( "reflect" "testing" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -18,7 +18,7 @@ func testConfig() map[string]interface{} { "shutdown_command": "foo", "ssh_username": "foo", - packer.BuildNameConfigKey: "foo", + common.BuildNameConfigKey: "foo", } } diff --git a/builder/vmware/iso/step_create_vmx_test.go b/builder/vmware/iso/step_create_vmx_test.go index 19c441de5..a741eddde 100644 --- a/builder/vmware/iso/step_create_vmx_test.go +++ b/builder/vmware/iso/step_create_vmx_test.go @@ -138,16 +138,16 @@ func setupVMwareBuild(t *testing.T, builderConfig map[string]string, provisioner // create our config to test the vmware-iso builder components := packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ + BuilderStore: packersdk.MapOfBuilder{ "vmware-iso": func() (packersdk.Builder, error) { return &Builder{}, nil }, }, Hook: func(n string) (packersdk.Hook, error) { return &packersdk.DispatchHook{}, nil }, - ProvisionerStore: packer.MapOfProvisioner{ + ProvisionerStore: packersdk.MapOfProvisioner{ "shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil }, }, - PostProcessorStore: packer.MapOfPostProcessor{ + PostProcessorStore: packersdk.MapOfPostProcessor{ "something": func() (packersdk.PostProcessor, error) { return &packer.MockPostProcessor{}, nil }, }, } diff --git a/command/build_parallel_test.go b/command/build_parallel_test.go index 00b43258f..f07b2acd7 100644 --- a/command/build_parallel_test.go +++ b/command/build_parallel_test.go @@ -68,12 +68,12 @@ func testMetaParallel(t *testing.T, builder *ParallelTestBuilder, locked *Locked return Meta{ CoreConfig: &packer.CoreConfig{ Components: packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ + BuilderStore: packersdk.MapOfBuilder{ "parallel-test": func() (packersdk.Builder, error) { return builder, nil }, "file": func() (packersdk.Builder, error) { return &file.Builder{}, nil }, "lock": func() (packersdk.Builder, error) { return locked, nil }, }, - ProvisionerStore: packer.MapOfProvisioner{ + ProvisionerStore: packersdk.MapOfProvisioner{ "sleep": func() (packersdk.Provisioner, error) { return &sleep.Provisioner{}, nil }, }, }, diff --git a/command/build_test.go b/command/build_test.go index b5d9694c9..fef620203 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -836,16 +836,16 @@ func fileExists(filename string) bool { // available. This allows us to test a builder that writes files to disk. func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { components := packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ + BuilderStore: packersdk.MapOfBuilder{ "file": func() (packersdk.Builder, error) { return &file.Builder{}, nil }, "null": func() (packersdk.Builder, error) { return &null.Builder{}, nil }, }, - ProvisionerStore: packer.MapOfProvisioner{ + ProvisionerStore: packersdk.MapOfProvisioner{ "shell-local": func() (packersdk.Provisioner, error) { return &shell_local.Provisioner{}, nil }, "shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil }, "file": func() (packersdk.Provisioner, error) { return &filep.Provisioner{}, nil }, }, - PostProcessorStore: packer.MapOfPostProcessor{ + PostProcessorStore: packersdk.MapOfPostProcessor{ "shell-local": func() (packersdk.PostProcessor, error) { return &shell_local_pp.PostProcessor{}, nil }, "manifest": func() (packersdk.PostProcessor, error) { return &manifest.PostProcessor{}, nil }, }, diff --git a/command/build_timeout_test.go b/command/build_timeout_test.go index 59b4c2b3f..54cfa72c6 100644 --- a/command/build_timeout_test.go +++ b/command/build_timeout_test.go @@ -16,10 +16,10 @@ import ( // available. This allows us to test a builder that writes files to disk. func testCoreConfigSleepBuilder(t *testing.T) *packer.CoreConfig { components := packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ + BuilderStore: packersdk.MapOfBuilder{ "file": func() (packersdk.Builder, error) { return &file.Builder{}, nil }, }, - ProvisionerStore: packer.MapOfProvisioner{ + ProvisionerStore: packersdk.MapOfProvisioner{ "sleep": func() (packersdk.Provisioner, error) { return &sleep.Provisioner{}, nil }, "shell-local": func() (packersdk.Provisioner, error) { return &shell_local.Provisioner{}, nil }, }, diff --git a/command/exec_test.go b/command/exec_test.go index 8f3d2d345..400a3fccb 100644 --- a/command/exec_test.go +++ b/command/exec_test.go @@ -118,17 +118,17 @@ func commandMeta() Meta { func getBareComponentFinder() packer.ComponentFinder { return packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ + BuilderStore: packersdk.MapOfBuilder{ "file": func() (packersdk.Builder, error) { return &file.Builder{}, nil }, "null": func() (packersdk.Builder, error) { return &null.Builder{}, nil }, "amazon-ebs": func() (packersdk.Builder, error) { return &ebs.Builder{}, nil }, }, - ProvisionerStore: packer.MapOfProvisioner{ + ProvisionerStore: packersdk.MapOfProvisioner{ "shell-local": func() (packersdk.Provisioner, error) { return &shell_local.Provisioner{}, nil }, "shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil }, "file": func() (packersdk.Provisioner, error) { return &filep.Provisioner{}, nil }, }, - PostProcessorStore: packer.MapOfPostProcessor{ + PostProcessorStore: packersdk.MapOfPostProcessor{ "shell-local": func() (packersdk.PostProcessor, error) { return &shell_local_pp.PostProcessor{}, nil }, "manifest": func() (packersdk.PostProcessor, error) { return &manifest.PostProcessor{}, nil }, }, diff --git a/config.go b/config.go index d3d345e97..5feab7d60 100644 --- a/config.go +++ b/config.go @@ -12,7 +12,6 @@ import ( "strings" "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer/plugin" ) @@ -22,14 +21,14 @@ import ( const PACKERSPACE = "-PACKERSPACE-" type config struct { - DisableCheckpoint bool `json:"disable_checkpoint"` - DisableCheckpointSignature bool `json:"disable_checkpoint_signature"` - RawBuilders map[string]string `json:"builders"` - RawProvisioners map[string]string `json:"provisioners"` - RawPostProcessors map[string]string `json:"post-processors"` - Builders packer.MapOfBuilder `json:"-"` - Provisioners packer.MapOfProvisioner `json:"-"` - PostProcessors packer.MapOfPostProcessor `json:"-"` + DisableCheckpoint bool `json:"disable_checkpoint"` + DisableCheckpointSignature bool `json:"disable_checkpoint_signature"` + RawBuilders map[string]string `json:"builders"` + RawProvisioners map[string]string `json:"provisioners"` + RawPostProcessors map[string]string `json:"post-processors"` + Builders packersdk.MapOfBuilder `json:"-"` + Provisioners packersdk.MapOfProvisioner `json:"-"` + PostProcessors packersdk.MapOfPostProcessor `json:"-"` Plugins plugin.Config } diff --git a/config_test.go b/config_test.go index cf28533cd..a1d3e2ca3 100644 --- a/config_test.go +++ b/config_test.go @@ -11,9 +11,109 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" + "github.com/hashicorp/packer/packer/plugin" ) +func newConfig() config { + var conf config + conf.PluginMinPort = 10000 + conf.PluginMaxPort = 25000 + conf.Builders = packersdk.MapOfBuilder{} + conf.PostProcessors = packersdk.MapOfPostProcessor{} + conf.Provisioners = packersdk.MapOfProvisioner{} + + return conf +} +func TestDiscoverReturnsIfMagicCookieSet(t *testing.T) { + config := newConfig() + + os.Setenv(plugin.MagicCookieKey, plugin.MagicCookieValue) + defer os.Unsetenv(plugin.MagicCookieKey) + + err := config.Discover() + if err != nil { + t.Fatalf("Should not have errored: %s", err) + } + + if len(config.Builders) != 0 { + t.Fatalf("Should not have tried to find builders") + } +} + +func TestEnvVarPackerPluginPath(t *testing.T) { + // Create a temporary directory to store plugins in + dir, _, cleanUpFunc, err := generateFakePlugins("custom_plugin_dir", + []string{"packer-provisioner-partyparrot"}) + if err != nil { + t.Fatalf("Error creating fake custom plugins: %s", err) + } + + defer cleanUpFunc() + + // Add temp dir to path. + os.Setenv("PACKER_PLUGIN_PATH", dir) + defer os.Unsetenv("PACKER_PLUGIN_PATH") + + config := newConfig() + + err = config.Discover() + if err != nil { + t.Fatalf("Should not have errored: %s", err) + } + + if len(config.Provisioners) == 0 { + t.Fatalf("Should have found partyparrot provisioner") + } + if _, ok := config.Provisioners["partyparrot"]; !ok { + t.Fatalf("Should have found partyparrot provisioner.") + } +} + +func TestEnvVarPackerPluginPath_MultiplePaths(t *testing.T) { + // Create a temporary directory to store plugins in + dir, _, cleanUpFunc, err := generateFakePlugins("custom_plugin_dir", + []string{"packer-provisioner-partyparrot"}) + if err != nil { + t.Fatalf("Error creating fake custom plugins: %s", err) + } + + defer cleanUpFunc() + + pathsep := ":" + if runtime.GOOS == "windows" { + pathsep = ";" + } + + // Create a second dir to look in that will be empty + decoyDir, err := ioutil.TempDir("", "decoy") + if err != nil { + t.Fatalf("Failed to create a temporary test dir.") + } + defer os.Remove(decoyDir) + + pluginPath := dir + pathsep + decoyDir + + // Add temp dir to path. + os.Setenv("PACKER_PLUGIN_PATH", pluginPath) + defer os.Unsetenv("PACKER_PLUGIN_PATH") + + config := newConfig() + + err = config.Discover() + if err != nil { + t.Fatalf("Should not have errored: %s", err) + } + + if len(config.Provisioners) == 0 { + t.Fatalf("Should have found partyparrot provisioner") + } + if _, ok := config.Provisioners["partyparrot"]; !ok { + t.Fatalf("Should have found partyparrot provisioner.") + } +} + +>>>>>>> move packer config constants next to the packer config func TestDecodeConfig(t *testing.T) { packerConfig := ` @@ -49,9 +149,9 @@ func TestLoadExternalComponentsFromConfig(t *testing.T) { defer cleanUpFunc() var cfg config - cfg.Builders = packer.MapOfBuilder{} - cfg.PostProcessors = packer.MapOfPostProcessor{} - cfg.Provisioners = packer.MapOfProvisioner{} + cfg.Builders = packersdk.MapOfBuilder{} + cfg.PostProcessors = packersdk.MapOfPostProcessor{} + cfg.Provisioners = packersdk.MapOfProvisioner{} if err := decodeConfig(strings.NewReader(packerConfigData), &cfg); err != nil { t.Fatalf("error encountered decoding configuration: %v", err) @@ -81,7 +181,7 @@ func TestLoadExternalComponentsFromConfig_onlyProvisioner(t *testing.T) { defer cleanUpFunc() var cfg config - cfg.Provisioners = packer.MapOfProvisioner{} + cfg.Provisioners = packersdk.MapOfProvisioner{} if err := decodeConfig(strings.NewReader(packerConfigData), &cfg); err != nil { t.Fatalf("error encountered decoding configuration: %v", err) @@ -126,9 +226,9 @@ func TestLoadSingleComponent(t *testing.T) { } var cfg config - cfg.Builders = packer.MapOfBuilder{} - cfg.PostProcessors = packer.MapOfPostProcessor{} - cfg.Provisioners = packer.MapOfProvisioner{} + cfg.Builders = packersdk.MapOfBuilder{} + cfg.PostProcessors = packersdk.MapOfPostProcessor{} + cfg.Provisioners = packersdk.MapOfProvisioner{} for _, tc := range tt { tc := tc diff --git a/hcl2template/common_test.go b/hcl2template/common_test.go index b0eb30aa4..260452278 100644 --- a/hcl2template/common_test.go +++ b/hcl2template/common_test.go @@ -20,16 +20,16 @@ import ( func getBasicParser() *Parser { return &Parser{ Parser: hclparse.NewParser(), - BuilderSchemas: packer.MapOfBuilder{ + BuilderSchemas: packersdk.MapOfBuilder{ "amazon-ebs": func() (packersdk.Builder, error) { return &MockBuilder{}, nil }, "virtualbox-iso": func() (packersdk.Builder, error) { return &MockBuilder{}, nil }, "null": func() (packersdk.Builder, error) { return &null.Builder{}, nil }, }, - ProvisionersSchemas: packer.MapOfProvisioner{ + ProvisionersSchemas: packersdk.MapOfProvisioner{ "shell": func() (packersdk.Provisioner, error) { return &MockProvisioner{}, nil }, "file": func() (packersdk.Provisioner, error) { return &MockProvisioner{}, nil }, }, - PostProcessorsSchemas: packer.MapOfPostProcessor{ + PostProcessorsSchemas: packersdk.MapOfPostProcessor{ "amazon-import": func() (packersdk.PostProcessor, error) { return &MockPostProcessor{}, nil }, "manifest": func() (packersdk.PostProcessor, error) { return &MockPostProcessor{}, nil }, }, diff --git a/helper/tests/acc/provisioners.go b/helper/tests/acc/provisioners.go index 82c11856c..5c7a780c9 100644 --- a/helper/tests/acc/provisioners.go +++ b/helper/tests/acc/provisioners.go @@ -14,7 +14,7 @@ import ( amazonEBS "github.com/hashicorp/packer/builder/amazon/ebs/acceptance" virtualboxISO "github.com/hashicorp/packer/builder/virtualbox/iso/acceptance" "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/packer" + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) func TestProvisionersAgainstBuilders(provisionerAcc ProvisionerAcceptance, t *testing.T) { @@ -133,14 +133,14 @@ func buildCommand(t *testing.T, builder BuilderAcceptance, provisioner Provision type ProvisionerAcceptance interface { GetName() string GetConfig() (string, error) - GetProvisionerStore() packer.MapOfProvisioner + GetProvisionerStore() packersdk.MapOfProvisioner IsCompatible(builder string, vmOS string) bool RunTest(c *command.BuildCommand, args []string) error } type BuilderAcceptance interface { GetConfigs() (map[string]string, error) - GetBuilderStore() packer.MapOfBuilder + GetBuilderStore() packersdk.MapOfBuilder CleanUp() error } diff --git a/helper/tests/core.go b/helper/tests/core.go index 87b19d0b5..5e8e2764f 100644 --- a/helper/tests/core.go +++ b/helper/tests/core.go @@ -25,14 +25,14 @@ func FileExists(filename string) bool { // available. This allows us to test a builder that writes files to disk. func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { components := packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ + BuilderStore: packersdk.MapOfBuilder{ "amazon-ebs": func() (packersdk.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, }, - ProvisionerStore: packer.MapOfProvisioner{ + ProvisionerStore: packersdk.MapOfProvisioner{ "shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil }, "file": func() (packersdk.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, }, - PostProcessorStore: packer.MapOfPostProcessor{}, + PostProcessorStore: packersdk.MapOfPostProcessor{}, } return &packer.CoreConfig{ Components: components, diff --git a/packer-plugin-sdk/common/packer_config.go b/packer-plugin-sdk/common/packer_config.go index 2a666bc5d..34c3091a9 100644 --- a/packer-plugin-sdk/common/packer_config.go +++ b/packer-plugin-sdk/common/packer_config.go @@ -1,5 +1,42 @@ package common +const ( + // This is the key in configurations that is set to the name of the + // build. + BuildNameConfigKey = "packer_build_name" + + // This is the key in the configuration that is set to the type + // of the builder that is run. This is useful for provisioners and + // such who want to make use of this. + BuilderTypeConfigKey = "packer_builder_type" + + // this is the key in the configuration that is set to the version of the + // Packer Core. This can be used by plugins to set user agents, etc, without + // having to import the Core to find out the Packer version. + CoreVersionConfigKey = "packer_core_version" + + // This is the key in configurations that is set to "true" when Packer + // debugging is enabled. + DebugConfigKey = "packer_debug" + + // This is the key in configurations that is set to "true" when Packer + // force build is enabled. + ForceConfigKey = "packer_force" + + // This key determines what to do when a normal multistep step fails + // - "cleanup" - run cleanup steps + // - "abort" - exit without cleanup + // - "ask" - ask the user + OnErrorConfigKey = "packer_on_error" + + // TemplatePathKey is the path to the template that configured this build + TemplatePathKey = "packer_template_path" + + // This key contains a map[string]string of the user variables for + // template processing. + UserVariablesConfigKey = "packer_user_variables" +) + // PackerConfig is a struct that contains the configuration keys that // are sent by packer, properly tagged already so mapstructure can load // them. Embed this structure into your configuration class to get access to diff --git a/packer/maps.go b/packer-plugin-sdk/packer/maps.go similarity index 67% rename from packer/maps.go rename to packer-plugin-sdk/packer/maps.go index 388aa2381..7cad0a014 100644 --- a/packer/maps.go +++ b/packer-plugin-sdk/packer/maps.go @@ -2,18 +2,16 @@ package packer import ( "fmt" - - packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) -type MapOfProvisioner map[string]func() (packersdk.Provisioner, error) +type MapOfProvisioner map[string]func() (Provisioner, error) func (mop MapOfProvisioner) Has(provisioner string) bool { _, res := mop[provisioner] return res } -func (mop MapOfProvisioner) Start(provisioner string) (packersdk.Provisioner, error) { +func (mop MapOfProvisioner) Start(provisioner string) (Provisioner, error) { p, found := mop[provisioner] if !found { return nil, fmt.Errorf("Unknown provisioner %s", provisioner) @@ -29,14 +27,14 @@ func (mop MapOfProvisioner) List() []string { return res } -type MapOfPostProcessor map[string]func() (packersdk.PostProcessor, error) +type MapOfPostProcessor map[string]func() (PostProcessor, error) func (mopp MapOfPostProcessor) Has(postProcessor string) bool { _, res := mopp[postProcessor] return res } -func (mopp MapOfPostProcessor) Start(postProcessor string) (packersdk.PostProcessor, error) { +func (mopp MapOfPostProcessor) Start(postProcessor string) (PostProcessor, error) { p, found := mopp[postProcessor] if !found { return nil, fmt.Errorf("Unknown post-processor %s", postProcessor) @@ -52,14 +50,14 @@ func (mopp MapOfPostProcessor) List() []string { return res } -type MapOfBuilder map[string]func() (packersdk.Builder, error) +type MapOfBuilder map[string]func() (Builder, error) func (mob MapOfBuilder) Has(builder string) bool { _, res := mob[builder] return res } -func (mob MapOfBuilder) Start(builder string) (packersdk.Builder, error) { +func (mob MapOfBuilder) Start(builder string) (Builder, error) { d, found := mob[builder] if !found { return nil, fmt.Errorf("Unknown builder %s", builder) diff --git a/packer/build.go b/packer/build.go index d36503426..03da65d0a 100644 --- a/packer/build.go +++ b/packer/build.go @@ -6,48 +6,12 @@ import ( "log" "sync" + "github.com/hashicorp/packer/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata" "github.com/hashicorp/packer/version" ) -const ( - // This is the key in configurations that is set to the name of the - // build. - BuildNameConfigKey = "packer_build_name" - - // This is the key in the configuration that is set to the type - // of the builder that is run. This is useful for provisioners and - // such who want to make use of this. - BuilderTypeConfigKey = "packer_builder_type" - - // this is the key in the configuration that is set to the version of the - // Packer Core. This can be used by plugins to set user agents, etc, without - // having to import the Core to find out the Packer version. - CoreVersionConfigKey = "packer_core_version" - - // This is the key in configurations that is set to "true" when Packer - // debugging is enabled. - DebugConfigKey = "packer_debug" - - // This is the key in configurations that is set to "true" when Packer - // force build is enabled. - ForceConfigKey = "packer_force" - - // This key determines what to do when a normal multistep step fails - // - "cleanup" - run cleanup steps - // - "abort" - exit without cleanup - // - "ask" - ask the user - OnErrorConfigKey = "packer_on_error" - - // TemplatePathKey is the path to the template that configured this build - TemplatePathKey = "packer_template_path" - - // This key contains a map[string]string of the user variables for - // template processing. - UserVariablesConfigKey = "packer_user_variables" -) - // A Build represents a single job within Packer that is responsible for // building some machine image artifact. Builds are meant to be parallelized. type Build interface { @@ -165,14 +129,14 @@ func (b *CoreBuild) Prepare() (warn []string, err error) { b.prepareCalled = true packerConfig := map[string]interface{}{ - BuildNameConfigKey: b.Type, - BuilderTypeConfigKey: b.BuilderType, - CoreVersionConfigKey: version.FormattedVersion(), - DebugConfigKey: b.debug, - ForceConfigKey: b.force, - OnErrorConfigKey: b.onError, - TemplatePathKey: b.TemplatePath, - UserVariablesConfigKey: b.Variables, + common.BuildNameConfigKey: b.Type, + common.BuilderTypeConfigKey: b.BuilderType, + common.CoreVersionConfigKey: version.FormattedVersion(), + common.DebugConfigKey: b.debug, + common.ForceConfigKey: b.force, + common.OnErrorConfigKey: b.onError, + common.TemplatePathKey: b.TemplatePath, + common.UserVariablesConfigKey: b.Variables, } // Prepare the builder diff --git a/packer/build_test.go b/packer/build_test.go index ce680c9d5..987f46366 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -5,6 +5,7 @@ import ( "reflect" "testing" + "github.com/hashicorp/packer/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata" "github.com/hashicorp/packer/version" @@ -41,14 +42,14 @@ func testBuild() *CoreBuild { func testDefaultPackerConfig() map[string]interface{} { return map[string]interface{}{ - BuildNameConfigKey: "test", - BuilderTypeConfigKey: "foo", - CoreVersionConfigKey: version.FormattedVersion(), - DebugConfigKey: false, - ForceConfigKey: false, - OnErrorConfigKey: "cleanup", - TemplatePathKey: "", - UserVariablesConfigKey: make(map[string]string), + common.BuildNameConfigKey: "test", + common.BuilderTypeConfigKey: "foo", + common.CoreVersionConfigKey: version.FormattedVersion(), + common.DebugConfigKey: false, + common.ForceConfigKey: false, + common.OnErrorConfigKey: "cleanup", + common.TemplatePathKey: "", + common.UserVariablesConfigKey: make(map[string]string), } } func TestBuild_Name(t *testing.T) { @@ -144,7 +145,7 @@ func TestBuildPrepare_BuilderWarnings(t *testing.T) { func TestBuild_Prepare_Debug(t *testing.T) { packerConfig := testDefaultPackerConfig() - packerConfig[DebugConfigKey] = true + packerConfig[common.DebugConfigKey] = true build := testBuild() builder := build.Builder.(*MockBuilder) @@ -170,7 +171,7 @@ func TestBuild_Prepare_Debug(t *testing.T) { func TestBuildPrepare_variables_default(t *testing.T) { packerConfig := testDefaultPackerConfig() - packerConfig[UserVariablesConfigKey] = map[string]string{ + packerConfig[common.UserVariablesConfigKey] = map[string]string{ "foo": "bar", } diff --git a/packer/core_test.go b/packer/core_test.go index 813c58d18..d73c0b036 100644 --- a/packer/core_test.go +++ b/packer/core_test.go @@ -800,7 +800,7 @@ func TestCoreBuild_provRetry(t *testing.T) { b := TestBuilder(t, config, "test") pString := new(MockProvisioner) pInt := new(MockProvisioner) - config.Components.ProvisionerStore = MapOfProvisioner{ + config.Components.ProvisionerStore = packersdk.MapOfProvisioner{ "test-string": func() (packersdk.Provisioner, error) { return pString, nil }, // backwards compatibility "test-integer": func() (packersdk.Provisioner, error) { return pInt, nil }, diff --git a/packer/testing.go b/packer/testing.go index b6162093a..aaed749db 100644 --- a/packer/testing.go +++ b/packer/testing.go @@ -11,7 +11,7 @@ import ( func TestCoreConfig(t *testing.T) *CoreConfig { // Create some test components components := ComponentFinder{ - BuilderStore: MapOfBuilder{ + BuilderStore: packersdk.MapOfBuilder{ "test": func() (packersdk.Builder, error) { return &MockBuilder{}, nil }, }, } @@ -45,7 +45,7 @@ func TestUi(t *testing.T) packersdk.Ui { func TestBuilder(t *testing.T, c *CoreConfig, n string) *MockBuilder { var b MockBuilder - c.Components.BuilderStore = MapOfBuilder{ + c.Components.BuilderStore = packersdk.MapOfBuilder{ n: func() (packersdk.Builder, error) { return &b, nil }, } @@ -57,7 +57,7 @@ func TestBuilder(t *testing.T, c *CoreConfig, n string) *MockBuilder { func TestProvisioner(t *testing.T, c *CoreConfig, n string) *MockProvisioner { var b MockProvisioner - c.Components.ProvisionerStore = MapOfProvisioner{ + c.Components.ProvisionerStore = packersdk.MapOfProvisioner{ n: func() (packersdk.Provisioner, error) { return &b, nil }, } @@ -69,7 +69,7 @@ func TestProvisioner(t *testing.T, c *CoreConfig, n string) *MockProvisioner { func TestPostProcessor(t *testing.T, c *CoreConfig, n string) *MockPostProcessor { var b MockPostProcessor - c.Components.PostProcessorStore = MapOfPostProcessor{ + c.Components.PostProcessorStore = packersdk.MapOfPostProcessor{ n: func() (packersdk.PostProcessor, error) { return &b, nil }, } diff --git a/provisioner/chef-solo/provisioner_test.go b/provisioner/chef-solo/provisioner_test.go index 9a378efc0..132cb36de 100644 --- a/provisioner/chef-solo/provisioner_test.go +++ b/provisioner/chef-solo/provisioner_test.go @@ -5,7 +5,7 @@ import ( "os" "testing" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -303,7 +303,7 @@ func TestProvisionerPrepare_json(t *testing.T) { "foo": "{{ user `foo` }}", } - config[packer.UserVariablesConfigKey] = map[string]string{ + config[common.UserVariablesConfigKey] = map[string]string{ "foo": `"bar\baz"`, } diff --git a/provisioner/powershell/provisioner_acc_test.go b/provisioner/powershell/provisioner_acc_test.go index baad86fad..62837e992 100644 --- a/provisioner/powershell/provisioner_acc_test.go +++ b/provisioner/powershell/provisioner_acc_test.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/go-uuid" "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/helper/tests/acc" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/provisioner/powershell" windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell" @@ -63,8 +62,8 @@ func (s *PowershellProvisionerAccTest) GetConfig() (string, error) { return string(file), nil } -func (s *PowershellProvisionerAccTest) GetProvisionerStore() packer.MapOfProvisioner { - return packer.MapOfProvisioner{ +func (s *PowershellProvisionerAccTest) GetProvisionerStore() packersdk.MapOfProvisioner { + return packersdk.MapOfProvisioner{ TestProvisionerName: func() (packersdk.Provisioner, error) { return &powershell.Provisioner{}, nil }, "windows-shell": func() (packersdk.Provisioner, error) { return &windowsshellprovisioner.Provisioner{}, nil }, } diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index feb14c2f1..a14e21910 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer-plugin-sdk/common" "github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/stretchr/testify/assert" @@ -89,7 +89,7 @@ func TestProvisionerPrepare_Config(t *testing.T) { config := testConfig() config["elevated_user"] = "{{user `user`}}" config["elevated_password"] = "{{user `password`}}" - config[packer.UserVariablesConfigKey] = map[string]string{ + config[common.UserVariablesConfigKey] = map[string]string{ "user": "myusername", "password": "mypassword", } diff --git a/provisioner/shell-local/provisioner_acc_test.go b/provisioner/shell-local/provisioner_acc_test.go index 0b2e608a1..91c58534b 100644 --- a/provisioner/shell-local/provisioner_acc_test.go +++ b/provisioner/shell-local/provisioner_acc_test.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/packer/helper/tests/acc" "github.com/hashicorp/packer/provisioner/shell" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/command" @@ -40,8 +39,8 @@ func (s *ShellLocalProvisionerAccTest) GetConfig() (string, error) { return string(file), err } -func (s *ShellLocalProvisionerAccTest) GetProvisionerStore() packer.MapOfProvisioner { - return packer.MapOfProvisioner{ +func (s *ShellLocalProvisionerAccTest) GetProvisionerStore() packersdk.MapOfProvisioner { + return packersdk.MapOfProvisioner{ "shell-local": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil }, } } diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index 5ba6ac42f..4cde4db80 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -12,7 +12,6 @@ import ( "github.com/hashicorp/packer/provisioner/file" "github.com/hashicorp/packer/provisioner/shell" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/go-uuid" @@ -43,8 +42,8 @@ func (s *ShellProvisionerAccTest) GetConfig() (string, error) { return string(file), err } -func (s *ShellProvisionerAccTest) GetProvisionerStore() packer.MapOfProvisioner { - return packer.MapOfProvisioner{ +func (s *ShellProvisionerAccTest) GetProvisionerStore() packersdk.MapOfProvisioner { + return packersdk.MapOfProvisioner{ "shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil }, "file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil }, }