From dc2124aa2a425a96607925ff40e03c8902334c7f Mon Sep 17 00:00:00 2001 From: bugbuilder Date: Sun, 9 Jul 2017 14:12:37 -0400 Subject: [PATCH] Initial vSphere-tpl post-processor --- command/plugin.go | 2 + post-processor/vsphere-tpl/post-processor.go | 66 ++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 post-processor/vsphere-tpl/post-processor.go diff --git a/command/plugin.go b/command/plugin.go index 4bc8b1936..11d2a16af 100644 --- a/command/plugin.go +++ b/command/plugin.go @@ -54,6 +54,7 @@ import ( vagrantpostprocessor "github.com/hashicorp/packer/post-processor/vagrant" vagrantcloudpostprocessor "github.com/hashicorp/packer/post-processor/vagrant-cloud" vspherepostprocessor "github.com/hashicorp/packer/post-processor/vsphere" + vspheretplpostprocessor "github.com/hashicorp/packer/post-processor/vsphere-tpl" ansibleprovisioner "github.com/hashicorp/packer/provisioner/ansible" ansiblelocalprovisioner "github.com/hashicorp/packer/provisioner/ansible-local" chefclientprovisioner "github.com/hashicorp/packer/provisioner/chef-client" @@ -136,6 +137,7 @@ var PostProcessors = map[string]packer.PostProcessor{ "vagrant": new(vagrantpostprocessor.PostProcessor), "vagrant-cloud": new(vagrantcloudpostprocessor.PostProcessor), "vsphere": new(vspherepostprocessor.PostProcessor), + "vsphere-tpl": new(vspheretplpostprocessor.PostProcessor), } var pluginRegexp = regexp.MustCompile("packer-(builder|post-processor|provisioner)-(.+)") diff --git a/post-processor/vsphere-tpl/post-processor.go b/post-processor/vsphere-tpl/post-processor.go new file mode 100644 index 000000000..a7b8da056 --- /dev/null +++ b/post-processor/vsphere-tpl/post-processor.go @@ -0,0 +1,66 @@ +package vsphere_tpl + +import ( + "fmt" + "net/url" + + "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/config" + "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/template/interpolate" +) +type Config struct { + common.PackerConfig `mapstructure:",squash"` + Host string `mapstructure:"host"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + Datacenter string `mapstructure:"datacenter"` + VMName string `mapstructure:"vm_name"` + Folder string `mapstructure:"folder"` + + ctx interpolate.Context +} + +type PostProcessor struct { + config Config + url *url.URL +} + +func (p *PostProcessor) Configure(raws ...interface{}) error { + err := config.Decode(&p.config, &config.DecodeOpts{ + Interpolate: true, + InterpolateContext: &p.config.ctx, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{}, + }, + }, raws...) + + if err != nil { + return err + } + + errs := new(packer.MultiError) + vc := map[string]*string{ + "host": &p.config.Host, + "username": &p.config.Username, + "password": &p.config.Password, + } + + for key, ptr := range vc { + if *ptr == "" { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("%s must be set", key)) + } + } + + if len(errs.Errors) > 0 { + return errs + } + return nil +} + +func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { + + return artifact, true, nil +} +