move setup back into builder
This commit is contained in:
parent
0e594a5d31
commit
a12ff09f0a
37
builder.go
37
builder.go
|
@ -10,6 +10,10 @@ import (
|
||||||
"github.com/hashicorp/packer/helper/communicator"
|
"github.com/hashicorp/packer/helper/communicator"
|
||||||
gossh "golang.org/x/crypto/ssh"
|
gossh "golang.org/x/crypto/ssh"
|
||||||
"github.com/hashicorp/packer/communicator/ssh"
|
"github.com/hashicorp/packer/communicator/ssh"
|
||||||
|
"github.com/vmware/govmomi"
|
||||||
|
"context"
|
||||||
|
"net/url"
|
||||||
|
"github.com/vmware/govmomi/find"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
|
@ -28,16 +32,39 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
||||||
// Set up the state.
|
|
||||||
state := new(multistep.BasicStateBag)
|
state := new(multistep.BasicStateBag)
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
state.Put("ui", ui)
|
||||||
|
ctx := context.TODO()
|
||||||
|
state.Put("ctx", ctx)
|
||||||
|
|
||||||
|
vcenter_url, err := url.Parse(b.config.Url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
vcenter_url.User = url.UserPassword(b.config.Username, b.config.Password)
|
||||||
|
client, err := govmomi.NewClient(ctx, vcenter_url,true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
state.Put("client", client)
|
||||||
|
|
||||||
|
finder := find.NewFinder(client.Client, false)
|
||||||
|
dc, err := finder.DatacenterOrDefault(ctx, b.config.DCName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
finder.SetDatacenter(dc)
|
||||||
|
state.Put("finder", finder)
|
||||||
|
state.Put("dc", dc)
|
||||||
|
|
||||||
|
vmSrc, err := finder.VirtualMachine(ctx, b.config.Template)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
state.Put("vmSrc", vmSrc)
|
||||||
|
|
||||||
// Build the steps.
|
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&StepSetup{
|
|
||||||
config: b.config,
|
|
||||||
},
|
|
||||||
&StepCloneVM{
|
&StepCloneVM{
|
||||||
config: b.config,
|
config: b.config,
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/vmware/govmomi"
|
|
||||||
"context"
|
"context"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/vmware/govmomi/vim25/types"
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
|
@ -14,13 +13,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type CloneParameters struct {
|
type CloneParameters struct {
|
||||||
client *govmomi.Client
|
ctx context.Context
|
||||||
|
vmSrc *object.VirtualMachine
|
||||||
|
vmName string
|
||||||
folder *object.Folder
|
folder *object.Folder
|
||||||
resourcePool *object.ResourcePool
|
resourcePool *object.ResourcePool
|
||||||
datastore *object.Datastore
|
datastore *object.Datastore
|
||||||
vmSrc *object.VirtualMachine
|
|
||||||
ctx context.Context
|
|
||||||
vmName string
|
|
||||||
linkedClone bool
|
linkedClone bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,12 +28,10 @@ type StepCloneVM struct{
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
client := state.Get("client").(*govmomi.Client)
|
|
||||||
ctx := state.Get("ctx").(context.Context)
|
ctx := state.Get("ctx").(context.Context)
|
||||||
finder := state.Get("finder").(*find.Finder)
|
finder := state.Get("finder").(*find.Finder)
|
||||||
dc := state.Get("dc").(*object.Datacenter)
|
dc := state.Get("dc").(*object.Datacenter)
|
||||||
vmSrc := state.Get("vmSrc").(*object.VirtualMachine)
|
vmSrc := state.Get("vmSrc").(*object.VirtualMachine)
|
||||||
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
ui.Say("start cloning...")
|
ui.Say("start cloning...")
|
||||||
|
|
||||||
|
@ -64,13 +60,12 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
vm, err := cloneVM(&CloneParameters{
|
vm, err := cloneVM(&CloneParameters{
|
||||||
client: client,
|
ctx: ctx,
|
||||||
|
vmSrc: vmSrc,
|
||||||
|
vmName: s.config.VMName,
|
||||||
folder: folder,
|
folder: folder,
|
||||||
resourcePool: pool,
|
resourcePool: pool,
|
||||||
datastore: datastore,
|
datastore: datastore,
|
||||||
vmSrc: vmSrc,
|
|
||||||
ctx: ctx,
|
|
||||||
vmName: s.config.VMName,
|
|
||||||
linkedClone: s.config.LinkedClone,
|
linkedClone: s.config.LinkedClone,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -79,7 +74,6 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
state.Put("vm", vm)
|
state.Put("vm", vm)
|
||||||
state.Put("ctx", ctx)
|
|
||||||
s.success = true
|
s.success = true
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
@ -158,6 +152,6 @@ func cloneVM(params *CloneParameters) (vm *object.VirtualMachine, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
vm = object.NewVirtualMachine(params.client.Client, info.Result.(types.ManagedObjectReference))
|
vm = object.NewVirtualMachine(params.vmSrc.Client(), info.Result.(types.ManagedObjectReference))
|
||||||
return vm, nil
|
return vm, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,73 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/mitchellh/multistep"
|
|
||||||
"github.com/hashicorp/packer/packer"
|
|
||||||
"github.com/vmware/govmomi/find"
|
|
||||||
"fmt"
|
|
||||||
"github.com/vmware/govmomi"
|
|
||||||
"context"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
type StepSetup struct{
|
|
||||||
config *Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StepSetup) Run(state multistep.StateBag) multistep.StepAction {
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
|
||||||
ui.Say("setup...")
|
|
||||||
|
|
||||||
// Prepare entities: client (authentification), finder, folder, virtual machine
|
|
||||||
client, ctx, err := createClient(s.config.Url, s.config.Username, s.config.Password)
|
|
||||||
if err != nil {
|
|
||||||
state.Put("error", err)
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up finder
|
|
||||||
finder := find.NewFinder(client.Client, false)
|
|
||||||
dc, err := finder.DatacenterOrDefault(ctx, s.config.DCName)
|
|
||||||
if err != nil {
|
|
||||||
state.Put("error", err)
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
finder.SetDatacenter(dc)
|
|
||||||
|
|
||||||
// Get source VM
|
|
||||||
vmSrc, err := finder.VirtualMachine(ctx, s.config.Template)
|
|
||||||
if err != nil {
|
|
||||||
state.Put("error", err)
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
|
|
||||||
state.Put("client", client)
|
|
||||||
state.Put("ctx", ctx)
|
|
||||||
state.Put("finder", finder)
|
|
||||||
state.Put("dc", dc)
|
|
||||||
state.Put("vmSrc", vmSrc)
|
|
||||||
return multistep.ActionContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StepSetup) Cleanup(state multistep.StateBag) {}
|
|
||||||
|
|
||||||
func createClient(URL, username, password string) (*govmomi.Client, context.Context, error) {
|
|
||||||
// create context
|
|
||||||
ctx := context.TODO() // an empty, default context (for those, who is unsure)
|
|
||||||
|
|
||||||
// create a client
|
|
||||||
// (connected to the specified URL,
|
|
||||||
// logged in with the username-password)
|
|
||||||
u, err := url.Parse(URL) // create a URL object from string
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
u.User = url.UserPassword(username, password) // set username and password for automatical authentification
|
|
||||||
fmt.Println(u.String())
|
|
||||||
client, err := govmomi.NewClient(ctx, u,true) // creating a client (logs in with given uname&pswd)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return client, ctx, nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue