utils.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. package docker
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "github.com/dotcloud/docker/rcli"
  9. "index/suffixarray"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "os"
  14. "os/exec"
  15. "path/filepath"
  16. "runtime"
  17. "strings"
  18. "sync"
  19. "time"
  20. )
  21. // Go is a basic promise implementation: it wraps calls a function in a goroutine,
  22. // and returns a channel which will later return the function's return value.
  23. func Go(f func() error) chan error {
  24. ch := make(chan error)
  25. go func() {
  26. ch <- f()
  27. }()
  28. return ch
  29. }
  30. // Request a given URL and return an io.Reader
  31. func Download(url string, stderr io.Writer) (*http.Response, error) {
  32. var resp *http.Response
  33. var err error = nil
  34. if resp, err = http.Get(url); err != nil {
  35. return nil, err
  36. }
  37. if resp.StatusCode >= 400 {
  38. return nil, errors.New("Got HTTP status code >= 400: " + resp.Status)
  39. }
  40. return resp, nil
  41. }
  42. // Debug function, if the debug flag is set, then display. Do nothing otherwise
  43. // If Docker is in damon mode, also send the debug info on the socket
  44. func Debugf(format string, a ...interface{}) {
  45. if os.Getenv("DEBUG") != "" {
  46. // Retrieve the stack infos
  47. _, file, line, ok := runtime.Caller(1)
  48. if !ok {
  49. file = "<unknown>"
  50. line = -1
  51. } else {
  52. file = file[strings.LastIndex(file, "/")+1:]
  53. }
  54. fmt.Fprintf(os.Stderr, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
  55. if rcli.CLIENT_SOCKET != nil {
  56. fmt.Fprintf(rcli.CLIENT_SOCKET, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
  57. }
  58. }
  59. }
  60. // Reader with progress bar
  61. type progressReader struct {
  62. reader io.ReadCloser // Stream to read from
  63. output io.Writer // Where to send progress bar to
  64. readTotal int // Expected stream length (bytes)
  65. readProgress int // How much has been read so far (bytes)
  66. lastUpdate int // How many bytes read at least update
  67. template string // Template to print. Default "%v/%v (%v)"
  68. }
  69. func (r *progressReader) Read(p []byte) (n int, err error) {
  70. read, err := io.ReadCloser(r.reader).Read(p)
  71. r.readProgress += read
  72. updateEvery := 4096
  73. if r.readTotal > 0 {
  74. // Only update progress for every 1% read
  75. if increment := int(0.01 * float64(r.readTotal)); increment > updateEvery {
  76. updateEvery = increment
  77. }
  78. }
  79. if r.readProgress-r.lastUpdate > updateEvery || err != nil {
  80. if r.readTotal > 0 {
  81. fmt.Fprintf(r.output, r.template+"\r", r.readProgress, r.readTotal, fmt.Sprintf("%.0f%%", float64(r.readProgress)/float64(r.readTotal)*100))
  82. } else {
  83. fmt.Fprintf(r.output, r.template+"\r", r.readProgress, "?", "n/a")
  84. }
  85. r.lastUpdate = r.readProgress
  86. }
  87. // Send newline when complete
  88. if err != nil {
  89. fmt.Fprintf(r.output, "\n")
  90. }
  91. return read, err
  92. }
  93. func (r *progressReader) Close() error {
  94. return io.ReadCloser(r.reader).Close()
  95. }
  96. func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string) *progressReader {
  97. if template == "" {
  98. template = "%v/%v (%v)"
  99. }
  100. return &progressReader{r, output, size, 0, 0, template}
  101. }
  102. // HumanDuration returns a human-readable approximation of a duration
  103. // (eg. "About a minute", "4 hours ago", etc.)
  104. func HumanDuration(d time.Duration) string {
  105. if seconds := int(d.Seconds()); seconds < 1 {
  106. return "Less than a second"
  107. } else if seconds < 60 {
  108. return fmt.Sprintf("%d seconds", seconds)
  109. } else if minutes := int(d.Minutes()); minutes == 1 {
  110. return "About a minute"
  111. } else if minutes < 60 {
  112. return fmt.Sprintf("%d minutes", minutes)
  113. } else if hours := int(d.Hours()); hours == 1 {
  114. return "About an hour"
  115. } else if hours < 48 {
  116. return fmt.Sprintf("%d hours", hours)
  117. } else if hours < 24*7*2 {
  118. return fmt.Sprintf("%d days", hours/24)
  119. } else if hours < 24*30*3 {
  120. return fmt.Sprintf("%d weeks", hours/24/7)
  121. } else if hours < 24*365*2 {
  122. return fmt.Sprintf("%d months", hours/24/30)
  123. }
  124. return fmt.Sprintf("%d years", d.Hours()/24/365)
  125. }
  126. func Trunc(s string, maxlen int) string {
  127. if len(s) <= maxlen {
  128. return s
  129. }
  130. return s[:maxlen]
  131. }
  132. // Figure out the absolute path of our own binary
  133. func SelfPath() string {
  134. path, err := exec.LookPath(os.Args[0])
  135. if err != nil {
  136. panic(err)
  137. }
  138. path, err = filepath.Abs(path)
  139. if err != nil {
  140. panic(err)
  141. }
  142. return path
  143. }
  144. type nopWriter struct {
  145. }
  146. func (w *nopWriter) Write(buf []byte) (int, error) {
  147. return len(buf), nil
  148. }
  149. type nopWriteCloser struct {
  150. io.Writer
  151. }
  152. func (w *nopWriteCloser) Close() error { return nil }
  153. func NopWriteCloser(w io.Writer) io.WriteCloser {
  154. return &nopWriteCloser{w}
  155. }
  156. type bufReader struct {
  157. buf *bytes.Buffer
  158. reader io.Reader
  159. err error
  160. l sync.Mutex
  161. wait sync.Cond
  162. }
  163. func newBufReader(r io.Reader) *bufReader {
  164. reader := &bufReader{
  165. buf: &bytes.Buffer{},
  166. reader: r,
  167. }
  168. reader.wait.L = &reader.l
  169. go reader.drain()
  170. return reader
  171. }
  172. func (r *bufReader) drain() {
  173. buf := make([]byte, 1024)
  174. for {
  175. n, err := r.reader.Read(buf)
  176. r.l.Lock()
  177. if err != nil {
  178. r.err = err
  179. } else {
  180. r.buf.Write(buf[0:n])
  181. }
  182. r.wait.Signal()
  183. r.l.Unlock()
  184. if err != nil {
  185. break
  186. }
  187. }
  188. }
  189. func (r *bufReader) Read(p []byte) (n int, err error) {
  190. r.l.Lock()
  191. defer r.l.Unlock()
  192. for {
  193. n, err = r.buf.Read(p)
  194. if n > 0 {
  195. return n, err
  196. }
  197. if r.err != nil {
  198. return 0, r.err
  199. }
  200. r.wait.Wait()
  201. }
  202. panic("unreachable")
  203. }
  204. func (r *bufReader) Close() error {
  205. closer, ok := r.reader.(io.ReadCloser)
  206. if !ok {
  207. return nil
  208. }
  209. return closer.Close()
  210. }
  211. type writeBroadcaster struct {
  212. mu sync.Mutex
  213. writers map[io.WriteCloser]struct{}
  214. }
  215. func (w *writeBroadcaster) AddWriter(writer io.WriteCloser) {
  216. w.mu.Lock()
  217. w.writers[writer] = struct{}{}
  218. w.mu.Unlock()
  219. }
  220. // FIXME: Is that function used?
  221. // FIXME: This relies on the concrete writer type used having equality operator
  222. func (w *writeBroadcaster) RemoveWriter(writer io.WriteCloser) {
  223. w.mu.Lock()
  224. delete(w.writers, writer)
  225. w.mu.Unlock()
  226. }
  227. func (w *writeBroadcaster) Write(p []byte) (n int, err error) {
  228. w.mu.Lock()
  229. defer w.mu.Unlock()
  230. for writer := range w.writers {
  231. if n, err := writer.Write(p); err != nil || n != len(p) {
  232. // On error, evict the writer
  233. delete(w.writers, writer)
  234. }
  235. }
  236. return len(p), nil
  237. }
  238. func (w *writeBroadcaster) CloseWriters() error {
  239. w.mu.Lock()
  240. defer w.mu.Unlock()
  241. for writer := range w.writers {
  242. writer.Close()
  243. }
  244. w.writers = make(map[io.WriteCloser]struct{})
  245. return nil
  246. }
  247. func newWriteBroadcaster() *writeBroadcaster {
  248. return &writeBroadcaster{writers: make(map[io.WriteCloser]struct{})}
  249. }
  250. func getTotalUsedFds() int {
  251. if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
  252. Debugf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
  253. } else {
  254. return len(fds)
  255. }
  256. return -1
  257. }
  258. // TruncIndex allows the retrieval of string identifiers by any of their unique prefixes.
  259. // This is used to retrieve image and container IDs by more convenient shorthand prefixes.
  260. type TruncIndex struct {
  261. index *suffixarray.Index
  262. ids map[string]bool
  263. bytes []byte
  264. }
  265. func NewTruncIndex() *TruncIndex {
  266. return &TruncIndex{
  267. index: suffixarray.New([]byte{' '}),
  268. ids: make(map[string]bool),
  269. bytes: []byte{' '},
  270. }
  271. }
  272. func (idx *TruncIndex) Add(id string) error {
  273. if strings.Contains(id, " ") {
  274. return fmt.Errorf("Illegal character: ' '")
  275. }
  276. if _, exists := idx.ids[id]; exists {
  277. return fmt.Errorf("Id already exists: %s", id)
  278. }
  279. idx.ids[id] = true
  280. idx.bytes = append(idx.bytes, []byte(id+" ")...)
  281. idx.index = suffixarray.New(idx.bytes)
  282. return nil
  283. }
  284. func (idx *TruncIndex) Delete(id string) error {
  285. if _, exists := idx.ids[id]; !exists {
  286. return fmt.Errorf("No such id: %s", id)
  287. }
  288. before, after, err := idx.lookup(id)
  289. if err != nil {
  290. return err
  291. }
  292. delete(idx.ids, id)
  293. idx.bytes = append(idx.bytes[:before], idx.bytes[after:]...)
  294. idx.index = suffixarray.New(idx.bytes)
  295. return nil
  296. }
  297. func (idx *TruncIndex) lookup(s string) (int, int, error) {
  298. offsets := idx.index.Lookup([]byte(" "+s), -1)
  299. //log.Printf("lookup(%s): %v (index bytes: '%s')\n", s, offsets, idx.index.Bytes())
  300. if offsets == nil || len(offsets) == 0 || len(offsets) > 1 {
  301. return -1, -1, fmt.Errorf("No such id: %s", s)
  302. }
  303. offsetBefore := offsets[0] + 1
  304. offsetAfter := offsetBefore + strings.Index(string(idx.bytes[offsetBefore:]), " ")
  305. return offsetBefore, offsetAfter, nil
  306. }
  307. func (idx *TruncIndex) Get(s string) (string, error) {
  308. before, after, err := idx.lookup(s)
  309. //log.Printf("Get(%s) bytes=|%s| before=|%d| after=|%d|\n", s, idx.bytes, before, after)
  310. if err != nil {
  311. return "", err
  312. }
  313. return string(idx.bytes[before:after]), err
  314. }
  315. // TruncateId returns a shorthand version of a string identifier for convenience.
  316. // A collision with other shorthands is very unlikely, but possible.
  317. // In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
  318. // will need to use a langer prefix, or the full-length Id.
  319. func TruncateId(id string) string {
  320. shortLen := 12
  321. if len(id) < shortLen {
  322. shortLen = len(id)
  323. }
  324. return id[:shortLen]
  325. }
  326. // Code c/c from io.Copy() modified to handle escape sequence
  327. func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) {
  328. buf := make([]byte, 32*1024)
  329. for {
  330. nr, er := src.Read(buf)
  331. if nr > 0 {
  332. // ---- Docker addition
  333. // char 16 is C-p
  334. if nr == 1 && buf[0] == 16 {
  335. nr, er = src.Read(buf)
  336. // char 17 is C-q
  337. if nr == 1 && buf[0] == 17 {
  338. if err := src.Close(); err != nil {
  339. return 0, err
  340. }
  341. return 0, io.EOF
  342. }
  343. }
  344. // ---- End of docker
  345. nw, ew := dst.Write(buf[0:nr])
  346. if nw > 0 {
  347. written += int64(nw)
  348. }
  349. if ew != nil {
  350. err = ew
  351. break
  352. }
  353. if nr != nw {
  354. err = io.ErrShortWrite
  355. break
  356. }
  357. }
  358. if er == io.EOF {
  359. break
  360. }
  361. if er != nil {
  362. err = er
  363. break
  364. }
  365. }
  366. return written, err
  367. }
  368. func HashData(src io.Reader) (string, error) {
  369. h := sha256.New()
  370. if _, err := io.Copy(h, src); err != nil {
  371. return "", err
  372. }
  373. return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
  374. }
  375. type KernelVersionInfo struct {
  376. Kernel int
  377. Major int
  378. Minor int
  379. Flavor string
  380. }
  381. // FIXME: this doens't build on Darwin
  382. func GetKernelVersion() (*KernelVersionInfo, error) {
  383. return getKernelVersion()
  384. }
  385. func (k *KernelVersionInfo) String() string {
  386. return fmt.Sprintf("%d.%d.%d-%s", k.Kernel, k.Major, k.Minor, k.Flavor)
  387. }
  388. // Compare two KernelVersionInfo struct.
  389. // Returns -1 if a < b, = if a == b, 1 it a > b
  390. func CompareKernelVersion(a, b *KernelVersionInfo) int {
  391. if a.Kernel < b.Kernel {
  392. return -1
  393. } else if a.Kernel > b.Kernel {
  394. return 1
  395. }
  396. if a.Major < b.Major {
  397. return -1
  398. } else if a.Major > b.Major {
  399. return 1
  400. }
  401. if a.Minor < b.Minor {
  402. return -1
  403. } else if a.Minor > b.Minor {
  404. return 1
  405. }
  406. return 0
  407. }
  408. func FindCgroupMountpoint(cgroupType string) (string, error) {
  409. output, err := ioutil.ReadFile("/proc/mounts")
  410. if err != nil {
  411. return "", err
  412. }
  413. // /proc/mounts has 6 fields per line, one mount per line, e.g.
  414. // cgroup /sys/fs/cgroup/devices cgroup rw,relatime,devices 0 0
  415. for _, line := range strings.Split(string(output), "\n") {
  416. parts := strings.Split(line, " ")
  417. if len(parts) == 6 && parts[2] == "cgroup" {
  418. for _, opt := range strings.Split(parts[3], ",") {
  419. if opt == cgroupType {
  420. return parts[1], nil
  421. }
  422. }
  423. }
  424. }
  425. return "", fmt.Errorf("cgroup mountpoint not found for %s", cgroupType)
  426. }
  427. // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
  428. // If OpenStdin is set, then it differs
  429. func CompareConfig(a, b *Config) bool {
  430. if a == nil || b == nil ||
  431. a.OpenStdin || b.OpenStdin {
  432. return false
  433. }
  434. if a.AttachStdout != b.AttachStdout ||
  435. a.AttachStderr != b.AttachStderr ||
  436. a.User != b.User ||
  437. a.Memory != b.Memory ||
  438. a.MemorySwap != b.MemorySwap ||
  439. a.OpenStdin != b.OpenStdin ||
  440. a.Tty != b.Tty {
  441. return false
  442. }
  443. if len(a.Cmd) != len(b.Cmd) ||
  444. len(a.Dns) != len(b.Dns) ||
  445. len(a.Env) != len(b.Env) ||
  446. len(a.PortSpecs) != len(b.PortSpecs) {
  447. return false
  448. }
  449. for i := 0; i < len(a.Cmd); i++ {
  450. if a.Cmd[i] != b.Cmd[i] {
  451. return false
  452. }
  453. }
  454. for i := 0; i < len(a.Dns); i++ {
  455. if a.Dns[i] != b.Dns[i] {
  456. return false
  457. }
  458. }
  459. for i := 0; i < len(a.Env); i++ {
  460. if a.Env[i] != b.Env[i] {
  461. return false
  462. }
  463. }
  464. for i := 0; i < len(a.PortSpecs); i++ {
  465. if a.PortSpecs[i] != b.PortSpecs[i] {
  466. return false
  467. }
  468. }
  469. return true
  470. }