diff --git a/builder/azure/chroot/builder.go b/builder/azure/chroot/builder.go index abc75d677..398b2230e 100644 --- a/builder/azure/chroot/builder.go +++ b/builder/azure/chroot/builder.go @@ -43,6 +43,7 @@ type Config struct { OSDiskSizeGB int32 `mapstructure:"os_disk_size_gb"` OSDiskStorageAccountType string `mapstructure:"os_disk_storage_account_type"` OSDiskCacheType string `mapstructure:"os_disk_cache_type"` + OSDiskSkipCleanup bool `mapstructure:"os_disk_skip_cleanup"` ImageResourceID string `mapstructure:"image_resource_id"` ImageOSState string `mapstructure:"image_os_state"` @@ -320,6 +321,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack HyperVGeneration: b.config.ImageHyperVGeneration, Location: info.Location, PlatformImage: pi, + + SkipCleanup: b.config.OSDiskSkipCleanup, }) } else { panic("Unknown image source: " + b.config.Source) diff --git a/builder/azure/chroot/step_create_new_disk.go b/builder/azure/chroot/step_create_new_disk.go index 8d334a76a..d8df8b6b2 100644 --- a/builder/azure/chroot/step_create_new_disk.go +++ b/builder/azure/chroot/step_create_new_disk.go @@ -21,6 +21,8 @@ type StepCreateNewDisk struct { HyperVGeneration string Location string PlatformImage *client.PlatformImage + + SkipCleanup bool } func (s StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -81,21 +83,23 @@ 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").(string) + if !s.SkipCleanup { + azcli := state.Get("azureclient").(client.AzureClientSet) + ui := state.Get("ui").(packer.Ui) + diskResourceID := state.Get("os_disk_resource_id").(string) - ui.Say(fmt.Sprintf("Waiting for disk %q detach to complete", diskResourceID)) - err := NewDiskAttacher(azcli).WaitForDetach(context.Background(), 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)) + ui.Say(fmt.Sprintf("Deleting disk %q", diskResourceID)) - f, err := azcli.DisksClient().Delete(context.TODO(), s.ResourceGroup, s.DiskName) - if err == nil { - err = f.WaitForCompletionRef(context.TODO(), azcli.PollClient()) - } - if err != nil { - log.Printf("StepCreateNewDisk.Cleanup: error: %+v", err) - ui.Error(fmt.Sprintf("error deleting new disk '%s': %v.", diskResourceID, err)) + f, err := azcli.DisksClient().Delete(context.TODO(), s.ResourceGroup, s.DiskName) + if err == nil { + err = f.WaitForCompletionRef(context.TODO(), azcli.PollClient()) + } + if err != nil { + log.Printf("StepCreateNewDisk.Cleanup: error: %+v", err) + ui.Error(fmt.Sprintf("error deleting new disk '%s': %v.", diskResourceID, err)) + } } }