2020-07-24 04:58:03 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2022-10-05 09:21:50 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
2020-07-24 04:58:03 -04:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2021-09-13 21:07:54 -04:00
|
|
|
packerregistry "github.com/hashicorp/packer/internal/registry"
|
2020-07-24 04:58:03 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2021-02-02 12:05:04 -05:00
|
|
|
plugingetter "github.com/hashicorp/packer/packer/plugin-getter"
|
2020-07-24 04:58:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CoreWrapper wraps a packer.Core in order to have it's Initialize func return
|
|
|
|
|
// a diagnostic.
|
|
|
|
|
type CoreWrapper struct {
|
|
|
|
|
*packer.Core
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 04:37:16 -05:00
|
|
|
func (c *CoreWrapper) Initialize(_ packer.InitializeOptions) hcl.Diagnostics {
|
2020-07-24 04:58:03 -04:00
|
|
|
err := c.Core.Initialize()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return hcl.Diagnostics{
|
|
|
|
|
&hcl.Diagnostic{
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-02-02 12:05:04 -05:00
|
|
|
|
|
|
|
|
func (c *CoreWrapper) PluginRequirements() (plugingetter.Requirements, hcl.Diagnostics) {
|
|
|
|
|
return nil, hcl.Diagnostics{
|
|
|
|
|
&hcl.Diagnostic{
|
2022-02-10 16:53:50 -05:00
|
|
|
Summary: "Packer plugins currently only works with HCL2 configuration templates",
|
|
|
|
|
Detail: "Please manually install plugins with the plugins command or use a HCL2 configuration that will do that for you.",
|
2021-02-02 12:05:04 -05:00
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-05 09:25:19 -04:00
|
|
|
|
|
|
|
|
// ConfiguredArtifactMetadataPublisher returns a configured image bucket that can be used for publishing
|
|
|
|
|
// build image artifacts to a configured Packer Registry destination.
|
|
|
|
|
func (c *CoreWrapper) ConfiguredArtifactMetadataPublisher() (*packerregistry.Bucket, hcl.Diagnostics) {
|
|
|
|
|
bucket := c.Core.GetRegistryBucket()
|
|
|
|
|
|
|
|
|
|
// If at this point the bucket is nil, it means the HCP Packer registry is not enabled
|
|
|
|
|
if bucket == nil {
|
|
|
|
|
return nil, hcl.Diagnostics{
|
|
|
|
|
&hcl.Diagnostic{
|
|
|
|
|
Summary: "Publishing build artifacts to HCP Packer Registry not enabled",
|
|
|
|
|
Detail: "No Packer Registry configuration detected; skipping all publishing steps " +
|
|
|
|
|
"See publishing to a Packer registry for Packer configuration details",
|
|
|
|
|
Severity: hcl.DiagWarning,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := bucket.Validate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, hcl.Diagnostics{
|
|
|
|
|
&hcl.Diagnostic{
|
2022-10-05 09:21:50 -04:00
|
|
|
Summary: "Invalid HCP Packer configuration",
|
|
|
|
|
Detail: fmt.Sprintf("Packer could not validate the provided "+
|
|
|
|
|
"HCP Packer registry configuration. Check the error message for details "+
|
|
|
|
|
"or contact HCP Packer support for further assistance.\nError: %s", err),
|
2021-08-05 09:25:19 -04:00
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bucket, nil
|
|
|
|
|
}
|