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
|
|
|
|
2020-04-23 05:03:17 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
|
2020-03-25 17:55:37 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
2019-05-27 02:20:11 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/client"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ multistep.Step = &StepCreateNewDisk{}
|
|
|
|
|
|
|
|
type StepCreateNewDisk struct {
|
2020-03-25 17:55:37 -04:00
|
|
|
ResourceID string // Disk ID
|
|
|
|
subscriptionID, resourceGroup, diskName string // split out resource id
|
|
|
|
|
|
|
|
DiskSizeGB int32 // optional, ignored if 0
|
|
|
|
DiskStorageAccountType string // from compute.DiskStorageAccountTypes
|
|
|
|
HyperVGeneration string
|
2019-09-10 08:48:55 -04:00
|
|
|
|
|
|
|
Location string
|
|
|
|
PlatformImage *client.PlatformImage
|
|
|
|
|
|
|
|
SourceDiskResourceID string
|
2019-06-03 19:06:19 -04:00
|
|
|
|
2020-04-23 05:03:17 -04:00
|
|
|
SourceImageResourceID string
|
|
|
|
|
2019-06-03 19:06:19 -04:00
|
|
|
SkipCleanup bool
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 17:55:37 -04:00
|
|
|
func parseDiskResourceID(resourceID string) (subscriptionID, resourceGroup, diskName string, err error) {
|
|
|
|
r, err := azure.ParseResourceID(resourceID)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.EqualFold(r.Provider, "Microsoft.Compute") ||
|
|
|
|
!strings.EqualFold(r.ResourceType, "disks") {
|
|
|
|
return "", "", "", fmt.Errorf("Resource %q is not of type Microsoft.Compute/disks", resourceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.SubscriptionID, r.ResourceGroup, r.ResourceName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-05-27 02:20:11 -04:00
|
|
|
azcli := state.Get("azureclient").(client.AzureClientSet)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2020-03-25 17:55:37 -04:00
|
|
|
state.Put(stateBagKey_OSDiskResourceID, s.ResourceID)
|
|
|
|
ui.Say(fmt.Sprintf("Creating disk '%s'", s.ResourceID))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s.subscriptionID, s.resourceGroup, s.diskName, err = parseDiskResourceID(s.ResourceID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("StepCreateNewDisk.Run: error: %+v", err)
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"error parsing resource id '%s': %v", s.ResourceID, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if s.DiskStorageAccountType != "" {
|
|
|
|
disk.Sku = &compute.DiskSku{
|
|
|
|
Name: compute.DiskStorageAccountTypes(s.DiskStorageAccountType),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.HyperVGeneration != "" {
|
|
|
|
disk.DiskProperties.HyperVGeneration = compute.HyperVGeneration(s.HyperVGeneration)
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.DiskSizeGB > 0 {
|
|
|
|
disk.DiskProperties.DiskSizeGB = to.Int32Ptr(s.DiskSizeGB)
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:03:17 -04:00
|
|
|
switch {
|
|
|
|
case s.PlatformImage != 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-03-25 17:55:37 -04:00
|
|
|
s.subscriptionID, s.Location, s.PlatformImage.Publisher, s.PlatformImage.Offer, s.PlatformImage.Sku, s.PlatformImage.Version)),
|
2019-06-03 04:33:31 -04:00
|
|
|
}
|
2020-04-23 05:03:17 -04:00
|
|
|
case s.SourceDiskResourceID != "":
|
|
|
|
disk.CreationData.CreateOption = compute.Copy
|
|
|
|
disk.CreationData.SourceResourceID = to.StringPtr(s.SourceDiskResourceID)
|
|
|
|
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-03-25 17:55:37 -04:00
|
|
|
f, err := azcli.DisksClient().CreateOrUpdate(ctx, s.resourceGroup, s.diskName, disk)
|
2019-05-27 02:20:11 -04:00
|
|
|
if err == nil {
|
2020-03-31 18:19:58 -04:00
|
|
|
cli := azcli.PollClient() // quick polling for quick operations
|
|
|
|
cli.PollingDelay = time.Second
|
|
|
|
err = f.WaitForCompletionRef(ctx, cli)
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("StepCreateNewDisk.Run: error: %+v", err)
|
|
|
|
err := fmt.Errorf(
|
2020-03-25 17:55:37 -04:00
|
|
|
"error creating new disk '%s': %v", s.ResourceID, err)
|
2019-05-27 02:20:11 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:55:37 -04:00
|
|
|
func (s *StepCreateNewDisk) Cleanup(state multistep.StateBag) {
|
2019-06-03 19:06:19 -04:00
|
|
|
if !s.SkipCleanup {
|
|
|
|
azcli := state.Get("azureclient").(client.AzureClientSet)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-05-27 02:20:11 -04:00
|
|
|
|
2020-03-25 17:55:37 -04:00
|
|
|
ui.Say(fmt.Sprintf("Waiting for disk %q detach to complete", s.ResourceID))
|
|
|
|
err := NewDiskAttacher(azcli).WaitForDetach(context.Background(), s.ResourceID)
|
2019-10-30 14:14:32 -04:00
|
|
|
if err != nil {
|
2020-03-25 17:55:37 -04:00
|
|
|
ui.Error(fmt.Sprintf("error detaching disk %q: %s", s.ResourceID, err))
|
2019-10-30 14:14:32 -04:00
|
|
|
}
|
2019-06-03 19:01:53 -04:00
|
|
|
|
2020-03-25 17:55:37 -04:00
|
|
|
ui.Say(fmt.Sprintf("Deleting disk %q", s.ResourceID))
|
2019-05-27 02:20:11 -04:00
|
|
|
|
2020-03-25 17:55:37 -04:00
|
|
|
f, err := azcli.DisksClient().Delete(context.TODO(), s.resourceGroup, s.diskName)
|
2019-06-03 19:06:19 -04:00
|
|
|
if err == nil {
|
|
|
|
err = f.WaitForCompletionRef(context.TODO(), azcli.PollClient())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("StepCreateNewDisk.Cleanup: error: %+v", err)
|
2020-03-25 17:55:37 -04:00
|
|
|
ui.Error(fmt.Sprintf("error deleting disk '%s': %v.", s.ResourceID, err))
|
2019-06-03 19:06:19 -04:00
|
|
|
}
|
2019-05-27 02:20:11 -04:00
|
|
|
}
|
|
|
|
}
|