Cleaning code and adding insecure option to vSphere connection

This commit is contained in:
bugbuilder 2017-07-09 15:58:42 -04:00
parent c8747f138f
commit 35b29847dc
4 changed files with 32 additions and 35 deletions

View File

@ -1,19 +1,23 @@
package vsphere_tpl package vsphere_tpl
import ( import (
"context"
"fmt" "fmt"
"net/url" "net/url"
"time"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"time" "github.com/vmware/govmomi"
) )
type Config struct { type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
Host string `mapstructure:"host"` Host string `mapstructure:"host"`
Insecure bool `mapstructure:"insecure"`
Username string `mapstructure:"username"` Username string `mapstructure:"username"`
Password string `mapstructure:"password"` Password string `mapstructure:"password"`
Datacenter string `mapstructure:"datacenter"` 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) { func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
state := new(multistep.BasicStateBag) ctx := context.Background()
state.Put("ui", ui)
//FIXME I've trash environment, so I need to wait :( //FIXME I've trash environment, so I need to wait :(
ui.Message("Waiting 10s for VMWare vSphere to start") ui.Message("Waiting 10s for VMWare vSphere to start")
time.Sleep(10 * time.Second) 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{ steps := []multistep.Step{
&StepVSphereClient{ &StepPickDatacenter{
Datacenter: p.config.Datacenter, Datacenter: p.config.Datacenter,
VMName: p.config.VMName,
Url: p.url,
}, },
&StepFetchVm{ &StepFetchVm{
VMName: p.config.VMName, VMName: p.config.VMName,
}, },
&StepCreateFolder{ &StepCreateFolder{
Folder: p.config.Folder, Folder: p.config.Folder,
}, },
&StepMarkAsTemplate{}, &StepMarkAsTemplate{},
&StepMoveTemplate{ &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 { 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 { if err != nil {
return nil return nil
@ -111,4 +122,4 @@ func (p *PostProcessor) configureURL() error {
sdk.User = url.UserPassword(p.config.Username, p.config.Password) sdk.User = url.UserPassword(p.config.Username, p.config.Password)
p.url = sdk p.url = sdk
return nil return nil
} }

View File

@ -8,9 +8,7 @@ import (
"github.com/vmware/govmomi/object" "github.com/vmware/govmomi/object"
) )
type StepMarkAsTemplate struct { type StepMarkAsTemplate struct{}
Folder string
}
func (s *StepMarkAsTemplate) Run(state multistep.StateBag) multistep.StepAction { func (s *StepMarkAsTemplate) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) 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) {} func (s *StepMarkAsTemplate) Cleanup(multistep.StateBag) {}

View File

@ -2,7 +2,7 @@ package vsphere_tpl
import ( import (
"context" "context"
"fmt" "path/filepath"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
@ -19,13 +19,13 @@ func (s *StepMoveTemplate) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ctx := state.Get("context").(context.Context) ctx := state.Get("context").(context.Context)
finder := state.Get("finder").(*find.Finder) finder := state.Get("finder").(*find.Finder)
vm := state.Get("vm").(*object.VirtualMachine)
d := state.Get("datacenter").(string) d := state.Get("datacenter").(string)
vm := state.Get("vm").(*object.VirtualMachine)
if s.Folder != "" { if s.Folder != "" {
ui.Say("Moving template...") 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 { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())

View File

@ -1,7 +1,7 @@
package vsphere_tpl package vsphere_tpl
import ( import (
"context" "context"
"net/url"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
@ -9,26 +9,17 @@ import (
"github.com/vmware/govmomi/find" "github.com/vmware/govmomi/find"
) )
type StepVSphereClient struct { type StepPickDatacenter struct {
Url *url.URL
Datacenter string 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) ui := state.Get("ui").(packer.Ui)
ctx := context.Background() cli := state.Get("client").(*govmomi.Client)
cli, err := govmomi.NewClient(ctx, s.Url, true) ctx := state.Get("context").(context.Context)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
finder := find.NewFinder(cli.Client, false) finder := find.NewFinder(cli.Client, false)
datacenter, err := finder.DatacenterOrDefault(ctx, s.Datacenter)
datacenter, err := finder.DatacenterOrDefault(ctx, s.Datacenter)
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -37,11 +28,9 @@ func (s *StepVSphereClient) Run(state multistep.StateBag) multistep.StepAction {
} }
finder.SetDatacenter(datacenter) finder.SetDatacenter(datacenter)
state.Put("datacenter", datacenter.Name()) state.Put("datacenter", datacenter.Name())
state.Put("finder", finder) state.Put("finder", finder)
state.Put("context", ctx)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *StepVSphereClient) Cleanup(multistep.StateBag) {} func (s *StepPickDatacenter) Cleanup(multistep.StateBag) {}