2013-11-11 03:56:51 -05:00
|
|
|
package vsphere
|
|
|
|
|
|
|
|
import (
|
2013-11-18 18:57:31 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-05-15 07:53:17 -04:00
|
|
|
"log"
|
2013-11-18 18:57:31 -05:00
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2014-05-15 08:04:27 -04:00
|
|
|
"net/url"
|
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-12-11 17:01:14 -05:00
|
|
|
Insecure bool `mapstructure:"insecure"`
|
|
|
|
Cluster string `mapstructure:"cluster"`
|
|
|
|
Datacenter string `mapstructure:"datacenter"`
|
|
|
|
Datastore string `mapstructure:"datastore"`
|
2014-04-22 00:10:20 -04:00
|
|
|
DiskMode string `mapstructure:"disk_mode"`
|
2013-12-11 17:01:14 -05:00
|
|
|
Host string `mapstructure:"host"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
ResourcePool string `mapstructure:"resource_pool"`
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
VMFolder string `mapstructure:"vm_folder"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
|
|
|
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
|
|
|
|
2014-01-03 02:07:36 -05:00
|
|
|
// Defaults
|
|
|
|
if p.config.DiskMode == "" {
|
|
|
|
p.config.DiskMode = "thick"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-30 18:52:01 -04:00
|
|
|
// First define all our templatable parameters that are _required_
|
2013-12-11 17:01:14 -05:00
|
|
|
templates := map[string]*string{
|
|
|
|
"cluster": &p.config.Cluster,
|
|
|
|
"datacenter": &p.config.Datacenter,
|
2014-01-03 02:07:36 -05:00
|
|
|
"diskmode": &p.config.DiskMode,
|
2013-12-11 17:01:14 -05:00
|
|
|
"host": &p.config.Host,
|
|
|
|
"password": &p.config.Password,
|
2013-11-29 11:33:26 -05:00
|
|
|
"resource_pool": &p.config.ResourcePool,
|
2013-12-11 17:01:14 -05:00
|
|
|
"username": &p.config.Username,
|
|
|
|
"vm_name": &p.config.VMName,
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
2013-12-11 17:01:14 -05:00
|
|
|
for key, ptr := range templates {
|
|
|
|
if *ptr == "" {
|
2013-11-18 18:57:31 -05:00
|
|
|
errs = packer.MultiErrorAppend(
|
2013-12-11 17:01:14 -05:00
|
|
|
errs, fmt.Errorf("%s must be set", key))
|
|
|
|
}
|
2014-04-30 08:02:09 -04:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:52:01 -04:00
|
|
|
// Then define the ones that are optional
|
|
|
|
templates["datastore"] = &p.config.Datastore
|
|
|
|
templates["vm_network"] = &p.config.VMNetwork
|
|
|
|
templates["vm_folder"] = &p.config.VMFolder
|
2013-12-11 17:01:14 -05:00
|
|
|
|
2014-04-30 18:52:01 -04:00
|
|
|
// Template process
|
2014-04-30 08:02:09 -04:00
|
|
|
for key, ptr := range templates {
|
2013-12-11 17:01:14 -05:00
|
|
|
*ptr, err = p.config.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", key, err))
|
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-11-18 19:04:38 -05:00
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
|
|
|
|
"--acceptAllEulas",
|
2013-12-11 17:01:14 -05:00
|
|
|
fmt.Sprintf("--name=%s", p.config.VMName),
|
|
|
|
fmt.Sprintf("--datastore=%s", p.config.Datastore),
|
2014-01-03 02:07:36 -05:00
|
|
|
fmt.Sprintf("--diskMode=%s", p.config.DiskMode),
|
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",
|
2014-05-15 08:04:27 -04:00
|
|
|
url.QueryEscape(p.config.Username),
|
2013-12-11 17:01:14 -05:00
|
|
|
p.config.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-12-11 17:01:14 -05:00
|
|
|
ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))
|
2013-11-18 18:57:31 -05:00
|
|
|
var out bytes.Buffer
|
2014-05-15 07:53:17 -04:00
|
|
|
log.Printf("Starting ovftool with parameters: %s", strings.Join(args, " "))
|
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
|
|
|
}
|