github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_power_off_compute.go (about) 1 package arm 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/hashicorp/packer/builder/azure/common/constants" 8 "github.com/hashicorp/packer/helper/multistep" 9 "github.com/hashicorp/packer/packer" 10 ) 11 12 type StepPowerOffCompute struct { 13 client *AzureClient 14 powerOff func(ctx context.Context, resourceGroupName string, computeName string) 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(ctx context.Context, resourceGroupName string, computeName string) error { 31 f, err := s.client.VirtualMachinesClient.PowerOff(ctx, resourceGroupName, computeName) 32 if err == nil { 33 err = f.WaitForCompletion(ctx, s.client.VirtualMachinesClient.Client) 34 } 35 if err != nil { 36 s.say(s.client.LastError.Error()) 37 } 38 return err 39 } 40 41 func (s *StepPowerOffCompute) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { 42 s.say("Powering off machine ...") 43 44 var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) 45 var computeName = state.Get(constants.ArmComputeName).(string) 46 47 s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) 48 s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName)) 49 50 err := s.powerOff(ctx, resourceGroupName, computeName) 51 52 return processStepResult(err, s.error, state) 53 } 54 55 func (*StepPowerOffCompute) Cleanup(multistep.StateBag) { 56 }