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"
|
2018-02-23 13:26:31 -08:00
|
|
|
sl "github.com/hashicorp/packer/common/shell-local"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-09-02 11:29:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
2018-02-27 12:50:42 -08:00
|
|
|
config sl.Config
|
2015-09-02 11:29:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteCommandTemplate struct {
|
2016-11-21 16:39:34 +03:00
|
|
|
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
|
|
|
|
}
|
2018-03-02 12:32:34 -08: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 08:48:21 -08:00
|
|
|
p.config.ExecuteCommand = append([]string{"sh", "-c"}, p.config.ExecuteCommand...)
|
|
|
|
}
|
2015-09-02 11:29:26 +01:00
|
|
|
|
2018-02-27 12:50:42 -08:00
|
|
|
return sl.Validate(&p.config)
|
2015-09-02 11:29:26 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 19:59:42 +02:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
|
2020-01-30 11:27:58 +01:00
|
|
|
generatedData := make(map[string]interface{})
|
|
|
|
artifactSateData := artifact.State("generated_data")
|
|
|
|
if artifactSateData != nil {
|
|
|
|
for k, v := range artifactSateData.(map[interface{}]interface{}) {
|
|
|
|
generatedData[k.(string)] = v
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 11:29:26 +01:00
|
|
|
|
2020-01-30 11:27:58 +01:00
|
|
|
success, retErr := sl.Run(ctx, ui, &p.config, generatedData)
|
2019-04-03 12:03:40 -07:00
|
|
|
if !success {
|
|
|
|
return nil, false, false, retErr
|
2017-01-23 22:15:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 12:03:40 -07:00
|
|
|
// 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
|
2019-04-03 12:03:40 -07: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 22:15:51 +00:00
|
|
|
}
|