2013-05-12 17:47:55 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2013-06-03 02:21:55 -04:00
|
|
|
"encoding/gob"
|
2013-05-12 17:47:55 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/rpc"
|
2014-05-10 00:03:35 -04:00
|
|
|
"os"
|
2013-05-12 17:47:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Communicator where the communicator is actually
|
|
|
|
// executed over an RPC connection.
|
|
|
|
type communicator struct {
|
|
|
|
client *rpc.Client
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommunicatorServer wraps a packer.Communicator implementation and makes
|
|
|
|
// it exportable as part of a Golang RPC server.
|
|
|
|
type CommunicatorServer struct {
|
2013-12-10 14:43:02 -05:00
|
|
|
c packer.Communicator
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:21:55 -04:00
|
|
|
type CommandFinished struct {
|
|
|
|
ExitStatus int
|
|
|
|
}
|
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
type CommunicatorStartArgs struct {
|
2013-12-10 14:43:02 -05:00
|
|
|
Command string
|
|
|
|
StdinStreamId uint32
|
|
|
|
StdoutStreamId uint32
|
|
|
|
StderrStreamId uint32
|
|
|
|
ResponseStreamId uint32
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-12 20:09:14 -04:00
|
|
|
type CommunicatorDownloadArgs struct {
|
2013-12-10 14:43:02 -05:00
|
|
|
Path string
|
|
|
|
WriterStreamId uint32
|
2013-05-12 20:09:14 -04:00
|
|
|
}
|
|
|
|
|
2013-05-12 19:48:46 -04:00
|
|
|
type CommunicatorUploadArgs struct {
|
2013-12-10 14:43:02 -05:00
|
|
|
Path string
|
|
|
|
ReaderStreamId uint32
|
2013-05-12 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
2013-08-23 22:18:15 -04:00
|
|
|
type CommunicatorUploadDirArgs struct {
|
|
|
|
Dst string
|
|
|
|
Src string
|
|
|
|
Exclude []string
|
|
|
|
}
|
|
|
|
|
2013-05-12 17:47:55 -04:00
|
|
|
func Communicator(client *rpc.Client) *communicator {
|
2013-12-10 14:43:02 -05:00
|
|
|
return &communicator{client: client}
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
func (c *communicator) Start(cmd *packer.RemoteCmd) (err error) {
|
|
|
|
var args CommunicatorStartArgs
|
|
|
|
args.Command = cmd.Command
|
2013-05-12 17:47:55 -04:00
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
if cmd.Stdin != nil {
|
2013-12-10 14:43:02 -05:00
|
|
|
args.StdinStreamId = c.mux.NextId()
|
|
|
|
go serveSingleCopy("stdin", c.mux, args.StdinStreamId, nil, cmd.Stdin)
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
if cmd.Stdout != nil {
|
2013-12-10 14:43:02 -05:00
|
|
|
args.StdoutStreamId = c.mux.NextId()
|
|
|
|
go serveSingleCopy("stdout", c.mux, args.StdoutStreamId, cmd.Stdout, nil)
|
2013-05-12 19:18:17 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
if cmd.Stderr != nil {
|
2013-12-10 14:43:02 -05:00
|
|
|
args.StderrStreamId = c.mux.NextId()
|
|
|
|
go serveSingleCopy("stderr", c.mux, args.StderrStreamId, cmd.Stderr, nil)
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 14:43:02 -05:00
|
|
|
responseStreamId := c.mux.NextId()
|
|
|
|
args.ResponseStreamId = responseStreamId
|
2013-06-03 02:21:55 -04:00
|
|
|
|
|
|
|
go func() {
|
2013-12-10 14:43:02 -05:00
|
|
|
conn, err := c.mux.Accept(responseStreamId)
|
2013-06-03 02:21:55 -04:00
|
|
|
if err != nil {
|
2013-12-10 14:43:02 -05:00
|
|
|
log.Printf("[ERR] Error accepting response stream %d: %s",
|
|
|
|
responseStreamId, err)
|
2013-08-20 02:21:53 -04:00
|
|
|
cmd.SetExited(123)
|
|
|
|
return
|
2013-06-03 02:21:55 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
var finished CommandFinished
|
2013-12-10 14:43:02 -05:00
|
|
|
decoder := gob.NewDecoder(conn)
|
2013-06-03 02:21:55 -04:00
|
|
|
if err := decoder.Decode(&finished); err != nil {
|
2013-12-10 14:43:02 -05:00
|
|
|
log.Printf("[ERR] Error decoding response stream %d: %s",
|
|
|
|
responseStreamId, err)
|
2013-08-20 02:21:53 -04:00
|
|
|
cmd.SetExited(123)
|
|
|
|
return
|
2013-06-03 02:21:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 18:12:16 -05:00
|
|
|
log.Printf("[INFO] RPC client: Communicator ended with: %d", finished.ExitStatus)
|
2013-07-29 15:07:37 -04:00
|
|
|
cmd.SetExited(finished.ExitStatus)
|
2013-06-03 02:21:55 -04:00
|
|
|
}()
|
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
err = c.client.Call("Communicator.Start", &args, new(interface{}))
|
2013-05-12 17:47:55 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
func (c *communicator) Upload(path string, r io.Reader, fi *os.FileInfo) (err error) {
|
2013-05-12 19:48:46 -04:00
|
|
|
// Pipe the reader through to the connection
|
2013-12-10 14:43:02 -05:00
|
|
|
streamId := c.mux.NextId()
|
2013-12-10 18:30:58 -05:00
|
|
|
go serveSingleCopy("uploadData", c.mux, streamId, nil, r)
|
2013-05-12 19:48:46 -04:00
|
|
|
|
|
|
|
args := CommunicatorUploadArgs{
|
2013-12-10 14:43:02 -05:00
|
|
|
Path: path,
|
|
|
|
ReaderStreamId: streamId,
|
2013-05-12 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
2013-05-12 20:09:14 -04:00
|
|
|
err = c.client.Call("Communicator.Upload", &args, new(interface{}))
|
2013-05-12 19:48:46 -04:00
|
|
|
return
|
2013-05-12 19:25:40 -04:00
|
|
|
}
|
2013-05-12 19:19:34 -04:00
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
func (c *communicator) UploadDir(dst string, src string, exclude []string) error {
|
2013-08-23 22:18:15 -04:00
|
|
|
args := &CommunicatorUploadDirArgs{
|
|
|
|
Dst: dst,
|
|
|
|
Src: src,
|
|
|
|
Exclude: exclude,
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply error
|
|
|
|
err := c.client.Call("Communicator.UploadDir", args, &reply)
|
|
|
|
if err == nil {
|
|
|
|
err = reply
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2013-08-23 21:29:46 -04:00
|
|
|
}
|
|
|
|
|
2013-05-12 20:09:14 -04:00
|
|
|
func (c *communicator) Download(path string, w io.Writer) (err error) {
|
|
|
|
// Serve a single connection and a single copy
|
2013-12-10 14:43:02 -05:00
|
|
|
streamId := c.mux.NextId()
|
|
|
|
go serveSingleCopy("downloadWriter", c.mux, streamId, w, nil)
|
2013-05-12 20:09:14 -04:00
|
|
|
|
|
|
|
args := CommunicatorDownloadArgs{
|
2013-12-10 14:43:02 -05:00
|
|
|
Path: path,
|
|
|
|
WriterStreamId: streamId,
|
2013-05-12 20:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = c.client.Call("Communicator.Download", &args, new(interface{}))
|
|
|
|
return
|
2013-05-12 19:25:40 -04:00
|
|
|
}
|
2013-05-12 19:19:34 -04:00
|
|
|
|
2013-12-11 14:19:36 -05:00
|
|
|
func (c *CommunicatorServer) Start(args *CommunicatorStartArgs, reply *interface{}) error {
|
2013-06-03 02:08:40 -04:00
|
|
|
// Build the RemoteCmd on this side so that it all pipes over
|
|
|
|
// to the remote side.
|
|
|
|
var cmd packer.RemoteCmd
|
|
|
|
cmd.Command = args.Command
|
|
|
|
|
2013-12-10 18:12:16 -05:00
|
|
|
// Create a channel to signal we're done so that we can close
|
|
|
|
// our stdin/stdout/stderr streams
|
2013-12-10 14:43:02 -05:00
|
|
|
toClose := make([]io.Closer, 0)
|
2013-12-10 18:12:16 -05:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
<-doneCh
|
|
|
|
for _, conn := range toClose {
|
|
|
|
defer conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-12-21 11:07:42 -05:00
|
|
|
if args.StdinStreamId > 0 {
|
2013-12-10 14:43:02 -05:00
|
|
|
conn, err := c.mux.Dial(args.StdinStreamId)
|
2013-06-03 02:08:40 -04:00
|
|
|
if err != nil {
|
2013-12-10 18:12:16 -05:00
|
|
|
close(doneCh)
|
|
|
|
return NewBasicError(err)
|
2013-06-03 02:08:40 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 14:43:02 -05:00
|
|
|
toClose = append(toClose, conn)
|
|
|
|
cmd.Stdin = conn
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-21 11:07:42 -05:00
|
|
|
if args.StdoutStreamId > 0 {
|
2013-12-10 14:43:02 -05:00
|
|
|
conn, err := c.mux.Dial(args.StdoutStreamId)
|
2013-06-03 02:08:40 -04:00
|
|
|
if err != nil {
|
2013-12-10 18:12:16 -05:00
|
|
|
close(doneCh)
|
|
|
|
return NewBasicError(err)
|
2013-06-03 02:08:40 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 14:43:02 -05:00
|
|
|
toClose = append(toClose, conn)
|
|
|
|
cmd.Stdout = conn
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-21 11:07:42 -05:00
|
|
|
if args.StderrStreamId > 0 {
|
2013-12-10 14:43:02 -05:00
|
|
|
conn, err := c.mux.Dial(args.StderrStreamId)
|
2013-06-03 02:08:40 -04:00
|
|
|
if err != nil {
|
2013-12-10 18:12:16 -05:00
|
|
|
close(doneCh)
|
|
|
|
return NewBasicError(err)
|
2013-06-03 02:08:40 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 14:43:02 -05:00
|
|
|
toClose = append(toClose, conn)
|
|
|
|
cmd.Stderr = conn
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:21:55 -04:00
|
|
|
// Connect to the response address so we can write our result to it
|
|
|
|
// when ready.
|
2013-12-10 14:43:02 -05:00
|
|
|
responseC, err := c.mux.Dial(args.ResponseStreamId)
|
2013-06-03 02:21:55 -04:00
|
|
|
if err != nil {
|
2013-12-10 18:12:16 -05:00
|
|
|
close(doneCh)
|
|
|
|
return NewBasicError(err)
|
2013-06-03 02:21:55 -04:00
|
|
|
}
|
|
|
|
responseWriter := gob.NewEncoder(responseC)
|
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
// Start the actual command
|
|
|
|
err = c.c.Start(&cmd)
|
2013-12-10 18:12:16 -05:00
|
|
|
if err != nil {
|
|
|
|
close(doneCh)
|
|
|
|
return NewBasicError(err)
|
|
|
|
}
|
2013-06-03 02:21:55 -04:00
|
|
|
|
|
|
|
// Start a goroutine to spin and wait for the process to actual
|
|
|
|
// exit. When it does, report it back to caller...
|
|
|
|
go func() {
|
2013-12-10 18:12:16 -05:00
|
|
|
defer close(doneCh)
|
2013-06-03 02:21:55 -04:00
|
|
|
defer responseC.Close()
|
2013-07-29 15:12:42 -04:00
|
|
|
cmd.Wait()
|
2013-12-10 18:12:16 -05:00
|
|
|
log.Printf("[INFO] RPC endpoint: Communicator ended with: %d", cmd.ExitStatus)
|
2013-06-03 02:21:55 -04:00
|
|
|
responseWriter.Encode(&CommandFinished{cmd.ExitStatus})
|
|
|
|
}()
|
|
|
|
|
2013-12-10 18:12:16 -05:00
|
|
|
return nil
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-12 19:48:46 -04:00
|
|
|
func (c *CommunicatorServer) Upload(args *CommunicatorUploadArgs, reply *interface{}) (err error) {
|
2013-12-10 14:43:02 -05:00
|
|
|
readerC, err := c.mux.Dial(args.ReaderStreamId)
|
2013-05-12 19:48:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer readerC.Close()
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
err = c.c.Upload(args.Path, readerC, nil)
|
2013-05-12 19:48:46 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-23 22:18:15 -04:00
|
|
|
func (c *CommunicatorServer) UploadDir(args *CommunicatorUploadDirArgs, reply *error) error {
|
|
|
|
return c.c.UploadDir(args.Dst, args.Src, args.Exclude)
|
|
|
|
}
|
|
|
|
|
2013-05-12 20:09:14 -04:00
|
|
|
func (c *CommunicatorServer) Download(args *CommunicatorDownloadArgs, reply *interface{}) (err error) {
|
2013-12-10 14:43:02 -05:00
|
|
|
writerC, err := c.mux.Dial(args.WriterStreamId)
|
2013-05-12 20:09:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer writerC.Close()
|
|
|
|
|
|
|
|
err = c.c.Download(args.Path, writerC)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-02 17:23:06 -04:00
|
|
|
func serveSingleCopy(name string, mux *muxBroker, id uint32, dst io.Writer, src io.Reader) {
|
2013-12-10 14:43:02 -05:00
|
|
|
conn, err := mux.Accept(id)
|
2013-05-12 17:47:55 -04:00
|
|
|
if err != nil {
|
2013-12-12 02:50:41 -05:00
|
|
|
log.Printf("[ERR] '%s' accept error: %s", name, err)
|
2013-05-12 17:47:55 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-28 02:36:34 -04:00
|
|
|
// Be sure to close the connection after we're done copying so
|
|
|
|
// that an EOF will successfully be sent to the remote side
|
|
|
|
defer conn.Close()
|
|
|
|
|
2013-05-12 17:47:55 -04:00
|
|
|
// The connection is the destination/source that is nil
|
|
|
|
if dst == nil {
|
|
|
|
dst = conn
|
|
|
|
} else {
|
|
|
|
src = conn
|
|
|
|
}
|
|
|
|
|
|
|
|
written, err := io.Copy(dst, src)
|
2013-12-12 02:50:41 -05:00
|
|
|
log.Printf("[INFO] %d bytes written for '%s'", written, name)
|
2013-05-12 17:47:55 -04:00
|
|
|
if err != nil {
|
2013-12-12 02:50:41 -05:00
|
|
|
log.Printf("[ERR] '%s' copy error: %s", name, err)
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
}
|