This commit is contained in:
Heinrich Kruger 2023-08-03 08:26:40 +02:00 committed by GitHub
commit c3578fe9c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 0 deletions

View File

@ -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"`

View File

@ -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
}

View File

@ -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) {}

View File

@ -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),
}

View File

@ -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),
}

View File

@ -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).