2015-09-02 06:29:26 -04:00
|
|
|
package shell_local
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2018-02-23 16:26:31 -05:00
|
|
|
sl "github.com/hashicorp/packer/common/shell-local"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-09-02 06:29:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
2018-02-27 15:50:42 -05:00
|
|
|
config sl.Config
|
2015-09-02 06:29:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteCommandTemplate struct {
|
2016-11-21 08:39:34 -05:00
|
|
|
Vars string
|
|
|
|
Script string
|
2015-09-02 06:29:26 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2015-09-02 06:29:26 -04:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2018-02-28 17:35:42 -05:00
|
|
|
err := sl.Decode(&p.config, raws...)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-02 15:32:34 -05:00
|
|
|
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.
|
2018-03-01 11:48:21 -05:00
|
|
|
p.config.ExecuteCommand = append([]string{"sh", "-c"}, p.config.ExecuteCommand...)
|
|
|
|
}
|
2015-09-02 06:29:26 -04:00
|
|
|
|
2018-02-27 15:50:42 -05:00
|
|
|
return sl.Validate(&p.config)
|
2015-09-02 06:29:26 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
|
2020-01-30 05:27:58 -05:00
|
|
|
generatedData := make(map[string]interface{})
|
2020-06-29 07:44:57 -04:00
|
|
|
artifactStateData := artifact.State("generated_data")
|
|
|
|
if artifactStateData != nil {
|
|
|
|
for k, v := range artifactStateData.(map[interface{}]interface{}) {
|
2020-01-30 05:27:58 -05:00
|
|
|
generatedData[k.(string)] = v
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 06:29:26 -04:00
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
success, retErr := sl.Run(ctx, ui, &p.config, generatedData)
|
2019-04-03 15:03:40 -04:00
|
|
|
if !success {
|
|
|
|
return nil, false, false, retErr
|
2017-01-23 17:15:51 -05:00
|
|
|
}
|
|
|
|
|
2019-04-03 15:03:40 -04:00
|
|
|
// Force shell-local pp to keep the input artifact, because otherwise we'll
|
2019-08-05 08:47:41 -04:00
|
|
|
// lose it instead of being able to pass it through. If you want to delete
|
2019-04-03 15:03:40 -04:00
|
|
|
// the input artifact for a shell local pp, use the artifice pp to create a
|
|
|
|
// new artifact
|
|
|
|
return artifact, true, true, retErr
|
2017-01-23 17:15:51 -05:00
|
|
|
}
|