github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_power_off_compute.go (about)

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/builder/azure/common"
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/mitchellh/multistep"
    10  )
    11  
    12  type StepPowerOffCompute struct {
    13  	client   *AzureClient
    14  	powerOff func(resourceGroupName string, computeName string, cancelCh <-chan struct{}) error
    15  	say      func(message string)
    16  	error    func(e error)
    17  }
    18  
    19  func NewStepPowerOffCompute(client *AzureClient, ui packer.Ui) *StepPowerOffCompute {
    20  	var step = &StepPowerOffCompute{
    21  		client: client,
    22  		say:    func(message string) { ui.Say(message) },
    23  		error:  func(e error) { ui.Error(e.Error()) },
    24  	}
    25  
    26  	step.powerOff = step.powerOffCompute
    27  	return step
    28  }
    29  
    30  func (s *StepPowerOffCompute) powerOffCompute(resourceGroupName string, computeName string, cancelCh <-chan struct{}) error {
    31  	_, errChan := s.client.PowerOff(resourceGroupName, computeName, cancelCh)
    32  
    33  	err := <-errChan
    34  	if err != nil {
    35  		s.say(s.client.LastError.Error())
    36  	}
    37  	return err
    38  }
    39  
    40  func (s *StepPowerOffCompute) Run(state multistep.StateBag) multistep.StepAction {
    41  	s.say("Powering off machine ...")
    42  
    43  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    44  	var computeName = state.Get(constants.ArmComputeName).(string)
    45  
    46  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    47  	s.say(fmt.Sprintf(" -> ComputeName       : '%s'", computeName))
    48  
    49  	result := common.StartInterruptibleTask(
    50  		func() bool { return common.IsStateCancelled(state) },
    51  		func(cancelCh <-chan struct{}) error { return s.powerOff(resourceGroupName, computeName, cancelCh) })
    52  
    53  	return processInterruptibleResult(result, s.error, state)
    54  }
    55  
    56  func (*StepPowerOffCompute) Cleanup(multistep.StateBag) {
    57  }