From 1f1a8684b88837a51226b87fcd9dc3b7663e0bb0 Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Fri, 18 Jun 2021 14:18:19 +1000 Subject: [PATCH] HCL and Packer 1.7.x working --- .goreleaser.yml | 5 +++-- README.md | 9 +++++---- TestPackerFile.pkr.hcl | 7 ++++++- go.mod | 37 +++++++++++++++++++++---------------- provisioner.go | 41 ++++++++++++++--------------------------- provisioner.hcl2spec.go | 2 ++ 6 files changed, 51 insertions(+), 50 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index b78eb2b..4cbac3b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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: @@ -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' diff --git a/README.md b/README.md index c102dd6..dd04fd5 100644 --- a/README.md +++ b/README.md @@ -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" } } @@ -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" } } diff --git a/TestPackerFile.pkr.hcl b/TestPackerFile.pkr.hcl index 4435b44..7086a15 100644 --- a/TestPackerFile.pkr.hcl +++ b/TestPackerFile.pkr.hcl @@ -1,7 +1,7 @@ packer { required_plugins { terraform = { - version = "= 0.0.6" + version = "= 0.0.7" source = "github.com/servian/terraform" } } @@ -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" } } diff --git a/go.mod b/go.mod index 766b6aa..bbce1da 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/provisioner.go b/provisioner.go index 62aa90c..6b4d7fe 100644 --- a/provisioner.go +++ b/provisioner.go @@ -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"` @@ -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 { @@ -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 { @@ -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) @@ -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") diff --git a/provisioner.hcl2spec.go b/provisioner.hcl2spec.go index f64e546..2e3b8de 100644 --- a/provisioner.hcl2spec.go +++ b/provisioner.hcl2spec.go @@ -24,6 +24,7 @@ type FlatConfig struct { InstallCommand *string `mapstructure:"install_command" cty:"install_command" hcl:"install_command"` StagingDir *string `mapstructure:"staging_dir" cty:"staging_dir" hcl:"staging_dir"` PreventSudo *bool `mapstructure:"prevent_sudo" cty:"prevent_sudo" hcl:"prevent_sudo"` + VariableString *string `mapstructure:"variable_string" cty:"variable_string" hcl:"variable_string"` GuestOSType *string `mapstructure:"guest_os_type" cty:"guest_os_type" hcl:"guest_os_type"` } @@ -53,6 +54,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "install_command": &hcldec.AttrSpec{Name: "install_command", Type: cty.String, Required: false}, "staging_dir": &hcldec.AttrSpec{Name: "staging_dir", Type: cty.String, Required: false}, "prevent_sudo": &hcldec.AttrSpec{Name: "prevent_sudo", Type: cty.Bool, Required: false}, + "variable_string": &hcldec.AttrSpec{Name: "variable_string", Type: cty.String, Required: false}, "guest_os_type": &hcldec.AttrSpec{Name: "guest_os_type", Type: cty.String, Required: false}, } return s