2018-11-06 14:17:03 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-09 14:28:37 -05:00
|
|
|
"fmt"
|
2018-11-06 14:17:03 -05:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"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"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2018-11-06 14:17:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepSnapshotDataDisks struct {
|
2018-12-13 16:54:19 -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)
|
|
|
|
enable func() bool
|
2018-11-06 14:17:03 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func NewStepSnapshotDataDisks(client *AzureClient, ui packersdk.Ui, config *Config) *StepSnapshotDataDisks {
|
2018-11-06 14:17:03 -05:00
|
|
|
var step = &StepSnapshotDataDisks{
|
2018-12-13 16:54:19 -05:00
|
|
|
client: client,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
enable: func() bool { return config.isManagedImage() && config.ManagedImageDataDiskSnapshotPrefix != "" },
|
2018-11-06 14:17:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
step.create = step.createDataDiskSnapshot
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSnapshotDataDisks) createDataDiskSnapshot(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-06 14:17:03 -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
|
2018-11-06 14:17:03 -05:00
|
|
|
}
|
|
|
|
|
2019-05-30 17:25:43 -04:00
|
|
|
err = f.WaitForCompletionRef(ctx, s.client.SnapshotsClient.Client)
|
2018-11-09 14:28:37 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
createdSnapshot, err := f.Result(s.client.SnapshotsClient)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:54:19 -05:00
|
|
|
s.say(fmt.Sprintf(" -> Snapshot ID : '%s'", *(createdSnapshot.ID)))
|
2018-11-09 14:28:37 -05:00
|
|
|
return nil
|
2018-11-06 14:17:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction {
|
2018-12-13 16:54:19 -05:00
|
|
|
if !s.enable() {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2018-11-06 14:17:03 -05:00
|
|
|
|
2018-12-13 16:54:19 -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 additionalDisks = stateBag.Get(constants.ArmAdditionalDiskVhds).([]string)
|
|
|
|
var dstSnapshotPrefix = stateBag.Get(constants.ArmManagedImageDataDiskSnapshotPrefix).(string)
|
|
|
|
|
|
|
|
if len(additionalDisks) == 1 {
|
|
|
|
s.say(fmt.Sprintf("Snapshotting data disk ..."))
|
|
|
|
} else {
|
|
|
|
s.say(fmt.Sprintf("Snapshotting data disks ..."))
|
|
|
|
}
|
2018-11-06 14:17:03 -05:00
|
|
|
|
2018-12-13 16:54:19 -05:00
|
|
|
for i, disk := range additionalDisks {
|
|
|
|
s.say(fmt.Sprintf(" -> Data Disk : '%s'", disk))
|
2018-11-06 14:17:03 -05:00
|
|
|
|
2018-12-13 16:54:19 -05:00
|
|
|
dstSnapshotName := dstSnapshotPrefix + strconv.Itoa(i)
|
|
|
|
err := s.create(ctx, resourceGroupName, disk, location, tags, dstSnapshotName)
|
2018-11-06 14:17:03 -05:00
|
|
|
|
2018-12-13 16:54:19 -05:00
|
|
|
if err != nil {
|
|
|
|
stateBag.Put(constants.Error, err)
|
|
|
|
s.error(err)
|
2018-11-09 14:28:37 -05:00
|
|
|
|
2018-12-13 16:54:19 -05:00
|
|
|
return multistep.ActionHalt
|
2018-11-13 20:47:48 -05:00
|
|
|
}
|
2018-11-06 14:17:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepSnapshotDataDisks) Cleanup(multistep.StateBag) {
|
|
|
|
}
|