Skip to content

Commit 5671726

Browse files
committed
restart the update provisioning session when interrupted
1 parent 5bca8d7 commit 5671726

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Configure your packer template to require a [release version of the plugin](http
1414
packer {
1515
required_plugins {
1616
windows-update = {
17-
version = "0.16.10"
17+
version = "0.17.0"
1818
source = "github.com/rgl/windows-update"
1919
}
2020
}

update/provisioner.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,16 @@ func (p *Provisioner) update(ctx context.Context, ui packer.Ui, comm packer.Comm
204204
RetryDelay: func() time.Duration { return retryableDelay },
205205
Tries: p.config.UpdateMaxRetries,
206206
}.Run(ctx, func(ctx context.Context) error {
207+
ui := NewUpdateUi(ui)
207208
cmd := &packer.RemoteCmd{Command: elevatedCommand}
208209
err := cmd.RunWithUi(ctx, comm, ui)
209210
if err != nil {
210211
return err
211212
}
212213
var exitStatus = cmd.ExitStatus()
214+
if !ui.finished {
215+
return fmt.Errorf("Windows update script did not finish")
216+
}
213217
switch exitStatus {
214218
case 0:
215219
return nil
@@ -380,5 +384,5 @@ func escapePowerShellString(value string) string {
380384
return fmt.Sprintf(
381385
"'%s'",
382386
// escape single quotes with another single quote.
383-
strings.Replace(value, "'", "''", -1))
387+
strings.ReplaceAll(value, "'", "''"))
384388
}

update/update_ui.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package update
2+
3+
import (
4+
"io"
5+
"strings"
6+
7+
"github.com/hashicorp/packer-plugin-sdk/packer"
8+
)
9+
10+
type UpdateUi struct {
11+
ui packer.Ui
12+
finished bool
13+
}
14+
15+
func NewUpdateUi(ui packer.Ui) *UpdateUi {
16+
return &UpdateUi{
17+
ui: ui,
18+
finished: false,
19+
}
20+
}
21+
22+
func (u *UpdateUi) Askf(s string, args ...any) (string, error) {
23+
return u.ui.Askf(s, args...)
24+
}
25+
26+
func (u *UpdateUi) Ask(s string) (string, error) {
27+
return u.ui.Ask(s)
28+
}
29+
30+
func (u *UpdateUi) Sayf(s string, args ...any) {
31+
u.ui.Sayf(s, args...)
32+
}
33+
34+
func (u *UpdateUi) Say(s string) {
35+
if strings.HasPrefix(s, "Exiting with code ") {
36+
u.finished = true
37+
return
38+
}
39+
u.ui.Say(s)
40+
}
41+
42+
func (u *UpdateUi) Message(s string) {
43+
u.Say(s)
44+
}
45+
46+
func (u *UpdateUi) Errorf(s string, args ...any) {
47+
u.ui.Errorf(s, args...)
48+
}
49+
50+
func (u *UpdateUi) Error(s string) {
51+
u.ui.Error(s)
52+
}
53+
54+
func (u *UpdateUi) Machine(t string, args ...string) {
55+
u.ui.Machine(t, args...)
56+
}
57+
58+
func (u *UpdateUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) {
59+
return u.ui.TrackProgress(src, currentSize, totalSize, stream)
60+
}

update/windows-update.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ $mock = $false
3535

3636
function ExitWithCode($exitCode) {
3737
$host.SetShouldExit($exitCode)
38+
Write-Output "Exiting with code $exitCode"
3839
Exit
3940
}
4041

@@ -314,3 +315,5 @@ if ($updatesToInstall.Count) {
314315
ExitWhenRebootRequired $rebootRequired
315316
Write-Output 'No Windows updates found'
316317
}
318+
319+
ExitWithCode 0

0 commit comments

Comments
 (0)