From c983230548a5e0ced1ae1b02424dbff3660d7be1 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 4 Oct 2022 17:20:21 -0400 Subject: [PATCH] registry: add function to detect explicit HCP The HCP_PACKER_REGISTRY environment variable had its behaviour changed recently, as prior versions of Packer expected the attribute to be forcefully set to a value that is neither "off" nor "0" in order to get HCP integration to work, or that a "hcp_packer_registry" block was defined in an HCL template. Now, this environment variable defaults to not explicitely enable, but instead to explicitely disable HCP integration, and the feature switch fall upon HCP_PACKER_BUCKET_NAME. As an extra feature, we keep the prior behaviour alive when it is explicitely defined as a value to enable it. That way we can report errors if the rest is not defined, rather than silently ignore it. This function we add to env is the first stone to enable this behaviour. --- command/registry.go | 6 +++++- internal/registry/env/env.go | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/command/registry.go b/command/registry.go index 960049197..60ba0e1f0 100644 --- a/command/registry.go +++ b/command/registry.go @@ -63,6 +63,10 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics hasHCP = true } + if env.IsHCPExplicitelyEnabled() { + hasHCP = true + } + if !hasHCP { return nil } @@ -270,7 +274,7 @@ func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics { return nil } - if !env.HasPackerRegistryBucket() { + if !env.HasPackerRegistryBucket() && !env.IsHCPExplicitelyEnabled() { return nil } diff --git a/internal/registry/env/env.go b/internal/registry/env/env.go index beb9397ef..5661e5bd9 100644 --- a/internal/registry/env/env.go +++ b/internal/registry/env/env.go @@ -44,3 +44,9 @@ func IsHCPDisabled() bool { hcp, ok := os.LookupEnv(HCPPackerRegistry) return ok && strings.ToLower(hcp) == "off" || hcp == "0" } + +// IsHCPExplicitelyEnabled returns true if the client enabled HCP_PACKER_REGISTRY explicitely, i.e. it is defined and not 0 or off +func IsHCPExplicitelyEnabled() bool { + hcp, ok := os.LookupEnv(HCPPackerRegistry) + return ok && strings.ToLower(hcp) != "off" && hcp != "0" +}