2016-03-04 05:14:55 -05:00
|
|
|
package lin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-03-04 05:14:55 -05:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
"log"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepGeneralizeOS struct {
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepGeneralizeOS) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-03-04 05:14:55 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
|
|
|
|
|
|
|
ui.Say("Executing OS generalization...")
|
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: s.Command,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
2016-07-16 01:23:53 -04:00
|
|
|
err = fmt.Errorf("Failed executing OS generalization command: %s", err)
|
2016-03-04 05:14:55 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the command to run
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
// If the command failed to run, notify the user in some way.
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
"OS generalization has non-zero exit status.\n\nStdout: %s\n\nStderr: %s",
|
|
|
|
stdout.String(), stderr.String()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("OS generalization stdout: %s", stdout.String())
|
|
|
|
log.Printf("OS generalization stderr: %s", stderr.String())
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepGeneralizeOS) Cleanup(state multistep.StateBag) {
|
|
|
|
// do nothing
|
|
|
|
}
|