2016-03-04 05:14:55 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2016-05-06 23:32:18 -04:00
|
|
|
// Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-05-18 20:25:57 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepPowerOffCompute struct {
|
|
|
|
client *AzureClient
|
2016-04-21 19:50:03 -04:00
|
|
|
powerOff func(resourceGroupName string, computeName string, cancelCh <-chan struct{}) error
|
2016-03-04 05:14:55 -05:00
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStepPowerOffCompute(client *AzureClient, ui packer.Ui) *StepPowerOffCompute {
|
|
|
|
var step = &StepPowerOffCompute{
|
|
|
|
client: client,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.powerOff = step.powerOffCompute
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (s *StepPowerOffCompute) powerOffCompute(resourceGroupName string, computeName string, cancelCh <-chan struct{}) error {
|
|
|
|
_, err := s.client.PowerOff(resourceGroupName, computeName, cancelCh)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepPowerOffCompute) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
s.say("Powering off machine ...")
|
|
|
|
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
|
|
var computeName = state.Get(constants.ArmComputeName).(string)
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName))
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
result := common.StartInterruptibleTask(
|
|
|
|
func() bool { return common.IsStateCancelled(state) },
|
|
|
|
func(cancelCh <-chan struct{}) error { return s.powerOff(resourceGroupName, computeName, cancelCh) })
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
return processInterruptibleResult(result, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepPowerOffCompute) Cleanup(multistep.StateBag) {
|
|
|
|
}
|