Skip to content

Commit ed683a3

Browse files
authored
implement image_delete (#106)
1 parent 22648cd commit ed683a3

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

builder/nutanix/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Config struct {
3838
ImageDescription string `mapstructure:"image_description" json:"image_description" required:"false"`
3939
ImageCategoryKey string `mapstructure:"image_category_key" json:"image_category_key" required:"false"`
4040
ImageCategoryValue string `mapstructure:"image_category_value" json:"image_category_value" required:"false"`
41+
ImageDelete bool `mapstructure:"image_delete" json:"image_delete" required:"false"`
4142
WaitTimeout time.Duration `mapstructure:"ip_wait_timeout" json:"ip_wait_timeout" required:"false"`
4243

4344
ctx interpolate.Context

builder/nutanix/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/nutanix/step_copy_image.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,22 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
5252
}
5353

5454
func (s *stepCopyImage) Cleanup(state multistep.StateBag) {
55+
ui := state.Get("ui").(packer.Ui)
56+
d := state.Get("driver").(Driver)
57+
58+
if !s.Config.ImageDelete {
59+
return
60+
}
61+
62+
if imgUUID, ok := state.GetOk("image_uuid"); ok {
63+
ui.Say(fmt.Sprintf("Deleting image %s...", s.Config.ImageName))
64+
65+
err := d.DeleteImage(imgUUID.(string))
66+
if err != nil {
67+
ui.Error("An error occurred while deleting image")
68+
return
69+
} else {
70+
ui.Message("Image successfully deleted")
71+
}
72+
}
5573
}

docs/builders/nutanix.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ These parameters allow to configure everything around image creation, from the t
4141
- `image_category_key` (string) - Name of the category to assign to the image.
4242
- `image_category_value` (string) - Value of the category to assign to the image.
4343
- `force_deregister` (bool) - Allow output image override if already exists.
44+
- `image_delete` (bool) - Delete image once build process is completed.
4445
- `shutdown_command` (string) - Command line to shutdown your temporary VM.
4546
- `shutdown_timeout` (string) - Timeout for VM shutdown (format : 2m).
4647
- `communicator` (string) - Protocol used for Packer connection (ex "winrm" or "ssh"). Default is : "ssh".

0 commit comments

Comments
 (0)