summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitchellh/packer/builder/azure/common/lin/step_generalize_os.go
blob: c4dacc47fd7277201e7daa3c65fcb3a9dc592180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package lin

import (
	"bytes"
	"fmt"
	"github.com/hashicorp/packer/packer"
	"github.com/mitchellh/multistep"
	"log"
)

type StepGeneralizeOS struct {
	Command string
}

func (s *StepGeneralizeOS) Run(state multistep.StateBag) multistep.StepAction {
	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 {
		err = fmt.Errorf("Failed executing OS generalization command: %s", err)
		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
}