2020-01-06 16:36:49 -05:00
|
|
|
package dtl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-01-06 16:36:49 -05:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepPowerOffCompute struct {
|
|
|
|
client *AzureClient
|
|
|
|
config *Config
|
|
|
|
powerOff func(ctx context.Context, resourceGroupName string, labName, computeName string) error
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func NewStepPowerOffCompute(client *AzureClient, ui packersdk.Ui, config *Config) *StepPowerOffCompute {
|
2020-01-06 16:36:49 -05:00
|
|
|
|
|
|
|
var step = &StepPowerOffCompute{
|
|
|
|
client: client,
|
|
|
|
config: config,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.powerOff = step.powerOffCompute
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepPowerOffCompute) powerOffCompute(ctx context.Context, resourceGroupName string, labName, computeName string) error {
|
|
|
|
//f, err := s.client.VirtualMachinesClient.Deallocate(ctx, resourceGroupName, computeName)
|
|
|
|
f, err := s.client.DtlVirtualMachineClient.Stop(ctx, resourceGroupName, labName, computeName)
|
|
|
|
if err == nil {
|
|
|
|
err = f.WaitForCompletionRef(ctx, s.client.DtlVirtualMachineClient.Client)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepPowerOffCompute) Run(ctx context.Context, 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))
|
|
|
|
|
2020-03-30 20:15:58 -04:00
|
|
|
err := s.powerOff(ctx, s.config.LabResourceGroupName, s.config.LabName, computeName)
|
2020-01-06 16:36:49 -05:00
|
|
|
|
|
|
|
s.say("Powering off machine ...Complete")
|
|
|
|
return processStepResult(err, s.error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepPowerOffCompute) Cleanup(multistep.StateBag) {
|
|
|
|
}
|