2017-07-09 14:12:37 -04:00
|
|
|
package vsphere_tpl
|
|
|
|
|
|
|
|
import (
|
2017-07-09 15:58:42 -04:00
|
|
|
"context"
|
2017-07-09 14:12:37 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2017-07-09 20:33:03 -04:00
|
|
|
"strings"
|
2017-07-09 22:13:31 -04:00
|
|
|
"time"
|
2017-07-09 14:12:37 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2017-07-09 14:56:39 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2017-07-09 15:58:42 -04:00
|
|
|
"github.com/vmware/govmomi"
|
2017-07-09 14:12:37 -04:00
|
|
|
)
|
2017-07-09 15:58:42 -04:00
|
|
|
|
2017-07-09 20:33:03 -04:00
|
|
|
var builtins = map[string]string{
|
|
|
|
"mitchellh.vmware-esx": "vmware",
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:12:37 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Host string `mapstructure:"host"`
|
2017-07-09 15:58:42 -04:00
|
|
|
Insecure bool `mapstructure:"insecure"`
|
2017-07-09 14:12:37 -04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:56:39 -04:00
|
|
|
if err := p.configureURL(); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, err)
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:12:37 -04:00
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
2017-07-09 20:33:03 -04:00
|
|
|
if _, ok := builtins[artifact.BuilderId()]; !ok {
|
|
|
|
return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
|
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2017-07-09 20:33:03 -04:00
|
|
|
source := ""
|
|
|
|
for _, path := range artifact.Files() {
|
|
|
|
if strings.HasSuffix(path, ".vmx") {
|
|
|
|
source = path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//We give a vSphere-ESXI 10s to sync
|
2017-07-09 14:56:39 -04:00
|
|
|
ui.Message("Waiting 10s for VMWare vSphere to start")
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
2017-07-09 20:33:03 -04:00
|
|
|
ctx := context.Background()
|
2017-07-09 15:58:42 -04:00
|
|
|
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)
|
|
|
|
|
2017-07-09 14:56:39 -04:00
|
|
|
steps := []multistep.Step{
|
2017-07-09 20:33:03 -04:00
|
|
|
&StepChooseDatacenter{
|
2017-07-09 14:56:39 -04:00
|
|
|
Datacenter: p.config.Datacenter,
|
|
|
|
},
|
|
|
|
&StepFetchVm{
|
|
|
|
VMName: p.config.VMName,
|
2017-07-09 20:33:03 -04:00
|
|
|
Source: source,
|
2017-07-09 14:56:39 -04:00
|
|
|
},
|
|
|
|
&StepCreateFolder{
|
2017-07-09 20:33:03 -04:00
|
|
|
Folder: p.config.Folder,
|
2017-07-09 14:56:39 -04:00
|
|
|
},
|
|
|
|
&StepMarkAsTemplate{},
|
|
|
|
&StepMoveTemplate{
|
2017-07-09 20:33:03 -04:00
|
|
|
Folder: p.config.Folder,
|
2017-07-09 14:56:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runner := &multistep.BasicRunner{Steps: steps}
|
|
|
|
runner.Run(state)
|
2017-07-09 14:12:37 -04:00
|
|
|
|
2017-07-09 14:56:39 -04:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return artifact, true, rawErr.(error)
|
|
|
|
}
|
2017-07-09 14:12:37 -04:00
|
|
|
return artifact, true, nil
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:56:39 -04:00
|
|
|
func (p *PostProcessor) configureURL() error {
|
2017-07-09 15:58:42 -04:00
|
|
|
sdk, err := url.Parse(fmt.Sprintf("https://%v/sdk", p.config.Host))
|
2017-07-09 14:56:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sdk.User = url.UserPassword(p.config.Username, p.config.Password)
|
|
|
|
p.url = sdk
|
|
|
|
return nil
|
2017-07-09 15:58:42 -04:00
|
|
|
}
|