Skip to content

Commit

Permalink
Add retry to update func
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyushi committed Jul 30, 2021
1 parent c8ae899 commit a280561
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
42 changes: 28 additions & 14 deletions update/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Config struct {
// The timeout for waiting for the machine to restart
RestartTimeout time.Duration `mapstructure:"restart_timeout"`

UpdateMaxRetries int `mapstructure:"update_max_retries"`

// Instructs the communicator to run the remote script as a
// Windows scheduled task, effectively elevating the remote
// user by impersonating a logged-in user.
Expand Down Expand Up @@ -87,6 +89,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
p.config.RestartTimeout = 4 * time.Hour
}

if p.config.UpdateMaxRetries == 0 {
p.config.UpdateMaxRetries = 5
}

if p.config.Username == "" {
p.config.Username = "SYSTEM"
}
Expand Down Expand Up @@ -176,20 +182,28 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C

func (p *Provisioner) update(ctx context.Context, ui packer.Ui, comm packer.Communicator) (bool, error) {
ui.Say("Running Windows update...")
cmd := &packer.RemoteCmd{Command: elevatedCommand}
err := cmd.RunWithUi(ctx, comm, ui)
if err != nil {
return false, err
}
var exitStatus = cmd.ExitStatus()
switch exitStatus {
case 0:
return false, nil
case 101:
return true, nil
default:
return false, fmt.Errorf("Windows update script exited with non-zero exit status: %d", exitStatus)
}
var restartPending bool
err := retry.Config{
RetryDelay: func() time.Duration { return retryableDelay },
Tries: p.config.UpdateMaxRetries,
}.Run(ctx, func(ctx context.Context) error {
cmd := &packer.RemoteCmd{Command: elevatedCommand}
err := cmd.RunWithUi(ctx, comm, ui)
if err != nil {
return err
}
var exitStatus = cmd.ExitStatus()
switch exitStatus {
case 0:
return nil
case 101:
restartPending = true
return nil
default:
return fmt.Errorf("Windows update script exited with non-zero exit status: %d", exitStatus)
}
})
return restartPending, err
}

func (p *Provisioner) restart(ctx context.Context, ui packer.Ui, comm packer.Communicator) error {
Expand Down
2 changes: 2 additions & 0 deletions update/provisioner.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a280561

Please sign in to comment.