2013-01-20 00:07:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-03-13 07:29:40 +00:00
|
|
|
"flag"
|
|
|
|
"github.com/dotcloud/docker"
|
|
|
|
"github.com/dotcloud/docker/future"
|
|
|
|
"github.com/dotcloud/docker/rcli"
|
|
|
|
"github.com/dotcloud/docker/term"
|
|
|
|
"io"
|
2013-01-20 00:07:19 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2013-01-24 07:14:46 +00:00
|
|
|
)
|
|
|
|
|
2013-01-20 00:07:19 +00:00
|
|
|
func main() {
|
2013-03-13 07:29:40 +00:00
|
|
|
if docker.SelfPath() == "/sbin/init" {
|
|
|
|
// Running in init mode
|
|
|
|
docker.SysInit()
|
|
|
|
return
|
2013-01-25 19:26:18 +00:00
|
|
|
}
|
2013-03-13 07:29:40 +00:00
|
|
|
fl_daemon := flag.Bool("d", false, "Daemon mode")
|
|
|
|
flag.Parse()
|
|
|
|
if *fl_daemon {
|
|
|
|
if flag.NArg() != 0 {
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := daemon(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := runCommand(flag.Args()); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func daemon() error {
|
2013-03-14 09:26:46 +00:00
|
|
|
service, err := docker.NewServer()
|
2013-03-13 07:29:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rcli.ListenAndServe("tcp", "127.0.0.1:4242", service)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommand(args []string) error {
|
|
|
|
var oldState *term.State
|
|
|
|
var err error
|
|
|
|
if term.IsTerminal(0) && os.Getenv("NORAW") == "" {
|
|
|
|
oldState, err = term.MakeRaw(0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer term.Restore(0, oldState)
|
|
|
|
}
|
|
|
|
// FIXME: we want to use unix sockets here, but net.UnixConn doesn't expose
|
|
|
|
// CloseWrite(), which we need to cleanly signal that stdin is closed without
|
|
|
|
// closing the connection.
|
|
|
|
// See http://code.google.com/p/go/issues/detail?id=3345
|
|
|
|
if conn, err := rcli.Call("tcp", "127.0.0.1:4242", args...); err == nil {
|
|
|
|
receive_stdout := future.Go(func() error {
|
|
|
|
_, err := io.Copy(os.Stdout, conn)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
send_stdin := future.Go(func() error {
|
|
|
|
_, err := io.Copy(conn, os.Stdin)
|
|
|
|
if err := conn.CloseWrite(); err != nil {
|
|
|
|
log.Printf("Couldn't send EOF: " + err.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err := <-receive_stdout; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !term.IsTerminal(0) {
|
|
|
|
if err := <-send_stdin; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-03-14 09:26:46 +00:00
|
|
|
service, err := docker.NewServer()
|
2013-03-13 07:29:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := rcli.LocalCall(service, os.Stdin, os.Stdout, args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if oldState != nil {
|
|
|
|
term.Restore(0, oldState)
|
|
|
|
}
|
|
|
|
return nil
|
2013-01-20 00:07:19 +00:00
|
|
|
}
|