diff --git a/builder/azure/chroot/diskattacher.go b/builder/azure/chroot/diskattacher.go index 97ba4174c..c52e294eb 100644 --- a/builder/azure/chroot/diskattacher.go +++ b/builder/azure/chroot/diskattacher.go @@ -20,9 +20,10 @@ import ( type DiskAttacher interface { AttachDisk(ctx context.Context, disk string) (lun int32, err error) - DetachDisk(ctx context.Context, disk string) (err error) - WaitForDevice(ctx context.Context, i int32) (device string, err error) DiskPathForLun(lun int32) string + WaitForDevice(ctx context.Context, i int32) (device string, err error) + DetachDisk(ctx context.Context, disk string) (err error) + WaitForDetach(ctx context.Context, diskID string) error } func NewDiskAttacher(azureClient client.AzureClientSet) DiskAttacher { @@ -89,7 +90,10 @@ func (da *diskAttacher) DetachDisk(ctx context.Context, diskID string) error { return err } - // waiting for VM update to finish takes way to long + return nil +} + +func (da *diskAttacher) WaitForDetach(ctx context.Context, diskID string) error { for { // loop until disk is not attached, timeout or error list, err := da.getDisks(ctx) if err != nil { diff --git a/builder/azure/chroot/step_create_new_disk.go b/builder/azure/chroot/step_create_new_disk.go index 6873c827a..8d334a76a 100644 --- a/builder/azure/chroot/step_create_new_disk.go +++ b/builder/azure/chroot/step_create_new_disk.go @@ -83,9 +83,12 @@ func (s StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) mu func (s StepCreateNewDisk) Cleanup(state multistep.StateBag) { azcli := state.Get("azureclient").(client.AzureClientSet) ui := state.Get("ui").(packer.Ui) - diskResourceID := state.Get("os_disk_resource_id") + diskResourceID := state.Get("os_disk_resource_id").(string) - ui.Say(fmt.Sprintf("Deleting disk '%s'", diskResourceID)) + ui.Say(fmt.Sprintf("Waiting for disk %q detach to complete", diskResourceID)) + err := NewDiskAttacher(azcli).WaitForDetach(context.Background(), diskResourceID) + + ui.Say(fmt.Sprintf("Deleting disk %q", diskResourceID)) f, err := azcli.DisksClient().Delete(context.TODO(), s.ResourceGroup, s.DiskName) if err == nil {