github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/azure/common/lin/step_generalize_os.go (about)

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