diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index dd040d1..c3aa12f 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -52,6 +52,8 @@ type CommonConfig struct { RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"` SSHWaitTimeout time.Duration + DestroyVIFs bool `mapstructure:"destroy_vifs"` + OutputDir string `mapstructure:"output_directory"` Format string `mapstructure:"format"` KeepVM string `mapstructure:"keep_vm"` diff --git a/builder/xenserver/common/config.hcl2spec.go b/builder/xenserver/common/config.hcl2spec.go index 288c308..c917b7d 100644 --- a/builder/xenserver/common/config.hcl2spec.go +++ b/builder/xenserver/common/config.hcl2spec.go @@ -109,6 +109,7 @@ type FlatConfig struct { RawInstallTimeout *string `mapstructure:"install_timeout" cty:"install_timeout" hcl:"install_timeout"` SourcePath *string `mapstructure:"source_path" cty:"source_path" hcl:"source_path"` Firmware *string `mapstructure:"firmware" cty:"firmware" hcl:"firmware"` + DestroyVIFs *bool `mapstructure:"destroy_vifs" cty:"destroy_vifs" hcl:"destroy_vifs"` } // FlatMapstructure returns a new FlatConfig. @@ -222,6 +223,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "install_timeout": &hcldec.AttrSpec{Name: "install_timeout", Type: cty.String, Required: false}, "source_path": &hcldec.AttrSpec{Name: "source_path", Type: cty.String, Required: false}, "firmware": &hcldec.AttrSpec{Name: "firmware", Type: cty.String, Required: false}, + "destroy_vifs": &hcldec.AttrSpec{Name: "destroy_vifs", Type: cty.Bool, Required: false}, } return s } diff --git a/builder/xenserver/common/step_destroy_vifs.go b/builder/xenserver/common/step_destroy_vifs.go new file mode 100644 index 0000000..81678d1 --- /dev/null +++ b/builder/xenserver/common/step_destroy_vifs.go @@ -0,0 +1,50 @@ +package common + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/packer-plugin-sdk/multistep" + "github.com/hashicorp/packer-plugin-sdk/packer" +) + +type StepDestroyVIFs struct{} + +func (self *StepDestroyVIFs) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + c := state.Get("client").(*Connection) + config := state.Get("config").(Config) + ui := state.Get("ui").(packer.Ui) + + if !config.DestroyVIFs { + log.Printf("Not destroying VIFs") + return multistep.ActionContinue + } + + ui.Say("Step: Destroy VIFs") + + uuid := state.Get("instance_uuid").(string) + instance, err := c.client.VM.GetByUUID(c.session, uuid) + if err != nil { + ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error())) + return multistep.ActionHalt + } + + vifs, err := c.client.VM.GetVIFs(c.session, instance) + if err != nil { + ui.Error(fmt.Sprintf("Error getting VIFs: %s", err.Error())) + return multistep.ActionHalt + } + + for _, vif := range vifs { + err = c.client.VIF.Destroy(c.session, vif) + if err != nil { + ui.Error(fmt.Sprintf("Error destroying VIF: %s", err.Error())) + return multistep.ActionHalt + } + } + + return multistep.ActionContinue +} + +func (self *StepDestroyVIFs) Cleanup(state multistep.StateBag) {} diff --git a/builder/xenserver/iso/builder.go b/builder/xenserver/iso/builder.go index 7a3c5cc..8e8fe9a 100644 --- a/builder/xenserver/iso/builder.go +++ b/builder/xenserver/iso/builder.go @@ -299,6 +299,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p &xscommon.StepDetachVdi{ VdiUuidKey: "floppy_vdi_uuid", }, + new(xscommon.StepDestroyVIFs), new(xscommon.StepExport), } diff --git a/builder/xenserver/xva/builder.go b/builder/xenserver/xva/builder.go index e948413..3a9bf1c 100644 --- a/builder/xenserver/xva/builder.go +++ b/builder/xenserver/xva/builder.go @@ -185,6 +185,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p &xscommon.StepDetachVdi{ VdiUuidKey: "floppy_vdi_uuid", }, + new(xscommon.StepDestroyVIFs), new(xscommon.StepExport), } diff --git a/docs/builders/iso/xenserver-iso.html.markdown b/docs/builders/iso/xenserver-iso.html.markdown index 09adebd..7d24792 100644 --- a/docs/builders/iso/xenserver-iso.html.markdown +++ b/docs/builders/iso/xenserver-iso.html.markdown @@ -86,6 +86,10 @@ each category, the available options are alphabetized and described. run `xe template-list`. Setting the correct value hints to XenServer how to optimize the virtual hardware to work best with that operating system. +* `destroy_vifs` (boolean) - Whether to destroy VIFs on the VM prior to + exporting. Removing them may make the image more generic and reusable. + Default is `false`. + * `disk_size` (integer) - The size, in megabytes, of the hard disk to create for the VM. By default, this is 40000 (about 40 GB).