Cleaning code and adding insecure option to vSphere connection
This commit is contained in:
parent
c8747f138f
commit
35b29847dc
|
@ -1,19 +1,23 @@
|
|||
package vsphere_tpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/packer/common"
|
||||
"github.com/hashicorp/packer/helper/config"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/hashicorp/packer/template/interpolate"
|
||||
"github.com/mitchellh/multistep"
|
||||
"time"
|
||||
"github.com/vmware/govmomi"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
Host string `mapstructure:"host"`
|
||||
Insecure bool `mapstructure:"insecure"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
Datacenter string `mapstructure:"datacenter"`
|
||||
|
@ -67,28 +71,35 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|||
}
|
||||
|
||||
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("ui", ui)
|
||||
ctx := context.Background()
|
||||
|
||||
//FIXME I've trash environment, so I need to wait :(
|
||||
ui.Message("Waiting 10s for VMWare vSphere to start")
|
||||
time.Sleep(10 * time.Second)
|
||||
|
||||
c, err := govmomi.NewClient(ctx, p.url, p.config.Insecure)
|
||||
if err != nil {
|
||||
return artifact, true, fmt.Errorf("Error trying to connect: %s", err)
|
||||
}
|
||||
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("ui", ui)
|
||||
state.Put("context", ctx)
|
||||
state.Put("client", c)
|
||||
|
||||
steps := []multistep.Step{
|
||||
&StepVSphereClient{
|
||||
&StepPickDatacenter{
|
||||
Datacenter: p.config.Datacenter,
|
||||
VMName: p.config.VMName,
|
||||
Url: p.url,
|
||||
},
|
||||
&StepFetchVm{
|
||||
VMName: p.config.VMName,
|
||||
},
|
||||
&StepCreateFolder{
|
||||
Folder: p.config.Folder,
|
||||
Folder: p.config.Folder,
|
||||
},
|
||||
&StepMarkAsTemplate{},
|
||||
&StepMoveTemplate{
|
||||
Folder: p.config.Folder,
|
||||
Folder: p.config.Folder,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -102,7 +113,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
|
|||
}
|
||||
|
||||
func (p *PostProcessor) configureURL() error {
|
||||
sdk, err := url.Parse("https://" + p.config.Host + "/sdk")
|
||||
sdk, err := url.Parse(fmt.Sprintf("https://%v/sdk", p.config.Host))
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
|
|
|
@ -8,9 +8,7 @@ import (
|
|||
"github.com/vmware/govmomi/object"
|
||||
)
|
||||
|
||||
type StepMarkAsTemplate struct {
|
||||
Folder string
|
||||
}
|
||||
type StepMarkAsTemplate struct{}
|
||||
|
||||
func (s *StepMarkAsTemplate) Run(state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
|
@ -28,4 +26,3 @@ func (s *StepMarkAsTemplate) Run(state multistep.StateBag) multistep.StepAction
|
|||
}
|
||||
|
||||
func (s *StepMarkAsTemplate) Cleanup(multistep.StateBag) {}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package vsphere_tpl
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/mitchellh/multistep"
|
||||
|
@ -19,13 +19,13 @@ func (s *StepMoveTemplate) Run(state multistep.StateBag) multistep.StepAction {
|
|||
ui := state.Get("ui").(packer.Ui)
|
||||
ctx := state.Get("context").(context.Context)
|
||||
finder := state.Get("finder").(*find.Finder)
|
||||
vm := state.Get("vm").(*object.VirtualMachine)
|
||||
d := state.Get("datacenter").(string)
|
||||
vm := state.Get("vm").(*object.VirtualMachine)
|
||||
|
||||
if s.Folder != "" {
|
||||
ui.Say("Moving template...")
|
||||
|
||||
folder, err := finder.Folder(ctx, fmt.Sprintf("/%v/vm/%v", d, s.Folder))
|
||||
folder, err := finder.Folder(ctx, filepath.ToSlash(filepath.Join("/", d, "vm", s.Folder)))
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package vsphere_tpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/mitchellh/multistep"
|
||||
|
@ -9,26 +9,17 @@ import (
|
|||
"github.com/vmware/govmomi/find"
|
||||
)
|
||||
|
||||
type StepVSphereClient struct {
|
||||
Url *url.URL
|
||||
type StepPickDatacenter struct {
|
||||
Datacenter string
|
||||
VMName string
|
||||
}
|
||||
|
||||
func (s *StepVSphereClient) Run(state multistep.StateBag) multistep.StepAction {
|
||||
func (s *StepPickDatacenter) Run(state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
ctx := context.Background()
|
||||
cli, err := govmomi.NewClient(ctx, s.Url, true)
|
||||
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
cli := state.Get("client").(*govmomi.Client)
|
||||
ctx := state.Get("context").(context.Context)
|
||||
finder := find.NewFinder(cli.Client, false)
|
||||
datacenter, err := finder.DatacenterOrDefault(ctx, s.Datacenter)
|
||||
|
||||
datacenter, err := finder.DatacenterOrDefault(ctx, s.Datacenter)
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
|
@ -37,11 +28,9 @@ func (s *StepVSphereClient) Run(state multistep.StateBag) multistep.StepAction {
|
|||
}
|
||||
|
||||
finder.SetDatacenter(datacenter)
|
||||
|
||||
state.Put("datacenter", datacenter.Name())
|
||||
state.Put("finder", finder)
|
||||
state.Put("context", ctx)
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s *StepVSphereClient) Cleanup(multistep.StateBag) {}
|
||||
func (s *StepPickDatacenter) Cleanup(multistep.StateBag) {}
|
Loading…
Reference in New Issue