2021-02-02 12:05:04 -05:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
2021-03-30 09:53:04 -04:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/didyoumean"
|
2021-02-02 12:05:04 -05:00
|
|
|
pluginsdk "github.com/hashicorp/packer-plugin-sdk/plugin"
|
|
|
|
plugingetter "github.com/hashicorp/packer/packer/plugin-getter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PluginRequirements returns a sorted list of plugin requirements.
|
|
|
|
func (cfg *PackerConfig) PluginRequirements() (plugingetter.Requirements, hcl.Diagnostics) {
|
|
|
|
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
var reqs plugingetter.Requirements
|
|
|
|
reqPluginsBlocks := cfg.Packer.RequiredPlugins
|
|
|
|
|
|
|
|
// Take all required plugins, make sure there are no conflicting blocks
|
|
|
|
// and append them to the list.
|
|
|
|
uniq := map[string]*RequiredPlugin{}
|
|
|
|
for _, requiredPluginsBlock := range reqPluginsBlocks {
|
|
|
|
for name, block := range requiredPluginsBlock.RequiredPlugins {
|
|
|
|
|
|
|
|
if previouslySeenBlock, found := uniq[name]; found {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: fmt.Sprintf("Duplicate required_plugin.%q block", name),
|
|
|
|
Detail: fmt.Sprintf("Block previously seen at %s is already named %q.\n", previouslySeenBlock.DeclRange, name) +
|
|
|
|
"Names at the left hand side of required_plugins are made available to use in your HCL2 configurations.\n" +
|
|
|
|
"To allow to calling to their features correctly two plugins have to have different accessors.",
|
|
|
|
Context: &block.DeclRange,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs = append(reqs, &plugingetter.Requirement{
|
|
|
|
Accessor: name,
|
|
|
|
Identifier: block.Type,
|
|
|
|
VersionConstraints: block.Requirement.Required,
|
2021-03-24 06:31:39 -04:00
|
|
|
Implicit: block.PluginDependencyReason == PluginDependencyImplicit,
|
2021-02-02 12:05:04 -05:00
|
|
|
})
|
|
|
|
uniq[name] = block
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return reqs, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *PackerConfig) detectPluginBinaries() hcl.Diagnostics {
|
|
|
|
opts := plugingetter.ListInstallationsOptions{
|
|
|
|
FromFolders: cfg.parser.PluginConfig.KnownPluginFolders,
|
|
|
|
BinaryInstallationOptions: plugingetter.BinaryInstallationOptions{
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
ARCH: runtime.GOARCH,
|
|
|
|
APIVersionMajor: pluginsdk.APIVersionMajor,
|
|
|
|
APIVersionMinor: pluginsdk.APIVersionMinor,
|
|
|
|
Checksummers: []plugingetter.Checksummer{
|
|
|
|
{Type: "sha256", Hash: sha256.New()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" && opts.Ext == "" {
|
|
|
|
opts.BinaryInstallationOptions.Ext = ".exe"
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginReqs, diags := cfg.PluginRequirements()
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pluginRequirement := range pluginReqs {
|
|
|
|
sortedInstalls, err := pluginRequirement.ListInstallations(opts)
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2021-02-15 09:32:42 -05:00
|
|
|
Summary: fmt.Sprintf("Failed to list installation for %s", pluginRequirement.Identifier),
|
2021-02-02 12:05:04 -05:00
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(sortedInstalls) == 0 {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2021-02-15 09:32:42 -05:00
|
|
|
Summary: fmt.Sprintf("no plugin installed for %s %v", pluginRequirement.Identifier, pluginRequirement.VersionConstraints.String()),
|
2021-02-02 12:05:04 -05:00
|
|
|
Detail: "Did you run packer init for this project ?",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2021-02-15 09:32:42 -05:00
|
|
|
log.Printf("[TRACE] Found the following %q installations: %v", pluginRequirement.Identifier, sortedInstalls)
|
2021-02-02 12:05:04 -05:00
|
|
|
install := sortedInstalls[len(sortedInstalls)-1]
|
|
|
|
err = cfg.parser.PluginConfig.DiscoverMultiPlugin(pluginRequirement.Accessor, install.BinaryPath)
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2021-02-15 09:32:42 -05:00
|
|
|
Summary: fmt.Sprintf("Error discovering plugin %s", pluginRequirement.Identifier),
|
2021-02-09 11:07:59 -05:00
|
|
|
Detail: err.Error(),
|
2021-02-02 12:05:04 -05:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *PackerConfig) initializeBlocks() hcl.Diagnostics {
|
2021-03-30 09:53:04 -04:00
|
|
|
// verify that all used plugins do exist
|
2021-02-02 12:05:04 -05:00
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
|
|
|
for _, build := range cfg.Builds {
|
|
|
|
for i := range build.Sources {
|
|
|
|
// here we grab a pointer to the source usage because we will set
|
|
|
|
// its body.
|
|
|
|
srcUsage := &(build.Sources[i])
|
|
|
|
if !cfg.parser.PluginConfig.Builders.Has(srcUsage.Type) {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: "Unknown " + buildSourceLabel + " type " + srcUsage.Type,
|
|
|
|
Subject: &build.HCL2Ref.DefRange,
|
|
|
|
Detail: fmt.Sprintf("known builders: %v", cfg.parser.PluginConfig.Builders.List()),
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceDefinition, found := cfg.Sources[srcUsage.SourceRef]
|
|
|
|
if !found {
|
2021-03-30 09:53:04 -04:00
|
|
|
availableSrcs := listAvailableSourceNames(cfg.Sources)
|
|
|
|
detail := fmt.Sprintf("Known: %v", availableSrcs)
|
|
|
|
if sugg := didyoumean.NameSuggestion(srcUsage.SourceRef.String(), availableSrcs); sugg != "" {
|
|
|
|
detail = fmt.Sprintf("Did you mean to use %q?", sugg)
|
|
|
|
}
|
2021-02-02 12:05:04 -05:00
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
2021-03-30 09:53:04 -04:00
|
|
|
Summary: "Unknown " + sourceLabel + " " + srcUsage.SourceRef.String(),
|
2021-02-02 12:05:04 -05:00
|
|
|
Subject: build.HCL2Ref.DefRange.Ptr(),
|
|
|
|
Severity: hcl.DiagError,
|
2021-03-30 09:53:04 -04:00
|
|
|
Detail: detail,
|
2021-02-02 12:05:04 -05:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
body := sourceDefinition.block.Body
|
|
|
|
if srcUsage.Body != nil {
|
|
|
|
// merge additions into source definition to get a new body.
|
|
|
|
body = hcl.MergeBodies([]hcl.Body{body, srcUsage.Body})
|
|
|
|
}
|
|
|
|
|
|
|
|
srcUsage.Body = body
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, provBlock := range build.ProvisionerBlocks {
|
|
|
|
if !cfg.parser.PluginConfig.Provisioners.Has(provBlock.PType) {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: fmt.Sprintf("Unknown "+buildProvisionerLabel+" type %q", provBlock.PType),
|
|
|
|
Subject: provBlock.HCL2Ref.TypeRange.Ptr(),
|
|
|
|
Detail: fmt.Sprintf("known "+buildProvisionerLabel+"s: %v", cfg.parser.PluginConfig.Provisioners.List()),
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 04:23:15 -05:00
|
|
|
if build.ErrorCleanupProvisionerBlock != nil {
|
|
|
|
if !cfg.parser.PluginConfig.Provisioners.Has(build.ErrorCleanupProvisionerBlock.PType) {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: fmt.Sprintf("Unknown "+buildErrorCleanupProvisionerLabel+" type %q", build.ErrorCleanupProvisionerBlock.PType),
|
|
|
|
Subject: build.ErrorCleanupProvisionerBlock.HCL2Ref.TypeRange.Ptr(),
|
|
|
|
Detail: fmt.Sprintf("known "+buildErrorCleanupProvisionerLabel+"s: %v", cfg.parser.PluginConfig.Provisioners.List()),
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
for _, ppList := range build.PostProcessorsLists {
|
|
|
|
for _, ppBlock := range ppList {
|
|
|
|
if !cfg.parser.PluginConfig.PostProcessors.Has(ppBlock.PType) {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Summary: fmt.Sprintf("Unknown "+buildPostProcessorLabel+" type %q", ppBlock.PType),
|
|
|
|
Subject: ppBlock.HCL2Ref.TypeRange.Ptr(),
|
2021-02-04 04:56:59 -05:00
|
|
|
Detail: fmt.Sprintf("known "+buildPostProcessorLabel+"s: %v", cfg.parser.PluginConfig.PostProcessors.List()),
|
2021-02-02 12:05:04 -05:00
|
|
|
Severity: hcl.DiagError,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|