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"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-09 00:05:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdBuilder struct {
|
|
|
|
builder packer.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
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *cmdBuilder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.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)
|
|
|
|
}
|
|
|
|
}
|