2018-11-05 18:48:22 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-09 14:28:37 -05:00
|
|
|
"fmt"
|
2018-11-05 18:48:22 -05:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
|
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepSnapshotOSDisk struct {
|
2018-11-13 20:47:48 -05:00
|
|
|
client *AzureClient
|
|
|
|
create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
isManagedImage bool
|
2018-11-05 18:48:22 -05:00
|
|
|
}
|
|
|
|
|
2018-11-13 20:47:48 -05:00
|
|
|
func NewStepSnapshotOSDisk(client *AzureClient, ui packer.Ui, isManagedImage bool) *StepSnapshotOSDisk {
|
2018-11-05 18:48:22 -05:00
|
|
|
var step = &StepSnapshotOSDisk{
|
2018-11-13 20:47:48 -05:00
|
|
|
client: client,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
isManagedImage: isManagedImage,
|
2018-11-05 18:48:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
step.create = step.createSnapshot
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSnapshotOSDisk) createSnapshot(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error {
|
|
|
|
|
|
|
|
srcVhdToSnapshot := compute.Snapshot{
|
|
|
|
DiskProperties: &compute.DiskProperties{
|
|
|
|
CreationData: &compute.CreationData{
|
2018-11-08 22:30:57 -05:00
|
|
|
CreateOption: compute.Copy,
|
|
|
|
SourceResourceID: to.StringPtr(srcUriVhd),
|
2018-11-05 18:48:22 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Location: to.StringPtr(location),
|
|
|
|
Tags: tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := s.client.SnapshotsClient.CreateOrUpdate(ctx, resourceGroupName, dstSnapshotName, srcVhdToSnapshot)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
2018-11-09 14:28:37 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.WaitForCompletion(ctx, s.client.SnapshotsClient.Client)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
return err
|
2018-11-05 18:48:22 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 14:28:37 -05:00
|
|
|
createdSnapshot, err := f.Result(s.client.SnapshotsClient)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> Managed Image OS Disk Snapshot : '%s'", *(createdSnapshot.ID)))
|
|
|
|
|
|
|
|
return nil
|
2018-11-05 18:48:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction {
|
2018-11-13 20:47:48 -05:00
|
|
|
if s.isManagedImage {
|
2018-11-05 18:48:22 -05:00
|
|
|
|
2018-11-13 20:47:48 -05:00
|
|
|
s.say("Taking snapshot of OS disk ...")
|
2018-11-05 18:48:22 -05:00
|
|
|
|
2018-11-13 20:47:48 -05:00
|
|
|
var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string)
|
|
|
|
var location = stateBag.Get(constants.ArmLocation).(string)
|
|
|
|
var tags = stateBag.Get(constants.ArmTags).(map[string]*string)
|
|
|
|
var srcUriVhd = stateBag.Get(constants.ArmOSDiskVhd).(string)
|
|
|
|
var dstSnapshotName = stateBag.Get(constants.ArmManagedImageOSDiskSnapshotName).(string)
|
2018-11-05 18:48:22 -05:00
|
|
|
|
2018-11-13 20:47:48 -05:00
|
|
|
err := s.create(ctx, resourceGroupName, srcUriVhd, location, tags, dstSnapshotName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
stateBag.Put(constants.Error, err)
|
|
|
|
s.error(err)
|
2018-11-05 18:48:22 -05:00
|
|
|
|
2018-11-13 20:47:48 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-11-05 18:48:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepSnapshotOSDisk) Cleanup(multistep.StateBag) {
|
|
|
|
}
|