mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-05 06:42:30 -04:00
hcp: use HCP_PACKER_REGISTRY to disable HCP
The current behaviour for HCP integration is based on the presence of the HCP_PACKER_REGISTRY environment variable, where if it is either not "0" or "off", the HCP integration is enabled. This commit changes the behaviour to only use this variable to explicitely disable HCP, and the HCP_PACKER_BUCKET_NAME variable will condition if the HCP integration is enabled for a build or not in JSON templates. For HCL templates, the integration can also be disabled if the HCP_PACKER_REGISTRY variable is set to "0" or "off", and will be considered enabled if the HCP_PACKER_BUCKET_NAME is set, or there is a "hcp_packer_registry" block in a build.
This commit is contained in:
parent
d4b03769f2
commit
779a31a25f
3 changed files with 68 additions and 43 deletions
|
|
@ -47,6 +47,10 @@ func TrySetupHCP(cfg packer.Handler) hcl.Diagnostics {
|
|||
func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics {
|
||||
var diags hcl.Diagnostics
|
||||
|
||||
if env.IsHCPDisabled() {
|
||||
return nil
|
||||
}
|
||||
|
||||
hasHCP := false
|
||||
|
||||
for _, build := range pc.Builds {
|
||||
|
|
@ -55,6 +59,14 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics
|
|||
}
|
||||
}
|
||||
|
||||
if env.HasPackerRegistryBucket() {
|
||||
hasHCP = true
|
||||
}
|
||||
|
||||
if !hasHCP {
|
||||
return nil
|
||||
}
|
||||
|
||||
if hasHCP && len(pc.Builds) > 1 {
|
||||
diags = append(diags, &hcl.Diagnostic{
|
||||
Severity: hcl.DiagError,
|
||||
|
|
@ -67,14 +79,6 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics
|
|||
return diags
|
||||
}
|
||||
|
||||
if env.IsPAREnabled() {
|
||||
hasHCP = true
|
||||
}
|
||||
|
||||
if !hasHCP {
|
||||
return diags
|
||||
}
|
||||
|
||||
var err error
|
||||
pc.Bucket, err = registry.NewBucketWithIteration(registry.IterationOptions{
|
||||
TemplateBaseDir: pc.Basedir,
|
||||
|
|
@ -111,6 +115,14 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics
|
|||
}
|
||||
}
|
||||
|
||||
if pc.Bucket.Slug == "" {
|
||||
return append(diags, &hcl.Diagnostic{
|
||||
Summary: "bucket name cannot be empty",
|
||||
Detail: "empty bucket name, please set it with the HCP_PACKER_BUCKET_NAME environment variable, or in a `hcp_packer_registry` block",
|
||||
Severity: hcl.DiagError,
|
||||
})
|
||||
}
|
||||
|
||||
vals, diags := pc.Datasources.Values()
|
||||
if diags != nil {
|
||||
return diags
|
||||
|
|
@ -254,7 +266,11 @@ func iterValueToDSOutput(iterVal map[string]cty.Value) iterds.DatasourceOutput {
|
|||
}
|
||||
|
||||
func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics {
|
||||
if !env.IsPAREnabled() {
|
||||
if env.IsHCPDisabled() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !env.HasPackerRegistryBucket() {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -276,6 +292,14 @@ func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics {
|
|||
}
|
||||
core.Bucket.LoadDefaultSettingsFromEnv()
|
||||
|
||||
if core.Bucket.Slug == "" {
|
||||
return append(diags, &hcl.Diagnostic{
|
||||
Summary: "bucket name cannot be empty",
|
||||
Detail: "empty bucket name, please set it with the HCP_PACKER_BUCKET_NAME environment variable",
|
||||
Severity: hcl.DiagError,
|
||||
})
|
||||
}
|
||||
|
||||
for _, b := range core.Template.Builders {
|
||||
// Get all builds slated within config ignoring any only or exclude flags.
|
||||
core.Bucket.RegisterBuildForComponent(b.Name)
|
||||
|
|
|
|||
23
internal/registry/env/env.go
vendored
23
internal/registry/env/env.go
vendored
|
|
@ -6,18 +6,23 @@ import (
|
|||
)
|
||||
|
||||
func HasClientID() bool {
|
||||
_, ok := os.LookupEnv(HCPClientID)
|
||||
return ok
|
||||
return hasEnvVar(HCPClientID)
|
||||
}
|
||||
|
||||
func HasClientSecret() bool {
|
||||
_, ok := os.LookupEnv(HCPClientSecret)
|
||||
return ok
|
||||
return hasEnvVar(HCPClientSecret)
|
||||
}
|
||||
|
||||
func HasPackerRegistryBucket() bool {
|
||||
_, ok := os.LookupEnv(HCPPackerBucket)
|
||||
return ok
|
||||
return hasEnvVar(HCPPackerBucket)
|
||||
}
|
||||
|
||||
func hasEnvVar(varName string) bool {
|
||||
val, ok := os.LookupEnv(varName)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return val != ""
|
||||
}
|
||||
|
||||
func HasHCPCredentials() bool {
|
||||
|
|
@ -35,7 +40,7 @@ func HasHCPCredentials() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func IsPAREnabled() bool {
|
||||
val, ok := os.LookupEnv(HCPPackerRegistry)
|
||||
return ok && strings.ToLower(val) != "off" && val != "0"
|
||||
func IsHCPDisabled() bool {
|
||||
hcp, ok := os.LookupEnv(HCPPackerRegistry)
|
||||
return ok && strings.ToLower(hcp) == "off" || hcp == "0"
|
||||
}
|
||||
|
|
|
|||
46
internal/registry/env/env_test.go
vendored
46
internal/registry/env/env_test.go
vendored
|
|
@ -1,50 +1,46 @@
|
|||
package env
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_IsPAREnabled(t *testing.T) {
|
||||
func Test_IsHCPDisabled(t *testing.T) {
|
||||
tcs := []struct {
|
||||
name string
|
||||
value string
|
||||
output bool
|
||||
name string
|
||||
registry_value string
|
||||
output bool
|
||||
}{
|
||||
{
|
||||
name: "set with 1",
|
||||
value: "1",
|
||||
output: true,
|
||||
name: "nothing set",
|
||||
registry_value: "",
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
name: "set with ON",
|
||||
value: "ON",
|
||||
output: true,
|
||||
name: "registry set with 1",
|
||||
registry_value: "1",
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
name: "set with 0",
|
||||
value: "0",
|
||||
output: false,
|
||||
name: "registry set with 0",
|
||||
registry_value: "0",
|
||||
output: true,
|
||||
},
|
||||
{
|
||||
name: "set with OFF",
|
||||
value: "OFF",
|
||||
output: false,
|
||||
name: "registry set with OFF",
|
||||
registry_value: "OFF",
|
||||
output: true,
|
||||
},
|
||||
{
|
||||
name: "unset",
|
||||
value: "",
|
||||
output: false,
|
||||
name: "registry set with off",
|
||||
registry_value: "off",
|
||||
output: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if tc.value != "" {
|
||||
_ = os.Setenv(HCPPackerRegistry, tc.value)
|
||||
defer os.Unsetenv(HCPPackerRegistry)
|
||||
}
|
||||
out := IsPAREnabled()
|
||||
t.Setenv(HCPPackerRegistry, tc.registry_value)
|
||||
out := IsHCPDisabled()
|
||||
if out != tc.output {
|
||||
t.Fatalf("unexpected output: %t", out)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue