Skip to content

Commit

Permalink
HCL and Packer 1.7.x working
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Jun 18, 2021
1 parent 7cd7063 commit 1f1a868
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
5 changes: 3 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ builds:
post:
# This will check plugin compatibility against latest version of Packer
- cmd: |
go mod download github.com/hashicorp/packer &&
go install github.com/hashicorp/packer/cmd/packer-plugins-check &&
go install github.com/hashicorp/packer/cmd/packer-plugins-check@latest &&
packer-plugins-check -load={{ .Name }}
dir: "{{ dir .Path}}"
flags:
Expand Down Expand Up @@ -49,6 +48,8 @@ builds:
binary: '{{ .ProjectName }}_v{{ .Version }}_x5.0_{{ .Os }}_{{ .Arch }}'
archives:
- format: zip
files:
- none*
name_template: '{{ .ProjectName }}_v{{ .Version }}_x5.0_{{ .Os }}_{{ .Arch }}'
checksum:
name_template: '{{ .ProjectName }}_v{{ .Version }}_SHA256SUMS'
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ I bit the bullet and started making my own ill advised provisioner for Terraform
packer {
required_plugins {
terraform = {
version = "= 0.0.6"
version = "= 0.0.7"
source = "github.com/servian/terraform"
}
}
Expand All @@ -30,9 +30,10 @@ I bit the bullet and started making my own ill advised provisioner for Terraform
provisioner "terraform" {
code_path = "./tfcode"
prevent_sudo = "true"
variables = {
consul_server_node = false
}
variable_string = jsonencode({
consul_server_node = false
})
version = "1.0.0"
}
}

Expand Down
7 changes: 6 additions & 1 deletion TestPackerFile.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
packer {
required_plugins {
terraform = {
version = "= 0.0.6"
version = "= 0.0.7"
source = "github.com/servian/terraform"
}
}
Expand All @@ -17,5 +17,10 @@ build {
provisioner "terraform" {
code_path = "./tfcode"
prevent_sudo = "true"
variable_string = jsonencode({
consul_server_node = false
nomad_alt_url = "https://example.com"
})
version = "1.0.0"
}
}
37 changes: 21 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,37 @@ go 1.15

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/armon/go-metrics v0.3.6 // indirect
github.com/aws/aws-sdk-go v1.38.5 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/armon/go-metrics v0.3.9 // indirect
github.com/aws/aws-sdk-go v1.38.64 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/frankban/quicktest v1.11.3 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/hashicorp/consul/api v1.8.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-hclog v0.15.0 // indirect
github.com/hashicorp/go-hclog v0.16.1 // indirect
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl/v2 v2.9.1
github.com/hashicorp/packer-plugin-sdk v0.1.0
github.com/hashicorp/hcl/v2 v2.10.0
github.com/hashicorp/packer-plugin-sdk v0.2.3
github.com/hashicorp/vault/api v1.1.0 // indirect
github.com/hashicorp/vault/sdk v0.2.0 // indirect
github.com/hashicorp/yamux v0.0.0-20210316155119-a95892c5f864 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/ugorji/go v1.2.6 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zclconf/go-cty v1.8.1
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/net v0.0.0-20210324205630-d1beb07c2056 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
github.com/zclconf/go-cty v1.8.3
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
)
41 changes: 14 additions & 27 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Config struct {
StagingDir string `mapstructure:"staging_dir"`
PreventSudo bool `mapstructure:"prevent_sudo"`

VariableString string `mapstructure:"variable_string"`

Variables map[string]interface{} `mapstructure:"variables" mapstructure-to-hcl2:",skip"`
GuestOSType string `mapstructure:"guest_os_type"`

Expand All @@ -83,11 +85,14 @@ func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapst
// Prepare parses the config and get everything ready
func (p *Provisioner) Prepare(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
PluginType: "packer.provisioner.terraform",
PluginType: "terraform",
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
Exclude: []string{
"run_command",
"install_command",
},
},
}, raws...)
if err != nil {
Expand Down Expand Up @@ -129,16 +134,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
p.config.RunCommand = p.guestOSTypeConfig.runCommand
}

p.config.Variables, err = p.processVariables()
if err != nil {
return fmt.Errorf("Error processing Variables in JSON: %s", err)
if p.config.VariableString != "" {
if err := json.Unmarshal([]byte(p.config.VariableString), &p.config.Variables); err != nil {
return fmt.Errorf("Error processing Variables: %s", err)
}
}

return nil
}

// Provision does the work of installing Terraform and running it on the remote
func (p *Provisioner) Provision(_ context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error {
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error {
ui.Say("Provisioning with Terraform...")

if err := p.createDir(ui, comm, p.config.StagingDir); err != nil {
Expand All @@ -161,6 +167,8 @@ func (p *Provisioner) Provision(_ context.Context, ui packer.Ui, comm packer.Com
Version: p.config.Version,
Sudo: !p.config.PreventSudo,
}

log.Println(fmt.Sprintf("templating command: %s with %#v", p.config.InstallCommand, &p.config.ctx))
command, err := interpolate.Render(p.config.InstallCommand, &p.config.ctx)
if err != nil {
return fmt.Errorf("Error rendering Template: %s", err)
Expand Down Expand Up @@ -229,27 +237,6 @@ func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, ds
return comm.UploadDir(dst, src, nil)
}

func (p *Provisioner) processVariables() (map[string]interface{}, error) {
jsonBytes, err := json.Marshal(p.config.Variables)
if err != nil {
panic(err)
}

// Process the bytes with the template processor
p.config.ctx.Data = nil
jsonBytesProcessed, err := interpolate.Render(string(jsonBytes), &p.config.ctx)
if err != nil {
return nil, err
}

var result map[string]interface{}
if err := json.Unmarshal([]byte(jsonBytesProcessed), &result); err != nil {
return nil, err
}

return result, nil
}

func (p *Provisioner) createTfvars(ui packer.Ui, comm packer.Communicator) error {
ui.Message("Creating tfvars file")

Expand Down
2 changes: 2 additions & 0 deletions provisioner.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f1a868

Please sign in to comment.