mirror of
https://github.com/hashicorp/packer.git
synced 2026-05-28 04:35:38 -04:00
allow to set provisioner timeout from buildfile
This commit is contained in:
parent
f555e7a9f2
commit
d8d5631dc2
11 changed files with 90 additions and 2 deletions
|
|
@ -168,6 +168,11 @@ func (c *Core) Build(n string) (Build, error) {
|
|||
PauseBefore: rawP.PauseBefore,
|
||||
Provisioner: provisioner,
|
||||
}
|
||||
} else if rawP.Timeout > 0 {
|
||||
provisioner = &TimeoutProvisioner{
|
||||
Timeout: rawP.Timeout,
|
||||
Provisioner: provisioner,
|
||||
}
|
||||
}
|
||||
|
||||
provisioners = append(provisioners, coreBuildProvisioner{
|
||||
|
|
|
|||
|
|
@ -53,8 +53,6 @@ func (h *ProvisionHook) Run(ctx context.Context, name string, ui Ui, comm Commun
|
|||
|
||||
for _, p := range h.Provisioners {
|
||||
ts := CheckpointReporter.AddSpan(p.TypeName, "provisioner", p.Config)
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := p.Provisioner.Provision(ctx, ui, comm)
|
||||
|
||||
|
|
|
|||
23
packer/provisioner_timeout.go
Normal file
23
packer/provisioner_timeout.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package packer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeoutProvisioner is a Provisioner implementation that can timeout after a
|
||||
// duration
|
||||
type TimeoutProvisioner struct {
|
||||
Provisioner
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func (p *TimeoutProvisioner) Provision(ctx context.Context, ui Ui, comm Communicator) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, p.Timeout)
|
||||
defer cancel()
|
||||
|
||||
// Use a select to determine if we get cancelled during the wait
|
||||
ui.Say(fmt.Sprintf("Setting a %s timeout for the next provisioner...", p.Timeout))
|
||||
return p.Provisioner.Provision(ctx, ui, comm)
|
||||
}
|
||||
|
|
@ -233,6 +233,7 @@ func (r *rawTemplate) Template() (*Template, error) {
|
|||
delete(p.Config, "override")
|
||||
delete(p.Config, "pause_before")
|
||||
delete(p.Config, "type")
|
||||
delete(p.Config, "timeout")
|
||||
|
||||
if len(p.Config) == 0 {
|
||||
p.Config = nil
|
||||
|
|
|
|||
|
|
@ -106,6 +106,19 @@ func TestParse(t *testing.T) {
|
|||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"parse-provisioner-timeout.json",
|
||||
&Template{
|
||||
Provisioners: []*Provisioner{
|
||||
{
|
||||
Type: "something",
|
||||
Timeout: 5 * time.Minute,
|
||||
},
|
||||
},
|
||||
},
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"parse-provisioner-only.json",
|
||||
&Template{
|
||||
|
|
|
|||
|
|
@ -148,6 +148,7 @@ type Provisioner struct {
|
|||
Config map[string]interface{} `json:"config,omitempty"`
|
||||
Override map[string]interface{} `json:"override,omitempty"`
|
||||
PauseBefore time.Duration `mapstructure:"pause_before" json:"pause_before,omitempty"`
|
||||
Timeout time.Duration `mapstructure:"timeout" json:"timeout,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalJSON conducts the necessary flattening of the Provisioner struct
|
||||
|
|
|
|||
|
|
@ -18,6 +18,11 @@ func TestTemplateValidate(t *testing.T) {
|
|||
File string
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
"validate-good-prov-timeout.json",
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"validate-no-builders.json",
|
||||
true,
|
||||
|
|
|
|||
8
template/test-fixtures/parse-provisioner-timeout.json
Normal file
8
template/test-fixtures/parse-provisioner-timeout.json
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"provisioners": [
|
||||
{
|
||||
"type": "something",
|
||||
"timeout": "5m"
|
||||
}
|
||||
]
|
||||
}
|
||||
11
template/test-fixtures/validate-good-prov-timeout.json
Normal file
11
template/test-fixtures/validate-good-prov-timeout.json
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"builders": [{
|
||||
"type": "foo"
|
||||
}],
|
||||
|
||||
"provisioners": [{
|
||||
"timeout": "5m",
|
||||
"type": "bar",
|
||||
"only": ["foo"]
|
||||
}]
|
||||
}
|
||||
|
|
@ -180,6 +180,7 @@ executing the next script:
|
|||
"type": "shell",
|
||||
"script": "script.sh",
|
||||
"pause_before": "10s"
|
||||
"timeout": "10s"
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -140,3 +140,25 @@ that provisioner. By default, there is no pause. An example is shown below:
|
|||
|
||||
For the above provisioner, Packer will wait 10 seconds before uploading and
|
||||
executing the shell script.
|
||||
|
||||
## Timeout
|
||||
|
||||
Sometimes a command can take much more time than expected
|
||||
|
||||
Every provisioner definition in a Packer template can take a special
|
||||
configuration `timeout` that is the amount of time to wait before
|
||||
considering that the provisioner failed. By default, there is no timeout. An
|
||||
example is shown below:
|
||||
|
||||
``` json
|
||||
{
|
||||
"type": "shell",
|
||||
"script": "script.sh",
|
||||
"timeout": "5m"
|
||||
}
|
||||
```
|
||||
|
||||
For the above provisioner, Packer will cancel the script if it takes more than
|
||||
5 minutes.
|
||||
|
||||
Timeout has no effect in debug mode.
|
||||
|
|
|
|||
Loading…
Reference in a new issue