github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/common/lin/step_generalize_os.go (about)

     1  package lin
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/hashicorp/packer/packer"
     7  	"github.com/mitchellh/multistep"
     8  	"log"
     9  )
    10  
    11  type StepGeneralizeOS struct {
    12  	Command string
    13  }
    14  
    15  func (s *StepGeneralizeOS) Run(state multistep.StateBag) multistep.StepAction {
    16  	ui := state.Get("ui").(packer.Ui)
    17  	comm := state.Get("communicator").(packer.Communicator)
    18  
    19  	ui.Say("Executing OS generalization...")
    20  
    21  	var stdout, stderr bytes.Buffer
    22  	cmd := &packer.RemoteCmd{
    23  		Command: s.Command,
    24  		Stdout:  &stdout,
    25  		Stderr:  &stderr,
    26  	}
    27  
    28  	if err := comm.Start(cmd); err != nil {
    29  		err = fmt.Errorf("Failed executing OS generalization command: %s", err)
    30  		state.Put("error", err)
    31  		ui.Error(err.Error())
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	// Wait for the command to run
    36  	cmd.Wait()
    37  
    38  	// If the command failed to run, notify the user in some way.
    39  	if cmd.ExitStatus != 0 {
    40  		state.Put("error", fmt.Errorf(
    41  			"OS generalization has non-zero exit status.\n\nStdout: %s\n\nStderr: %s",
    42  			stdout.String(), stderr.String()))
    43  		return multistep.ActionHalt
    44  	}
    45  
    46  	log.Printf("OS generalization stdout: %s", stdout.String())
    47  	log.Printf("OS generalization stderr: %s", stderr.String())
    48  
    49  	return multistep.ActionContinue
    50  }
    51  
    52  func (s *StepGeneralizeOS) Cleanup(state multistep.StateBag) {
    53  	// do nothing
    54  }