2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"container/list"
|
2013-03-21 03:54:54 -04:00
|
|
|
"errors"
|
2013-03-21 03:52:43 -04:00
|
|
|
"fmt"
|
2013-03-22 14:44:12 -04:00
|
|
|
"github.com/dotcloud/docker/rcli"
|
2013-01-18 19:13:39 -05:00
|
|
|
"io"
|
2013-03-21 03:54:54 -04:00
|
|
|
"net/http"
|
2013-02-13 16:58:28 -05:00
|
|
|
"os"
|
2013-01-27 03:59:49 -05:00
|
|
|
"os/exec"
|
2013-02-13 16:58:28 -05:00
|
|
|
"path/filepath"
|
2013-03-25 04:29:01 -04:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2013-01-28 17:30:05 -05:00
|
|
|
"sync"
|
2013-03-21 03:52:43 -04:00
|
|
|
"time"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2013-03-21 04:13:55 -04:00
|
|
|
// Go is a basic promise implementation: it wraps calls a function in a goroutine,
|
|
|
|
// and returns a channel which will later return the function's return value.
|
|
|
|
func Go(f func() error) chan error {
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
ch <- f()
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2013-03-21 03:54:54 -04:00
|
|
|
// Request a given URL and return an io.Reader
|
|
|
|
func Download(url string, stderr io.Writer) (*http.Response, error) {
|
|
|
|
var resp *http.Response
|
|
|
|
var err error = nil
|
|
|
|
if resp, err = http.Get(url); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, errors.New("Got HTTP status code >= 400: " + resp.Status)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 14:44:12 -04:00
|
|
|
// Debug function, if the debug flag is set, then display. Do nothing otherwise
|
|
|
|
// If Docker is in damon mode, also send the debug info on the socket
|
|
|
|
func Debugf(format string, a ...interface{}) {
|
|
|
|
if rcli.DEBUG_FLAG {
|
2013-03-25 04:29:01 -04:00
|
|
|
|
|
|
|
// Retrieve the stack infos
|
|
|
|
_, file, line, ok := runtime.Caller(1)
|
|
|
|
if !ok {
|
|
|
|
file = "<unknown>"
|
|
|
|
line = -1
|
|
|
|
} else {
|
|
|
|
file = file[strings.LastIndex(file, "/")+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
|
2013-03-22 14:44:12 -04:00
|
|
|
if rcli.CLIENT_SOCKET != nil {
|
2013-03-25 04:29:01 -04:00
|
|
|
fmt.Fprintf(rcli.CLIENT_SOCKET, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
|
2013-03-22 14:44:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 03:54:54 -04:00
|
|
|
// Reader with progress bar
|
|
|
|
type progressReader struct {
|
2013-03-28 20:12:23 -04:00
|
|
|
reader io.ReadCloser // Stream to read from
|
|
|
|
output io.Writer // Where to send progress bar to
|
|
|
|
readTotal int // Expected stream length (bytes)
|
|
|
|
readProgress int // How much has been read so far (bytes)
|
|
|
|
lastUpdate int // How many bytes read at least update
|
2013-03-21 03:54:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *progressReader) Read(p []byte) (n int, err error) {
|
|
|
|
read, err := io.ReadCloser(r.reader).Read(p)
|
2013-03-28 20:12:23 -04:00
|
|
|
r.readProgress += read
|
2013-03-21 03:54:54 -04:00
|
|
|
|
|
|
|
// Only update progress for every 1% read
|
2013-03-28 20:12:23 -04:00
|
|
|
updateEvery := int(0.01 * float64(r.readTotal))
|
|
|
|
if r.readProgress-r.lastUpdate > updateEvery || r.readProgress == r.readTotal {
|
2013-03-21 03:54:54 -04:00
|
|
|
fmt.Fprintf(r.output, "%d/%d (%.0f%%)\r",
|
2013-03-28 20:12:23 -04:00
|
|
|
r.readProgress,
|
|
|
|
r.readTotal,
|
|
|
|
float64(r.readProgress)/float64(r.readTotal)*100)
|
|
|
|
r.lastUpdate = r.readProgress
|
2013-03-21 03:54:54 -04:00
|
|
|
}
|
|
|
|
// Send newline when complete
|
|
|
|
if err == io.EOF {
|
|
|
|
fmt.Fprintf(r.output, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2013-03-21 03:52:43 -04:00
|
|
|
// HumanDuration returns a human-readable approximation of a duration
|
|
|
|
// (eg. "About a minute", "4 hours ago", etc.)
|
|
|
|
func HumanDuration(d time.Duration) string {
|
|
|
|
if seconds := int(d.Seconds()); seconds < 1 {
|
|
|
|
return "Less than a second"
|
|
|
|
} else if seconds < 60 {
|
|
|
|
return fmt.Sprintf("%d seconds", seconds)
|
|
|
|
} else if minutes := int(d.Minutes()); minutes == 1 {
|
|
|
|
return "About a minute"
|
|
|
|
} else if minutes < 60 {
|
|
|
|
return fmt.Sprintf("%d minutes", minutes)
|
|
|
|
} else if hours := int(d.Hours()); hours == 1 {
|
|
|
|
return "About an hour"
|
|
|
|
} else if hours < 48 {
|
|
|
|
return fmt.Sprintf("%d hours", hours)
|
|
|
|
} else if hours < 24*7*2 {
|
|
|
|
return fmt.Sprintf("%d days", hours/24)
|
|
|
|
} else if hours < 24*30*3 {
|
|
|
|
return fmt.Sprintf("%d weeks", hours/24/7)
|
|
|
|
} else if hours < 24*365*2 {
|
|
|
|
return fmt.Sprintf("%d months", hours/24/30)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d years", d.Hours()/24/365)
|
|
|
|
}
|
|
|
|
|
2013-01-29 06:18:07 -05:00
|
|
|
func Trunc(s string, maxlen int) string {
|
|
|
|
if len(s) <= maxlen {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[:maxlen]
|
|
|
|
}
|
|
|
|
|
2013-02-13 16:58:28 -05:00
|
|
|
// Figure out the absolute path of our own binary
|
|
|
|
func SelfPath() string {
|
|
|
|
path, err := exec.LookPath(os.Args[0])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
path, err = filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2013-01-26 18:56:42 -05:00
|
|
|
type nopWriteCloser struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *nopWriteCloser) Close() error { return nil }
|
|
|
|
|
|
|
|
func NopWriteCloser(w io.Writer) io.WriteCloser {
|
|
|
|
return &nopWriteCloser{w}
|
|
|
|
}
|
|
|
|
|
2013-01-18 19:13:39 -05:00
|
|
|
type bufReader struct {
|
|
|
|
buf *bytes.Buffer
|
|
|
|
reader io.Reader
|
|
|
|
err error
|
|
|
|
l sync.Mutex
|
|
|
|
wait sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBufReader(r io.Reader) *bufReader {
|
|
|
|
reader := &bufReader{
|
|
|
|
buf: &bytes.Buffer{},
|
|
|
|
reader: r,
|
|
|
|
}
|
|
|
|
reader.wait.L = &reader.l
|
|
|
|
go reader.drain()
|
|
|
|
return reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bufReader) drain() {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n, err := r.reader.Read(buf)
|
2013-03-29 06:00:50 -04:00
|
|
|
r.l.Lock()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
} else {
|
|
|
|
r.buf.Write(buf[0:n])
|
|
|
|
}
|
|
|
|
r.wait.Signal()
|
|
|
|
r.l.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bufReader) Read(p []byte) (n int, err error) {
|
2013-03-29 06:00:50 -04:00
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
2013-01-18 19:13:39 -05:00
|
|
|
for {
|
|
|
|
n, err = r.buf.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
|
|
|
}
|
|
|
|
r.wait.Wait()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bufReader) Close() error {
|
|
|
|
closer, ok := r.reader.(io.ReadCloser)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeBroadcaster struct {
|
|
|
|
writers *list.List
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writeBroadcaster) AddWriter(writer io.WriteCloser) {
|
|
|
|
w.writers.PushBack(writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writeBroadcaster) RemoveWriter(writer io.WriteCloser) {
|
|
|
|
for e := w.writers.Front(); e != nil; e = e.Next() {
|
|
|
|
v := e.Value.(io.Writer)
|
|
|
|
if v == writer {
|
|
|
|
w.writers.Remove(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writeBroadcaster) Write(p []byte) (n int, err error) {
|
|
|
|
failed := []*list.Element{}
|
|
|
|
for e := w.writers.Front(); e != nil; e = e.Next() {
|
|
|
|
writer := e.Value.(io.Writer)
|
|
|
|
if n, err := writer.Write(p); err != nil || n != len(p) {
|
|
|
|
// On error, evict the writer
|
|
|
|
failed = append(failed, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We cannot remove while iterating, so it has to be done in
|
|
|
|
// a separate step
|
|
|
|
for _, e := range failed {
|
|
|
|
w.writers.Remove(e)
|
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writeBroadcaster) Close() error {
|
|
|
|
for e := w.writers.Front(); e != nil; e = e.Next() {
|
|
|
|
writer := e.Value.(io.WriteCloser)
|
|
|
|
writer.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWriteBroadcaster() *writeBroadcaster {
|
|
|
|
return &writeBroadcaster{list.New()}
|
|
|
|
}
|