From 35b29847dc1529585e598445164fa17abf885433 Mon Sep 17 00:00:00 2001 From: bugbuilder Date: Sun, 9 Jul 2017 15:58:42 -0400 Subject: [PATCH] Cleaning code and adding insecure option to vSphere connection --- post-processor/vsphere-tpl/post-processor.go | 31 +++++++++++++------ .../vsphere-tpl/step_mark_as_template.go | 5 +-- .../vsphere-tpl/step_move_template.go | 6 ++-- ...here_client.go => step_pick_datacenter.go} | 25 +++++---------- 4 files changed, 32 insertions(+), 35 deletions(-) rename post-processor/vsphere-tpl/{step_vsphere_client.go => step_pick_datacenter.go} (58%) diff --git a/post-processor/vsphere-tpl/post-processor.go b/post-processor/vsphere-tpl/post-processor.go index d9d23e60f..855142abc 100644 --- a/post-processor/vsphere-tpl/post-processor.go +++ b/post-processor/vsphere-tpl/post-processor.go @@ -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 @@ -111,4 +122,4 @@ func (p *PostProcessor) configureURL() error { sdk.User = url.UserPassword(p.config.Username, p.config.Password) p.url = sdk return nil -} \ No newline at end of file +} diff --git a/post-processor/vsphere-tpl/step_mark_as_template.go b/post-processor/vsphere-tpl/step_mark_as_template.go index a5b02be9f..a3888849c 100644 --- a/post-processor/vsphere-tpl/step_mark_as_template.go +++ b/post-processor/vsphere-tpl/step_mark_as_template.go @@ -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) {} - diff --git a/post-processor/vsphere-tpl/step_move_template.go b/post-processor/vsphere-tpl/step_move_template.go index f38bfe8f5..80625d78e 100644 --- a/post-processor/vsphere-tpl/step_move_template.go +++ b/post-processor/vsphere-tpl/step_move_template.go @@ -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()) diff --git a/post-processor/vsphere-tpl/step_vsphere_client.go b/post-processor/vsphere-tpl/step_pick_datacenter.go similarity index 58% rename from post-processor/vsphere-tpl/step_vsphere_client.go rename to post-processor/vsphere-tpl/step_pick_datacenter.go index b28d2943f..c037fef96 100644 --- a/post-processor/vsphere-tpl/step_vsphere_client.go +++ b/post-processor/vsphere-tpl/step_pick_datacenter.go @@ -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) {} \ No newline at end of file +func (s *StepPickDatacenter) Cleanup(multistep.StateBag) {}