packer-cn/packer/rpc/hook.go

66 lines
1.4 KiB
Go
Raw Normal View History

2013-05-11 12:51:49 -04:00
package rpc
import (
"github.com/mitchellh/packer/packer"
2013-08-31 02:03:43 -04:00
"log"
2013-05-11 12:51:49 -04:00
"net/rpc"
)
// An implementation of packer.Hook where the hook is actually executed
// over an RPC connection.
type hook struct {
client *rpc.Client
}
// HookServer wraps a packer.Hook implementation and makes it exportable
// as part of a Golang RPC server.
type HookServer struct {
hook packer.Hook
}
type HookRunArgs struct {
2013-05-20 19:50:35 -04:00
Name string
Data interface{}
2013-05-11 12:51:49 -04:00
RPCAddress string
}
func Hook(client *rpc.Client) *hook {
return &hook{client}
}
func (h *hook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error {
2013-05-11 12:51:49 -04:00
server := rpc.NewServer()
RegisterCommunicator(server, comm)
2013-05-11 12:51:49 -04:00
RegisterUi(server, ui)
address := serveSingleConn(server)
args := &HookRunArgs{name, data, address}
return h.client.Call("Hook.Run", args, new(interface{}))
2013-05-11 12:51:49 -04:00
}
2013-08-30 20:03:55 -04:00
func (h *hook) Cancel() {
2013-08-31 02:03:43 -04:00
err := h.client.Call("Hook.Cancel", new(interface{}), new(interface{}))
if err != nil {
log.Printf("Hook.Cancel error: %s", err)
}
2013-08-30 20:03:55 -04:00
}
2013-05-11 12:51:49 -04:00
func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error {
client, err := rpcDial(args.RPCAddress)
2013-05-11 12:51:49 -04:00
if err != nil {
return err
}
if err := h.hook.Run(args.Name, &Ui{client: client}, Communicator(client), args.Data); err != nil {
return NewBasicError(err)
}
2013-05-11 12:51:49 -04:00
*reply = nil
return nil
}
2013-08-31 02:03:43 -04:00
func (h *HookServer) Cancel(args *interface{}, reply *interface{}) error {
h.hook.Cancel()
return nil
}