2019-04-04 10:37:46 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2020-11-19 14:54:31 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-04-04 10:37:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TimeoutProvisioner is a Provisioner implementation that can timeout after a
|
|
|
|
// duration
|
|
|
|
type TimeoutProvisioner struct {
|
2020-12-01 17:48:55 -05:00
|
|
|
packersdk.Provisioner
|
2019-04-04 10:37:46 -04:00
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *TimeoutProvisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
|
2019-04-04 10:37:46 -04:00
|
|
|
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))
|
2019-04-09 09:29:07 -04:00
|
|
|
|
|
|
|
errC := make(chan interface{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-errC:
|
|
|
|
// all good
|
|
|
|
case <-ctx.Done():
|
|
|
|
switch ctx.Err() {
|
|
|
|
case context.DeadlineExceeded:
|
|
|
|
ui.Error("Cancelling provisioner after a timeout...")
|
|
|
|
default:
|
|
|
|
// the context also gets cancelled when the provisioner is
|
|
|
|
// successful
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
err := p.Provisioner.Provision(ctx, ui, comm, generatedData)
|
2019-04-09 09:29:07 -04:00
|
|
|
close(errC)
|
|
|
|
return err
|
2019-04-04 10:37:46 -04:00
|
|
|
}
|