2020-11-04 18:44:05 -05:00
|
|
|
package packerbuilderdata
|
2020-01-20 10:29:38 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
import "github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-01-20 10:29:38 -05:00
|
|
|
|
2020-11-04 18:44:05 -05:00
|
|
|
// This is used in the BasicPlaceholderData() func in the packer/provisioner.go
|
|
|
|
// To force users to access generated data via the "generated" func.
|
|
|
|
const PlaceholderMsg = "To set this dynamically in the Packer template, " +
|
|
|
|
"you must use the `build` function"
|
|
|
|
|
2020-11-12 17:01:41 -05:00
|
|
|
// GeneratedData manages variables created and exported by a builder after
|
|
|
|
// it starts, so that provisioners and post-processors can have access to
|
|
|
|
// build data generated at runtime -- for example, instance ID or instance IP
|
|
|
|
// address. Internally, it uses the builder's multistep.StateBag. The user
|
|
|
|
// must make sure that the State field is not is not nil before calling Put().
|
2020-01-20 10:29:38 -05:00
|
|
|
type GeneratedData struct {
|
|
|
|
// The builder's StateBag
|
|
|
|
State multistep.StateBag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gd *GeneratedData) Put(key string, data interface{}) {
|
|
|
|
genData := make(map[string]interface{})
|
|
|
|
if _, ok := gd.State.GetOk("generated_data"); ok {
|
|
|
|
genData = gd.State.Get("generated_data").(map[string]interface{})
|
|
|
|
}
|
|
|
|
genData[key] = data
|
|
|
|
gd.State.Put("generated_data", genData)
|
|
|
|
}
|