github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_capture_image.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/Azure/azure-sdk-for-go/arm/compute" 10 "github.com/mitchellh/multistep" 11 "github.com/mitchellh/packer/builder/azure/common" 12 "github.com/mitchellh/packer/builder/azure/common/constants" 13 "github.com/mitchellh/packer/packer" 14 ) 15 16 type StepCaptureImage struct { 17 client *AzureClient 18 capture func(resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters, cancelCh <-chan struct{}) error 19 get func(client *AzureClient) *CaptureTemplate 20 say func(message string) 21 error func(e error) 22 } 23 24 func NewStepCaptureImage(client *AzureClient, ui packer.Ui) *StepCaptureImage { 25 var step = &StepCaptureImage{ 26 client: client, 27 get: func(client *AzureClient) *CaptureTemplate { return client.Template }, 28 say: func(message string) { ui.Say(message) }, 29 error: func(e error) { ui.Error(e.Error()) }, 30 } 31 32 step.capture = step.captureImage 33 return step 34 } 35 36 func (s *StepCaptureImage) captureImage(resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters, cancelCh <-chan struct{}) error { 37 _, err := s.client.Generalize(resourceGroupName, computeName) 38 if err != nil { 39 return err 40 } 41 42 _, err = s.client.Capture(resourceGroupName, computeName, *parameters, cancelCh) 43 if err != nil { 44 return err 45 } 46 47 return nil 48 } 49 50 func (s *StepCaptureImage) Run(state multistep.StateBag) multistep.StepAction { 51 s.say("Capturing image ...") 52 53 var computeName = state.Get(constants.ArmComputeName).(string) 54 var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) 55 var parameters = state.Get(constants.ArmVirtualMachineCaptureParameters).(*compute.VirtualMachineCaptureParameters) 56 57 s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) 58 s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName)) 59 60 result := common.StartInterruptibleTask( 61 func() bool { return common.IsStateCancelled(state) }, 62 func(cancelCh <-chan struct{}) error { 63 return s.capture(resourceGroupName, computeName, parameters, cancelCh) 64 }) 65 66 // HACK(chrboum): I do not like this. The capture method should be returning this value 67 // instead having to pass in another lambda. I'm in this pickle because I am using 68 // common.StartInterruptibleTask which is not parametric, and only returns a type of error. 69 // I could change it to interface{}, but I do not like that solution either. 70 // 71 // Having to resort to capturing the template via an inspector is hack, and once I can 72 // resolve that I can cleanup this code too. See the comments in azure_client.go for more 73 // details. 74 template := s.get(s.client) 75 state.Put(constants.ArmCaptureTemplate, template) 76 77 return processInterruptibleResult(result, s.error, state) 78 } 79 80 func (*StepCaptureImage) Cleanup(multistep.StateBag) { 81 }