Adrien Delorme f555e7a9f2 allow a provisioner to timeout
* I had to contextualise Communicator.Start and RemoteCmd.StartWithUi
NOTE: Communicator.Start starts a RemoteCmd but RemoteCmd.StartWithUi will run the cmd and wait for a return, so I renamed StartWithUi to RunWithUi so that the intent is clearer.
Ideally in the future RunWithUi will be named back to StartWithUi and the exit status or wait funcs of the command will allow to wait for a return. If you do so please read carrefully https://golang.org/pkg/os/exec/#Cmd.Stdout to avoid a deadlock
* cmd.ExitStatus to cmd.ExitStatus() is now blocking to avoid race conditions
* also had to simplify StartWithUi
2019-04-08 20:09:21 +02:00

55 lines
1.7 KiB
Go

package shell_local
import (
"context"
sl "github.com/hashicorp/packer/common/shell-local"
"github.com/hashicorp/packer/packer"
)
type PostProcessor struct {
config sl.Config
}
type ExecuteCommandTemplate struct {
Vars string
Script string
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := sl.Decode(&p.config, raws...)
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...)
}
return sl.Validate(&p.config)
}
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
// this particular post-processor doesn't do anything with the artifact
// except to return it.
success, retErr := sl.Run(ctx, ui, &p.config)
if !success {
return nil, false, false, retErr
}
// Force shell-local pp to keep the input artifact, because otherwise we'll
// lose it instead of being able to pass it through. If oyu 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
}