Skip to content

Commit e8afa11

Browse files
authored
fix: update post-processor regex, backoff, and msg (#708)
- Adjust `hostnameRegex` to enforce full-string matching by adding grouping and anchors, fixing a validation bug. - Extract backoff configuration into a variable and use Exponential backoff (InitialBackoff=200ms, MaxBackoff=30s, Multiplier=2) for retries. - Improve error handling by wrapping the underlying error with %w and returning an explicit error when the remote command exits with a non-zero status. These changes improve retry behavior and provide clearer diagnostics for uploads. Signed-off-by: Ryan Johnson <ryan.johnson@broadcom.com>
1 parent d5031fc commit e8afa11

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

post-processor/vsphere/post-processor.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var (
4141
// Regular expression to validate an RFC1035 hostname from an FQDN or simple
4242
// hostname. For example "esxi-01". Requires proper DNS setup and/or correct DNS
4343
// search domain setting.
44-
hostnameRegex = regexp.MustCompile(`^[[:alnum:]][[:alnum:]\-]{0,61}[[:alnum:]]|[[:alpha:]]$`)
44+
hostnameRegex = regexp.MustCompile(`^([[:alnum:]][[:alnum:]\-]{0,61}[[:alnum:]]|[[:alpha:]])$`)
4545

4646
// Simple regular expression to validate IPv4 values.
4747
// For example "192.168.168.1".
@@ -310,17 +310,25 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa
310310
ExecuteCommand: commandAndArgs,
311311
}
312312
flattenedCmd := strings.Join(commandAndArgs, " ")
313+
backoff := &retry.Backoff{
314+
InitialBackoff: 200 * time.Millisecond,
315+
MaxBackoff: 30 * time.Second,
316+
Multiplier: 2,
317+
}
313318
err = retry.Config{
314319
Tries: p.config.MaxRetries,
315320
ShouldRetry: func(err error) bool {
316321
return err != nil
317322
},
318-
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Exponential,
323+
RetryDelay: backoff.Exponential,
319324
}.Run(ctx, func(ctx context.Context) error {
320325
cmd := &packersdk.RemoteCmd{Command: flattenedCmd}
321326
err = cmd.RunWithUi(ctx, comm, ui)
322-
if err != nil || cmd.ExitStatus() != 0 {
323-
return fmt.Errorf("error uploading virtual machine")
327+
if err != nil {
328+
return fmt.Errorf("error uploading virtual machine: %w", err)
329+
}
330+
if cmd.ExitStatus() != 0 {
331+
return fmt.Errorf("error uploading virtual machine: exit status %d", cmd.ExitStatus())
324332
}
325333
return nil
326334
})

0 commit comments

Comments
 (0)