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