2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/compute"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-05-18 20:25:57 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepCaptureImage struct {
|
2017-05-29 00:06:09 -04:00
|
|
|
client *AzureClient
|
|
|
|
generalizeVM func(resourceGroupName, computeName string) error
|
|
|
|
captureVhd func(resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters, cancelCh <-chan struct{}) error
|
|
|
|
captureManagedImage func(resourceGroupName string, computeName string, parameters *compute.Image, cancelCh <-chan struct{}) error
|
|
|
|
get func(client *AzureClient) *CaptureTemplate
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStepCaptureImage(client *AzureClient, ui packer.Ui) *StepCaptureImage {
|
|
|
|
var step = &StepCaptureImage{
|
|
|
|
client: client,
|
2017-05-29 00:06:09 -04:00
|
|
|
get: func(client *AzureClient) *CaptureTemplate {
|
|
|
|
return client.Template
|
|
|
|
},
|
|
|
|
say: func(message string) {
|
|
|
|
ui.Say(message)
|
|
|
|
},
|
|
|
|
error: func(e error) {
|
|
|
|
ui.Error(e.Error())
|
|
|
|
},
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2017-05-29 00:06:09 -04:00
|
|
|
step.generalizeVM = step.generalize
|
|
|
|
step.captureVhd = step.captureImage
|
|
|
|
step.captureManagedImage = step.captureImageFromVM
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2017-05-29 00:06:09 -04:00
|
|
|
func (s *StepCaptureImage) generalize(resourceGroupName string, computeName string) error {
|
2016-04-21 19:50:03 -04:00
|
|
|
_, err := s.client.Generalize(resourceGroupName, computeName)
|
2017-06-08 20:57:59 -04:00
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
}
|
2017-05-29 00:06:09 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-05-28 03:38:45 -04:00
|
|
|
|
2017-05-29 00:06:09 -04:00
|
|
|
func (s *StepCaptureImage) captureImageFromVM(resourceGroupName string, imageName string, image *compute.Image, cancelCh <-chan struct{}) error {
|
|
|
|
_, errChan := s.client.ImagesClient.CreateOrUpdate(resourceGroupName, imageName, *image, cancelCh)
|
2017-06-08 20:57:59 -04:00
|
|
|
err := <-errChan
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
}
|
2017-05-29 00:06:09 -04:00
|
|
|
return <-errChan
|
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-05-29 00:06:09 -04:00
|
|
|
func (s *StepCaptureImage) captureImage(resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters, cancelCh <-chan struct{}) error {
|
|
|
|
_, errChan := s.client.Capture(resourceGroupName, computeName, *parameters, cancelCh)
|
2017-06-08 20:57:59 -04:00
|
|
|
err := <-errChan
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
}
|
2017-05-29 00:06:09 -04:00
|
|
|
return <-errChan
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCaptureImage) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
s.say("Capturing image ...")
|
|
|
|
|
|
|
|
var computeName = state.Get(constants.ArmComputeName).(string)
|
2017-05-30 03:33:54 -04:00
|
|
|
var location = state.Get(constants.ArmLocation).(string)
|
2016-03-04 05:14:55 -05:00
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
2017-05-29 00:06:09 -04:00
|
|
|
var vmCaptureParameters = state.Get(constants.ArmVirtualMachineCaptureParameters).(*compute.VirtualMachineCaptureParameters)
|
|
|
|
var imageParameters = state.Get(constants.ArmImageParameters).(*compute.Image)
|
|
|
|
|
|
|
|
var isManagedImage = state.Get(constants.ArmIsManagedImage).(bool)
|
2017-05-30 14:25:46 -04:00
|
|
|
var targetManagedImageResourceGroupName = state.Get(constants.ArmManagedImageResourceGroupName).(string)
|
|
|
|
var targetManagedImageName = state.Get(constants.ArmManagedImageName).(string)
|
|
|
|
var targetManagedImageLocation = state.Get(constants.ArmManagedImageLocation).(string)
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-05-30 03:33:54 -04:00
|
|
|
s.say(fmt.Sprintf(" -> Compute ResourceGroupName : '%s'", resourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> Compute Name : '%s'", computeName))
|
|
|
|
s.say(fmt.Sprintf(" -> Compute Location : '%s'", location))
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
result := common.StartInterruptibleTask(
|
2017-05-29 00:06:09 -04:00
|
|
|
func() bool {
|
|
|
|
return common.IsStateCancelled(state)
|
|
|
|
},
|
2016-04-21 19:50:03 -04:00
|
|
|
func(cancelCh <-chan struct{}) error {
|
2017-05-29 00:06:09 -04:00
|
|
|
err := s.generalizeVM(resourceGroupName, computeName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isManagedImage {
|
2017-05-30 03:33:54 -04:00
|
|
|
s.say(fmt.Sprintf(" -> Image ResourceGroupName : '%s'", targetManagedImageResourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> Image Name : '%s'", targetManagedImageName))
|
|
|
|
s.say(fmt.Sprintf(" -> Image Location : '%s'", targetManagedImageLocation))
|
2017-05-29 00:06:09 -04:00
|
|
|
return s.captureManagedImage(targetManagedImageResourceGroupName, targetManagedImageName, imageParameters, cancelCh)
|
|
|
|
} else {
|
|
|
|
return s.captureVhd(resourceGroupName, computeName, vmCaptureParameters, cancelCh)
|
|
|
|
}
|
2016-04-21 19:50:03 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
}
|