mirror of
https://github.com/hashicorp/packer.git
synced 2026-04-15 22:20:33 -04:00
hcl2_upgrade: add required_plugins in out template
When a legacy JSON template references built-in plugins, we add them to the resulting template as a `required_plugins' block in HCL2.
This commit is contained in:
parent
a2930bda4f
commit
b522edca36
12 changed files with 178 additions and 6 deletions
|
|
@ -797,16 +797,93 @@ type PackerParser struct {
|
|||
}
|
||||
|
||||
func (p *PackerParser) Parse(tpl *template.Template) error {
|
||||
if tpl.MinVersion != "" {
|
||||
fileContent := hclwrite.NewEmptyFile()
|
||||
body := fileContent.Body()
|
||||
packerBody := body.AppendNewBlock("packer", nil).Body()
|
||||
packerBody.SetAttributeValue("required_version", cty.StringVal(fmt.Sprintf(">= %s", tpl.MinVersion)))
|
||||
p.out = fileContent.Bytes()
|
||||
reqPlugins, err := p.generateRequiredPluginsBlock(tpl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if tpl.MinVersion == "" && reqPlugins == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
fileContent := hclwrite.NewEmptyFile()
|
||||
body := fileContent.Body()
|
||||
packerBody := body.AppendNewBlock("packer", nil).Body()
|
||||
|
||||
if tpl.MinVersion != "" {
|
||||
packerBody.SetAttributeValue("required_version", cty.StringVal(fmt.Sprintf(">= %s", tpl.MinVersion)))
|
||||
}
|
||||
|
||||
if reqPlugins != nil {
|
||||
packerBody.AppendBlock(reqPlugins)
|
||||
}
|
||||
|
||||
p.out = fileContent.Bytes()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func gatherPluginsFromTemplate(tpl *template.Template) []string {
|
||||
plugins := map[string]struct{}{}
|
||||
|
||||
for _, b := range tpl.Builders {
|
||||
for prefix, plugin := range knownPluginPrefixes {
|
||||
if strings.HasPrefix(b.Type, prefix) {
|
||||
plugins[plugin] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range tpl.Provisioners {
|
||||
for prefix, plugin := range knownPluginPrefixes {
|
||||
if strings.HasPrefix(p.Type, prefix) {
|
||||
plugins[plugin] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, pps := range tpl.PostProcessors {
|
||||
for _, pp := range pps {
|
||||
for prefix, plugin := range knownPluginPrefixes {
|
||||
if strings.HasPrefix(pp.Type, prefix) {
|
||||
plugins[plugin] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(plugins) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
retPlugins := make([]string, 0, len(plugins))
|
||||
for plugin := range plugins {
|
||||
retPlugins = append(retPlugins, plugin)
|
||||
}
|
||||
|
||||
sort.Strings(retPlugins)
|
||||
|
||||
return retPlugins
|
||||
}
|
||||
|
||||
func (p *PackerParser) generateRequiredPluginsBlock(tpl *template.Template) (*hclwrite.Block, error) {
|
||||
plugins := gatherPluginsFromTemplate(tpl)
|
||||
if len(plugins) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
reqPlugins := hclwrite.NewBlock("required_plugins", nil)
|
||||
for _, plugin := range plugins {
|
||||
pluginBlock := cty.ObjectVal(map[string]cty.Value{
|
||||
"source": cty.StringVal(plugin),
|
||||
"version": cty.StringVal("~> 1"),
|
||||
})
|
||||
reqPlugins.Body().SetAttributeValue(strings.Replace(plugin, "github.com/hashicorp/", "", 1), pluginBlock)
|
||||
}
|
||||
|
||||
return reqPlugins, nil
|
||||
}
|
||||
|
||||
func (p *PackerParser) Write(out *bytes.Buffer) {
|
||||
if len(p.out) > 0 {
|
||||
if p.WithAnnotations {
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ func Test_hcl2_upgrade(t *testing.T) {
|
|||
{folder: "complete-variables-with-template-engine", flags: []string{}},
|
||||
{folder: "undeclared-variables", flags: []string{}, exitCode: 0},
|
||||
{folder: "varfile-with-no-variables-block", flags: []string{}, exitCode: 0},
|
||||
{folder: "bundled-plugin-used", flags: []string{}, exitCode: 0},
|
||||
}
|
||||
|
||||
for _, tc := range tc {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,11 @@
|
|||
packer {
|
||||
required_plugins {
|
||||
amazon = {
|
||||
source = "github.com/hashicorp/amazon"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source "amazon-ebs" "autogenerated_1" {
|
||||
run_tags = {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
packer {
|
||||
required_version = ">= 1.6.0"
|
||||
required_plugins {
|
||||
amazon = {
|
||||
source = "github.com/hashicorp/amazon"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variable "aws_access_key" {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,11 @@
|
|||
packer {
|
||||
required_plugins {
|
||||
azure = {
|
||||
source = "github.com/hashicorp/azure"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source "azure-arm" "autogenerated_1" {
|
||||
shared_image_gallery {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
packer {
|
||||
required_plugins {
|
||||
amazon = {
|
||||
source = "github.com/hashicorp/amazon"
|
||||
version = "~> 1"
|
||||
}
|
||||
ansible = {
|
||||
source = "github.com/hashicorp/ansible"
|
||||
version = "~> 1"
|
||||
}
|
||||
googlecompute = {
|
||||
source = "github.com/hashicorp/googlecompute"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source "amazon-ebs" "autogenerated_1" {
|
||||
}
|
||||
|
||||
build {
|
||||
sources = ["source.amazon-ebs.autogenerated_1"]
|
||||
|
||||
provisioner "ansible-local" {
|
||||
}
|
||||
|
||||
post-processor "googlecompute-import" {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"builders": [{
|
||||
"type": "amazon-ebs"
|
||||
}],
|
||||
"provisioners": [{
|
||||
"type": "ansible-local"
|
||||
}],
|
||||
"post-processors": [{
|
||||
"type": "googlecompute-import"
|
||||
}]
|
||||
}
|
||||
|
|
@ -14,6 +14,12 @@
|
|||
# See https://www.packer.io/docs/templates/hcl_templates/blocks/packer for more info
|
||||
packer {
|
||||
required_version = ">= 1.6.0"
|
||||
required_plugins {
|
||||
amazon = {
|
||||
source = "github.com/hashicorp/amazon"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# All generated input variables will be of 'string' type as this is how Packer JSON
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@
|
|||
# See https://www.packer.io/docs/templates/hcl_templates/blocks/packer for more info
|
||||
packer {
|
||||
required_version = ">= 1.6.0"
|
||||
required_plugins {
|
||||
amazon = {
|
||||
source = "github.com/hashicorp/amazon"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# All generated input variables will be of 'string' type as this is how Packer JSON
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@
|
|||
# See https://www.packer.io/docs/templates/hcl_templates/blocks/packer for more info
|
||||
packer {
|
||||
required_version = ">= 1.6.0"
|
||||
required_plugins {
|
||||
amazon = {
|
||||
source = "github.com/hashicorp/amazon"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# All generated input variables will be of 'string' type as this is how Packer JSON
|
||||
|
|
|
|||
|
|
@ -1,3 +1,11 @@
|
|||
packer {
|
||||
required_plugins {
|
||||
vsphere = {
|
||||
source = "github.com/hashicorp/vsphere"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source "vsphere-clone" "autogenerated_1" {
|
||||
RAM_reserve_all = false
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
packer {
|
||||
required_version = ">= 1.6.0"
|
||||
required_plugins {
|
||||
amazon = {
|
||||
source = "github.com/hashicorp/amazon"
|
||||
version = "~> 1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variable "aws_access_key" {
|
||||
|
|
|
|||
Loading…
Reference in a new issue