2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
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"
|
2014-07-17 15:33:09 -04:00
|
|
|
"log"
|
|
|
|
"net/url"
|
2013-11-18 18:57:31 -05:00
|
|
|
"os/exec"
|
2017-09-20 14:45:41 -04:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
2013-11-18 18:57:31 -05:00
|
|
|
"strings"
|
2020-07-24 19:36:12 -04:00
|
|
|
"time"
|
2015-05-27 17:56:22 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
shelllocal "github.com/hashicorp/packer/packer-plugin-sdk/shell-local"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-11-11 03:56:51 -05:00
|
|
|
)
|
|
|
|
|
2017-09-20 14:45:41 -04: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"`
|
|
|
|
|
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"`
|
2017-09-20 14:45:41 -04:00
|
|
|
ESXiHost string `mapstructure:"esxi_host"`
|
2015-06-24 06:50:19 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
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{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: BuilderId,
|
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
|
2020-11-19 15:07:02 -05:00
|
|
|
errs := new(packersdk.MultiError)
|
2013-12-02 06:39:52 -05:00
|
|
|
|
2017-09-20 14:45:41 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
ovftool = "ovftool.exe"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := exec.LookPath(ovftool); err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.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 == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.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
|
|
|
}
|
|
|
|
|
2020-07-15 15:10:09 -04:00
|
|
|
func (p *PostProcessor) generateURI() (*url.URL, error) {
|
|
|
|
// use net/url lib to encode and escape url elements
|
|
|
|
ovftool_uri := fmt.Sprintf("vi://%s/%s/host/%s",
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:10:09 -04:00
|
|
|
u, err := url.Parse(ovftool_uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't generate uri for ovftool: %s", err)
|
|
|
|
}
|
|
|
|
u.User = url.UserPassword(p.config.Username, p.config.Password)
|
|
|
|
|
2017-09-20 14:45:41 -04:00
|
|
|
if p.config.ESXiHost != "" {
|
2020-07-17 12:42:05 -04:00
|
|
|
q := u.Query()
|
2017-09-20 14:45:41 -04:00
|
|
|
if ipv4Regex.MatchString(p.config.ESXiHost) {
|
2020-07-17 12:42:05 -04:00
|
|
|
q.Add("ip", p.config.ESXiHost)
|
2017-09-20 14:45:41 -04:00
|
|
|
} else if hostnameRegex.MatchString(p.config.ESXiHost) {
|
2020-07-17 12:42:05 -04:00
|
|
|
q.Add("dns", p.config.ESXiHost)
|
2017-09-20 14:45:41 -04:00
|
|
|
}
|
2020-07-17 12:42:05 -04:00
|
|
|
u.RawQuery = q.Encode()
|
2017-09-20 14:45:41 -04:00
|
|
|
}
|
2020-07-15 15:10:09 -04:00
|
|
|
return u, nil
|
2020-07-15 14:39:04 -04:00
|
|
|
}
|
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
func getEncodedPassword(u *url.URL) (string, bool) {
|
|
|
|
// filter password from all logging
|
|
|
|
password, passwordSet := u.User.Password()
|
|
|
|
if passwordSet && password != "" {
|
2020-07-27 04:33:39 -04:00
|
|
|
encodedPassword := strings.Split(u.User.String(), ":")[1]
|
2020-07-24 19:36:12 -04:00
|
|
|
return encodedPassword, true
|
|
|
|
}
|
|
|
|
return password, false
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
|
2020-07-15 14:39:04 -04:00
|
|
|
source := ""
|
|
|
|
for _, path := range artifact.Files() {
|
|
|
|
if strings.HasSuffix(path, ".vmx") || strings.HasSuffix(path, ".ovf") || strings.HasSuffix(path, ".ova") {
|
|
|
|
source = path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if source == "" {
|
|
|
|
return nil, false, false, fmt.Errorf("VMX, OVF or OVA file not found")
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:10:09 -04:00
|
|
|
ovftool_uri, err := p.generateURI()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, false, err
|
|
|
|
}
|
2020-07-24 19:36:12 -04:00
|
|
|
encodedPassword, isSet := getEncodedPassword(ovftool_uri)
|
|
|
|
if isSet {
|
|
|
|
packer.LogSecretFilter.Set(encodedPassword)
|
|
|
|
}
|
2017-09-20 14:45:41 -04:00
|
|
|
|
2020-07-15 15:10:09 -04:00
|
|
|
args, err := p.BuildArgs(source, ovftool_uri.String())
|
2016-05-18 21:26:46 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Message(fmt.Sprintf("Failed: %s\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Uploading %s to vSphere", source))
|
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
log.Printf("Starting ovftool with parameters: %s", strings.Join(args, " "))
|
2017-09-20 14:45:41 -04:00
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
ui.Message("Validating Username and Password with dry-run")
|
|
|
|
err = p.ValidateOvfTool(args, ovftool)
|
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2016-05-18 21:26:46 -04:00
|
|
|
}
|
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
// Validation has passed, so run for real.
|
|
|
|
ui.Message("Calling OVFtool to upload vm")
|
|
|
|
commandAndArgs := []string{ovftool}
|
|
|
|
commandAndArgs = append(commandAndArgs, args...)
|
|
|
|
comm := &shelllocal.Communicator{
|
|
|
|
ExecuteCommand: commandAndArgs,
|
|
|
|
}
|
|
|
|
flattenedCmd := strings.Join(commandAndArgs, " ")
|
|
|
|
cmd := &packer.RemoteCmd{Command: flattenedCmd}
|
|
|
|
log.Printf("[INFO] (vsphere): starting ovftool command: %s", flattenedCmd)
|
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
|
|
|
return nil, false, false, fmt.Errorf(
|
|
|
|
"Error uploading virtual machine: Please see output above for more information.")
|
|
|
|
}
|
2017-10-23 18:38:37 -04:00
|
|
|
|
2017-11-10 20:57:39 -05:00
|
|
|
artifact = NewArtifact(p.config.Datastore, p.config.VMFolder, p.config.VMName, artifact.Files())
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
return artifact, false, false, nil
|
2016-05-18 21:26:46 -04:00
|
|
|
}
|
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
func (p *PostProcessor) ValidateOvfTool(args []string, ofvtool string) error {
|
|
|
|
args = append([]string{"--verifyOnly"}, args...)
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmdCtx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(cmdCtx, ovftool, args...)
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
|
|
|
// Need to manually close stdin or else the ofvtool call will hang
|
|
|
|
// forever in a situation where the user has provided an invalid
|
|
|
|
// password or username
|
2020-07-27 11:52:29 -04:00
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2020-07-27 11:53:33 -04:00
|
|
|
return err
|
2020-07-27 11:52:29 -04:00
|
|
|
}
|
2020-07-24 19:36:12 -04:00
|
|
|
defer stdin.Close()
|
2020-07-15 15:10:09 -04:00
|
|
|
|
2020-07-24 19:36:12 -04:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
outString := out.String()
|
|
|
|
if strings.Contains(outString, "Enter login information for") {
|
|
|
|
err = fmt.Errorf("Error performing OVFtool dry run; the username " +
|
|
|
|
"or password you provided to ovftool is likely invalid.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
2017-10-24 14:38:56 -04:00
|
|
|
}
|
|
|
|
|
2016-05-18 21:26:46 -04:00
|
|
|
func (p *PostProcessor) BuildArgs(source, ovftool_uri string) ([]string, error) {
|
2013-11-18 19:04:38 -05:00
|
|
|
args := []string{
|
|
|
|
"--acceptAllEulas",
|
2016-05-18 21:26:46 -04:00
|
|
|
fmt.Sprintf(`--name=%s`, p.config.VMName),
|
|
|
|
fmt.Sprintf(`--datastore=%s`, p.config.Datastore),
|
2013-11-29 11:33:26 -05:00
|
|
|
}
|
2013-12-02 06:39:52 -05:00
|
|
|
|
2016-05-18 21:26:46 -04: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))
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-03-04 15:31:30 -05:00
|
|
|
args = append(args, source)
|
|
|
|
args = append(args, ovftool_uri)
|
2013-11-18 18:57:31 -05:00
|
|
|
|
2016-05-18 21:26:46 -04:00
|
|
|
return args, nil
|
2013-11-11 03:56:51 -05:00
|
|
|
}
|