Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add pause_after config to powershell provisioner like shell #11792

Merged
merged 2 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions provisioner/powershell/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ type Config struct {
// ```
DebugMode int `mapstructure:"debug_mode"`

// A duration of how long to pause after the provisioner
PauseAfter time.Duration `mapstructure:"pause_after"`

ctx interpolate.Context
}

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

if p.config.PauseAfter != 0 {
ui.Say(fmt.Sprintf("Pausing %s after this provisioner...", p.config.PauseAfter))
time.Sleep(p.config.PauseAfter)
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions provisioner/powershell/provisioner.hcl2spec.go

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

36 changes: 36 additions & 0 deletions provisioner/powershell/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,42 @@ func TestProvisionerProvision_ValidExitCodes(t *testing.T) {
}
}

func TestProvisionerProvision_PauseAfter(t *testing.T) {
config := testConfig()
delete(config, "inline")

// Defaults provided by Packer
config["remote_path"] = "c:/Windows/Temp/inlineScript.ps1"
config["inline"] = []string{"whoami"}
ui := testUi()
p := new(Provisioner)

// Defaults provided by Packer
p.config.PackerBuildName = "vmware"
p.config.PackerBuilderType = "iso"
p.config.ValidExitCodes = []int{0, 200}
pause_amount := time.Second
p.config.PauseAfter = pause_amount
comm := new(packersdk.MockCommunicator)
comm.StartExitStatus = 200
err := p.Prepare(config)
if err != nil {
t.Fatalf("Prepar failed: %s", err)
}

start_time := time.Now()
err = p.Provision(context.Background(), ui, comm, generatedData())
end_time := time.Now()

if err != nil {
t.Fatal("should not have error")
}

if end_time.Sub(start_time) < pause_amount {
t.Fatal("Didn't wait pause_amount")
}
}

func TestProvisionerProvision_InvalidExitCodes(t *testing.T) {
config := testConfig()
delete(config, "inline")
Expand Down
3 changes: 3 additions & 0 deletions website/content/docs/provisioners/powershell.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ provisioner "powershell" {
exists in order to deal with times when SSH may restart, such as a system
reboot. Set this to a higher value if reboots take a longer amount of time.

- `pause_after` (string) - Wait the amount of time after provisioning a powershell
script, this pause be taken if all previous steps were successful.

@include 'provisioners/common-config.mdx'

## Default Environmental Variables
Expand Down