2021-02-02 12:05:04 -05:00
|
|
|
package packer
|
2013-05-11 13:46:17 -04:00
|
|
|
|
|
|
|
import (
|
2019-03-22 09:50:33 -04:00
|
|
|
"context"
|
2013-05-11 13:46:17 -04:00
|
|
|
"log"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2013-05-11 13:46:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdHook struct {
|
2020-11-19 18:10:00 -05:00
|
|
|
hook packersdk.Hook
|
2021-02-02 12:05:04 -05:00
|
|
|
client *PluginClient
|
2013-05-11 13:46:17 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (c *cmdHook) Run(ctx context.Context, name string, ui packersdk.Ui, comm packersdk.Communicator, data interface{}) error {
|
2013-05-11 13:46:17 -04:00
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
c.checkExit(r, nil)
|
|
|
|
}()
|
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
return c.hook.Run(ctx, name, ui, comm, data)
|
2013-08-30 20:03:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 13:46:17 -04:00
|
|
|
func (c *cmdHook) checkExit(p interface{}, cb func()) {
|
2013-08-30 20:03:55 -04:00
|
|
|
if c.client.Exited() && cb != nil {
|
2013-05-11 13:46:17 -04:00
|
|
|
cb()
|
2013-08-20 02:38:22 -04:00
|
|
|
} else if p != nil && !Killed {
|
2013-05-11 13:46:17 -04:00
|
|
|
log.Panic(p)
|
|
|
|
}
|
|
|
|
}
|