packer-cn/post-processor/vsphere/post-processor.go

153 lines
3.9 KiB
Go
Raw Normal View History

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"
2014-07-17 15:33:09 -04:00
"log"
"net/url"
2013-11-18 18:57:31 -05:00
"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"`
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"`
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"`
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
}
p.config.tpl, err = packer.NewConfigTemplate()
2013-11-18 18:57:31 -05:00
if err != nil {
return err
}
p.config.tpl.UserVars = p.config.PackerUserVars
2013-11-18 18:57:31 -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)
if _, err := exec.LookPath("ovftool"); err != nil {
2013-11-18 18:57:31 -05:00
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("ovftool not found: %s", err))
2013-11-18 18:57:31 -05:00
}
// First define all our templatable parameters that are _required_
templates := map[string]*string{
"cluster": &p.config.Cluster,
"datacenter": &p.config.Datacenter,
"diskmode": &p.config.DiskMode,
"host": &p.config.Host,
"password": &p.config.Password,
2013-11-29 11:33:26 -05:00
"resource_pool": &p.config.ResourcePool,
"username": &p.config.Username,
"vm_name": &p.config.VMName,
2013-11-18 18:57:31 -05:00
}
for key, ptr := range templates {
if *ptr == "" {
2013-11-18 18:57:31 -05:00
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("%s must be set", key))
}
}
// Then define the ones that are optional
templates["datastore"] = &p.config.Datastore
templates["vm_network"] = &p.config.VMNetwork
templates["vm_folder"] = &p.config.VMFolder
// Template process
for key, ptr := range templates {
*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) {
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
break
2013-11-18 18:57:31 -05:00
}
}
if vmx == "" {
return nil, false, fmt.Errorf("VMX file not found")
}
args := []string{
fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
"--acceptAllEulas",
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),
2013-11-29 11:33:26 -05:00
fmt.Sprintf("%s", vmx),
fmt.Sprintf("vi://%s:%s@%s/%s/host/%s/Resources/%s/",
url.QueryEscape(p.config.Username),
url.QueryEscape(p.config.Password),
p.config.Host,
p.config.Datacenter,
2013-11-29 11:33:26 -05:00
p.config.Cluster,
p.config.ResourcePool),
}
ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))
2013-11-18 18:57:31 -05:00
var out bytes.Buffer
log.Printf("Starting ovftool with parameters: %s", strings.Join(args, " "))
cmd := exec.Command("ovftool", args...)
2013-11-18 18:57:31 -05:00
cmd.Stdout = &out
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
}