2013-05-11 12:51:49 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:50:33 -04:00
|
|
|
"context"
|
2019-03-26 10:21:07 -04:00
|
|
|
"log"
|
2019-05-10 13:25:54 -04:00
|
|
|
"sync"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-05-11 12:51:49 -04:00
|
|
|
)
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
// An implementation of packersdk.Hook where the hook is actually executed
|
2013-05-11 12:51:49 -04:00
|
|
|
// over an RPC connection.
|
|
|
|
type hook struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
commonClient
|
2013-05-11 12:51:49 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
// HookServer wraps a packersdk.Hook implementation and makes it exportable
|
2013-05-11 12:51:49 -04:00
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type HookServer struct {
|
2019-03-27 07:29:09 -04:00
|
|
|
context context.Context
|
|
|
|
contextCancel func()
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
hook packersdk.Hook
|
2019-05-10 13:25:54 -04:00
|
|
|
lock sync.Mutex
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-05-11 12:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type HookRunArgs struct {
|
2013-12-10 14:50:30 -05:00
|
|
|
Name string
|
|
|
|
Data interface{}
|
|
|
|
StreamId uint32
|
2013-05-11 12:51:49 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (h *hook) Run(ctx context.Context, name string, ui packersdk.Ui, comm packersdk.Communicator, data interface{}) error {
|
2013-12-10 14:50:30 -05:00
|
|
|
nextId := h.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(h.mux, nextId)
|
2013-12-10 14:50:30 -05:00
|
|
|
server.RegisterCommunicator(comm)
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
go server.Serve()
|
2013-05-11 12:51:49 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
done := make(chan interface{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("Cancelling hook after context cancellation %v", ctx.Err())
|
2019-12-17 05:25:56 -05:00
|
|
|
if err := h.client.Call(h.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 14:50:30 -05:00
|
|
|
args := HookRunArgs{
|
|
|
|
Name: name,
|
|
|
|
Data: data,
|
|
|
|
StreamId: nextId,
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return h.client.Call(h.endpoint+".Run", &args, new(interface{}))
|
2019-03-26 10:21:07 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error {
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(h.mux, args.StreamId)
|
2013-05-11 12:51:49 -04:00
|
|
|
if err != nil {
|
2013-12-10 14:50:30 -05:00
|
|
|
return NewBasicError(err)
|
2013-05-11 12:51:49 -04:00
|
|
|
}
|
2013-12-10 14:50:30 -05:00
|
|
|
defer client.Close()
|
2013-05-11 12:51:49 -04:00
|
|
|
|
2019-05-10 13:25:54 -04:00
|
|
|
h.lock.Lock()
|
2019-03-27 07:29:09 -04:00
|
|
|
if h.context == nil {
|
|
|
|
h.context, h.contextCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
2019-05-10 13:25:54 -04:00
|
|
|
h.lock.Unlock()
|
2019-03-27 07:29:09 -04:00
|
|
|
if err := h.hook.Run(h.context, args.Name, client.Ui(), client.Communicator(), args.Data); err != nil {
|
2013-06-26 20:50:25 -04:00
|
|
|
return NewBasicError(err)
|
|
|
|
}
|
2013-05-11 12:51:49 -04:00
|
|
|
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-27 07:29:09 -04:00
|
|
|
|
|
|
|
func (h *HookServer) Cancel(args *interface{}, reply *interface{}) error {
|
2019-05-10 13:25:54 -04:00
|
|
|
h.lock.Lock()
|
2019-03-27 07:29:09 -04:00
|
|
|
if h.contextCancel != nil {
|
|
|
|
h.contextCancel()
|
|
|
|
}
|
2019-05-10 13:25:54 -04:00
|
|
|
h.lock.Unlock()
|
2019-03-27 07:29:09 -04:00
|
|
|
return nil
|
|
|
|
}
|