Skip to content

Commit cbc67b7

Browse files
Merge pull request #11792 from teddylear/feat-pause_after-powershell
feat: Add pause_after config to powershell provisioner like shell
2 parents 035c133 + d00a41c commit cbc67b7

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

provisioner/powershell/provisioner.go

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ type Config struct {
8686
// ```
8787
DebugMode int `mapstructure:"debug_mode"`
8888

89+
// A duration of how long to pause after the provisioner
90+
PauseAfter time.Duration `mapstructure:"pause_after"`
91+
8992
ctx interpolate.Context
9093
}
9194

@@ -349,6 +352,11 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packe
349352
log.Printf("remote cleanup script failed to upload; skipping the removal of temporary files: %s; ", strings.Join(uploadedScripts, ","))
350353
}
351354

355+
if p.config.PauseAfter != 0 {
356+
ui.Say(fmt.Sprintf("Pausing %s after this provisioner...", p.config.PauseAfter))
357+
time.Sleep(p.config.PauseAfter)
358+
}
359+
352360
return nil
353361
}
354362

provisioner/powershell/provisioner.hcl2spec.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provisioner/powershell/provisioner_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,42 @@ func TestProvisionerProvision_ValidExitCodes(t *testing.T) {
381381
}
382382
}
383383

384+
func TestProvisionerProvision_PauseAfter(t *testing.T) {
385+
config := testConfig()
386+
delete(config, "inline")
387+
388+
// Defaults provided by Packer
389+
config["remote_path"] = "c:/Windows/Temp/inlineScript.ps1"
390+
config["inline"] = []string{"whoami"}
391+
ui := testUi()
392+
p := new(Provisioner)
393+
394+
// Defaults provided by Packer
395+
p.config.PackerBuildName = "vmware"
396+
p.config.PackerBuilderType = "iso"
397+
p.config.ValidExitCodes = []int{0, 200}
398+
pause_amount := time.Second
399+
p.config.PauseAfter = pause_amount
400+
comm := new(packersdk.MockCommunicator)
401+
comm.StartExitStatus = 200
402+
err := p.Prepare(config)
403+
if err != nil {
404+
t.Fatalf("Prepar failed: %s", err)
405+
}
406+
407+
start_time := time.Now()
408+
err = p.Provision(context.Background(), ui, comm, generatedData())
409+
end_time := time.Now()
410+
411+
if err != nil {
412+
t.Fatal("should not have error")
413+
}
414+
415+
if end_time.Sub(start_time) < pause_amount {
416+
t.Fatal("Didn't wait pause_amount")
417+
}
418+
}
419+
384420
func TestProvisionerProvision_InvalidExitCodes(t *testing.T) {
385421
config := testConfig()
386422
delete(config, "inline")

website/content/docs/provisioners/powershell.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ provisioner "powershell" {
244244
exists in order to deal with times when SSH may restart, such as a system
245245
reboot. Set this to a higher value if reboots take a longer amount of time.
246246

247+
- `pause_after` (string) - Wait the amount of time after provisioning a powershell
248+
script, this pause be taken if all previous steps were successful.
249+
247250
@include 'provisioners/common-config.mdx'
248251

249252
## Default Environmental Variables

0 commit comments

Comments
 (0)