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"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/compute"
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/packer/builder/azure/common"
|
2016-03-10 20:46:22 -05:00
|
|
|
"github.com/mitchellh/packer/builder/azure/common/constants"
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepCaptureImage struct {
|
|
|
|
client *AzureClient
|
2016-04-21 19:50:03 -04:00
|
|
|
capture func(resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters, cancelCh <-chan struct{}) error
|
|
|
|
get func(client *AzureClient) *CaptureTemplate
|
2016-03-04 05:14:55 -05:00
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStepCaptureImage(client *AzureClient, ui packer.Ui) *StepCaptureImage {
|
|
|
|
var step = &StepCaptureImage{
|
|
|
|
client: client,
|
2016-04-21 19:50:03 -04:00
|
|
|
get: func(client *AzureClient) *CaptureTemplate { return client.Template },
|
2016-03-04 05:14:55 -05:00
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.capture = step.captureImage
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (s *StepCaptureImage) captureImage(resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters, cancelCh <-chan struct{}) error {
|
|
|
|
_, err := s.client.Generalize(resourceGroupName, computeName)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
_, err = s.client.Capture(resourceGroupName, computeName, *parameters, cancelCh)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCaptureImage) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
s.say("Capturing image ...")
|
|
|
|
|
|
|
|
var computeName = state.Get(constants.ArmComputeName).(string)
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
|
|
var parameters = state.Get(constants.ArmVirtualMachineCaptureParameters).(*compute.VirtualMachineCaptureParameters)
|
|
|
|
|
|
|
|
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.capture(resourceGroupName, computeName, parameters, cancelCh)
|
|
|
|
})
|
|
|
|
|
|
|
|
// HACK(chrboum): I do not like this. The capture method should be returning this value
|
|
|
|
// instead having to pass in another lambda. I'm in this pickle because I am using
|
|
|
|
// common.StartInterruptibleTask which is not parametric, and only returns a type of error.
|
|
|
|
// I could change it to interface{}, but I do not like that solution either.
|
|
|
|
//
|
|
|
|
// Having to resort to capturing the template via an inspector is hack, and once I can
|
|
|
|
// resolve that I can cleanup this code too. See the comments in azure_client.go for more
|
|
|
|
// details.
|
|
|
|
template := s.get(s.client)
|
|
|
|
state.Put(constants.ArmCaptureTemplate, template)
|
|
|
|
|
|
|
|
return processInterruptibleResult(result, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepCaptureImage) Cleanup(multistep.StateBag) {
|
|
|
|
}
|