2013-11-11 03:56:51 -05:00
|
|
|
package vsphere
|
|
|
|
|
|
|
|
import (
|
2013-11-18 18:57:31 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2013-11-11 03:56:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var builtins = map[string]string{
|
2013-11-18 18:57:31 -05:00
|
|
|
"mitchellh.vmware": "vmware",
|
2013-11-11 03:56:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2013-11-18 18:57:31 -05:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2013-11-18 19:04:38 -05:00
|
|
|
Insecure bool `mapstructure:"insecure"`
|
2013-11-29 11:33:26 -05:00
|
|
|
Cluster string `mapstructure:"cluster"`
|
2013-11-18 19:04:38 -05:00
|
|
|
Datacenter string `mapstructure:"datacenter"`
|
|
|
|
Datastore string `mapstructure:"datastore"`
|
2013-11-29 11:33:26 -05:00
|
|
|
Debug bool `mapstructure:"debug"`
|
2013-11-18 19:04:38 -05:00
|
|
|
Host string `mapstructure:"host"`
|
|
|
|
Password string `mapstructure:"password"`
|
2013-11-29 11:33:26 -05:00
|
|
|
ResourcePool string `mapstructure:"resource_pool"`
|
2013-11-18 19:04:38 -05:00
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
VMFolder string `mapstructure:"vm_folder"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
2013-11-18 19:23:28 -05:00
|
|
|
VMNetwork string `mapstructure:"vm_network"`
|
2013-12-02 06:39:52 -05:00
|
|
|
|
|
|
|
tpl *packer.ConfigTemplate
|
2013-11-11 03:56:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
2013-11-18 18:57:31 -05:00
|
|
|
config Config
|
2013-11-11 03:56:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2013-11-18 18:57:31 -05:00
|
|
|
_, err := common.DecodeConfig(&p.config, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-02 06:39:52 -05:00
|
|
|
|
|
|
|
p.config.tpl, err = packer.NewConfigTemplate()
|
2013-11-18 18:57:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-02 06:39:52 -05:00
|
|
|
p.config.tpl.UserVars = p.config.PackerUserVars
|
2013-11-18 18:57:31 -05:00
|
|
|
|
|
|
|
// Accumulate any errors
|
|
|
|
errs := new(packer.MultiError)
|
2013-12-02 06:39:52 -05:00
|
|
|
|
2013-11-18 19:04:38 -05:00
|
|
|
if _, err := exec.LookPath("ovftool"); err != nil {
|
2013-11-18 18:57:31 -05:00
|
|
|
errs = packer.MultiErrorAppend(
|
2013-11-18 19:04:38 -05:00
|
|
|
errs, fmt.Errorf("ovftool not found: %s", err))
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
validates := map[string]*string{
|
2013-11-29 11:33:26 -05:00
|
|
|
"cluster": &p.config.Cluster,
|
|
|
|
"datacenter": &p.config.Datacenter,
|
|
|
|
"datastore": &p.config.Datastore,
|
|
|
|
"host": &p.config.Host,
|
|
|
|
"vm_network": &p.config.VMNetwork,
|
|
|
|
"password": &p.config.Password,
|
|
|
|
"resource_pool": &p.config.ResourcePool,
|
|
|
|
"username": &p.config.Username,
|
|
|
|
"vm_folder": &p.config.VMFolder,
|
|
|
|
"vm_name": &p.config.VMName,
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for n := range validates {
|
|
|
|
if *validates[n] == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2013-11-18 19:04:38 -05:00
|
|
|
errs, fmt.Errorf("%s must be set", n))
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-11-11 03:56:51 -05:00
|
|
|
}
|
|
|
|
|
2013-11-18 18:57:31 -05:00
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
2013-11-18 19:04:38 -05:00
|
|
|
if _, ok := builtins[artifact.BuilderId()]; !ok {
|
2013-11-18 18:57:31 -05:00
|
|
|
return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
|
|
|
|
}
|
|
|
|
|
|
|
|
vmx := ""
|
|
|
|
for _, path := range artifact.Files() {
|
|
|
|
if strings.HasSuffix(path, ".vmx") {
|
|
|
|
vmx = path
|
2013-11-18 19:04:38 -05:00
|
|
|
break
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmx == "" {
|
|
|
|
return nil, false, fmt.Errorf("VMX file not found")
|
|
|
|
}
|
|
|
|
|
2013-12-02 07:53:13 -05:00
|
|
|
// Get user variables from template
|
2013-12-02 06:39:52 -05:00
|
|
|
vm_name, err := p.config.tpl.Process(p.config.VMName, p.config.PackerUserVars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("Failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
username, err := p.config.tpl.Process(p.config.Username, p.config.PackerUserVars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("Failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
password, err := p.config.tpl.Process(p.config.Password, p.config.PackerUserVars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("Failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-02 07:53:13 -05:00
|
|
|
datastore, err := p.config.tpl.Process(p.config.Datastore, p.config.PackerUserVars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("Failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-11-18 19:04:38 -05:00
|
|
|
ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
|
|
|
|
"--acceptAllEulas",
|
2013-12-02 06:39:52 -05:00
|
|
|
fmt.Sprintf("--name=%s", vm_name),
|
2013-12-02 07:53:13 -05:00
|
|
|
fmt.Sprintf("--datastore=%s", datastore),
|
2013-11-18 19:04:38 -05:00
|
|
|
fmt.Sprintf("--network=%s", p.config.VMNetwork),
|
|
|
|
fmt.Sprintf("--vmFolder=%s", p.config.VMFolder),
|
2013-11-29 11:33:26 -05:00
|
|
|
fmt.Sprintf("%s", vmx),
|
|
|
|
fmt.Sprintf("vi://%s:%s@%s/%s/host/%s/Resources/%s",
|
2013-12-02 06:39:52 -05:00
|
|
|
username,
|
|
|
|
password,
|
2013-11-18 19:04:38 -05:00
|
|
|
p.config.Host,
|
|
|
|
p.config.Datacenter,
|
2013-11-29 11:33:26 -05:00
|
|
|
p.config.Cluster,
|
|
|
|
p.config.ResourcePool),
|
|
|
|
}
|
2013-12-02 06:39:52 -05:00
|
|
|
|
2013-11-29 11:33:26 -05:00
|
|
|
if p.config.Debug {
|
|
|
|
ui.Message(fmt.Sprintf("DEBUG: %s", args))
|
2013-11-18 19:04:38 -05:00
|
|
|
}
|
2013-11-18 18:57:31 -05:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
2013-11-18 19:04:38 -05:00
|
|
|
cmd := exec.Command("ovftool", args...)
|
2013-11-18 18:57:31 -05:00
|
|
|
cmd.Stdout = &out
|
2013-11-18 19:04:38 -05:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, false, fmt.Errorf("Failed: %s\nStdout: %s", err, out.String())
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("%s", out.String()))
|
|
|
|
|
|
|
|
return artifact, false, nil
|
2013-11-11 03:56:51 -05:00
|
|
|
}
|