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

236 lines
6.1 KiB
Go
Raw Normal View History

2013-11-11 03:56:51 -05:00
package vsphere
import (
2017-10-23 18:38:37 -04:00
"bytes"
2019-03-22 09:56:02 -04:00
"context"
2013-11-18 18:57:31 -05:00
"fmt"
"io"
2014-07-17 15:33:09 -04:00
"log"
"net/url"
"os"
2013-11-18 18:57:31 -05:00
"os/exec"
"regexp"
"runtime"
2013-11-18 18:57:31 -05:00
"strings"
2015-05-27 17:56:22 -04:00
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
2013-11-11 03:56:51 -05:00
)
var builtins = map[string]string{
"mitchellh.vmware": "vmware",
"mitchellh.vmware-esx": "vmware",
2013-11-11 03:56:51 -05:00
}
var ovftool string = "ovftool"
var (
// Regular expression to validate RFC1035 hostnames from full fqdn or simple hostname.
// For example "packer-esxi1". Requires proper DNS setup and/or correct DNS search domain setting.
hostnameRegex = regexp.MustCompile(`^[[:alnum:]][[:alnum:]\-]{0,61}[[:alnum:]]|[[:alpha:]]$`)
// Simple regular expression to validate IPv4 values.
// For example "192.168.1.1".
ipv4Regex = regexp.MustCompile(`^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`)
)
2013-11-11 03:56:51 -05:00
type Config struct {
2013-11-18 18:57:31 -05:00
common.PackerConfig `mapstructure:",squash"`
Cluster string `mapstructure:"cluster"`
Datacenter string `mapstructure:"datacenter"`
Datastore string `mapstructure:"datastore"`
DiskMode string `mapstructure:"disk_mode"`
Host string `mapstructure:"host"`
ESXiHost string `mapstructure:"esxi_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"`
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{
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
}
// 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 runtime.GOOS == "windows" {
ovftool = "ovftool.exe"
}
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{
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
}
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))
}
}
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
}
2019-03-22 09:56:02 -04:00
func (p *PostProcessor) PostProcess(ctx context.Context, 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())
}
source := ""
2013-11-18 18:57:31 -05:00
for _, path := range artifact.Files() {
if strings.HasSuffix(path, ".vmx") || strings.HasSuffix(path, ".ovf") || strings.HasSuffix(path, ".ova") {
source = path
break
2013-11-18 18:57:31 -05:00
}
}
if source == "" {
return nil, false, fmt.Errorf("VMX, OVF or OVA file not found")
2013-11-18 18:57:31 -05:00
}
password := escapeWithSpaces(p.config.Password)
2015-06-22 13:13:49 -04:00
ovftool_uri := fmt.Sprintf("vi://%s:%s@%s/%s/host/%s",
escapeWithSpaces(p.config.Username),
2017-02-10 04:01:03 -05:00
password,
2015-06-22 15:37:52 -04:00
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
}
if p.config.ESXiHost != "" {
if ipv4Regex.MatchString(p.config.ESXiHost) {
ovftool_uri += "/?ip=" + p.config.ESXiHost
} else if hostnameRegex.MatchString(p.config.ESXiHost) {
ovftool_uri += "/?dns=" + p.config.ESXiHost
}
}
args, err := p.BuildArgs(source, ovftool_uri)
if err != nil {
ui.Message(fmt.Sprintf("Failed: %s\n", err))
}
ui.Message(fmt.Sprintf("Uploading %s to vSphere", source))
log.Printf("Starting ovftool with parameters: %s",
strings.Replace(
strings.Join(args, " "),
password,
"<password>",
-1))
var errWriter io.Writer
var errOut bytes.Buffer
cmd := exec.Command(ovftool, args...)
errWriter = io.MultiWriter(os.Stderr, &errOut)
cmd.Stdout = os.Stdout
cmd.Stderr = errWriter
2017-10-23 18:38:37 -04:00
if err := cmd.Run(); err != nil {
err := fmt.Errorf("Error uploading virtual machine: %s\n%s\n", err, p.filterLog(errOut.String()))
return nil, false, err
}
ui.Message(p.filterLog(errOut.String()))
2017-10-23 18:38:37 -04:00
artifact = NewArtifact(p.config.Datastore, p.config.VMFolder, p.config.VMName, artifact.Files())
return artifact, false, nil
}
2017-10-24 14:38:56 -04:00
func (p *PostProcessor) filterLog(s string) string {
password := escapeWithSpaces(p.config.Password)
2017-10-24 14:38:56 -04:00
return strings.Replace(s, password, "<password>", -1)
}
func (p *PostProcessor) BuildArgs(source, ovftool_uri string) ([]string, error) {
args := []string{
"--acceptAllEulas",
fmt.Sprintf(`--name=%s`, p.config.VMName),
fmt.Sprintf(`--datastore=%s`, p.config.Datastore),
2013-11-29 11:33:26 -05:00
}
if p.config.Insecure {
args = append(args, fmt.Sprintf(`--noSSLVerify=%t`, p.config.Insecure))
}
if p.config.DiskMode != "" {
args = append(args, fmt.Sprintf(`--diskMode=%s`, p.config.DiskMode))
}
if p.config.VMFolder != "" {
args = append(args, fmt.Sprintf(`--vmFolder=%s`, p.config.VMFolder))
}
if p.config.VMNetwork != "" {
args = append(args, fmt.Sprintf(`--network=%s`, p.config.VMNetwork))
}
if p.config.Overwrite == true {
args = append(args, "--overwrite")
}
if len(p.config.Options) > 0 {
args = append(args, p.config.Options...)
}
args = append(args, fmt.Sprintf(`%s`, source))
args = append(args, fmt.Sprintf(`%s`, ovftool_uri))
2013-11-18 18:57:31 -05:00
return args, nil
2013-11-11 03:56:51 -05:00
}
// Encode everything except for + signs
func escapeWithSpaces(stringToEscape string) string {
escapedString := url.QueryEscape(stringToEscape)
escapedString = strings.Replace(escapedString, "+", `%20`, -1)
return escapedString
}