From feb8791decfa50d1cdce0c5913221b9d058d272e Mon Sep 17 00:00:00 2001 From: Dongyu Zheng Date: Wed, 26 Oct 2016 22:43:37 -0700 Subject: [PATCH] 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 6907e18..4c9f9e4 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 0000000..0ebf109 --- /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 707cead..0c799ac 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 00235d2..cceeada 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 9c62c3f..bfbb2c9 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).