github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/command.go (about) 1 package rpc 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "net/rpc" 6 ) 7 8 // A Command is an implementation of the packer.Command interface where the 9 // command is actually executed over an RPC connection. 10 type command struct { 11 client *rpc.Client 12 mux *muxBroker 13 } 14 15 // A CommandServer wraps a packer.Command and makes it exportable as part 16 // of a Golang RPC server. 17 type CommandServer struct { 18 command packer.Command 19 mux *muxBroker 20 } 21 22 type CommandRunArgs struct { 23 Args []string 24 StreamId uint32 25 } 26 27 type CommandSynopsisArgs byte 28 29 func (c *command) Help() (result string) { 30 err := c.client.Call("Command.Help", new(interface{}), &result) 31 if err != nil { 32 panic(err) 33 } 34 35 return 36 } 37 38 func (c *command) Run(env packer.Environment, args []string) (result int) { 39 nextId := c.mux.NextId() 40 server := newServerWithMux(c.mux, nextId) 41 server.RegisterEnvironment(env) 42 go server.Serve() 43 44 rpcArgs := &CommandRunArgs{ 45 Args: args, 46 StreamId: nextId, 47 } 48 err := c.client.Call("Command.Run", rpcArgs, &result) 49 if err != nil { 50 panic(err) 51 } 52 53 return 54 } 55 56 func (c *command) Synopsis() (result string) { 57 err := c.client.Call("Command.Synopsis", CommandSynopsisArgs(0), &result) 58 if err != nil { 59 panic(err) 60 } 61 62 return 63 } 64 65 func (c *CommandServer) Help(args *interface{}, reply *string) error { 66 *reply = c.command.Help() 67 return nil 68 } 69 70 func (c *CommandServer) Run(args *CommandRunArgs, reply *int) error { 71 client, err := newClientWithMux(c.mux, args.StreamId) 72 if err != nil { 73 return NewBasicError(err) 74 } 75 defer client.Close() 76 77 *reply = c.command.Run(client.Environment(), args.Args) 78 return nil 79 } 80 81 func (c *CommandServer) Synopsis(args *CommandSynopsisArgs, reply *string) error { 82 *reply = c.command.Synopsis() 83 return nil 84 }