2021-02-02 12:05:04 -05:00
|
|
|
package packer
|
2013-05-24 00:37:16 -04:00
|
|
|
|
|
|
|
import (
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2013-05-24 00:37:16 -04:00
|
|
|
"log"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2013-05-24 00:37:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdProvisioner struct {
|
2020-12-01 17:25:14 -05:00
|
|
|
p packersdk.Provisioner
|
2021-02-02 12:05:04 -05:00
|
|
|
client *PluginClient
|
2013-05-24 00:37:16 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *cmdProvisioner) ConfigSpec() hcldec.ObjectSpec {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
p.checkExit(r, nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return p.p.ConfigSpec()
|
|
|
|
}
|
|
|
|
|
2013-06-06 20:07:42 -04:00
|
|
|
func (c *cmdProvisioner) Prepare(configs ...interface{}) error {
|
2013-05-24 00:37:16 -04:00
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
c.checkExit(r, nil)
|
|
|
|
}()
|
|
|
|
|
2013-06-06 20:07:42 -04:00
|
|
|
return c.p.Prepare(configs...)
|
2013-05-24 00:37:16 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (c *cmdProvisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
|
2013-05-24 00:37:16 -04:00
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
c.checkExit(r, nil)
|
|
|
|
}()
|
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
return c.p.Provision(ctx, ui, comm, generatedData)
|
2013-08-31 02:21:15 -04:00
|
|
|
}
|
|
|
|
|
2013-05-24 00:37:16 -04:00
|
|
|
func (c *cmdProvisioner) checkExit(p interface{}, cb func()) {
|
2013-08-28 14:21:02 -04:00
|
|
|
if c.client.Exited() && cb != nil {
|
2013-05-24 00:37:16 -04:00
|
|
|
cb()
|
2013-08-20 02:38:22 -04:00
|
|
|
} else if p != nil && !Killed {
|
2013-05-24 00:37:16 -04:00
|
|
|
log.Panic(p)
|
|
|
|
}
|
|
|
|
}
|