Skip to content

Commit

Permalink
Bump plugin sdk, and more code tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Mar 29, 2021
1 parent bebac9a commit 7cd7063
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"runtime"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
31 changes: 28 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
19 changes: 14 additions & 5 deletions provisioner.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate mapstructure-to-hcl2 -type ProvisionerConfig
//go:generate mapstructure-to-hcl2 -type Config

package main

Expand All @@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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"`
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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"))
Expand Down
59 changes: 59 additions & 0 deletions provisioner.hcl2spec.go

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

61 changes: 0 additions & 61 deletions provisionerconfig.hcl2spec.go

This file was deleted.

0 comments on commit 7cd7063

Please sign in to comment.