Files

63 lines
2.0 KiB
Go
Raw Permalink Normal View History

2015-09-02 11:29:26 +01:00
package shell_local
import (
2019-03-22 14:56:02 +01:00
"context"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2020-12-17 13:29:25 -08:00
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
sl "github.com/hashicorp/packer-plugin-sdk/shell-local"
2015-09-02 11:29:26 +01:00
)
type PostProcessor struct {
config sl.Config
2015-09-02 11:29:26 +01:00
}
type ExecuteCommandTemplate struct {
Vars string
Script string
2015-09-02 11:29:26 +01:00
}
2019-12-17 11:25:56 +01:00
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
2015-09-02 11:29:26 +01:00
func (p *PostProcessor) Configure(raws ...interface{}) error {
2018-02-28 14:35:42 -08:00
err := sl.Decode(&p.config, raws...)
2015-09-02 11:29:26 +01:00
if err != nil {
return err
}
if len(p.config.ExecuteCommand) == 1 {
// Backwards compatibility -- before we merged the shell-local
// post-processor and provisioners, the post-processor accepted
// execute_command as a string rather than a slice of strings. It didn't
// have a configurable call to shell program, automatically prepending
// the user-supplied execute_command string with "sh -c". If users are
// still using the old way of defining ExecuteCommand (by supplying a
// single string rather than a slice of strings) then we need to
// prepend this command with the call that the post-processor defaulted
// to before.
p.config.ExecuteCommand = append([]string{"sh", "-c"}, p.config.ExecuteCommand...)
}
2015-09-02 11:29:26 +01:00
return sl.Validate(&p.config)
2015-09-02 11:29:26 +01:00
}
2020-11-19 12:17:11 -08:00
func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, bool, error) {
generatedData := make(map[string]interface{})
artifactStateData := artifact.State("generated_data")
if artifactStateData != nil {
for k, v := range artifactStateData.(map[interface{}]interface{}) {
generatedData[k.(string)] = v
}
}
2015-09-02 11:29:26 +01:00
success, retErr := sl.Run(ctx, ui, &p.config, generatedData)
if !success {
return nil, false, false, retErr
}
// Force shell-local pp to keep the input artifact, because otherwise we'll
2019-08-05 13:47:41 +01:00
// lose it instead of being able to pass it through. If you want to delete
// the input artifact for a shell local pp, use the artifice pp to create a
// new artifact
return artifact, true, true, retErr
}