2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-03-04 05:14:55 -05:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
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 {
|
2017-05-28 03:38:45 -04:00
|
|
|
_, errChan := s.client.PowerOff(resourceGroupName, computeName, cancelCh)
|
|
|
|
|
|
|
|
err := <-errChan
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
2017-06-08 20:57:59 -04:00
|
|
|
s.say(s.client.LastError.Error())
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
2017-06-08 20:57:59 -04:00
|
|
|
return err
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepPowerOffCompute) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-03-04 05:14:55 -05:00
|
|
|
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) {
|
|
|
|
}
|