|
@@ -1,30 +1,38 @@
|
|
-package main
|
|
|
|
|
|
+package server
|
|
|
|
|
|
import (
|
|
import (
|
|
- "bufio"
|
|
|
|
- "bytes"
|
|
|
|
- "encoding/json"
|
|
|
|
- "errors"
|
|
|
|
- "flag"
|
|
|
|
- "fmt"
|
|
|
|
"github.com/dotcloud/docker"
|
|
"github.com/dotcloud/docker"
|
|
- "github.com/dotcloud/docker/future"
|
|
|
|
- "github.com/dotcloud/docker/image"
|
|
|
|
"github.com/dotcloud/docker/rcli"
|
|
"github.com/dotcloud/docker/rcli"
|
|
- "io"
|
|
|
|
|
|
+ "github.com/dotcloud/docker/image"
|
|
|
|
+ "github.com/dotcloud/docker/future"
|
|
|
|
+ "bufio"
|
|
|
|
+ "errors"
|
|
"log"
|
|
"log"
|
|
- "net/http"
|
|
|
|
- "net/url"
|
|
|
|
- "os"
|
|
|
|
- "path"
|
|
|
|
|
|
+ "io"
|
|
|
|
+ "fmt"
|
|
"strings"
|
|
"strings"
|
|
- "sync"
|
|
|
|
"text/tabwriter"
|
|
"text/tabwriter"
|
|
|
|
+ "os"
|
|
"time"
|
|
"time"
|
|
|
|
+ "net/http"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "bytes"
|
|
|
|
+ "sync"
|
|
|
|
+ "net/url"
|
|
|
|
+ "path"
|
|
)
|
|
)
|
|
|
|
|
|
const VERSION = "0.0.1"
|
|
const VERSION = "0.0.1"
|
|
|
|
|
|
|
|
+func (srv *Server) ListenAndServe() error {
|
|
|
|
+ go rcli.ListenAndServeHTTP("127.0.0.1:8080", srv)
|
|
|
|
+ // 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
|
|
|
|
+ return rcli.ListenAndServe("tcp", "127.0.0.1:4242", srv)
|
|
|
|
+}
|
|
|
|
+
|
|
func (srv *Server) Name() string {
|
|
func (srv *Server) Name() string {
|
|
return "docker"
|
|
return "docker"
|
|
}
|
|
}
|
|
@@ -176,6 +184,7 @@ func (srv *Server) CmdWrite(stdin io.ReadCloser, stdout io.Writer, args ...strin
|
|
return errors.New("No such container: " + name)
|
|
return errors.New("No such container: " + name)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
func (srv *Server) CmdLs(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
func (srv *Server) CmdLs(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
cmd := rcli.Subcmd(stdout, "ls", "[OPTIONS] CONTAINER PATH", "List the contents of a container's directory")
|
|
cmd := rcli.Subcmd(stdout, "ls", "[OPTIONS] CONTAINER PATH", "List the contents of a container's directory")
|
|
if err := cmd.Parse(args); err != nil {
|
|
if err := cmd.Parse(args); err != nil {
|
|
@@ -267,7 +276,7 @@ func (srv *Server) CmdRm(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
return errors.New("No such container: " + name)
|
|
return errors.New("No such container: " + name)
|
|
}
|
|
}
|
|
if err := srv.containers.Destroy(container); err != nil {
|
|
if err := srv.containers.Destroy(container); err != nil {
|
|
- fmt.Fprintln(stdout, "Error destroying container "+name+": "+err.Error())
|
|
|
|
|
|
+ fmt.Fprintln(stdout, "Error destroying container " + name + ": " + err.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
@@ -285,7 +294,7 @@ func (srv *Server) CmdKill(stdin io.ReadCloser, stdout io.Writer, args ...string
|
|
return errors.New("No such container: " + name)
|
|
return errors.New("No such container: " + name)
|
|
}
|
|
}
|
|
if err := container.Kill(); err != nil {
|
|
if err := container.Kill(); err != nil {
|
|
- fmt.Fprintln(stdout, "Error killing container "+name+": "+err.Error())
|
|
|
|
|
|
+ fmt.Fprintln(stdout, "Error killing container " + name + ": " + err.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
@@ -372,7 +381,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
nameFilter = cmd.Arg(0)
|
|
nameFilter = cmd.Arg(0)
|
|
}
|
|
}
|
|
w := tabwriter.NewWriter(stdout, 20, 1, 3, ' ', 0)
|
|
w := tabwriter.NewWriter(stdout, 20, 1, 3, ' ', 0)
|
|
- if !*quiet {
|
|
|
|
|
|
+ if (!*quiet) {
|
|
fmt.Fprintf(w, "NAME\tID\tCREATED\tPARENT\n")
|
|
fmt.Fprintf(w, "NAME\tID\tCREATED\tPARENT\n")
|
|
}
|
|
}
|
|
for _, name := range srv.images.Names() {
|
|
for _, name := range srv.images.Names() {
|
|
@@ -389,10 +398,10 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
id += "..."
|
|
id += "..."
|
|
}
|
|
}
|
|
for idx, field := range []string{
|
|
for idx, field := range []string{
|
|
- /* NAME */ name,
|
|
|
|
- /* ID */ id,
|
|
|
|
- /* CREATED */ future.HumanDuration(time.Now().Sub(img.Created)) + " ago",
|
|
|
|
- /* PARENT */ img.Parent,
|
|
|
|
|
|
+ /* NAME */ name,
|
|
|
|
+ /* ID */ id,
|
|
|
|
+ /* CREATED */ future.HumanDuration(time.Now().Sub(img.Created)) + " ago",
|
|
|
|
+ /* PARENT */ img.Parent,
|
|
} {
|
|
} {
|
|
if idx == 0 {
|
|
if idx == 0 {
|
|
w.Write([]byte(field))
|
|
w.Write([]byte(field))
|
|
@@ -406,7 +415,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if !*quiet {
|
|
|
|
|
|
+ if (!*quiet) {
|
|
w.Flush()
|
|
w.Flush()
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
@@ -423,7 +432,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
w := tabwriter.NewWriter(stdout, 12, 1, 3, ' ', 0)
|
|
w := tabwriter.NewWriter(stdout, 12, 1, 3, ' ', 0)
|
|
- if !*quiet {
|
|
|
|
|
|
+ if (!*quiet) {
|
|
fmt.Fprintf(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tCOMMENT\n")
|
|
fmt.Fprintf(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tCOMMENT\n")
|
|
}
|
|
}
|
|
for _, container := range srv.containers.List() {
|
|
for _, container := range srv.containers.List() {
|
|
@@ -436,13 +445,13 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
if !*fl_full {
|
|
if !*fl_full {
|
|
command = docker.Trunc(command, 20)
|
|
command = docker.Trunc(command, 20)
|
|
}
|
|
}
|
|
- for idx, field := range []string{
|
|
|
|
- /* ID */ container.Id,
|
|
|
|
- /* IMAGE */ container.GetUserData("image"),
|
|
|
|
- /* COMMAND */ command,
|
|
|
|
- /* CREATED */ future.HumanDuration(time.Now().Sub(container.Created)) + " ago",
|
|
|
|
- /* STATUS */ container.State.String(),
|
|
|
|
- /* COMMENT */ comment,
|
|
|
|
|
|
+ for idx, field := range[]string {
|
|
|
|
+ /* ID */ container.Id,
|
|
|
|
+ /* IMAGE */ container.GetUserData("image"),
|
|
|
|
+ /* COMMAND */ command,
|
|
|
|
+ /* CREATED */ future.HumanDuration(time.Now().Sub(container.Created)) + " ago",
|
|
|
|
+ /* STATUS */ container.State.String(),
|
|
|
|
+ /* COMMENT */ comment,
|
|
} {
|
|
} {
|
|
if idx == 0 {
|
|
if idx == 0 {
|
|
w.Write([]byte(field))
|
|
w.Write([]byte(field))
|
|
@@ -455,7 +464,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
stdout.Write([]byte(container.Id + "\n"))
|
|
stdout.Write([]byte(container.Id + "\n"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if !*quiet {
|
|
|
|
|
|
+ if (!*quiet) {
|
|
w.Flush()
|
|
w.Flush()
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
@@ -474,6 +483,7 @@ func (srv *Server) CmdLayers(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
func (srv *Server) CmdCp(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
func (srv *Server) CmdCp(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
cmd := rcli.Subcmd(stdout,
|
|
cmd := rcli.Subcmd(stdout,
|
|
"cp", "[OPTIONS] IMAGE NAME",
|
|
"cp", "[OPTIONS] IMAGE NAME",
|
|
@@ -519,6 +529,7 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
return errors.New("No such container: " + containerName)
|
|
return errors.New("No such container: " + containerName)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
func (srv *Server) CmdTar(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
func (srv *Server) CmdTar(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
cmd := rcli.Subcmd(stdout,
|
|
cmd := rcli.Subcmd(stdout,
|
|
"tar", "CONTAINER",
|
|
"tar", "CONTAINER",
|
|
@@ -589,6 +600,7 @@ func (srv *Server) CmdReset(stdin io.ReadCloser, stdout io.Writer, args ...strin
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
func (srv *Server) CmdLogs(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
func (srv *Server) CmdLogs(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
cmd := rcli.Subcmd(stdout, "logs", "[OPTIONS] CONTAINER", "Fetch the logs of a container")
|
|
cmd := rcli.Subcmd(stdout, "logs", "[OPTIONS] CONTAINER", "Fetch the logs of a container")
|
|
if err := cmd.Parse(args); err != nil {
|
|
if err := cmd.Parse(args); err != nil {
|
|
@@ -611,10 +623,11 @@ func (srv *Server) CmdLogs(stdin io.ReadCloser, stdout io.Writer, args ...string
|
|
return errors.New("No such container: " + cmd.Arg(0))
|
|
return errors.New("No such container: " + cmd.Arg(0))
|
|
}
|
|
}
|
|
|
|
|
|
-func (srv *Server) CreateContainer(img *image.Image, user string, tty bool, openStdin bool, comment string, cmd string, args ...string) (*docker.Container, error) {
|
|
|
|
|
|
+
|
|
|
|
+func (srv *Server) CreateContainer(img *image.Image, tty bool, openStdin bool, comment string, cmd string, args ...string) (*docker.Container, error) {
|
|
id := future.RandomId()[:8]
|
|
id := future.RandomId()[:8]
|
|
container, err := srv.containers.Create(id, cmd, args, img.Layers,
|
|
container, err := srv.containers.Create(id, cmd, args, img.Layers,
|
|
- &docker.Config{Hostname: id, User: user, Tty: tty, OpenStdin: openStdin})
|
|
|
|
|
|
+ &docker.Config{Hostname: id, Tty: tty, OpenStdin: openStdin})
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -653,7 +666,7 @@ func (srv *Server) CmdAttach(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
wg.Add(1)
|
|
wg.Add(1)
|
|
- go func() { io.Copy(c_stdin, stdin); wg.Add(-1) }()
|
|
|
|
|
|
+ go func() { io.Copy(c_stdin, stdin); wg.Add(-1); }()
|
|
}
|
|
}
|
|
if *fl_o {
|
|
if *fl_o {
|
|
c_stdout, err := container.StdoutPipe()
|
|
c_stdout, err := container.StdoutPipe()
|
|
@@ -661,7 +674,7 @@ func (srv *Server) CmdAttach(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
wg.Add(1)
|
|
wg.Add(1)
|
|
- go func() { io.Copy(stdout, c_stdout); wg.Add(-1) }()
|
|
|
|
|
|
+ go func() { io.Copy(stdout, c_stdout); wg.Add(-1); }()
|
|
}
|
|
}
|
|
if *fl_e {
|
|
if *fl_e {
|
|
c_stderr, err := container.StderrPipe()
|
|
c_stderr, err := container.StderrPipe()
|
|
@@ -669,7 +682,7 @@ func (srv *Server) CmdAttach(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
wg.Add(1)
|
|
wg.Add(1)
|
|
- go func() { io.Copy(stdout, c_stderr); wg.Add(-1) }()
|
|
|
|
|
|
+ go func() { io.Copy(stdout, c_stderr); wg.Add(-1); }()
|
|
}
|
|
}
|
|
wg.Wait()
|
|
wg.Wait()
|
|
return nil
|
|
return nil
|
|
@@ -680,13 +693,12 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
fl_attach := cmd.Bool("a", false, "Attach stdin and stdout")
|
|
fl_attach := cmd.Bool("a", false, "Attach stdin and stdout")
|
|
fl_stdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
|
|
fl_stdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
|
|
fl_tty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
|
fl_tty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
|
- fl_user := cmd.String("u", "0", "Username or UID")
|
|
|
|
fl_comment := cmd.String("c", "", "Comment")
|
|
fl_comment := cmd.String("c", "", "Comment")
|
|
if err := cmd.Parse(args); err != nil {
|
|
if err := cmd.Parse(args); err != nil {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
name := cmd.Arg(0)
|
|
name := cmd.Arg(0)
|
|
- var cmdline []string
|
|
|
|
|
|
+ var cmdline[]string
|
|
if len(cmd.Args()) >= 2 {
|
|
if len(cmd.Args()) >= 2 {
|
|
cmdline = cmd.Args()[1:]
|
|
cmdline = cmd.Args()[1:]
|
|
}
|
|
}
|
|
@@ -707,7 +719,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
return errors.New("No such image: " + name)
|
|
return errors.New("No such image: " + name)
|
|
}
|
|
}
|
|
// Create new container
|
|
// Create new container
|
|
- container, err := srv.CreateContainer(img, *fl_user, *fl_tty, *fl_stdin, *fl_comment, cmdline[0], cmdline[1:]...)
|
|
|
|
|
|
+ container, err := srv.CreateContainer(img, *fl_tty, *fl_stdin, *fl_comment, cmdline[0], cmdline[1:]...)
|
|
if err != nil {
|
|
if err != nil {
|
|
return errors.New("Error creating container: " + err.Error())
|
|
return errors.New("Error creating container: " + err.Error())
|
|
}
|
|
}
|
|
@@ -719,7 +731,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
if *fl_attach {
|
|
if *fl_attach {
|
|
future.Go(func() error {
|
|
future.Go(func() error {
|
|
log.Printf("CmdRun(): start receiving stdin\n")
|
|
log.Printf("CmdRun(): start receiving stdin\n")
|
|
- _, err := io.Copy(cmd_stdin, stdin)
|
|
|
|
|
|
+ _, err := io.Copy(cmd_stdin, stdin);
|
|
log.Printf("CmdRun(): done receiving stdin\n")
|
|
log.Printf("CmdRun(): done receiving stdin\n")
|
|
cmd_stdin.Close()
|
|
cmd_stdin.Close()
|
|
return err
|
|
return err
|
|
@@ -740,11 +752,11 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
sending_stdout := future.Go(func() error {
|
|
sending_stdout := future.Go(func() error {
|
|
- _, err := io.Copy(stdout, cmd_stdout)
|
|
|
|
|
|
+ _, err := io.Copy(stdout, cmd_stdout);
|
|
return err
|
|
return err
|
|
})
|
|
})
|
|
sending_stderr := future.Go(func() error {
|
|
sending_stderr := future.Go(func() error {
|
|
- _, err := io.Copy(stdout, cmd_stderr)
|
|
|
|
|
|
+ _, err := io.Copy(stdout, cmd_stderr);
|
|
return err
|
|
return err
|
|
})
|
|
})
|
|
err_sending_stdout := <-sending_stdout
|
|
err_sending_stdout := <-sending_stdout
|
|
@@ -765,33 +777,8 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func main() {
|
|
|
|
- if docker.SelfPath() == "/sbin/init" {
|
|
|
|
- // Running in init mode
|
|
|
|
- docker.SysInit()
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- future.Seed()
|
|
|
|
- flag.Parse()
|
|
|
|
- d, err := New()
|
|
|
|
- if err != nil {
|
|
|
|
- log.Fatal(err)
|
|
|
|
- }
|
|
|
|
- go func() {
|
|
|
|
- if err := rcli.ListenAndServeHTTP("127.0.0.1:8080", d); err != nil {
|
|
|
|
- log.Fatal(err)
|
|
|
|
- }
|
|
|
|
- }()
|
|
|
|
- // 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 err := rcli.ListenAndServe("tcp", "127.0.0.1:4242", d); err != nil {
|
|
|
|
- log.Fatal(err)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func New() (*Server, error) {
|
|
func New() (*Server, error) {
|
|
|
|
+ future.Seed()
|
|
images, err := image.New("/var/lib/docker/images")
|
|
images, err := image.New("/var/lib/docker/images")
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
@@ -801,7 +788,7 @@ func New() (*Server, error) {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
srv := &Server{
|
|
srv := &Server{
|
|
- images: images,
|
|
|
|
|
|
+ images: images,
|
|
containers: containers,
|
|
containers: containers,
|
|
}
|
|
}
|
|
return srv, nil
|
|
return srv, nil
|
|
@@ -846,7 +833,9 @@ func (srv *Server) CmdWeb(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
type Server struct {
|
|
type Server struct {
|
|
- containers *docker.Docker
|
|
|
|
- images *image.Store
|
|
|
|
|
|
+ containers *docker.Docker
|
|
|
|
+ images *image.Store
|
|
}
|
|
}
|
|
|
|
+
|