github.com/jerryclinesmith/packer@v0.3.7/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 } 13 14 // A CommandServer wraps a packer.Command and makes it exportable as part 15 // of a Golang RPC server. 16 type CommandServer struct { 17 command packer.Command 18 } 19 20 type CommandRunArgs struct { 21 RPCAddress string 22 Args []string 23 } 24 25 type CommandSynopsisArgs byte 26 27 func Command(client *rpc.Client) *command { 28 return &command{client} 29 } 30 31 func (c *command) Help() (result string) { 32 err := c.client.Call("Command.Help", new(interface{}), &result) 33 if err != nil { 34 panic(err) 35 } 36 37 return 38 } 39 40 func (c *command) Run(env packer.Environment, args []string) (result int) { 41 // Create and start the server for the Environment 42 server := rpc.NewServer() 43 RegisterEnvironment(server, env) 44 45 rpcArgs := &CommandRunArgs{serveSingleConn(server), args} 46 err := c.client.Call("Command.Run", rpcArgs, &result) 47 if err != nil { 48 panic(err) 49 } 50 51 return 52 } 53 54 func (c *command) Synopsis() (result string) { 55 err := c.client.Call("Command.Synopsis", CommandSynopsisArgs(0), &result) 56 if err != nil { 57 panic(err) 58 } 59 60 return 61 } 62 63 func (c *CommandServer) Help(args *interface{}, reply *string) error { 64 *reply = c.command.Help() 65 return nil 66 } 67 68 func (c *CommandServer) Run(args *CommandRunArgs, reply *int) error { 69 client, err := rpc.Dial("tcp", args.RPCAddress) 70 if err != nil { 71 return err 72 } 73 74 env := &Environment{client} 75 76 *reply = c.command.Run(env, args.Args) 77 return nil 78 } 79 80 func (c *CommandServer) Synopsis(args *CommandSynopsisArgs, reply *string) error { 81 *reply = c.command.Synopsis() 82 return nil 83 }