DEV: use proper interfaces for vmCreator

This commit is contained in:
Jeff Wong 2020-09-11 16:45:16 -07:00
parent 8aeafa986f
commit aa5eb770d0
4 changed files with 12 additions and 14 deletions

View File

@ -34,7 +34,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
state := new(multistep.BasicStateBag)
state.Put("clone-config", &b.config)
state.Put("vm-creator", &cloneVMCreator{})
state.Put("comm", &b.config.Comm)
preSteps := []multistep.Step{
@ -46,14 +45,12 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
}
postSteps := []multistep.Step{}
sb := proxmox.NewSharedBuilder(BuilderID, b.config.Config, preSteps, postSteps)
sb := proxmox.NewSharedBuilder(BuilderID, b.config.Config, preSteps, postSteps, &cloneVMCreator{})
return sb.Run(ctx, ui, hook, state)
}
type cloneVMCreator struct{}
var _ proxmox.ProxmoxVMCreator = &cloneVMCreator{}
func (*cloneVMCreator) Create(vmRef *proxmoxapi.VmRef, config proxmoxapi.ConfigQemu, state multistep.StateBag) error {
client := state.Get("proxmoxClient").(*proxmoxapi.Client)
c := state.Get("clone-config").(*Config)

View File

@ -13,12 +13,13 @@ import (
"github.com/hashicorp/packer/packer"
)
func NewSharedBuilder(id string, config Config, preSteps []multistep.Step, postSteps []multistep.Step) *Builder {
func NewSharedBuilder(id string, config Config, preSteps []multistep.Step, postSteps []multistep.Step, vmCreator ProxmoxVMCreator) *Builder {
return &Builder{
id: id,
config: config,
preSteps: preSteps,
postSteps: postSteps,
vmCreator: vmCreator,
}
}
@ -29,6 +30,7 @@ type Builder struct {
postSteps []multistep.Step
runner multistep.Runner
proxmoxClient *proxmox.Client
vmCreator ProxmoxVMCreator
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook, state multistep.StateBag) (packer.Artifact, error) {
@ -59,7 +61,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook, state
// Build the steps
coreSteps := []multistep.Step{
&stepStartVM{},
&stepStartVM{
vmCreator: b.vmCreator,
},
&common.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,

View File

@ -15,7 +15,9 @@ import (
//
// It sets the vmRef state which is used throughout the later steps to reference the VM
// in API calls.
type stepStartVM struct{}
type stepStartVM struct{
vmCreator ProxmoxVMCreator
}
type ProxmoxVMCreator interface {
Create(*proxmox.VmRef, proxmox.ConfigQemu, multistep.StateBag) error
@ -36,8 +38,6 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
kvm = false
}
vmStarter := state.Get("vm-creator").(ProxmoxVMCreator)
ui.Say("Creating VM")
config := proxmox.ConfigQemu{
Name: c.VMName,
@ -81,7 +81,7 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
vmRef.SetPool(c.Pool)
}
err := vmStarter.Create(vmRef, config, state)
err := s.vmCreator.Create(vmRef, config, state)
if err != nil {
err := fmt.Errorf("Error creating VM: %s", err)
state.Put("error", err)

View File

@ -34,7 +34,6 @@ const downloadPathKey = "downloaded_iso_path"
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
state := new(multistep.BasicStateBag)
state.Put("iso-config", &b.config)
state.Put("vm-creator", &isoVMCreator{})
preSteps := []multistep.Step{
&common.StepDownload{
@ -64,14 +63,12 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&stepFinalizeISOTemplate{},
}
sb := proxmox.NewSharedBuilder(BuilderID, b.config.Config, preSteps, postSteps)
sb := proxmox.NewSharedBuilder(BuilderID, b.config.Config, preSteps, postSteps, &isoVMCreator{})
return sb.Run(ctx, ui, hook, state)
}
type isoVMCreator struct{}
var _ proxmox.ProxmoxVMCreator = &isoVMCreator{}
func (*isoVMCreator) Create(vmRef *proxmoxapi.VmRef, config proxmoxapi.ConfigQemu, state multistep.StateBag) error {
isoFile := state.Get("iso_file").(string)
config.QemuIso = isoFile