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