2019-05-27 02:20:11 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-03-25 17:55:37 -04:00
|
|
|
"strings"
|
2020-03-31 18:19:58 -04:00
|
|
|
"time"
|
2019-05-27 02:20:11 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/client"
|
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"
|
2020-04-15 18:58:06 -04:00
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
|
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
2019-05-27 02:20:11 -04:00
|
|
|
)
|
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
var _ multistep.Step = &StepCreateNewDiskset{}
|
2019-05-27 02:20:11 -04:00
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
type StepCreateNewDiskset struct {
|
2020-04-29 16:28:10 -04:00
|
|
|
OSDiskID string // Disk ID
|
|
|
|
OSDiskSizeGB int32 // optional, ignored if 0
|
|
|
|
OSDiskStorageAccountType string // from compute.DiskStorageAccountTypes
|
|
|
|
DataDiskStorageAccountType string // from compute.DiskStorageAccountTypes
|
2020-03-25 17:55:37 -04:00
|
|
|
|
2020-04-28 17:43:39 -04:00
|
|
|
DataDiskIDPrefix string
|
|
|
|
|
2020-04-15 18:58:06 -04:00
|
|
|
disks Diskset
|
2019-09-10 08:48:55 -04:00
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
HyperVGeneration string // For OS disk
|
2019-09-10 08:48:55 -04:00
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
// Copy another disk
|
|
|
|
SourceOSDiskResourceID string
|
2019-06-03 19:06:19 -04:00
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
// Extract from platform image
|
|
|
|
SourcePlatformImage *client.PlatformImage
|
|
|
|
// Extract from shared image
|
2020-04-23 05:03:17 -04:00
|
|
|
SourceImageResourceID string
|
2020-04-15 17:46:46 -04:00
|
|
|
// Location is needed for platform and shared images
|
|
|
|
Location string
|
2020-04-23 05:03:17 -04:00
|
|
|
|
2019-06-03 19:06:19 -04:00
|
|
|
SkipCleanup bool
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
func (s *StepCreateNewDiskset) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-05-27 02:20:11 -04:00
|
|
|
azcli := state.Get("azureclient").(client.AzureClientSet)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-05-27 02:20:11 -04:00
|
|
|
|
2020-04-28 17:43:39 -04:00
|
|
|
s.disks = make(Diskset)
|
2020-03-25 17:55:37 -04:00
|
|
|
|
2020-04-15 18:58:06 -04:00
|
|
|
errorMessage := func(format string, params ...interface{}) multistep.StepAction {
|
2020-04-28 17:43:39 -04:00
|
|
|
err := fmt.Errorf("StepCreateNewDiskset.Run: error: "+format, params...)
|
2020-03-25 17:55:37 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-05-27 02:20:11 -04:00
|
|
|
|
2020-04-15 18:58:06 -04:00
|
|
|
// we always have an OS disk
|
|
|
|
osDisk, err := client.ParseResourceID(s.OSDiskID)
|
|
|
|
if err != nil {
|
|
|
|
return errorMessage("error parsing resource id '%s': %v", s.OSDiskID, err)
|
|
|
|
}
|
|
|
|
if !strings.EqualFold(osDisk.Provider, "Microsoft.Compute") ||
|
|
|
|
!strings.EqualFold(osDisk.ResourceType.String(), "disks") {
|
|
|
|
return errorMessage("Resource %q is not of type Microsoft.Compute/disks", s.OSDiskID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform step config to disk model
|
2020-04-28 17:43:39 -04:00
|
|
|
disk := s.getOSDiskDefinition(azcli.SubscriptionID())
|
2020-04-15 18:58:06 -04:00
|
|
|
|
|
|
|
// Initiate disk creation
|
|
|
|
f, err := azcli.DisksClient().CreateOrUpdate(ctx, osDisk.ResourceGroup, osDisk.ResourceName.String(), disk)
|
|
|
|
if err != nil {
|
|
|
|
return errorMessage("Failed to initiate resource creation: %q", osDisk)
|
|
|
|
}
|
|
|
|
s.disks[-1] = osDisk // save the resoure we just create in our disk set
|
|
|
|
state.Put(stateBagKey_Diskset, s.disks) // update the statebag
|
|
|
|
ui.Say(fmt.Sprintf("Creating disk %q", s.OSDiskID))
|
|
|
|
|
2020-04-28 17:43:39 -04:00
|
|
|
type Future struct {
|
|
|
|
client.Resource
|
|
|
|
compute.DisksCreateOrUpdateFuture
|
|
|
|
}
|
|
|
|
futures := []Future{{osDisk, f}}
|
|
|
|
|
|
|
|
if s.SourceImageResourceID != "" {
|
|
|
|
// retrieve image to see if there are any datadisks
|
|
|
|
imageID, err := client.ParseResourceID(s.SourceImageResourceID)
|
|
|
|
if err != nil {
|
|
|
|
return errorMessage("could not parse source image id %q: %v", s.SourceImageResourceID, err)
|
|
|
|
}
|
|
|
|
if !strings.EqualFold(imageID.Provider+"/"+imageID.ResourceType.String(),
|
|
|
|
"Microsoft.Compute/galleries/images/versions") {
|
|
|
|
return errorMessage("source image id is not a shared image version %q, expected type 'Microsoft.Compute/galleries/images/versions'", imageID)
|
|
|
|
}
|
|
|
|
image, err := azcli.GalleryImageVersionsClient().Get(ctx,
|
|
|
|
imageID.ResourceGroup,
|
|
|
|
imageID.ResourceName[0], imageID.ResourceName[1], imageID.ResourceName[2], "")
|
|
|
|
if err != nil {
|
|
|
|
return errorMessage("error retrieving source image %q: %v", imageID, err)
|
|
|
|
}
|
|
|
|
if image.GalleryImageVersionProperties != nil &&
|
|
|
|
image.GalleryImageVersionProperties.StorageProfile != nil &&
|
|
|
|
image.GalleryImageVersionProperties.StorageProfile.DataDiskImages != nil {
|
|
|
|
for i, ddi := range *image.GalleryImageVersionProperties.StorageProfile.DataDiskImages {
|
|
|
|
if ddi.Lun == nil {
|
|
|
|
return errorMessage("unexpected: lun is null for data disk # %d", i)
|
|
|
|
}
|
2020-04-29 18:11:16 -04:00
|
|
|
datadiskID, err := client.ParseResourceID(fmt.Sprintf("%s%d", s.DataDiskIDPrefix, *ddi.Lun))
|
2020-04-28 17:43:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return errorMessage("unable to construct resource id for datadisk: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
disk := s.getDatadiskDefinitionFromImage(*ddi.Lun)
|
|
|
|
// Initiate disk creation
|
|
|
|
f, err := azcli.DisksClient().CreateOrUpdate(ctx, datadiskID.ResourceGroup, datadiskID.ResourceName.String(), disk)
|
|
|
|
if err != nil {
|
|
|
|
return errorMessage("Failed to initiate resource creation: %q", datadiskID)
|
|
|
|
}
|
|
|
|
s.disks[*ddi.Lun] = datadiskID // save the resoure we just create in our disk set
|
|
|
|
state.Put(stateBagKey_Diskset, s.disks) // update the statebag
|
|
|
|
ui.Say(fmt.Sprintf("Creating disk %q", datadiskID))
|
|
|
|
|
|
|
|
futures = append(futures, Future{datadiskID, f})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for disks to be created.")
|
|
|
|
|
2020-04-15 18:58:06 -04:00
|
|
|
// Wait for completion
|
2020-04-28 17:43:39 -04:00
|
|
|
for _, f := range futures {
|
2020-04-15 18:58:06 -04:00
|
|
|
cli := azcli.PollClient() // quick polling for quick operations
|
|
|
|
cli.PollingDelay = time.Second
|
|
|
|
err = f.WaitForCompletionRef(ctx, cli)
|
|
|
|
if err != nil {
|
|
|
|
return errorMessage(
|
2020-04-28 17:43:39 -04:00
|
|
|
"error creating new disk '%s': %v", f.Resource, err)
|
2020-04-15 18:58:06 -04:00
|
|
|
}
|
2020-04-28 17:43:39 -04:00
|
|
|
ui.Say(fmt.Sprintf("Disk %q created", f.Resource))
|
2020-04-15 18:58:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-04-28 17:43:39 -04:00
|
|
|
func (s StepCreateNewDiskset) getOSDiskDefinition(subscriptionID string) compute.Disk {
|
2019-05-27 02:20:11 -04:00
|
|
|
disk := compute.Disk{
|
2019-06-03 01:27:33 -04:00
|
|
|
Location: to.StringPtr(s.Location),
|
2019-05-27 02:20:11 -04:00
|
|
|
DiskProperties: &compute.DiskProperties{
|
2020-04-23 05:03:17 -04:00
|
|
|
OsType: "Linux",
|
|
|
|
CreationData: &compute.CreationData{},
|
2019-05-27 02:20:11 -04:00
|
|
|
},
|
2020-04-23 05:03:17 -04:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
if s.OSDiskStorageAccountType != "" {
|
2020-04-23 05:03:17 -04:00
|
|
|
disk.Sku = &compute.DiskSku{
|
2020-04-15 17:46:46 -04:00
|
|
|
Name: compute.DiskStorageAccountTypes(s.OSDiskStorageAccountType),
|
2020-04-23 05:03:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.HyperVGeneration != "" {
|
|
|
|
disk.DiskProperties.HyperVGeneration = compute.HyperVGeneration(s.HyperVGeneration)
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
if s.OSDiskSizeGB > 0 {
|
|
|
|
disk.DiskProperties.DiskSizeGB = to.Int32Ptr(s.OSDiskSizeGB)
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
|
2020-04-23 05:03:17 -04:00
|
|
|
switch {
|
2020-04-15 17:46:46 -04:00
|
|
|
case s.SourcePlatformImage != nil:
|
2019-06-03 04:33:31 -04:00
|
|
|
disk.CreationData.CreateOption = compute.FromImage
|
|
|
|
disk.CreationData.ImageReference = &compute.ImageDiskReference{
|
|
|
|
ID: to.StringPtr(fmt.Sprintf(
|
|
|
|
"/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/artifacttypes/vmimage/offers/%s/skus/%s/versions/%s",
|
2020-04-28 17:43:39 -04:00
|
|
|
subscriptionID, s.Location,
|
2020-04-15 18:58:06 -04:00
|
|
|
s.SourcePlatformImage.Publisher, s.SourcePlatformImage.Offer, s.SourcePlatformImage.Sku, s.SourcePlatformImage.Version)),
|
2019-06-03 04:33:31 -04:00
|
|
|
}
|
2020-04-15 17:46:46 -04:00
|
|
|
case s.SourceOSDiskResourceID != "":
|
2020-04-23 05:03:17 -04:00
|
|
|
disk.CreationData.CreateOption = compute.Copy
|
2020-04-15 17:46:46 -04:00
|
|
|
disk.CreationData.SourceResourceID = to.StringPtr(s.SourceOSDiskResourceID)
|
2020-04-23 05:03:17 -04:00
|
|
|
case s.SourceImageResourceID != "":
|
|
|
|
disk.CreationData.CreateOption = compute.FromImage
|
|
|
|
disk.CreationData.GalleryImageReference = &compute.ImageDiskReference{
|
|
|
|
ID: to.StringPtr(s.SourceImageResourceID),
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
disk.CreationData.CreateOption = compute.Empty
|
2019-06-03 04:33:31 -04:00
|
|
|
}
|
2020-04-15 18:58:06 -04:00
|
|
|
return disk
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
|
2020-04-28 17:43:39 -04:00
|
|
|
func (s StepCreateNewDiskset) getDatadiskDefinitionFromImage(lun int32) compute.Disk {
|
|
|
|
disk := compute.Disk{
|
|
|
|
Location: to.StringPtr(s.Location),
|
|
|
|
DiskProperties: &compute.DiskProperties{
|
|
|
|
CreationData: &compute.CreationData{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
disk.CreationData.CreateOption = compute.FromImage
|
|
|
|
disk.CreationData.GalleryImageReference = &compute.ImageDiskReference{
|
|
|
|
ID: to.StringPtr(s.SourceImageResourceID),
|
|
|
|
Lun: to.Int32Ptr(lun),
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:28:10 -04:00
|
|
|
if s.DataDiskStorageAccountType != "" {
|
2020-04-28 17:43:39 -04:00
|
|
|
disk.Sku = &compute.DiskSku{
|
2020-04-29 16:28:10 -04:00
|
|
|
Name: compute.DiskStorageAccountTypes(s.DataDiskStorageAccountType),
|
2020-04-28 17:43:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return disk
|
|
|
|
}
|
|
|
|
|
2020-04-15 17:46:46 -04:00
|
|
|
func (s *StepCreateNewDiskset) Cleanup(state multistep.StateBag) {
|
2019-06-03 19:06:19 -04:00
|
|
|
if !s.SkipCleanup {
|
|
|
|
azcli := state.Get("azureclient").(client.AzureClientSet)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-05-27 02:20:11 -04:00
|
|
|
|
2020-04-15 18:58:06 -04:00
|
|
|
for _, d := range s.disks {
|
2019-06-03 19:01:53 -04:00
|
|
|
|
2020-04-15 18:58:06 -04:00
|
|
|
ui.Say(fmt.Sprintf("Waiting for disk %q detach to complete", d))
|
|
|
|
err := NewDiskAttacher(azcli).WaitForDetach(context.Background(), d.String())
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("error detaching disk %q: %s", d, err))
|
|
|
|
}
|
2019-05-27 02:20:11 -04:00
|
|
|
|
2020-04-15 18:58:06 -04:00
|
|
|
ui.Say(fmt.Sprintf("Deleting disk %q", d))
|
|
|
|
|
|
|
|
f, err := azcli.DisksClient().Delete(context.TODO(), d.ResourceGroup, d.ResourceName.String())
|
|
|
|
if err == nil {
|
|
|
|
err = f.WaitForCompletionRef(context.TODO(), azcli.PollClient())
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-04-28 17:43:39 -04:00
|
|
|
log.Printf("StepCreateNewDiskset.Cleanup: error: %+v", err)
|
2020-04-15 18:58:06 -04:00
|
|
|
ui.Error(fmt.Sprintf("error deleting disk '%s': %v.", d, err))
|
|
|
|
}
|
2019-06-03 19:06:19 -04:00
|
|
|
}
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
}
|