2013-01-24 23:01:32 -05:00
|
|
|
package rcli
|
|
|
|
|
|
|
|
import (
|
2013-03-09 22:44:09 -05:00
|
|
|
"bufio"
|
2013-04-03 15:25:19 -04:00
|
|
|
"bytes"
|
2013-03-09 22:44:09 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2013-01-24 23:01:32 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2013-03-09 22:44:09 -05:00
|
|
|
"net"
|
2013-01-24 23:01:32 -05:00
|
|
|
)
|
|
|
|
|
2013-03-22 14:44:12 -04:00
|
|
|
// Note: the globals are here to avoid import cycle
|
|
|
|
// FIXME: Handle debug levels mode?
|
|
|
|
var DEBUG_FLAG bool = false
|
|
|
|
var CLIENT_SOCKET io.Writer = nil
|
|
|
|
|
2013-04-03 15:25:19 -04:00
|
|
|
type DockerTCPConn struct {
|
|
|
|
conn *net.TCPConn
|
|
|
|
options *DockerConnOptions
|
|
|
|
optionsBuf *[]byte
|
|
|
|
handshaked bool
|
|
|
|
client bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDockerTCPConn(conn *net.TCPConn, client bool) *DockerTCPConn {
|
|
|
|
return &DockerTCPConn{
|
|
|
|
conn: conn,
|
|
|
|
options: &DockerConnOptions{},
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DockerTCPConn) SetOptionRawTerminal() {
|
|
|
|
c.options.RawTerminal = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DockerTCPConn) GetOptions() *DockerConnOptions {
|
|
|
|
if c.client && !c.handshaked {
|
|
|
|
// Attempt to parse options encoded as a JSON dict and store
|
|
|
|
// the reminder of what we read from the socket in a buffer.
|
|
|
|
//
|
|
|
|
// bufio (and its ReadBytes method) would have been nice here,
|
|
|
|
// but if json.Unmarshal() fails (which will happen if we speak
|
|
|
|
// to a version of docker that doesn't send any option), then
|
|
|
|
// we can't put the data back in it for the next Read().
|
|
|
|
c.handshaked = true
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
if n, _ := c.conn.Read(buf); n > 0 {
|
|
|
|
buf = buf[:n]
|
|
|
|
if nl := bytes.IndexByte(buf, '\n'); nl != -1 {
|
|
|
|
if err := json.Unmarshal(buf[:nl], c.options); err == nil {
|
|
|
|
buf = buf[nl+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.optionsBuf = &buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DockerTCPConn) Read(b []byte) (int, error) {
|
|
|
|
if c.optionsBuf != nil {
|
|
|
|
// Consume what we buffered in GetOptions() first:
|
|
|
|
optionsBuf := *c.optionsBuf
|
|
|
|
optionsBuflen := len(optionsBuf)
|
|
|
|
copied := copy(b, optionsBuf)
|
|
|
|
if copied < optionsBuflen {
|
|
|
|
optionsBuf = optionsBuf[copied:]
|
|
|
|
c.optionsBuf = &optionsBuf
|
|
|
|
return copied, nil
|
|
|
|
}
|
|
|
|
c.optionsBuf = nil
|
|
|
|
return copied, nil
|
|
|
|
}
|
|
|
|
return c.conn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DockerTCPConn) Write(b []byte) (int, error) {
|
|
|
|
optionsLen := 0
|
|
|
|
if !c.client && !c.handshaked {
|
|
|
|
c.handshaked = true
|
|
|
|
options, _ := json.Marshal(c.options)
|
|
|
|
options = append(options, '\n')
|
|
|
|
if optionsLen, err := c.conn.Write(options); err != nil {
|
|
|
|
return optionsLen, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err := c.conn.Write(b)
|
|
|
|
return n + optionsLen, err
|
|
|
|
}
|
|
|
|
|
2013-04-05 23:08:31 -04:00
|
|
|
func (c *DockerTCPConn) Flush() error {
|
2013-04-09 12:59:30 -04:00
|
|
|
_, err := c.Write([]byte{})
|
2013-04-05 23:08:31 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-04-03 15:25:19 -04:00
|
|
|
func (c *DockerTCPConn) Close() error { return c.conn.Close() }
|
|
|
|
|
|
|
|
func (c *DockerTCPConn) CloseWrite() error { return c.conn.CloseWrite() }
|
|
|
|
|
|
|
|
func (c *DockerTCPConn) CloseRead() error { return c.conn.CloseRead() }
|
|
|
|
|
2013-02-12 11:37:12 -05:00
|
|
|
// Connect to a remote endpoint using protocol `proto` and address `addr`,
|
|
|
|
// issue a single call, and return the result.
|
|
|
|
// `proto` may be "tcp", "unix", etc. See the `net` package for available protocols.
|
2013-04-03 15:25:19 -04:00
|
|
|
func Call(proto, addr string, args ...string) (DockerConn, error) {
|
2013-01-24 23:01:32 -05:00
|
|
|
cmd, err := json.Marshal(args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-04-03 15:25:19 -04:00
|
|
|
conn, err := dialDocker(proto, addr)
|
2013-01-24 23:01:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintln(conn, string(cmd)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-04-03 15:25:19 -04:00
|
|
|
return conn, nil
|
2013-01-24 23:01:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:37:12 -05:00
|
|
|
// Listen on `addr`, using protocol `proto`, for incoming rcli calls,
|
|
|
|
// and pass them to `service`.
|
|
|
|
func ListenAndServe(proto, addr string, service Service) error {
|
|
|
|
listener, err := net.Listen(proto, addr)
|
2013-01-24 23:01:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-02-12 11:37:12 -05:00
|
|
|
log.Printf("Listening for RCLI/%s on %s\n", proto, addr)
|
2013-01-24 23:01:32 -05:00
|
|
|
defer listener.Close()
|
|
|
|
for {
|
|
|
|
if conn, err := listener.Accept(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2013-04-03 15:25:19 -04:00
|
|
|
conn, err := newDockerServerConn(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-09 18:00:05 -04:00
|
|
|
go func(conn DockerConn) {
|
|
|
|
defer conn.Close()
|
2013-03-22 14:44:12 -04:00
|
|
|
if DEBUG_FLAG {
|
|
|
|
CLIENT_SOCKET = conn
|
|
|
|
}
|
2013-01-24 23:01:32 -05:00
|
|
|
if err := Serve(conn, service); err != nil {
|
2013-03-30 03:23:12 -04:00
|
|
|
log.Println("Error:", err.Error())
|
|
|
|
fmt.Fprintln(conn, "Error:", err.Error())
|
2013-01-24 23:01:32 -05:00
|
|
|
}
|
2013-04-09 18:00:05 -04:00
|
|
|
}(conn)
|
2013-01-24 23:01:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-12 11:42:15 -05:00
|
|
|
// Parse an rcli call on a new connection, and pass it to `service` if it
|
|
|
|
// is valid.
|
2013-04-03 15:25:19 -04:00
|
|
|
func Serve(conn DockerConn, service Service) error {
|
2013-01-24 23:01:32 -05:00
|
|
|
r := bufio.NewReader(conn)
|
|
|
|
var args []string
|
|
|
|
if line, err := r.ReadString('\n'); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err := json.Unmarshal([]byte(line), &args); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return call(service, ioutil.NopCloser(r), conn, args...)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|