2013-01-19 16:07:19 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-01-25 14:44:16 -08:00
|
|
|
"github.com/dotcloud/docker"
|
2013-01-24 20:01:32 -08:00
|
|
|
"github.com/dotcloud/docker/rcli"
|
|
|
|
"github.com/dotcloud/docker/fake"
|
|
|
|
"github.com/dotcloud/docker/future"
|
2013-01-23 23:14:46 -08:00
|
|
|
"bufio"
|
2013-01-19 16:07:19 -08:00
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"io"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
2013-01-20 14:13:25 -08:00
|
|
|
"sort"
|
2013-01-20 15:55:00 -08:00
|
|
|
"os"
|
2013-01-24 20:01:32 -08:00
|
|
|
"time"
|
2013-01-25 11:32:37 -08:00
|
|
|
"net/http"
|
2013-01-19 16:07:19 -08:00
|
|
|
)
|
|
|
|
|
2013-01-24 20:01:32 -08:00
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) Name() string {
|
2013-01-24 20:01:32 -08:00
|
|
|
return "docker"
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) Help() string {
|
2013-01-24 20:01:32 -08:00
|
|
|
help := "Usage: docker COMMAND [arg...]\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n"
|
|
|
|
for _, cmd := range [][]interface{}{
|
|
|
|
{"run", "Run a command in a container"},
|
|
|
|
{"list", "Display a list of containers"},
|
|
|
|
{"pull", "Download a tarball and create a container from it"},
|
|
|
|
{"put", "Upload a tarball and create a container from it"},
|
|
|
|
{"rm", "Remove containers"},
|
|
|
|
{"wait", "Wait for the state of a container to change"},
|
|
|
|
{"stop", "Stop a running container"},
|
|
|
|
{"logs", "Fetch the logs of a container"},
|
|
|
|
{"diff", "Inspect changes on a container's filesystem"},
|
|
|
|
{"commit", "Save the state of a container"},
|
|
|
|
{"attach", "Attach to the standard inputs and outputs of a running container"},
|
|
|
|
{"info", "Display system-wide information"},
|
|
|
|
{"web", "Generate a web UI"},
|
|
|
|
} {
|
|
|
|
help += fmt.Sprintf(" %-10.10s%s\n", cmd...)
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
2013-01-24 20:01:32 -08:00
|
|
|
return help
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
|
|
|
|
2013-01-24 20:01:32 -08:00
|
|
|
|
2013-01-26 15:56:42 -08:00
|
|
|
func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
|
|
cmd := rcli.Subcmd(stdout, "stop", "[OPTIONS] NAME", "Stop a running container")
|
|
|
|
if err := cmd.Parse(args); err != nil {
|
|
|
|
cmd.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if cmd.NArg() < 1 {
|
|
|
|
cmd.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, name := range cmd.Args() {
|
|
|
|
if container := srv.docker.Get(name); container != nil {
|
|
|
|
if err := container.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(stdout, container.Id)
|
|
|
|
} else {
|
|
|
|
return errors.New("No such container: " + container.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdList(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout, "list", "[OPTIONS] [NAME]", "List containers")
|
2013-01-22 18:29:44 -08:00
|
|
|
limit := flags.Int("l", 0, "Only show the N most recent versions of each name")
|
|
|
|
quiet := flags.Bool("q", false, "only show numeric IDs")
|
2013-01-20 00:37:52 -08:00
|
|
|
flags.Parse(args)
|
|
|
|
if flags.NArg() > 1 {
|
|
|
|
flags.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var nameFilter string
|
|
|
|
if flags.NArg() == 1 {
|
|
|
|
nameFilter = flags.Arg(0)
|
|
|
|
}
|
2013-01-20 14:13:25 -08:00
|
|
|
var names []string
|
2013-01-25 14:44:16 -08:00
|
|
|
for name := range srv.containersByName {
|
2013-01-20 14:13:25 -08:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
w := tabwriter.NewWriter(stdout, 20, 1, 3, ' ', 0)
|
|
|
|
if (!*quiet) {
|
2013-01-22 18:29:44 -08:00
|
|
|
fmt.Fprintf(w, "NAME\tID\tCREATED\tSOURCE\tSIZE\tCHANGES\tRUNNING\tCOMMAND\n")
|
2013-01-20 14:13:25 -08:00
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
if nameFilter != "" && nameFilter != name {
|
|
|
|
continue
|
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
for idx, container := range *srv.containersByName[name] {
|
2013-01-20 14:13:25 -08:00
|
|
|
if *limit > 0 && idx >= *limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !*quiet {
|
2013-01-22 18:29:44 -08:00
|
|
|
for idx, field := range []string{
|
2013-01-25 14:44:16 -08:00
|
|
|
/* NAME */ container.GetUserData("name"),
|
2013-01-22 18:29:44 -08:00
|
|
|
/* ID */ container.Id,
|
2013-01-24 20:01:32 -08:00
|
|
|
/* CREATED */ future.HumanDuration(time.Now().Sub(container.Created)) + " ago",
|
2013-01-25 14:44:16 -08:00
|
|
|
/* SOURCE */ container.GetUserData("source"),
|
|
|
|
/* SIZE */ fmt.Sprintf("%.1fM", float32(fake.RandomContainerSize()) / 1024 / 1024),
|
|
|
|
/* CHANGES */ fmt.Sprintf("%.1fM", float32(fake.RandomBytesChanged() / 1024 / 1024)),
|
2013-01-26 15:56:42 -08:00
|
|
|
/* RUNNING */ fmt.Sprintf("%v", container.State.Running),
|
2013-01-25 14:44:16 -08:00
|
|
|
/* COMMAND */ fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " ")),
|
2013-01-22 18:29:44 -08:00
|
|
|
} {
|
|
|
|
if idx == 0 {
|
|
|
|
w.Write([]byte(field))
|
|
|
|
} else {
|
|
|
|
w.Write([]byte("\t" + field))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte{'\n'})
|
2013-01-20 14:13:25 -08:00
|
|
|
} else {
|
2013-01-22 18:29:44 -08:00
|
|
|
stdout.Write([]byte(container.Id + "\n"))
|
2013-01-20 00:37:52 -08:00
|
|
|
}
|
|
|
|
}
|
2013-01-20 14:13:25 -08:00
|
|
|
}
|
|
|
|
if (!*quiet) {
|
2013-01-20 00:37:52 -08:00
|
|
|
w.Flush()
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) findContainer(name string) (*fake.Container, bool) {
|
2013-01-22 18:29:44 -08:00
|
|
|
// 1: look for container by ID
|
2013-01-25 14:44:16 -08:00
|
|
|
if container := srv.docker.Get(name); container != nil {
|
|
|
|
return fake.NewContainer(container), true
|
2013-01-21 18:12:56 -08:00
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
// 2: look for a container by name (and pick the most recent)
|
2013-01-25 14:44:16 -08:00
|
|
|
if containers, exists := srv.containersByName[name]; exists {
|
|
|
|
return fake.NewContainer((*containers)[0]), true
|
2013-01-21 18:12:56 -08:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdRm(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout, "rm", "[OPTIONS] CONTAINER", "Remove a container")
|
2013-01-21 18:12:56 -08:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, name := range flags.Args() {
|
2013-01-25 14:44:16 -08:00
|
|
|
if _, err := srv.rm(name); err != nil {
|
2013-01-21 18:12:56 -08:00
|
|
|
fmt.Fprintln(stdout, "Error: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-19 18:24:01 -08:00
|
|
|
if len(args) < 1 {
|
|
|
|
return errors.New("Not enough arguments")
|
|
|
|
}
|
2013-01-25 11:32:37 -08:00
|
|
|
resp, err := http.Get(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
layer, err := srv.layers.AddLayer(resp.Body, stdout)
|
2013-01-25 11:32:37 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
container, err := srv.addContainer(layer.Id(), []string{layer.Path}, args[0], "download")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(stdout, container.Id)
|
2013-01-19 18:24:01 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdPut(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-20 00:43:16 -08:00
|
|
|
if len(args) < 1 {
|
|
|
|
return errors.New("Not enough arguments")
|
|
|
|
}
|
2013-01-25 11:32:37 -08:00
|
|
|
fmt.Printf("Adding layer\n")
|
2013-01-25 14:44:16 -08:00
|
|
|
layer, err := srv.layers.AddLayer(stdin, stdout)
|
2013-01-25 11:32:37 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
id := layer.Id()
|
|
|
|
if !srv.docker.Exists(id) {
|
|
|
|
log.Println("Creating new container: " + id)
|
|
|
|
log.Printf("%v\n", srv.docker.List())
|
|
|
|
_, err := srv.addContainer(id, []string{layer.Path}, args[0], "upload")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintln(stdout, id)
|
2013-01-19 16:07:19 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:32:37 -08:00
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout,
|
2013-01-22 18:29:44 -08:00
|
|
|
"fork", "[OPTIONS] CONTAINER [DEST]",
|
|
|
|
"Duplicate a container")
|
|
|
|
// FIXME "-r" to reset changes in the new container
|
2013-01-22 09:54:56 -08:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
srcName, dstName := flags.Arg(0), flags.Arg(1)
|
|
|
|
if srcName == "" {
|
2013-01-22 09:54:56 -08:00
|
|
|
flags.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
if dstName == "" {
|
|
|
|
dstName = srcName
|
|
|
|
}
|
2013-01-26 15:56:42 -08:00
|
|
|
/*
|
|
|
|
if src, exists := srv.findContainer(srcName); exists {
|
|
|
|
baseLayer := src.Filesystem.Layers[0]
|
2013-01-25 14:44:16 -08:00
|
|
|
//dst := srv.addContainer(dstName, "snapshot:" + src.Id, src.Size)
|
|
|
|
//fmt.Fprintln(stdout, dst.Id)
|
2013-01-22 09:54:56 -08:00
|
|
|
return nil
|
|
|
|
}
|
2013-01-26 15:56:42 -08:00
|
|
|
*/
|
2013-01-22 18:29:44 -08:00
|
|
|
return errors.New("No such container: " + srcName)
|
2013-01-22 09:54:56 -08:00
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdTar(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout,
|
2013-01-22 18:29:44 -08:00
|
|
|
"tar", "CONTAINER",
|
|
|
|
"Stream the contents of a container as a tar archive")
|
2013-01-22 09:54:56 -08:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
name := flags.Arg(0)
|
2013-01-25 14:44:16 -08:00
|
|
|
if _, exists := srv.findContainer(name); exists {
|
2013-01-22 09:54:56 -08:00
|
|
|
// Stream the entire contents of the container (basically a volatile snapshot)
|
2013-01-24 20:01:32 -08:00
|
|
|
return fake.WriteFakeTar(stdout)
|
2013-01-22 09:54:56 -08:00
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
return errors.New("No such container: " + name)
|
2013-01-22 09:54:56 -08:00
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdDiff(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout,
|
2013-01-20 22:21:26 -08:00
|
|
|
"diff", "CONTAINER [OPTIONS]",
|
|
|
|
"Inspect changes on a container's filesystem")
|
2013-01-20 00:43:16 -08:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-20 22:21:26 -08:00
|
|
|
if flags.NArg() < 1 {
|
2013-01-20 00:43:16 -08:00
|
|
|
return errors.New("Not enough arguments")
|
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
if container, exists := srv.findContainer(flags.Arg(0)); !exists {
|
2013-01-20 00:43:16 -08:00
|
|
|
return errors.New("No such container")
|
2013-01-20 22:21:26 -08:00
|
|
|
} else {
|
2013-01-26 15:56:42 -08:00
|
|
|
changes, err := container.Filesystem.Changes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
fmt.Fprintln(stdout, change.String())
|
|
|
|
}
|
2013-01-20 00:43:16 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-26 15:56:42 -08:00
|
|
|
func (srv *Server) CmdReset(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
|
|
|
flags := rcli.Subcmd(stdout,
|
|
|
|
"reset", "CONTAINER [OPTIONS]",
|
|
|
|
"Reset changes to a container's filesystem")
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if flags.NArg() < 1 {
|
|
|
|
return errors.New("Not enough arguments")
|
|
|
|
}
|
|
|
|
for _, name := range flags.Args() {
|
|
|
|
if container, exists := srv.findContainer(name); exists {
|
|
|
|
if err := container.Filesystem.Reset(); err != nil {
|
|
|
|
return errors.New("Reset " + container.Id + ": " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-20 00:43:16 -08:00
|
|
|
|
2013-01-20 14:13:25 -08:00
|
|
|
// ByDate wraps an array of layers so they can be sorted by date (most recent first)
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
type ByDate []*docker.Container
|
2013-01-20 14:13:25 -08:00
|
|
|
|
2013-01-22 18:29:44 -08:00
|
|
|
func (c *ByDate) Len() int {
|
|
|
|
return len(*c)
|
2013-01-20 14:13:25 -08:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:29:44 -08:00
|
|
|
func (c *ByDate) Less(i, j int) bool {
|
|
|
|
containers := *c
|
|
|
|
return containers[j].Created.Before(containers[i].Created)
|
2013-01-20 14:13:25 -08:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:29:44 -08:00
|
|
|
func (c *ByDate) Swap(i, j int) {
|
|
|
|
containers := *c
|
|
|
|
tmp := containers[i]
|
|
|
|
containers[i] = containers[j]
|
|
|
|
containers[j] = tmp
|
2013-01-20 14:13:25 -08:00
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (c *ByDate) Add(container *docker.Container) {
|
2013-01-22 18:29:44 -08:00
|
|
|
*c = append(*c, container)
|
|
|
|
sort.Sort(c)
|
2013-01-20 14:13:25 -08:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:29:44 -08:00
|
|
|
func (c *ByDate) Del(id string) {
|
|
|
|
for idx, container := range *c {
|
|
|
|
if container.Id == id {
|
|
|
|
*c = append((*c)[:idx], (*c)[idx + 1:]...)
|
2013-01-21 18:12:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 14:13:25 -08:00
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) addContainer(id string, layers []string, name string, source string) (*fake.Container, error) {
|
|
|
|
c, err := srv.docker.Create(id, "", nil, layers, &docker.Config{Hostname: id, Ram: 512 * 1024 * 1024})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-01-20 00:43:16 -08:00
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
if err := c.SetUserData("name", name); err != nil {
|
|
|
|
srv.docker.Destroy(c)
|
|
|
|
return nil, err
|
2013-01-22 18:29:44 -08:00
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
if _, exists := srv.containersByName[name]; !exists {
|
|
|
|
srv.containersByName[name] = new(ByDate)
|
2013-01-20 14:13:25 -08:00
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
srv.containersByName[name].Add(c)
|
|
|
|
return fake.NewContainer(c), nil
|
2013-01-20 00:43:16 -08:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:29:44 -08:00
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) rm(id string) (*docker.Container, error) {
|
|
|
|
container := srv.docker.Get(id)
|
|
|
|
if container == nil {
|
|
|
|
return nil, errors.New("No such continer: " + id)
|
2013-01-21 18:12:56 -08:00
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
// Remove from name lookup
|
|
|
|
srv.containersByName[container.GetUserData("name")].Del(container.Id)
|
|
|
|
if err := srv.docker.Destroy(container); err != nil {
|
|
|
|
return container, err
|
|
|
|
}
|
|
|
|
return container, nil
|
2013-01-20 00:43:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdLogs(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout, "logs", "[OPTIONS] CONTAINER", "Fetch the logs of a container")
|
2013-01-21 18:31:12 -08:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if flags.NArg() != 1 {
|
|
|
|
flags.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
name := flags.Arg(0)
|
2013-01-25 14:44:16 -08:00
|
|
|
if container, exists := srv.findContainer(name); exists {
|
2013-01-21 18:31:12 -08:00
|
|
|
if _, err := io.Copy(stdout, container.StdoutLog()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-26 15:56:42 -08:00
|
|
|
if _, err := io.Copy(stdout, container.StderrLog()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
return nil
|
2013-01-21 18:31:12 -08:00
|
|
|
}
|
|
|
|
return errors.New("No such container: " + flags.Arg(0))
|
|
|
|
}
|
|
|
|
|
2013-01-26 15:56:42 -08:00
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout, "run", "[OPTIONS] CONTAINER COMMAND [ARG...]", "Run a command in a container")
|
2013-01-20 22:22:51 -08:00
|
|
|
fl_attach := flags.Bool("a", false, "Attach stdin and stdout")
|
2013-01-26 15:56:42 -08:00
|
|
|
//fl_tty := flags.Bool("t", false, "Allocate a pseudo-tty")
|
2013-01-20 00:43:16 -08:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
if flags.NArg() < 2 {
|
2013-01-20 00:43:16 -08:00
|
|
|
flags.Usage()
|
|
|
|
return nil
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
name, cmd := flags.Arg(0), flags.Args()[1:]
|
2013-01-25 14:44:16 -08:00
|
|
|
if container, exists := srv.findContainer(name); exists {
|
2013-01-26 15:56:42 -08:00
|
|
|
log.Printf("Running container %#v\n", container)
|
|
|
|
container.Path = cmd[0]
|
|
|
|
container.Args = cmd[1:]
|
2013-01-22 18:29:44 -08:00
|
|
|
if *fl_attach {
|
2013-01-26 15:56:42 -08:00
|
|
|
cmd_stdout, err := container.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd_stderr, err := container.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sending_stdout := future.Go(func() error { _, err := io.Copy(stdout, cmd_stdout); return err })
|
|
|
|
sending_stderr := future.Go(func() error { _, err := io.Copy(stdout, cmd_stderr); return err })
|
|
|
|
err_sending_stdout := <-sending_stdout
|
|
|
|
err_sending_stderr := <-sending_stderr
|
|
|
|
if err_sending_stdout != nil {
|
|
|
|
return err_sending_stdout
|
|
|
|
}
|
|
|
|
return err_sending_stderr
|
2013-01-22 18:29:44 -08:00
|
|
|
} else {
|
2013-01-26 15:56:42 -08:00
|
|
|
if output, err := container.Output(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
fmt.Printf("-->|%s|\n", output)
|
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
}
|
2013-01-26 15:56:42 -08:00
|
|
|
return nil
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
2013-01-22 18:29:44 -08:00
|
|
|
return errors.New("No such container: " + name)
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2013-01-25 11:23:18 -08:00
|
|
|
future.Seed()
|
2013-01-19 16:07:19 -08:00
|
|
|
flag.Parse()
|
2013-01-25 14:44:16 -08:00
|
|
|
d, err := New()
|
2013-01-25 11:32:37 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-01-23 23:14:46 -08:00
|
|
|
go func() {
|
2013-01-25 14:44:16 -08:00
|
|
|
if err := rcli.ListenAndServeHTTP(":8080", d); err != nil {
|
2013-01-23 23:14:46 -08:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2013-01-25 14:44:16 -08:00
|
|
|
if err := rcli.ListenAndServeTCP(":4242", d); err != nil {
|
2013-01-19 16:07:19 -08:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func New() (*Server, error) {
|
2013-01-25 11:32:37 -08:00
|
|
|
store, err := future.NewStore("/var/lib/docker/layers")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := store.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-01-25 14:44:16 -08:00
|
|
|
d, err := docker.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv := &Server{
|
2013-01-22 18:29:44 -08:00
|
|
|
containersByName: make(map[string]*ByDate),
|
2013-01-25 11:32:37 -08:00
|
|
|
layers: store,
|
2013-01-25 14:44:16 -08:00
|
|
|
docker: d,
|
|
|
|
}
|
|
|
|
for _, container := range srv.docker.List() {
|
|
|
|
name := container.GetUserData("name")
|
|
|
|
if _, exists := srv.containersByName[name]; !exists {
|
|
|
|
srv.containersByName[name] = new(ByDate)
|
|
|
|
}
|
|
|
|
srv.containersByName[name].Add(container)
|
|
|
|
}
|
|
|
|
log.Printf("Done building index\n")
|
|
|
|
return srv, nil
|
2013-01-20 00:43:16 -08:00
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdMirror(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-23 23:14:46 -08:00
|
|
|
_, err := io.Copy(stdout, stdin)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdDebug(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-23 23:14:46 -08:00
|
|
|
for {
|
|
|
|
if line, err := bufio.NewReader(stdin).ReadString('\n'); err == nil {
|
|
|
|
fmt.Printf("--- %s", line)
|
|
|
|
} else if err == io.EOF {
|
|
|
|
if len(line) > 0 {
|
|
|
|
fmt.Printf("--- %s\n", line)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
return err
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
|
|
|
}
|
2013-01-23 23:14:46 -08:00
|
|
|
return nil
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
func (srv *Server) CmdWeb(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
|
2013-01-24 20:01:32 -08:00
|
|
|
flags := rcli.Subcmd(stdout, "web", "[OPTIONS]", "A web UI for docker")
|
2013-01-20 22:21:59 -08:00
|
|
|
showurl := flags.Bool("u", false, "Return the URL of the web UI")
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if *showurl {
|
|
|
|
fmt.Fprintln(stdout, "http://localhost:4242/web")
|
|
|
|
} else {
|
|
|
|
if file, err := os.Open("dockerweb.html"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if _, err := io.Copy(stdout, file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-20 15:55:00 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-19 16:07:19 -08:00
|
|
|
|
2013-01-25 14:44:16 -08:00
|
|
|
type Server struct {
|
2013-01-22 18:29:44 -08:00
|
|
|
containersByName map[string]*ByDate
|
2013-01-25 11:32:37 -08:00
|
|
|
layers *future.Store
|
2013-01-25 14:44:16 -08:00
|
|
|
docker *docker.Docker
|
2013-01-19 16:07:19 -08:00
|
|
|
}
|
|
|
|
|