Skip to content

Commit

Permalink
Allow skipping OSDisk cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmey committed Jun 3, 2019
1 parent fa07992 commit da80784
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
3 changes: 3 additions & 0 deletions builder/azure/chroot/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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"`
Expand Down Expand Up @@ -319,6 +320,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)
Expand Down
30 changes: 17 additions & 13 deletions builder/azure/chroot/step_create_new_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,21 +78,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))
}
}
}

0 comments on commit da80784

Please sign in to comment.