2013-05-09 00:05:35 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-05-09 00:05:35 -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-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-05-09 00:05:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdBuilder struct {
|
2020-12-01 16:42:11 -05:00
|
|
|
builder packersdk.Builder
|
2013-06-11 14:03:36 -04:00
|
|
|
client *Client
|
2013-05-09 00:05:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *cmdBuilder) ConfigSpec() hcldec.ObjectSpec {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
b.checkExit(r, nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return b.builder.ConfigSpec()
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *cmdBuilder) Prepare(config ...interface{}) ([]string, []string, error) {
|
2013-05-09 00:05:35 -04:00
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
b.checkExit(r, nil)
|
|
|
|
}()
|
|
|
|
|
2013-06-14 15:27:50 -04:00
|
|
|
return b.builder.Prepare(config...)
|
2013-05-09 00:05:35 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *cmdBuilder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2013-05-09 00:05:35 -04:00
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
b.checkExit(r, nil)
|
|
|
|
}()
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
return b.builder.Run(ctx, ui, hook)
|
2013-06-03 17:44:34 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:05:35 -04:00
|
|
|
func (c *cmdBuilder) checkExit(p interface{}, cb func()) {
|
|
|
|
if c.client.Exited() && cb != nil {
|
|
|
|
cb()
|
2013-08-20 02:38:22 -04:00
|
|
|
} else if p != nil && !Killed {
|
2013-05-09 00:05:35 -04:00
|
|
|
log.Panic(p)
|
|
|
|
}
|
|
|
|
}
|