From feb8791decfa50d1cdce0c5913221b9d058d272e Mon Sep 17 00:00:00 2001 From: Dongyu Zheng Date: Wed, 26 Oct 2016 22:43:37 -0700 Subject: [PATCH 1/2] Added destroy vifs step --- builder/xenserver/common/common_config.go | 2 + builder/xenserver/common/step_destroy_vifs.go | 47 +++++++++++++++++++ builder/xenserver/iso/builder.go | 1 + builder/xenserver/xva/builder.go | 1 + docs/builders/iso/xenserver-iso.html.markdown | 4 ++ 5 files changed, 55 insertions(+) create mode 100644 builder/xenserver/common/step_destroy_vifs.go diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index 6907e189..4c9f9e41 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -51,6 +51,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/step_destroy_vifs.go b/builder/xenserver/common/step_destroy_vifs.go new file mode 100644 index 00000000..0ebf109f --- /dev/null +++ b/builder/xenserver/common/step_destroy_vifs.go @@ -0,0 +1,47 @@ +package common + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + xsclient "github.com/xenserver/go-xenserver-client" +) + +type StepDestroyVIFs struct{} + +func (self *StepDestroyVIFs) Run(state multistep.StateBag) multistep.StepAction { + config := state.Get("commonconfig").(CommonConfig) + if !config.DestroyVIFs { + return multistep.ActionContinue + } + + ui := state.Get("ui").(packer.Ui) + client := state.Get("client").(xsclient.XenAPIClient) + + ui.Say("Step: Destroy VIFs") + + uuid := state.Get("instance_uuid").(string) + instance, err := client.GetVMByUuid(uuid) + if err != nil { + ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error())) + return multistep.ActionHalt + } + + vifs, err := instance.GetVIFs() + if err != nil { + ui.Error(fmt.Sprintf("Error getting VIFs: %s", err.Error())) + return multistep.ActionHalt + } + + for _, vif := range vifs { + err = vif.Destroy() + 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 707cead0..0c799ac4 100644 --- a/builder/xenserver/iso/builder.go +++ b/builder/xenserver/iso/builder.go @@ -293,6 +293,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 00235d23..cceeadaa 100644 --- a/builder/xenserver/xva/builder.go +++ b/builder/xenserver/xva/builder.go @@ -167,6 +167,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p &xscommon.StepDetachVdi{ VdiUuidKey: "tools_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 9c62c3f2..bfbb2c94 100644 --- a/docs/builders/iso/xenserver-iso.html.markdown +++ b/docs/builders/iso/xenserver-iso.html.markdown @@ -73,6 +73,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). From 374a26c895e8e3a0e4806b8578d18b30d96f3b40 Mon Sep 17 00:00:00 2001 From: Heinrich Kruger Date: Mon, 30 May 2022 10:04:07 +0100 Subject: [PATCH 2/2] Update destroy vifs implementation --- builder/xenserver/common/config.hcl2spec.go | 2 ++ builder/xenserver/common/step_destroy_vifs.go | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/builder/xenserver/common/config.hcl2spec.go b/builder/xenserver/common/config.hcl2spec.go index 768c5ee2..a75de443 100644 --- a/builder/xenserver/common/config.hcl2spec.go +++ b/builder/xenserver/common/config.hcl2spec.go @@ -107,6 +107,7 @@ type FlatConfig struct { PlatformArgs map[string]string `mapstructure:"platform_args" cty:"platform_args" hcl:"platform_args"` RawInstallTimeout *string `mapstructure:"install_timeout" cty:"install_timeout" hcl:"install_timeout"` SourcePath *string `mapstructure:"source_path" cty:"source_path" hcl:"source_path"` + DestroyVIFs *bool `mapstructure:"destroy_vifs" cty:"destroy_vifs" hcl:"destroy_vifs"` } // FlatMapstructure returns a new FlatConfig. @@ -218,6 +219,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "platform_args": &hcldec.AttrSpec{Name: "platform_args", Type: cty.Map(cty.String), Required: false}, "install_timeout": &hcldec.AttrSpec{Name: "install_timeout", Type: cty.String, Required: false}, "source_path": &hcldec.AttrSpec{Name: "source_path", 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 index 0ebf109f..81678d19 100644 --- a/builder/xenserver/common/step_destroy_vifs.go +++ b/builder/xenserver/common/step_destroy_vifs.go @@ -1,40 +1,43 @@ package common import ( + "context" "fmt" - "github.com/mitchellh/multistep" - "github.com/mitchellh/packer/packer" - xsclient "github.com/xenserver/go-xenserver-client" + "log" + + "github.com/hashicorp/packer-plugin-sdk/multistep" + "github.com/hashicorp/packer-plugin-sdk/packer" ) type StepDestroyVIFs struct{} -func (self *StepDestroyVIFs) Run(state multistep.StateBag) multistep.StepAction { - config := state.Get("commonconfig").(CommonConfig) +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 := state.Get("ui").(packer.Ui) - client := state.Get("client").(xsclient.XenAPIClient) - ui.Say("Step: Destroy VIFs") uuid := state.Get("instance_uuid").(string) - instance, err := client.GetVMByUuid(uuid) + 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 := instance.GetVIFs() + 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 = vif.Destroy() + err = c.client.VIF.Destroy(c.session, vif) if err != nil { ui.Error(fmt.Sprintf("Error destroying VIF: %s", err.Error())) return multistep.ActionHalt