Skip to content

Commit

Permalink
Add triggers and use -var for packer variables
Browse files Browse the repository at this point in the history
  • Loading branch information
simaotwx committed Nov 11, 2021
1 parent e420ca7 commit 5f2234f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
5 changes: 4 additions & 1 deletion examples/example.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# file is empty on purpose (example)

variable "test_var1" {
type = string
}
5 changes: 4 additions & 1 deletion examples/example2.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# file is empty on purpose (example2)

variable "test_var2" {
type = string
}
20 changes: 19 additions & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@ provider "packer" {}

data "packer_version" "version" {}

resource "packer_build" "build" {
resource "packer_build" "build1" {
file = "example.pkr.hcl"
variables = {
test_var1 = "test 1"
}

triggers = {
packer_version = data.packer_version.version.version
}
}

resource "packer_build" "build2" {
file = "example2.pkr.hcl"
variables = {
test_var2 = "test 2"
}

triggers = {
packer_version = data.packer_version.version.version
}
}

output "packer_version" {
Expand Down
27 changes: 20 additions & 7 deletions provider/resource_packer_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import (
"context"
"os"

"terraform-provider-packer/crypto_util"
"terraform-provider-packer/funcs"

"github.com/pkg/errors"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/toowoxx/go-lib-userspace-common/cmds"
"terraform-provider-packer/crypto_util"
"terraform-provider-packer/funcs"
)

type resourceBuildType struct {
Expand All @@ -24,6 +27,7 @@ type resourceBuildType struct {
FileDependencies []string `tfsdk:"file_dependencies"`
FileDependenciesHash types.String `tfsdk:"file_dependencies_hash"`
Environment map[string]string `tfsdk:"environment"`
Triggers map[string]string `tfsdk:"triggers"`
}

func (r resourceBuildType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
Expand Down Expand Up @@ -73,6 +77,11 @@ func (r resourceBuildType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diag
Type: types.MapType{ElemType: types.StringType},
Optional: true,
},
"triggers": {
Description: "Values that, when changed, trigger an update of this resource",
Type: types.MapType{ElemType: types.StringType},
Optional: true,
},
},
}, nil
}
Expand All @@ -96,20 +105,24 @@ func (r resourceBuild) packerBuild(resourceState *resourceBuildType) error {
if envVars == nil {
envVars = map[string]string{}
}
envVars["TPP_RUN_PACKER"] = "true"

params := []string{"build"}
for key, value := range resourceState.Variables {
envVars["PKR_VAR_"+key] = value
params = append(params, "-var", key+"="+value)
}
envVars["TPP_RUN_PACKER"] = "true"
params = append(params, resourceState.File.Value)
params = append(params, resourceState.AdditionalParams...)

exe, _ := os.Executable()

err := cmds.RunCommandWithEnv(
output, err := cmds.RunCommandWithEnvReturnOutput(
exe,
envVars,
append([]string{"build", resourceState.File.Value}, resourceState.AdditionalParams...)...,
params...,
)
if err != nil {
return err
return errors.Wrap(err, "could not run packer command; output: "+string(output))
}

return nil
Expand Down

0 comments on commit 5f2234f

Please sign in to comment.