2013-05-04 16:47:11 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2019-03-27 07:29:09 -04:00
|
|
|
"log"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-05-04 16:47:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Builder where the builder is actually executed
|
|
|
|
// over an RPC connection.
|
2013-05-09 00:05:35 -04:00
|
|
|
type builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
commonClient
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuilderServer wraps a packer.Builder implementation and makes it exportable
|
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type BuilderServer struct {
|
2019-03-27 07:29:09 -04:00
|
|
|
context context.Context
|
|
|
|
contextCancel func()
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
commonServer
|
2013-05-04 16:47:11 -04:00
|
|
|
builder packer.Builder
|
|
|
|
}
|
|
|
|
|
|
|
|
type BuilderPrepareArgs struct {
|
2013-06-14 15:27:50 -04:00
|
|
|
Configs []interface{}
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:40:06 -04:00
|
|
|
type BuilderPrepareResponse struct {
|
2019-12-17 00:23:05 -05:00
|
|
|
GeneratedVars []string
|
|
|
|
Warnings []string
|
|
|
|
Error *BasicError
|
2013-11-02 23:40:06 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *builder) Prepare(config ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
config, err := encodeCTYValues(config)
|
|
|
|
if err != nil {
|
2019-12-17 05:57:09 -05:00
|
|
|
return nil, nil, err
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
2013-11-02 23:40:06 -04:00
|
|
|
var resp BuilderPrepareResponse
|
2019-12-17 05:25:56 -05:00
|
|
|
cerr := b.client.Call(b.endpoint+".Prepare", &BuilderPrepareArgs{config}, &resp)
|
2013-05-09 16:59:33 -04:00
|
|
|
if cerr != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, cerr
|
2013-05-09 16:59:33 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
|
2014-04-26 16:31:22 -04:00
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
2013-05-09 16:59:33 -04:00
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return resp.GeneratedVars, resp.Warnings, err
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func (b *builder) Run(ctx context.Context, ui packersdk.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2013-12-10 15:14:08 -05:00
|
|
|
nextId := b.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(b.mux, nextId)
|
2013-12-10 15:14:08 -05:00
|
|
|
server.RegisterHook(hook)
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
go server.Serve()
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
done := make(chan interface{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("Cancelling builder after context cancellation %v", ctx.Err())
|
2019-12-17 05:25:56 -05:00
|
|
|
if err := b.client.Call(b.endpoint+".Cancel", new(interface{}), new(interface{})); err != nil {
|
2019-03-27 07:29:09 -04:00
|
|
|
log.Printf("Error cancelling builder: %s", err)
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
var responseId uint32
|
2019-03-27 07:29:09 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
if err := b.client.Call(b.endpoint+".Run", nextId, &responseId); err != nil {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
if responseId == 0 {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, nil
|
2013-06-05 18:36:26 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(b.mux, responseId)
|
2013-05-22 01:13:29 -04:00
|
|
|
if err != nil {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, err
|
2013-05-22 01:13:29 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
return client.Artifact(), nil
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:40:06 -04:00
|
|
|
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error {
|
2019-12-17 05:25:56 -05:00
|
|
|
config, err := decodeCTYValues(args.Configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-17 05:57:09 -05:00
|
|
|
generated, warnings, err := b.builder.Prepare(config...)
|
2013-11-02 23:40:06 -04:00
|
|
|
*reply = BuilderPrepareResponse{
|
2019-12-17 00:23:05 -05:00
|
|
|
GeneratedVars: generated,
|
|
|
|
Warnings: warnings,
|
|
|
|
Error: NewBasicError(err),
|
2013-11-02 23:40:06 -04:00
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
func (b *BuilderServer) Run(streamId uint32, reply *uint32) error {
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(b.mux, streamId)
|
2013-05-04 16:47:11 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:14:08 -05:00
|
|
|
return NewBasicError(err)
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
2013-12-10 15:14:08 -05:00
|
|
|
defer client.Close()
|
2013-05-04 16:47:11 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
if b.context == nil {
|
|
|
|
b.context, b.contextCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := b.builder.Run(b.context, client.Ui(), client.Hook())
|
2013-06-03 18:00:35 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:14:08 -05:00
|
|
|
return NewBasicError(err)
|
2013-06-03 18:00:35 -04:00
|
|
|
}
|
2013-05-22 01:13:29 -04:00
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
*reply = 0
|
|
|
|
if artifact != nil {
|
|
|
|
streamId = b.mux.NextId()
|
2019-03-27 07:29:09 -04:00
|
|
|
artifactServer := newServerWithMux(b.mux, streamId)
|
|
|
|
artifactServer.RegisterArtifact(artifact)
|
|
|
|
go artifactServer.Serve()
|
2013-12-10 15:14:08 -05:00
|
|
|
*reply = streamId
|
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-27 07:29:09 -04:00
|
|
|
|
|
|
|
func (b *BuilderServer) Cancel(args *interface{}, reply *interface{}) error {
|
|
|
|
b.contextCancel()
|
|
|
|
return nil
|
|
|
|
}
|