forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.go
70 lines (58 loc) · 1.47 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package rpc
import (
"github.com/hashicorp/packer/packer"
"log"
"net/rpc"
)
// An implementation of packer.Hook where the hook is actually executed
// over an RPC connection.
type hook struct {
client *rpc.Client
mux *muxBroker
}
// HookServer wraps a packer.Hook implementation and makes it exportable
// as part of a Golang RPC server.
type HookServer struct {
hook packer.Hook
mux *muxBroker
}
type HookRunArgs struct {
Name string
Data interface{}
StreamId uint32
}
func (h *hook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error {
nextId := h.mux.NextId()
server := newServerWithMux(h.mux, nextId)
server.RegisterCommunicator(comm)
server.RegisterUi(ui)
go server.Serve()
args := HookRunArgs{
Name: name,
Data: data,
StreamId: nextId,
}
return h.client.Call("Hook.Run", &args, new(interface{}))
}
func (h *hook) Cancel() {
err := h.client.Call("Hook.Cancel", new(interface{}), new(interface{}))
if err != nil {
log.Printf("Hook.Cancel error: %s", err)
}
}
func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error {
client, err := newClientWithMux(h.mux, args.StreamId)
if err != nil {
return NewBasicError(err)
}
defer client.Close()
if err := h.hook.Run(args.Name, client.Ui(), client.Communicator(), args.Data); err != nil {
return NewBasicError(err)
}
*reply = nil
return nil
}
func (h *HookServer) Cancel(args *interface{}, reply *interface{}) error {
h.hook.Cancel()
return nil
}