This commit is contained in:
Andrea Luzzardi 2013-02-13 17:28:13 -08:00
parent cb6014d6fd
commit 2f84bab879
1 changed files with 42 additions and 49 deletions

View File

@ -1,25 +1,25 @@
package server package server
import ( import (
"github.com/dotcloud/docker"
"github.com/dotcloud/docker/rcli"
"github.com/dotcloud/docker/image"
"github.com/dotcloud/docker/future"
"bufio" "bufio"
"errors"
"log"
"io"
"fmt"
"strings"
"text/tabwriter"
"os"
"time"
"net/http"
"encoding/json"
"bytes" "bytes"
"sync" "encoding/json"
"errors"
"fmt"
"github.com/dotcloud/docker"
"github.com/dotcloud/docker/future"
"github.com/dotcloud/docker/image"
"github.com/dotcloud/docker/rcli"
"io"
"log"
"net/http"
"net/url" "net/url"
"os"
"path" "path"
"strings"
"sync"
"text/tabwriter"
"time"
) )
const VERSION = "0.0.1" const VERSION = "0.0.1"
@ -184,7 +184,6 @@ 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 {
@ -276,7 +275,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
@ -294,7 +293,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
@ -381,7 +380,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() {
@ -415,7 +414,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
@ -432,7 +431,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() {
@ -445,7 +444,7 @@ 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 { for idx, field := range []string{
/* ID */ container.Id, /* ID */ container.Id,
/* IMAGE */ container.GetUserData("image"), /* IMAGE */ container.GetUserData("image"),
/* COMMAND */ command, /* COMMAND */ command,
@ -464,7 +463,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
@ -483,7 +482,6 @@ 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",
@ -529,7 +527,6 @@ 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",
@ -600,7 +597,6 @@ 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 {
@ -623,7 +619,6 @@ 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, 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,
@ -666,7 +661,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()
@ -674,7 +669,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()
@ -682,7 +677,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
@ -698,7 +693,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
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:]
} }
@ -731,7 +726,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
@ -752,11 +747,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
@ -833,9 +828,7 @@ 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 containers *docker.Docker
images *image.Store images *image.Store
} }