github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/packer/rpc/hook.go (about) 1 package rpc 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "net/rpc" 6 ) 7 8 // An implementation of packer.Hook where the hook is actually executed 9 // over an RPC connection. 10 type hook struct { 11 client *rpc.Client 12 } 13 14 // HookServer wraps a packer.Hook implementation and makes it exportable 15 // as part of a Golang RPC server. 16 type HookServer struct { 17 hook packer.Hook 18 } 19 20 type HookRunArgs struct { 21 Name string 22 Data interface{} 23 RPCAddress string 24 } 25 26 func Hook(client *rpc.Client) *hook { 27 return &hook{client} 28 } 29 30 func (h *hook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error { 31 server := rpc.NewServer() 32 RegisterCommunicator(server, comm) 33 RegisterUi(server, ui) 34 address := serveSingleConn(server) 35 36 args := &HookRunArgs{name, data, address} 37 return h.client.Call("Hook.Run", args, new(interface{})) 38 } 39 40 func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error { 41 client, err := rpc.Dial("tcp", args.RPCAddress) 42 if err != nil { 43 return err 44 } 45 46 if err := h.hook.Run(args.Name, &Ui{client}, Communicator(client), args.Data); err != nil { 47 return NewBasicError(err) 48 } 49 50 *reply = nil 51 return nil 52 }