1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge branch 'pull-missing' of ssh://github.com/chooper/docker

This commit is contained in:
Solomon Hykes 2013-03-13 13:50:47 -07:00
commit 8f81feb1f0
2 changed files with 73 additions and 21 deletions

View file

@ -11,8 +11,9 @@ import (
"github.com/dotcloud/docker/future" "github.com/dotcloud/docker/future"
"github.com/dotcloud/docker/rcli" "github.com/dotcloud/docker/rcli"
"io" "io"
"net/http" "io/ioutil"
"net/url" "net/url"
"net/http"
"os" "os"
"path" "path"
"strconv" "strconv"
@ -425,10 +426,12 @@ func (srv *Server) CmdKill(stdin io.ReadCloser, stdout io.Writer, args ...string
func (srv *Server) CmdImport(stdin io.ReadCloser, stdout io.Writer, args ...string) error { func (srv *Server) CmdImport(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
cmd := rcli.Subcmd(stdout, "import", "[OPTIONS] NAME", "Create a new filesystem image from the contents of a tarball") cmd := rcli.Subcmd(stdout, "import", "[OPTIONS] NAME", "Create a new filesystem image from the contents of a tarball")
fl_stdin := cmd.Bool("stdin", false, "Read tarball from stdin") fl_stdin := cmd.Bool("stdin", false, "Read tarball from stdin")
var archive io.Reader
var resp *http.Response
if err := cmd.Parse(args); err != nil { if err := cmd.Parse(args); err != nil {
return nil return nil
} }
var archive io.Reader
name := cmd.Arg(0) name := cmd.Arg(0)
if name == "" { if name == "" {
return errors.New("Not enough arguments") return errors.New("Not enough arguments")
@ -451,14 +454,11 @@ func (srv *Server) CmdImport(stdin io.ReadCloser, stdout io.Writer, args ...stri
fmt.Fprintf(stdout, "Downloading from %s\n", u.String()) fmt.Fprintf(stdout, "Downloading from %s\n", u.String())
// Download with curl (pretty progress bar) // Download with curl (pretty progress bar)
// If curl is not available, fallback to http.Get() // If curl is not available, fallback to http.Get()
archive, err = future.Curl(u.String(), stdout) resp, err = future.Download(u.String(), stdout)
if err != nil { if err != nil {
if resp, err := http.Get(u.String()); err != nil { return err
return err
} else {
archive = resp.Body
}
} }
archive = future.ProgressReader(resp.Body, int(resp.ContentLength), stdout)
} }
fmt.Fprintf(stdout, "Unpacking to %s\n", name) fmt.Fprintf(stdout, "Unpacking to %s\n", name)
img, err := srv.images.Create(archive, nil, name, "") img, err := srv.images.Create(archive, nil, name, "")
@ -838,12 +838,16 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
fl_comment := cmd.String("c", "", "Comment") fl_comment := cmd.String("c", "", "Comment")
fl_memory := cmd.Int64("m", 0, "Memory limit (in bytes)") fl_memory := cmd.Int64("m", 0, "Memory limit (in bytes)")
var fl_ports ports var fl_ports ports
cmd.Var(&fl_ports, "p", "Map a network port to the container") cmd.Var(&fl_ports, "p", "Map a network port to the container")
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 img_name string
//var img_version string // Only here for reference
var cmdline []string var cmdline []string
if len(cmd.Args()) >= 2 { if len(cmd.Args()) >= 2 {
cmdline = cmd.Args()[1:] cmdline = cmd.Args()[1:]
} }
@ -851,6 +855,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
if name == "" { if name == "" {
name = "base" name = "base"
} }
// Choose a default command if needed // Choose a default command if needed
if len(cmdline) == 0 { if len(cmdline) == 0 {
*fl_stdin = true *fl_stdin = true
@ -858,13 +863,31 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
*fl_attach = true *fl_attach = true
cmdline = []string{"/bin/bash", "-i"} cmdline = []string{"/bin/bash", "-i"}
} }
// Find the image // Find the image
img, err := srv.images.Find(name) img, err := srv.images.Find(name)
if err != nil { if err != nil {
return err return err
} else if img == nil { } else if img == nil {
return errors.New("No such image: " + name) // Separate the name:version tag
if strings.Contains(name, ":") {
parts := strings.SplitN(name, ":", 2)
img_name = parts[0]
//img_version = parts[1] // Only here for reference
} else {
img_name = name
}
stdin_noclose := ioutil.NopCloser(stdin)
if err := srv.CmdImport(stdin_noclose, stdout, img_name); err != nil {
return err
}
img, err = srv.images.Find(name)
if err != nil || img == nil {
return errors.New("Could not find image after downloading: " + name)
}
} }
// Create new container // Create new container
container, err := srv.CreateContainer(img, fl_ports, *fl_user, *fl_tty, container, err := srv.CreateContainer(img, fl_ports, *fl_user, *fl_tty,
*fl_stdin, *fl_memory, *fl_comment, cmdline[0], cmdline[1:]...) *fl_stdin, *fl_memory, *fl_comment, cmdline[0], cmdline[1:]...)

View file

@ -3,10 +3,11 @@ package future
import ( import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"errors"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
"os/exec" "net/http"
"time" "time"
) )
@ -85,18 +86,46 @@ func Pv(src io.Reader, info io.Writer) io.Reader {
return r return r
} }
// Curl makes an http request by executing the unix command 'curl', and returns // Request a given URL and return an io.Reader
// the body of the response. If `stderr` is not nil, a progress bar will be func Download(url string, stderr io.Writer) (*http.Response, error) {
// written to it. var resp *http.Response
func Curl(url string, stderr io.Writer) (io.Reader, error) { var err error = nil
curl := exec.Command("curl", "-#", "-L", url) if resp, err = http.Get(url); err != nil {
output, err := curl.StdoutPipe()
if err != nil {
return nil, err return nil, err
} }
curl.Stderr = stderr if resp.StatusCode >= 400 {
if err := curl.Start(); err != nil { return nil, errors.New("Got HTTP status code >= 400: " + resp.Status)
return nil, err
} }
return output, nil return resp, nil
}
// Reader with progress bar
type progressReader struct {
reader io.ReadCloser // Stream to read from
output io.Writer // Where to send progress bar to
read_total int // Expected stream length (bytes)
read_progress int // How much has been read so far (bytes)
last_update int // How many bytes read at least update
}
func (r *progressReader) Read(p []byte) (n int, err error) {
read, err := io.ReadCloser(r.reader).Read(p)
r.read_progress += read
// Only update progress for every 1% read
update_every := int(0.01 * float64(r.read_total))
if r.read_progress - r.last_update > update_every {
fmt.Fprintf(r.output, "%d/%d (%.0f%%)\r",
r.read_progress,
r.read_total,
float64(r.read_progress) / float64(r.read_total) * 100)
r.last_update = r.read_progress
}
return read, err
}
func (r *progressReader) Close() error {
return io.ReadCloser(r.reader).Close()
}
func ProgressReader(r io.ReadCloser, size int, output io.Writer) *progressReader {
return &progressReader{r, output, size, 0, 0}
} }