diff --git a/.goreleaser.yml b/.goreleaser.yml index 7d0f009..b78eb2b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -12,7 +12,8 @@ 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 mod download github.com/hashicorp/packer && + go install github.com/hashicorp/packer/cmd/packer-plugins-check && packer-plugins-check -load={{ .Name }} dir: "{{ dir .Path}}" flags: diff --git a/checkpoint.go b/checkpoint.go index 7852b38..3397028 100644 --- a/checkpoint.go +++ b/checkpoint.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "net/url" "runtime" @@ -60,6 +61,7 @@ func FetchLatestTerraform() (string, error) { // enough to block on if checkpoint is broken/slow. client.Timeout = time.Duration(3000) * time.Millisecond + log.Println(fmt.Sprintf("About to fetch from URL: %s", u.String())) resp, err := client.Do(req) if err != nil { return "", err @@ -76,5 +78,6 @@ func FetchLatestTerraform() (string, error) { if err := json.NewDecoder(r).Decode(&result); err != nil { return "", err } + log.Println(fmt.Sprintf("Got version response: %s", result.CurrentVersion)) return result.CurrentVersion, nil } diff --git a/go.mod b/go.mod index 00f7e9d..766b6aa 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,33 @@ module github.com/servian/packer-provisioner-terraform go 1.15 require ( - github.com/hashicorp/go-cleanhttp v0.5.1 - github.com/hashicorp/hcl/v2 v2.8.0 + 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/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-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/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/zclconf/go-cty v1.7.0 + github.com/hashicorp/yamux v0.0.0-20210316155119-a95892c5f864 // 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/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 ) diff --git a/provisioner.go b/provisioner.go index 27b2f49..62aa90c 100644 --- a/provisioner.go +++ b/provisioner.go @@ -1,4 +1,4 @@ -//go:generate mapstructure-to-hcl2 -type ProvisionerConfig +//go:generate mapstructure-to-hcl2 -type Config package main @@ -7,6 +7,7 @@ import ( "context" "encoding/json" "fmt" + "log" "os" "path/filepath" "strings" @@ -46,7 +47,7 @@ var guestOSTypeConfigs = map[string]guestOSTypeConfig{ } // Config struct containing variables -type ProvisionerConfig struct { +type Config struct { common.PackerConfig `mapstructure:",squash"` Version string `mapstructure:"version"` @@ -55,15 +56,16 @@ type ProvisionerConfig struct { InstallCommand string `mapstructure:"install_command"` StagingDir string `mapstructure:"staging_dir"` PreventSudo bool `mapstructure:"prevent_sudo"` - Variables map[string]interface{} - GuestOSType string `mapstructure:"guest_os_type"` + + Variables map[string]interface{} `mapstructure:"variables" mapstructure-to-hcl2:",skip"` + GuestOSType string `mapstructure:"guest_os_type"` ctx interpolate.Context } // Provisioner is the interface to install and run Terraform type Provisioner struct { - config ProvisionerConfig + config Config guestOSTypeConfig guestOSTypeConfig guestCommands *guestexec.GuestCommands } @@ -99,6 +101,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { p.guestOSTypeConfig = guestOSTypeConfigs[p.config.GuestOSType] if p.config.StagingDir == "" { + log.Println(fmt.Sprintf("Setting default StagingDir: %s", p.guestOSTypeConfig.stagingDir)) p.config.StagingDir = p.guestOSTypeConfig.stagingDir } @@ -112,14 +115,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { if err != nil { return fmt.Errorf("unable to fetch Terraform Version %s", err) } + log.Println(fmt.Sprintf("Setting default Terraform Version: %s", tfVer)) p.config.Version = tfVer } if p.config.InstallCommand == "" { + log.Println(fmt.Sprintf("Setting default InstallCommand: %s", p.guestOSTypeConfig.installCommand)) p.config.InstallCommand = p.guestOSTypeConfig.installCommand } if p.config.RunCommand == "" { + log.Println(fmt.Sprintf("Setting default RunCommand: %s", p.guestOSTypeConfig.runCommand)) p.config.RunCommand = p.guestOSTypeConfig.runCommand } @@ -159,6 +165,7 @@ func (p *Provisioner) Provision(_ context.Context, ui packer.Ui, comm packer.Com if err != nil { return fmt.Errorf("Error rendering Template: %s", err) } + log.Println(fmt.Sprintf("Executing command: %s", command)) if err := p.runCommand(ui, comm, command); err != nil { return fmt.Errorf("Error running Terraform: %s", err) } @@ -168,6 +175,7 @@ func (p *Provisioner) Provision(_ context.Context, ui packer.Ui, comm packer.Com if err != nil { return fmt.Errorf("Error rendering Template: %s", err) } + log.Println(fmt.Sprintf("Executing command: %s", command)) if err := p.runCommand(ui, comm, command); err != nil { return fmt.Errorf("Error installing Terraform: %s", err) } @@ -254,6 +262,7 @@ func (p *Provisioner) createTfvars(ui packer.Ui, comm packer.Communicator) error if err != nil { return err } + log.Println(fmt.Sprintf("Templated Variables: %s", tfvarsData)) // Upload the bytes remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "terraform.auto.tfvars")) diff --git a/provisioner.hcl2spec.go b/provisioner.hcl2spec.go new file mode 100644 index 0000000..f64e546 --- /dev/null +++ b/provisioner.hcl2spec.go @@ -0,0 +1,59 @@ +// Code generated by "mapstructure-to-hcl2 -type Config"; DO NOT EDIT. + +package main + +import ( + "github.com/hashicorp/hcl/v2/hcldec" + "github.com/zclconf/go-cty/cty" +) + +// FlatConfig is an auto-generated flat version of Config. +// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. +type FlatConfig struct { + PackerBuildName *string `mapstructure:"packer_build_name" cty:"packer_build_name" hcl:"packer_build_name"` + PackerBuilderType *string `mapstructure:"packer_builder_type" cty:"packer_builder_type" hcl:"packer_builder_type"` + PackerCoreVersion *string `mapstructure:"packer_core_version" cty:"packer_core_version" hcl:"packer_core_version"` + PackerDebug *bool `mapstructure:"packer_debug" cty:"packer_debug" hcl:"packer_debug"` + PackerForce *bool `mapstructure:"packer_force" cty:"packer_force" hcl:"packer_force"` + PackerOnError *string `mapstructure:"packer_on_error" cty:"packer_on_error" hcl:"packer_on_error"` + PackerUserVars map[string]string `mapstructure:"packer_user_variables" cty:"packer_user_variables" hcl:"packer_user_variables"` + PackerSensitiveVars []string `mapstructure:"packer_sensitive_variables" cty:"packer_sensitive_variables" hcl:"packer_sensitive_variables"` + Version *string `mapstructure:"version" cty:"version" hcl:"version"` + CodePath *string `mapstructure:"code_path" cty:"code_path" hcl:"code_path"` + RunCommand *string `mapstructure:"run_command" cty:"run_command" hcl:"run_command"` + 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"` + GuestOSType *string `mapstructure:"guest_os_type" cty:"guest_os_type" hcl:"guest_os_type"` +} + +// FlatMapstructure returns a new FlatConfig. +// FlatConfig is an auto-generated flat version of Config. +// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up. +func (*Config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { + return new(FlatConfig) +} + +// HCL2Spec returns the hcl spec of a Config. +// This spec is used by HCL to read the fields of Config. +// The decoded values from this spec will then be applied to a FlatConfig. +func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { + s := map[string]hcldec.Spec{ + "packer_build_name": &hcldec.AttrSpec{Name: "packer_build_name", Type: cty.String, Required: false}, + "packer_builder_type": &hcldec.AttrSpec{Name: "packer_builder_type", Type: cty.String, Required: false}, + "packer_core_version": &hcldec.AttrSpec{Name: "packer_core_version", Type: cty.String, Required: false}, + "packer_debug": &hcldec.AttrSpec{Name: "packer_debug", Type: cty.Bool, Required: false}, + "packer_force": &hcldec.AttrSpec{Name: "packer_force", Type: cty.Bool, Required: false}, + "packer_on_error": &hcldec.AttrSpec{Name: "packer_on_error", Type: cty.String, Required: false}, + "packer_user_variables": &hcldec.AttrSpec{Name: "packer_user_variables", Type: cty.Map(cty.String), Required: false}, + "packer_sensitive_variables": &hcldec.AttrSpec{Name: "packer_sensitive_variables", Type: cty.List(cty.String), Required: false}, + "version": &hcldec.AttrSpec{Name: "version", Type: cty.String, Required: false}, + "code_path": &hcldec.AttrSpec{Name: "code_path", Type: cty.String, Required: false}, + "run_command": &hcldec.AttrSpec{Name: "run_command", Type: cty.String, Required: false}, + "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}, + "guest_os_type": &hcldec.AttrSpec{Name: "guest_os_type", Type: cty.String, Required: false}, + } + return s +} diff --git a/provisionerconfig.hcl2spec.go b/provisionerconfig.hcl2spec.go deleted file mode 100644 index 6037d28..0000000 --- a/provisionerconfig.hcl2spec.go +++ /dev/null @@ -1,61 +0,0 @@ -// Code generated by "mapstructure-to-hcl2 -type ProvisionerConfig"; DO NOT EDIT. - -package main - -import ( - "github.com/hashicorp/hcl/v2/hcldec" - "github.com/zclconf/go-cty/cty" -) - -// FlatProvisionerConfig is an auto-generated flat version of ProvisionerConfig. -// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. -type FlatProvisionerConfig struct { - PackerBuildName *string `mapstructure:"packer_build_name" cty:"packer_build_name" hcl:"packer_build_name"` - PackerBuilderType *string `mapstructure:"packer_builder_type" cty:"packer_builder_type" hcl:"packer_builder_type"` - PackerCoreVersion *string `mapstructure:"packer_core_version" cty:"packer_core_version" hcl:"packer_core_version"` - PackerDebug *bool `mapstructure:"packer_debug" cty:"packer_debug" hcl:"packer_debug"` - PackerForce *bool `mapstructure:"packer_force" cty:"packer_force" hcl:"packer_force"` - PackerOnError *string `mapstructure:"packer_on_error" cty:"packer_on_error" hcl:"packer_on_error"` - PackerUserVars map[string]string `mapstructure:"packer_user_variables" cty:"packer_user_variables" hcl:"packer_user_variables"` - PackerSensitiveVars []string `mapstructure:"packer_sensitive_variables" cty:"packer_sensitive_variables" hcl:"packer_sensitive_variables"` - Version *string `mapstructure:"version" cty:"version" hcl:"version"` - CodePath *string `mapstructure:"code_path" cty:"code_path" hcl:"code_path"` - RunCommand *string `mapstructure:"run_command" cty:"run_command" hcl:"run_command"` - 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"` - Variables map[string]interface{} `cty:"variables" hcl:"variables"` - GuestOSType *string `mapstructure:"guest_os_type" cty:"guest_os_type" hcl:"guest_os_type"` -} - -// FlatMapstructure returns a new FlatProvisionerConfig. -// FlatProvisionerConfig is an auto-generated flat version of ProvisionerConfig. -// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up. -func (*ProvisionerConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { - return new(FlatProvisionerConfig) -} - -// HCL2Spec returns the hcl spec of a ProvisionerConfig. -// This spec is used by HCL to read the fields of ProvisionerConfig. -// The decoded values from this spec will then be applied to a FlatProvisionerConfig. -func (*FlatProvisionerConfig) HCL2Spec() map[string]hcldec.Spec { - s := map[string]hcldec.Spec{ - "packer_build_name": &hcldec.AttrSpec{Name: "packer_build_name", Type: cty.String, Required: false}, - "packer_builder_type": &hcldec.AttrSpec{Name: "packer_builder_type", Type: cty.String, Required: false}, - "packer_core_version": &hcldec.AttrSpec{Name: "packer_core_version", Type: cty.String, Required: false}, - "packer_debug": &hcldec.AttrSpec{Name: "packer_debug", Type: cty.Bool, Required: false}, - "packer_force": &hcldec.AttrSpec{Name: "packer_force", Type: cty.Bool, Required: false}, - "packer_on_error": &hcldec.AttrSpec{Name: "packer_on_error", Type: cty.String, Required: false}, - "packer_user_variables": &hcldec.AttrSpec{Name: "packer_user_variables", Type: cty.Map(cty.String), Required: false}, - "packer_sensitive_variables": &hcldec.AttrSpec{Name: "packer_sensitive_variables", Type: cty.List(cty.String), Required: false}, - "version": &hcldec.AttrSpec{Name: "version", Type: cty.String, Required: false}, - "code_path": &hcldec.AttrSpec{Name: "code_path", Type: cty.String, Required: false}, - "run_command": &hcldec.AttrSpec{Name: "run_command", Type: cty.String, Required: false}, - "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}, - "variables": &hcldec.AttrSpec{Name: "variables", Type: cty.Map(cty.String), Required: false}, - "guest_os_type": &hcldec.AttrSpec{Name: "guest_os_type", Type: cty.String, Required: false}, - } - return s -}