github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/common/lin/step_generalize_os.go (about)

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