implemet override provisioner's option for hcl2
This commit is contained in:
parent
b5b28c55fb
commit
62c3743890
|
@ -6,7 +6,9 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/hcl/v2"
|
"github.com/hashicorp/hcl/v2"
|
||||||
"github.com/hashicorp/hcl/v2/gohcl"
|
"github.com/hashicorp/hcl/v2/gohcl"
|
||||||
|
hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
)
|
)
|
||||||
|
|
||||||
// OnlyExcept is a struct that is meant to be embedded that contains the
|
// OnlyExcept is a struct that is meant to be embedded that contains the
|
||||||
|
@ -62,6 +64,7 @@ type ProvisionerBlock struct {
|
||||||
PauseBefore time.Duration
|
PauseBefore time.Duration
|
||||||
MaxRetries int
|
MaxRetries int
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
|
Override map[string]interface{}
|
||||||
OnlyExcept OnlyExcept
|
OnlyExcept OnlyExcept
|
||||||
HCL2Ref
|
HCL2Ref
|
||||||
}
|
}
|
||||||
|
@ -78,6 +81,7 @@ func (p *Parser) decodeProvisioner(block *hcl.Block, cfg *PackerConfig) (*Provis
|
||||||
Timeout string `hcl:"timeout,optional"`
|
Timeout string `hcl:"timeout,optional"`
|
||||||
Only []string `hcl:"only,optional"`
|
Only []string `hcl:"only,optional"`
|
||||||
Except []string `hcl:"except,optional"`
|
Except []string `hcl:"except,optional"`
|
||||||
|
Override cty.Value `hcl:"override,optional"`
|
||||||
Rest hcl.Body `hcl:",remain"`
|
Rest hcl.Body `hcl:",remain"`
|
||||||
}
|
}
|
||||||
diags := gohcl.DecodeBody(block.Body, cfg.EvalContext(nil), &b)
|
diags := gohcl.DecodeBody(block.Body, cfg.EvalContext(nil), &b)
|
||||||
|
@ -98,6 +102,18 @@ func (p *Parser) decodeProvisioner(block *hcl.Block, cfg *PackerConfig) (*Provis
|
||||||
return nil, diags
|
return nil, diags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !b.Override.IsNull() {
|
||||||
|
override := make(map[string]interface{})
|
||||||
|
for buildName, overrides := range b.Override.AsValueMap() {
|
||||||
|
buildOverrides := make(map[string]interface{})
|
||||||
|
for option, value := range overrides.AsValueMap() {
|
||||||
|
buildOverrides[option] = hcl2shim.ConfigValueFromHCL2(value)
|
||||||
|
}
|
||||||
|
override[buildName] = buildOverrides
|
||||||
|
}
|
||||||
|
provisioner.Override = override
|
||||||
|
}
|
||||||
|
|
||||||
if b.PauseBefore != "" {
|
if b.PauseBefore != "" {
|
||||||
pauseBefore, err := time.ParseDuration(b.PauseBefore)
|
pauseBefore, err := time.ParseDuration(b.PauseBefore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -144,12 +160,20 @@ func (cfg *PackerConfig) startProvisioner(source SourceBlock, pb *ProvisionerBlo
|
||||||
})
|
})
|
||||||
return nil, diags
|
return nil, diags
|
||||||
}
|
}
|
||||||
|
|
||||||
hclProvisioner := &HCL2Provisioner{
|
hclProvisioner := &HCL2Provisioner{
|
||||||
Provisioner: provisioner,
|
Provisioner: provisioner,
|
||||||
provisionerBlock: pb,
|
provisionerBlock: pb,
|
||||||
evalContext: ectx,
|
evalContext: ectx,
|
||||||
builderVariables: source.builderVariables(),
|
builderVariables: source.builderVariables(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pb.Override != nil {
|
||||||
|
if override, ok := pb.Override[source.name()]; ok {
|
||||||
|
hclProvisioner.override = override.(map[string]interface{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = hclProvisioner.HCL2Prepare(nil)
|
err = hclProvisioner.HCL2Prepare(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
diags = append(diags, &hcl.Diagnostic{
|
diags = append(diags, &hcl.Diagnostic{
|
||||||
|
|
|
@ -3,7 +3,6 @@ package hcl2template
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/hashicorp/hcl/v2"
|
"github.com/hashicorp/hcl/v2"
|
||||||
"github.com/hashicorp/hcl/v2/hcldec"
|
"github.com/hashicorp/hcl/v2/hcldec"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
|
@ -19,6 +18,7 @@ type HCL2Provisioner struct {
|
||||||
provisionerBlock *ProvisionerBlock
|
provisionerBlock *ProvisionerBlock
|
||||||
evalContext *hcl.EvalContext
|
evalContext *hcl.EvalContext
|
||||||
builderVariables map[string]string
|
builderVariables map[string]string
|
||||||
|
override map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *HCL2Provisioner) ConfigSpec() hcldec.ObjectSpec {
|
func (p *HCL2Provisioner) ConfigSpec() hcldec.ObjectSpec {
|
||||||
|
@ -55,7 +55,7 @@ func (p *HCL2Provisioner) HCL2Prepare(buildVars map[string]interface{}) error {
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
return p.Provisioner.Prepare(p.builderVariables, flatProvisionerCfg)
|
return p.Provisioner.Prepare(p.builderVariables, flatProvisionerCfg, p.override)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *HCL2Provisioner) Prepare(args ...interface{}) error {
|
func (p *HCL2Provisioner) Prepare(args ...interface{}) error {
|
||||||
|
|
|
@ -10,13 +10,29 @@ Parameters common to all provisioners:
|
||||||
- `override` (object) - Override the builder with different settings for a
|
- `override` (object) - Override the builder with different settings for a
|
||||||
specific builder, eg :
|
specific builder, eg :
|
||||||
|
|
||||||
|
In JSON:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"script": "script.sh",
|
"script": "script.sh",
|
||||||
"override": {
|
"override": {
|
||||||
"vmware-iso": {
|
"example_one": {
|
||||||
"execute_command": "echo 'password' | sudo -S bash {{.Path}}"
|
"script": "basic_one-script.sh"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In HCL2:
|
||||||
|
```hcl
|
||||||
|
build {
|
||||||
|
sources = ["docker.example_one", "docker.example_two"]
|
||||||
|
provisioner "shell" {
|
||||||
|
script = "script.sh"
|
||||||
|
override = {
|
||||||
|
example_one = {
|
||||||
|
script = "basic-one-script.sh"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue