2013-11-11 03:56:51 -05:00
|
|
|
package vsphere
|
|
|
|
|
|
|
|
import (
|
2013-11-18 18:57:31 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-07-17 15:33:09 -04:00
|
|
|
"log"
|
|
|
|
"net/url"
|
2013-11-18 18:57:31 -05:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-05-27 17:56:22 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/helper/config"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-11-11 03:56:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var builtins = map[string]string{
|
2015-02-13 05:13:58 -05:00
|
|
|
"mitchellh.vmware": "vmware",
|
|
|
|
"mitchellh.vmware-esx": "vmware",
|
2013-11-11 03:56:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2013-11-18 18:57:31 -05:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2015-06-24 06:50:19 -04:00
|
|
|
Cluster string `mapstructure:"cluster"`
|
|
|
|
Datacenter string `mapstructure:"datacenter"`
|
|
|
|
Datastore string `mapstructure:"datastore"`
|
|
|
|
DiskMode string `mapstructure:"disk_mode"`
|
|
|
|
Host string `mapstructure:"host"`
|
|
|
|
Insecure bool `mapstructure:"insecure"`
|
|
|
|
Options []string `mapstructure:"options"`
|
|
|
|
Overwrite bool `mapstructure:"overwrite"`
|
|
|
|
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
|
|
|
|
2015-05-27 17:56:22 -04:00
|
|
|
ctx interpolate.Context
|
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 {
|
2015-05-27 17:56:22 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:24:27 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-05-27 17:56:22 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-11-18 18:57:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-02 06:39:52 -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{
|
2015-06-22 15:37:52 -04:00
|
|
|
"cluster": &p.config.Cluster,
|
|
|
|
"datacenter": &p.config.Datacenter,
|
|
|
|
"diskmode": &p.config.DiskMode,
|
|
|
|
"host": &p.config.Host,
|
|
|
|
"password": &p.config.Password,
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2015-02-13 05:13:58 -05:00
|
|
|
source := ""
|
2013-11-18 18:57:31 -05:00
|
|
|
for _, path := range artifact.Files() {
|
2015-02-13 05:13:58 -05:00
|
|
|
if strings.HasSuffix(path, ".vmx") || strings.HasSuffix(path, ".ovf") || strings.HasSuffix(path, ".ova") {
|
|
|
|
source = path
|
2013-11-18 19:04:38 -05:00
|
|
|
break
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 05:13:58 -05:00
|
|
|
if source == "" {
|
|
|
|
return nil, false, fmt.Errorf("VMX, OVF or OVA file not found")
|
2013-11-18 18:57:31 -05:00
|
|
|
}
|
|
|
|
|
2015-06-22 13:13:49 -04:00
|
|
|
ovftool_uri := fmt.Sprintf("vi://%s:%s@%s/%s/host/%s",
|
2015-06-22 15:37:52 -04:00
|
|
|
url.QueryEscape(p.config.Username),
|
|
|
|
url.QueryEscape(p.config.Password),
|
|
|
|
p.config.Host,
|
|
|
|
p.config.Datacenter,
|
|
|
|
p.config.Cluster)
|
2015-06-22 13:13:49 -04:00
|
|
|
|
|
|
|
if p.config.ResourcePool != "" {
|
|
|
|
ovftool_uri += "/Resources/" + p.config.ResourcePool
|
|
|
|
}
|
|
|
|
|
2013-11-18 19:04:38 -05:00
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
|
|
|
|
"--acceptAllEulas",
|
2016-01-25 12:00:30 -05:00
|
|
|
fmt.Sprintf("--name=\"%s\"", p.config.VMName),
|
|
|
|
fmt.Sprintf("--datastore=\"%s\"", p.config.Datastore),
|
|
|
|
fmt.Sprintf("--diskMode=\"%s\"", p.config.DiskMode),
|
|
|
|
fmt.Sprintf("--network=\"%s\"", p.config.VMNetwork),
|
|
|
|
fmt.Sprintf("--vmFolder=\"%s\"", p.config.VMFolder),
|
2015-02-13 05:13:58 -05:00
|
|
|
fmt.Sprintf("%s", source),
|
2016-01-26 04:41:30 -05:00
|
|
|
fmt.Sprintf("\"%s\"", ovftool_uri),
|
2013-11-29 11:33:26 -05:00
|
|
|
}
|
2013-12-02 06:39:52 -05:00
|
|
|
|
2015-02-13 05:13:58 -05:00
|
|
|
ui.Message(fmt.Sprintf("Uploading %s to vSphere", source))
|
2015-06-24 03:28:14 -04:00
|
|
|
|
|
|
|
if p.config.Overwrite == true {
|
2015-10-29 05:42:26 -04:00
|
|
|
args = append(args, "--overwrite")
|
2015-06-24 03:28:14 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 06:50:19 -04:00
|
|
|
if len(p.config.Options) > 0 {
|
2015-10-29 05:42:26 -04:00
|
|
|
args = append(args, p.config.Options...)
|
2015-06-24 06:50:19 -04:00
|
|
|
}
|
|
|
|
|
2015-02-13 05:13:58 -05:00
|
|
|
ui.Message(fmt.Sprintf("Uploading %s to vSphere", source))
|
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
|
|
|
}
|