Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions post-processor/vsphere/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ import (
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)

const DefaultMaxRetries = 5
const DefaultDiskMode = "thick"
const OvftoolWindows = "ovftool.exe"
const (
DefaultMaxRetries = 5
DefaultDiskMode = "thick"
OvftoolWindows = "ovftool.exe"
)

var ovftool = "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.
// Regular expression to validate an RFC1035 hostname from and FQDN or simple hostname.
// For example "esxi-01". 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.
Expand Down Expand Up @@ -127,15 +129,15 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {

// Set default value for MaxRetries if not provided.
if p.config.MaxRetries == 0 {
p.config.MaxRetries = DefaultMaxRetries // Set default value
p.config.MaxRetries = DefaultMaxRetries
}

// Defaults
if p.config.DiskMode == "" {
p.config.DiskMode = DefaultDiskMode
}

// Accumulate any errors
// Accumulate any errors.
errs := new(packersdk.MultiError)

if runtime.GOOS == "windows" {
Expand All @@ -147,7 +149,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
errs, fmt.Errorf("ovftool not found: %s", err))
}

// First define all our templatable parameters that are _required_
// Define the parameters that are required.
templates := map[string]*string{
"cluster": &p.config.Cluster,
"datacenter": &p.config.Datacenter,
Expand All @@ -172,7 +174,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
}

func (p *PostProcessor) generateURI() (*url.URL, error) {
// use net/url lib to encode and escape url elements
// Use the net/url standard library to encode and escape the URI.
ovftoolURI := fmt.Sprintf("vi://%s/%s/host/%s",
p.config.Host,
p.config.Datacenter,
Expand Down Expand Up @@ -201,7 +203,7 @@ func (p *PostProcessor) generateURI() (*url.URL, error) {
}

func getEncodedPassword(u *url.URL) (string, bool) {
// filter password from all logging
// Filter the password from the logs.
password, passwordSet := u.User.Password()
if passwordSet && password != "" {
encodedPassword := strings.Split(u.User.String(), ":")[1]
Expand Down Expand Up @@ -273,7 +275,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa
return artifact, false, false, nil
}

func (p *PostProcessor) ValidateOvfTool(args []string, ofvtool string, ui packersdk.Ui) error {
func (p *PostProcessor) ValidateOvfTool(args []string, ovftool string, ui packersdk.Ui) error {
args = append([]string{"--verifyOnly"}, args...)
if p.config.Insecure {
args = append(args, "--noSSLVerify")
Expand All @@ -285,9 +287,8 @@ func (p *PostProcessor) ValidateOvfTool(args []string, ofvtool string, ui packer
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
// Need to manually close stdin or else the ovftool call will hang if the
// user has provided an invalid credential.
stdin, err := cmd.StdinPipe()
if err != nil {
return err
Expand Down
Loading