utils.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "index/suffixarray"
  10. "io"
  11. "io/ioutil"
  12. "log"
  13. "net/http"
  14. "os"
  15. "os/exec"
  16. "path/filepath"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "time"
  22. )
  23. // Go is a basic promise implementation: it wraps calls a function in a goroutine,
  24. // and returns a channel which will later return the function's return value.
  25. func Go(f func() error) chan error {
  26. ch := make(chan error)
  27. go func() {
  28. ch <- f()
  29. }()
  30. return ch
  31. }
  32. // Request a given URL and return an io.Reader
  33. func Download(url string, stderr io.Writer) (*http.Response, error) {
  34. var resp *http.Response
  35. var err error
  36. if resp, err = http.Get(url); err != nil {
  37. return nil, err
  38. }
  39. if resp.StatusCode >= 400 {
  40. return nil, errors.New("Got HTTP status code >= 400: " + resp.Status)
  41. }
  42. return resp, nil
  43. }
  44. func logf(level string, format string, a ...interface{}) {
  45. // Retrieve the stack infos
  46. _, file, line, ok := runtime.Caller(2)
  47. if !ok {
  48. file = "<unknown>"
  49. line = -1
  50. } else {
  51. file = file[strings.LastIndex(file, "/")+1:]
  52. }
  53. fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] %s:%d %s\n", level, file, line, format), a...)
  54. }
  55. // Debug function, if the debug flag is set, then display. Do nothing otherwise
  56. // If Docker is in damon mode, also send the debug info on the socket
  57. func Debugf(format string, a ...interface{}) {
  58. if os.Getenv("DEBUG") != "" {
  59. logf("debug", format, a...)
  60. }
  61. }
  62. func Errorf(format string, a ...interface{}) {
  63. logf("error", format, a...)
  64. }
  65. // Reader with progress bar
  66. type progressReader struct {
  67. reader io.ReadCloser // Stream to read from
  68. output io.Writer // Where to send progress bar to
  69. readTotal int // Expected stream length (bytes)
  70. readProgress int // How much has been read so far (bytes)
  71. lastUpdate int // How many bytes read at least update
  72. template string // Template to print. Default "%v/%v (%v)"
  73. sf *StreamFormatter
  74. newLine bool
  75. }
  76. func (r *progressReader) Read(p []byte) (n int, err error) {
  77. read, err := io.ReadCloser(r.reader).Read(p)
  78. r.readProgress += read
  79. updateEvery := 1024 * 512 //512kB
  80. if r.readTotal > 0 {
  81. // Update progress for every 1% read if 1% < 512kB
  82. if increment := int(0.01 * float64(r.readTotal)); increment < updateEvery {
  83. updateEvery = increment
  84. }
  85. }
  86. if r.readProgress-r.lastUpdate > updateEvery || err != nil {
  87. if r.readTotal > 0 {
  88. fmt.Fprintf(r.output, r.template, HumanSize(int64(r.readProgress)), HumanSize(int64(r.readTotal)), fmt.Sprintf("%.0f%%", float64(r.readProgress)/float64(r.readTotal)*100))
  89. } else {
  90. fmt.Fprintf(r.output, r.template, r.readProgress, "?", "n/a")
  91. }
  92. r.lastUpdate = r.readProgress
  93. }
  94. // Send newline when complete
  95. if r.newLine && err != nil {
  96. r.output.Write(r.sf.FormatStatus("", ""))
  97. }
  98. return read, err
  99. }
  100. func (r *progressReader) Close() error {
  101. return io.ReadCloser(r.reader).Close()
  102. }
  103. func ProgressReader(r io.ReadCloser, size int, output io.Writer, tpl []byte, sf *StreamFormatter, newline bool) *progressReader {
  104. return &progressReader{
  105. reader: r,
  106. output: NewWriteFlusher(output),
  107. readTotal: size,
  108. template: string(tpl),
  109. sf: sf,
  110. newLine: newline,
  111. }
  112. }
  113. // HumanDuration returns a human-readable approximation of a duration
  114. // (eg. "About a minute", "4 hours ago", etc.)
  115. func HumanDuration(d time.Duration) string {
  116. if seconds := int(d.Seconds()); seconds < 1 {
  117. return "Less than a second"
  118. } else if seconds < 60 {
  119. return fmt.Sprintf("%d seconds", seconds)
  120. } else if minutes := int(d.Minutes()); minutes == 1 {
  121. return "About a minute"
  122. } else if minutes < 60 {
  123. return fmt.Sprintf("%d minutes", minutes)
  124. } else if hours := int(d.Hours()); hours == 1 {
  125. return "About an hour"
  126. } else if hours < 48 {
  127. return fmt.Sprintf("%d hours", hours)
  128. } else if hours < 24*7*2 {
  129. return fmt.Sprintf("%d days", hours/24)
  130. } else if hours < 24*30*3 {
  131. return fmt.Sprintf("%d weeks", hours/24/7)
  132. } else if hours < 24*365*2 {
  133. return fmt.Sprintf("%d months", hours/24/30)
  134. }
  135. return fmt.Sprintf("%f years", d.Hours()/24/365)
  136. }
  137. // HumanSize returns a human-readable approximation of a size
  138. // using SI standard (eg. "44kB", "17MB")
  139. func HumanSize(size int64) string {
  140. i := 0
  141. var sizef float64
  142. sizef = float64(size)
  143. units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
  144. for sizef >= 1000.0 {
  145. sizef = sizef / 1000.0
  146. i++
  147. }
  148. return fmt.Sprintf("%.4g %s", sizef, units[i])
  149. }
  150. func Trunc(s string, maxlen int) string {
  151. if len(s) <= maxlen {
  152. return s
  153. }
  154. return s[:maxlen]
  155. }
  156. // Figure out the absolute path of our own binary
  157. func SelfPath() string {
  158. path, err := exec.LookPath(os.Args[0])
  159. if err != nil {
  160. panic(err)
  161. }
  162. path, err = filepath.Abs(path)
  163. if err != nil {
  164. panic(err)
  165. }
  166. return path
  167. }
  168. type NopWriter struct{}
  169. func (*NopWriter) Write(buf []byte) (int, error) {
  170. return len(buf), nil
  171. }
  172. type nopWriteCloser struct {
  173. io.Writer
  174. }
  175. func (w *nopWriteCloser) Close() error { return nil }
  176. func NopWriteCloser(w io.Writer) io.WriteCloser {
  177. return &nopWriteCloser{w}
  178. }
  179. type bufReader struct {
  180. sync.Mutex
  181. buf *bytes.Buffer
  182. reader io.Reader
  183. err error
  184. wait sync.Cond
  185. }
  186. func NewBufReader(r io.Reader) *bufReader {
  187. reader := &bufReader{
  188. buf: &bytes.Buffer{},
  189. reader: r,
  190. }
  191. reader.wait.L = &reader.Mutex
  192. go reader.drain()
  193. return reader
  194. }
  195. func (r *bufReader) drain() {
  196. buf := make([]byte, 1024)
  197. for {
  198. n, err := r.reader.Read(buf)
  199. r.Lock()
  200. if err != nil {
  201. r.err = err
  202. } else {
  203. r.buf.Write(buf[0:n])
  204. }
  205. r.wait.Signal()
  206. r.Unlock()
  207. if err != nil {
  208. break
  209. }
  210. }
  211. }
  212. func (r *bufReader) Read(p []byte) (n int, err error) {
  213. r.Lock()
  214. defer r.Unlock()
  215. for {
  216. n, err = r.buf.Read(p)
  217. if n > 0 {
  218. return n, err
  219. }
  220. if r.err != nil {
  221. return 0, r.err
  222. }
  223. r.wait.Wait()
  224. }
  225. }
  226. func (r *bufReader) Close() error {
  227. closer, ok := r.reader.(io.ReadCloser)
  228. if !ok {
  229. return nil
  230. }
  231. return closer.Close()
  232. }
  233. type WriteBroadcaster struct {
  234. sync.Mutex
  235. buf *bytes.Buffer
  236. writers map[StreamWriter]bool
  237. }
  238. type StreamWriter struct {
  239. wc io.WriteCloser
  240. stream string
  241. }
  242. func (w *WriteBroadcaster) AddWriter(writer io.WriteCloser, stream string) {
  243. w.Lock()
  244. sw := StreamWriter{wc: writer, stream: stream}
  245. w.writers[sw] = true
  246. w.Unlock()
  247. }
  248. type JSONLog struct {
  249. Log string `json:"log,omitempty"`
  250. Stream string `json:"stream,omitempty"`
  251. Created time.Time `json:"time"`
  252. }
  253. func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
  254. w.Lock()
  255. defer w.Unlock()
  256. w.buf.Write(p)
  257. for sw := range w.writers {
  258. lp := p
  259. if sw.stream != "" {
  260. lp = nil
  261. for {
  262. line, err := w.buf.ReadString('\n')
  263. if err != nil {
  264. w.buf.Write([]byte(line))
  265. break
  266. }
  267. b, err := json.Marshal(&JSONLog{Log: line, Stream: sw.stream, Created: time.Now()})
  268. if err != nil {
  269. // On error, evict the writer
  270. delete(w.writers, sw)
  271. continue
  272. }
  273. lp = append(lp, b...)
  274. lp = append(lp, '\n')
  275. }
  276. }
  277. if n, err := sw.wc.Write(lp); err != nil || n != len(lp) {
  278. // On error, evict the writer
  279. delete(w.writers, sw)
  280. }
  281. }
  282. return len(p), nil
  283. }
  284. func (w *WriteBroadcaster) CloseWriters() error {
  285. w.Lock()
  286. defer w.Unlock()
  287. for sw := range w.writers {
  288. sw.wc.Close()
  289. }
  290. w.writers = make(map[StreamWriter]bool)
  291. return nil
  292. }
  293. func NewWriteBroadcaster() *WriteBroadcaster {
  294. return &WriteBroadcaster{writers: make(map[StreamWriter]bool), buf: bytes.NewBuffer(nil)}
  295. }
  296. func GetTotalUsedFds() int {
  297. if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
  298. Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
  299. } else {
  300. return len(fds)
  301. }
  302. return -1
  303. }
  304. // TruncIndex allows the retrieval of string identifiers by any of their unique prefixes.
  305. // This is used to retrieve image and container IDs by more convenient shorthand prefixes.
  306. type TruncIndex struct {
  307. index *suffixarray.Index
  308. ids map[string]bool
  309. bytes []byte
  310. }
  311. func NewTruncIndex() *TruncIndex {
  312. return &TruncIndex{
  313. index: suffixarray.New([]byte{' '}),
  314. ids: make(map[string]bool),
  315. bytes: []byte{' '},
  316. }
  317. }
  318. func (idx *TruncIndex) Add(id string) error {
  319. if strings.Contains(id, " ") {
  320. return fmt.Errorf("Illegal character: ' '")
  321. }
  322. if _, exists := idx.ids[id]; exists {
  323. return fmt.Errorf("Id already exists: %s", id)
  324. }
  325. idx.ids[id] = true
  326. idx.bytes = append(idx.bytes, []byte(id+" ")...)
  327. idx.index = suffixarray.New(idx.bytes)
  328. return nil
  329. }
  330. func (idx *TruncIndex) Delete(id string) error {
  331. if _, exists := idx.ids[id]; !exists {
  332. return fmt.Errorf("No such id: %s", id)
  333. }
  334. before, after, err := idx.lookup(id)
  335. if err != nil {
  336. return err
  337. }
  338. delete(idx.ids, id)
  339. idx.bytes = append(idx.bytes[:before], idx.bytes[after:]...)
  340. idx.index = suffixarray.New(idx.bytes)
  341. return nil
  342. }
  343. func (idx *TruncIndex) lookup(s string) (int, int, error) {
  344. offsets := idx.index.Lookup([]byte(" "+s), -1)
  345. //log.Printf("lookup(%s): %v (index bytes: '%s')\n", s, offsets, idx.index.Bytes())
  346. if offsets == nil || len(offsets) == 0 || len(offsets) > 1 {
  347. return -1, -1, fmt.Errorf("No such id: %s", s)
  348. }
  349. offsetBefore := offsets[0] + 1
  350. offsetAfter := offsetBefore + strings.Index(string(idx.bytes[offsetBefore:]), " ")
  351. return offsetBefore, offsetAfter, nil
  352. }
  353. func (idx *TruncIndex) Get(s string) (string, error) {
  354. before, after, err := idx.lookup(s)
  355. //log.Printf("Get(%s) bytes=|%s| before=|%d| after=|%d|\n", s, idx.bytes, before, after)
  356. if err != nil {
  357. return "", err
  358. }
  359. return string(idx.bytes[before:after]), err
  360. }
  361. // TruncateID returns a shorthand version of a string identifier for convenience.
  362. // A collision with other shorthands is very unlikely, but possible.
  363. // In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
  364. // will need to use a langer prefix, or the full-length Id.
  365. func TruncateID(id string) string {
  366. shortLen := 12
  367. if len(id) < shortLen {
  368. shortLen = len(id)
  369. }
  370. return id[:shortLen]
  371. }
  372. // Code c/c from io.Copy() modified to handle escape sequence
  373. func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) {
  374. buf := make([]byte, 32*1024)
  375. for {
  376. nr, er := src.Read(buf)
  377. if nr > 0 {
  378. // ---- Docker addition
  379. // char 16 is C-p
  380. if nr == 1 && buf[0] == 16 {
  381. nr, er = src.Read(buf)
  382. // char 17 is C-q
  383. if nr == 1 && buf[0] == 17 {
  384. if err := src.Close(); err != nil {
  385. return 0, err
  386. }
  387. return 0, io.EOF
  388. }
  389. }
  390. // ---- End of docker
  391. nw, ew := dst.Write(buf[0:nr])
  392. if nw > 0 {
  393. written += int64(nw)
  394. }
  395. if ew != nil {
  396. err = ew
  397. break
  398. }
  399. if nr != nw {
  400. err = io.ErrShortWrite
  401. break
  402. }
  403. }
  404. if er == io.EOF {
  405. break
  406. }
  407. if er != nil {
  408. err = er
  409. break
  410. }
  411. }
  412. return written, err
  413. }
  414. func HashData(src io.Reader) (string, error) {
  415. h := sha256.New()
  416. if _, err := io.Copy(h, src); err != nil {
  417. return "", err
  418. }
  419. return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
  420. }
  421. type KernelVersionInfo struct {
  422. Kernel int
  423. Major int
  424. Minor int
  425. Flavor string
  426. }
  427. func (k *KernelVersionInfo) String() string {
  428. flavor := ""
  429. if len(k.Flavor) > 0 {
  430. flavor = fmt.Sprintf("-%s", k.Flavor)
  431. }
  432. return fmt.Sprintf("%d.%d.%d%s", k.Kernel, k.Major, k.Minor, flavor)
  433. }
  434. // Compare two KernelVersionInfo struct.
  435. // Returns -1 if a < b, = if a == b, 1 it a > b
  436. func CompareKernelVersion(a, b *KernelVersionInfo) int {
  437. if a.Kernel < b.Kernel {
  438. return -1
  439. } else if a.Kernel > b.Kernel {
  440. return 1
  441. }
  442. if a.Major < b.Major {
  443. return -1
  444. } else if a.Major > b.Major {
  445. return 1
  446. }
  447. if a.Minor < b.Minor {
  448. return -1
  449. } else if a.Minor > b.Minor {
  450. return 1
  451. }
  452. return 0
  453. }
  454. func FindCgroupMountpoint(cgroupType string) (string, error) {
  455. output, err := ioutil.ReadFile("/proc/mounts")
  456. if err != nil {
  457. return "", err
  458. }
  459. // /proc/mounts has 6 fields per line, one mount per line, e.g.
  460. // cgroup /sys/fs/cgroup/devices cgroup rw,relatime,devices 0 0
  461. for _, line := range strings.Split(string(output), "\n") {
  462. parts := strings.Split(line, " ")
  463. if len(parts) == 6 && parts[2] == "cgroup" {
  464. for _, opt := range strings.Split(parts[3], ",") {
  465. if opt == cgroupType {
  466. return parts[1], nil
  467. }
  468. }
  469. }
  470. }
  471. return "", fmt.Errorf("cgroup mountpoint not found for %s", cgroupType)
  472. }
  473. func GetKernelVersion() (*KernelVersionInfo, error) {
  474. var (
  475. err error
  476. )
  477. uts, err := uname()
  478. if err != nil {
  479. return nil, err
  480. }
  481. release := make([]byte, len(uts.Release))
  482. i := 0
  483. for _, c := range uts.Release {
  484. release[i] = byte(c)
  485. i++
  486. }
  487. // Remove the \x00 from the release for Atoi to parse correctly
  488. release = release[:bytes.IndexByte(release, 0)]
  489. return ParseRelease(string(release))
  490. }
  491. func ParseRelease(release string) (*KernelVersionInfo, error) {
  492. var (
  493. flavor string
  494. kernel, major, minor int
  495. err error
  496. )
  497. tmp := strings.SplitN(release, "-", 2)
  498. tmp2 := strings.Split(tmp[0], ".")
  499. if len(tmp2) > 0 {
  500. kernel, err = strconv.Atoi(tmp2[0])
  501. if err != nil {
  502. return nil, err
  503. }
  504. }
  505. if len(tmp2) > 1 {
  506. major, err = strconv.Atoi(tmp2[1])
  507. if err != nil {
  508. return nil, err
  509. }
  510. }
  511. if len(tmp2) > 2 {
  512. // Removes "+" because git kernels might set it
  513. minorUnparsed := strings.Trim(tmp2[2], "+")
  514. minor, err = strconv.Atoi(minorUnparsed)
  515. if err != nil {
  516. return nil, err
  517. }
  518. }
  519. if len(tmp) == 2 {
  520. flavor = tmp[1]
  521. } else {
  522. flavor = ""
  523. }
  524. return &KernelVersionInfo{
  525. Kernel: kernel,
  526. Major: major,
  527. Minor: minor,
  528. Flavor: flavor,
  529. }, nil
  530. }
  531. // FIXME: this is deprecated by CopyWithTar in archive.go
  532. func CopyDirectory(source, dest string) error {
  533. if output, err := exec.Command("cp", "-ra", source, dest).CombinedOutput(); err != nil {
  534. return fmt.Errorf("Error copy: %s (%s)", err, output)
  535. }
  536. return nil
  537. }
  538. type NopFlusher struct{}
  539. func (f *NopFlusher) Flush() {}
  540. type WriteFlusher struct {
  541. sync.Mutex
  542. w io.Writer
  543. flusher http.Flusher
  544. }
  545. func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
  546. wf.Lock()
  547. defer wf.Unlock()
  548. n, err = wf.w.Write(b)
  549. wf.flusher.Flush()
  550. return n, err
  551. }
  552. func NewWriteFlusher(w io.Writer) *WriteFlusher {
  553. var flusher http.Flusher
  554. if f, ok := w.(http.Flusher); ok {
  555. flusher = f
  556. } else {
  557. flusher = &NopFlusher{}
  558. }
  559. return &WriteFlusher{w: w, flusher: flusher}
  560. }
  561. type JSONError struct {
  562. Code int `json:"code,omitempty"`
  563. Message string `json:"message,omitempty"`
  564. }
  565. type JSONMessage struct {
  566. Status string `json:"status,omitempty"`
  567. Progress string `json:"progress,omitempty"`
  568. ErrorMessage string `json:"error,omitempty"` //deprecated
  569. ID string `json:"id,omitempty"`
  570. From string `json:"from,omitempty"`
  571. Time int64 `json:"time,omitempty"`
  572. Error *JSONError `json:"errorDetail,omitempty"`
  573. }
  574. func (e *JSONError) Error() string {
  575. return e.Message
  576. }
  577. func NewHTTPRequestError(msg string, res *http.Response) error {
  578. return &JSONError{
  579. Message: msg,
  580. Code: res.StatusCode,
  581. }
  582. }
  583. func (jm *JSONMessage) Display(out io.Writer) error {
  584. if jm.Error != nil {
  585. if jm.Error.Code == 401 {
  586. return fmt.Errorf("Authentication is required.")
  587. }
  588. return jm.Error
  589. }
  590. fmt.Fprintf(out, "%c[2K\r", 27)
  591. if jm.Time != 0 {
  592. fmt.Fprintf(out, "[%s] ", time.Unix(jm.Time, 0))
  593. }
  594. if jm.ID != "" {
  595. fmt.Fprintf(out, "%s: ", jm.ID)
  596. }
  597. if jm.From != "" {
  598. fmt.Fprintf(out, "(from %s) ", jm.From)
  599. }
  600. if jm.Progress != "" {
  601. fmt.Fprintf(out, "%s %s\r", jm.Status, jm.Progress)
  602. } else {
  603. fmt.Fprintf(out, "%s\r\n", jm.Status)
  604. }
  605. return nil
  606. }
  607. func DisplayJSONMessagesStream(in io.Reader, out io.Writer) error {
  608. dec := json.NewDecoder(in)
  609. ids := make(map[string]int)
  610. diff := 0
  611. for {
  612. jm := JSONMessage{}
  613. if err := dec.Decode(&jm); err == io.EOF {
  614. break
  615. } else if err != nil {
  616. return err
  617. }
  618. if jm.Progress != "" && jm.ID != "" {
  619. line, ok := ids[jm.ID]
  620. if !ok {
  621. line = len(ids)
  622. ids[jm.ID] = line
  623. fmt.Fprintf(out, "\n")
  624. diff = 0
  625. } else {
  626. diff = len(ids) - line
  627. }
  628. fmt.Fprintf(out, "%c[%dA", 27, diff)
  629. }
  630. err := jm.Display(out)
  631. if jm.ID != "" {
  632. fmt.Fprintf(out, "%c[%dB", 27, diff)
  633. }
  634. if err != nil {
  635. return err
  636. }
  637. }
  638. return nil
  639. }
  640. type StreamFormatter struct {
  641. json bool
  642. used bool
  643. }
  644. func NewStreamFormatter(json bool) *StreamFormatter {
  645. return &StreamFormatter{json, false}
  646. }
  647. func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte {
  648. sf.used = true
  649. str := fmt.Sprintf(format, a...)
  650. if sf.json {
  651. b, err := json.Marshal(&JSONMessage{ID: id, Status: str})
  652. if err != nil {
  653. return sf.FormatError(err)
  654. }
  655. return b
  656. }
  657. return []byte(str + "\r\n")
  658. }
  659. func (sf *StreamFormatter) FormatError(err error) []byte {
  660. sf.used = true
  661. if sf.json {
  662. jsonError, ok := err.(*JSONError)
  663. if !ok {
  664. jsonError = &JSONError{Message: err.Error()}
  665. }
  666. if b, err := json.Marshal(&JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
  667. return b
  668. }
  669. return []byte("{\"error\":\"format error\"}")
  670. }
  671. return []byte("Error: " + err.Error() + "\r\n")
  672. }
  673. func (sf *StreamFormatter) FormatProgress(id, action, progress string) []byte {
  674. sf.used = true
  675. if sf.json {
  676. b, err := json.Marshal(&JSONMessage{Status: action, Progress: progress, ID: id})
  677. if err != nil {
  678. return nil
  679. }
  680. return b
  681. }
  682. return []byte(action + " " + progress + "\r")
  683. }
  684. func (sf *StreamFormatter) Used() bool {
  685. return sf.used
  686. }
  687. func IsURL(str string) bool {
  688. return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
  689. }
  690. func IsGIT(str string) bool {
  691. return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/")
  692. }
  693. // GetResolvConf opens and read the content of /etc/resolv.conf.
  694. // It returns it as byte slice.
  695. func GetResolvConf() ([]byte, error) {
  696. resolv, err := ioutil.ReadFile("/etc/resolv.conf")
  697. if err != nil {
  698. Errorf("Error openning resolv.conf: %s", err)
  699. return nil, err
  700. }
  701. return resolv, nil
  702. }
  703. // CheckLocalDns looks into the /etc/resolv.conf,
  704. // it returns true if there is a local nameserver or if there is no nameserver.
  705. func CheckLocalDns(resolvConf []byte) bool {
  706. var parsedResolvConf = StripComments(resolvConf, []byte("#"))
  707. if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
  708. return true
  709. }
  710. for _, ip := range [][]byte{
  711. []byte("127.0.0.1"),
  712. []byte("127.0.1.1"),
  713. } {
  714. if bytes.Contains(parsedResolvConf, ip) {
  715. return true
  716. }
  717. }
  718. return false
  719. }
  720. // StripComments parses input into lines and strips away comments.
  721. func StripComments(input []byte, commentMarker []byte) []byte {
  722. lines := bytes.Split(input, []byte("\n"))
  723. var output []byte
  724. for _, currentLine := range lines {
  725. var commentIndex = bytes.Index(currentLine, commentMarker)
  726. if commentIndex == -1 {
  727. output = append(output, currentLine...)
  728. } else {
  729. output = append(output, currentLine[:commentIndex]...)
  730. }
  731. output = append(output, []byte("\n")...)
  732. }
  733. return output
  734. }
  735. func ParseHost(host string, port int, addr string) string {
  736. var proto string
  737. switch {
  738. case strings.HasPrefix(addr, "unix://"):
  739. return addr
  740. case strings.HasPrefix(addr, "tcp://"):
  741. proto = "tcp"
  742. addr = strings.TrimPrefix(addr, "tcp://")
  743. default:
  744. if strings.Contains(addr, "://") {
  745. log.Fatal("Invalid bind address proto")
  746. }
  747. proto = "tcp"
  748. }
  749. if strings.Contains(addr, ":") {
  750. hostParts := strings.Split(addr, ":")
  751. if len(hostParts) != 2 {
  752. log.Fatal("Invalid bind address format.")
  753. }
  754. if hostParts[0] != "" {
  755. host = hostParts[0]
  756. }
  757. if p, err := strconv.Atoi(hostParts[1]); err == nil {
  758. port = p
  759. }
  760. } else {
  761. host = addr
  762. }
  763. return fmt.Sprintf("%s://%s:%d", proto, host, port)
  764. }
  765. func GetReleaseVersion() string {
  766. resp, err := http.Get("http://get.docker.io/latest")
  767. if err != nil {
  768. return ""
  769. }
  770. defer resp.Body.Close()
  771. if resp.ContentLength > 24 || resp.StatusCode != 200 {
  772. return ""
  773. }
  774. body, err := ioutil.ReadAll(resp.Body)
  775. if err != nil {
  776. return ""
  777. }
  778. return strings.TrimSpace(string(body))
  779. }
  780. // Get a repos name and returns the right reposName + tag
  781. // The tag can be confusing because of a port in a repository name.
  782. // Ex: localhost.localdomain:5000/samalba/hipache:latest
  783. func ParseRepositoryTag(repos string) (string, string) {
  784. n := strings.LastIndex(repos, ":")
  785. if n < 0 {
  786. return repos, ""
  787. }
  788. if tag := repos[n+1:]; !strings.Contains(tag, "/") {
  789. return repos[:n], tag
  790. }
  791. return repos, ""
  792. }
  793. type User struct {
  794. Uid string // user id
  795. Gid string // primary group id
  796. Username string
  797. Name string
  798. HomeDir string
  799. }
  800. // UserLookup check if the given username or uid is present in /etc/passwd
  801. // and returns the user struct.
  802. // If the username is not found, an error is returned.
  803. func UserLookup(uid string) (*User, error) {
  804. file, err := ioutil.ReadFile("/etc/passwd")
  805. if err != nil {
  806. return nil, err
  807. }
  808. for _, line := range strings.Split(string(file), "\n") {
  809. data := strings.Split(line, ":")
  810. if len(data) > 5 && (data[0] == uid || data[2] == uid) {
  811. return &User{
  812. Uid: data[2],
  813. Gid: data[3],
  814. Username: data[0],
  815. Name: data[4],
  816. HomeDir: data[5],
  817. }, nil
  818. }
  819. }
  820. return nil, fmt.Errorf("User not found in /etc/passwd")
  821. }
  822. type DependencyGraph struct {
  823. nodes map[string]*DependencyNode
  824. }
  825. type DependencyNode struct {
  826. id string
  827. deps map[*DependencyNode]bool
  828. }
  829. func NewDependencyGraph() DependencyGraph {
  830. return DependencyGraph{
  831. nodes: map[string]*DependencyNode{},
  832. }
  833. }
  834. func (graph *DependencyGraph) addNode(node *DependencyNode) string {
  835. if graph.nodes[node.id] == nil {
  836. graph.nodes[node.id] = node
  837. }
  838. return node.id
  839. }
  840. func (graph *DependencyGraph) NewNode(id string) string {
  841. if graph.nodes[id] != nil {
  842. return id
  843. }
  844. nd := &DependencyNode{
  845. id: id,
  846. deps: map[*DependencyNode]bool{},
  847. }
  848. graph.addNode(nd)
  849. return id
  850. }
  851. func (graph *DependencyGraph) AddDependency(node, to string) error {
  852. if graph.nodes[node] == nil {
  853. return fmt.Errorf("Node %s does not belong to this graph", node)
  854. }
  855. if graph.nodes[to] == nil {
  856. return fmt.Errorf("Node %s does not belong to this graph", to)
  857. }
  858. if node == to {
  859. return fmt.Errorf("Dependency loops are forbidden!")
  860. }
  861. graph.nodes[node].addDependency(graph.nodes[to])
  862. return nil
  863. }
  864. func (node *DependencyNode) addDependency(to *DependencyNode) bool {
  865. node.deps[to] = true
  866. return node.deps[to]
  867. }
  868. func (node *DependencyNode) Degree() int {
  869. return len(node.deps)
  870. }
  871. // The magic happens here ::
  872. func (graph *DependencyGraph) GenerateTraversalMap() ([][]string, error) {
  873. Debugf("Generating traversal map. Nodes: %d", len(graph.nodes))
  874. result := [][]string{}
  875. processed := map[*DependencyNode]bool{}
  876. // As long as we haven't processed all nodes...
  877. for len(processed) < len(graph.nodes) {
  878. // Use a temporary buffer for processed nodes, otherwise
  879. // nodes that depend on each other could end up in the same round.
  880. tmp_processed := []*DependencyNode{}
  881. for _, node := range graph.nodes {
  882. // If the node has more dependencies than what we have cleared,
  883. // it won't be valid for this round.
  884. if node.Degree() > len(processed) {
  885. continue
  886. }
  887. // If it's already processed, get to the next one
  888. if processed[node] {
  889. continue
  890. }
  891. // It's not been processed yet and has 0 deps. Add it!
  892. // (this is a shortcut for what we're doing below)
  893. if node.Degree() == 0 {
  894. tmp_processed = append(tmp_processed, node)
  895. continue
  896. }
  897. // If at least one dep hasn't been processed yet, we can't
  898. // add it.
  899. ok := true
  900. for dep := range node.deps {
  901. if !processed[dep] {
  902. ok = false
  903. break
  904. }
  905. }
  906. // All deps have already been processed. Add it!
  907. if ok {
  908. tmp_processed = append(tmp_processed, node)
  909. }
  910. }
  911. Debugf("Round %d: found %d available nodes", len(result), len(tmp_processed))
  912. // If no progress has been made this round,
  913. // that means we have circular dependencies.
  914. if len(tmp_processed) == 0 {
  915. return nil, fmt.Errorf("Could not find a solution to this dependency graph")
  916. }
  917. round := []string{}
  918. for _, nd := range tmp_processed {
  919. round = append(round, nd.id)
  920. processed[nd] = true
  921. }
  922. result = append(result, round)
  923. }
  924. return result, nil
  925. }
  926. // An StatusError reports an unsuccessful exit by a command.
  927. type StatusError struct {
  928. Status int
  929. }
  930. func (e *StatusError) Error() string {
  931. return fmt.Sprintf("Status: %d", e.Status)
  932. }
  933. func IsClosedError(err error) bool {
  934. /* This comparison is ugly, but unfortunately, net.go doesn't export errClosing.
  935. * See:
  936. * http://golang.org/src/pkg/net/net.go
  937. * https://code.google.com/p/go/issues/detail?id=4337
  938. * https://groups.google.com/forum/#!msg/golang-nuts/0_aaCvBmOcM/SptmDyX1XJMJ
  939. */
  940. return strings.HasSuffix(err.Error(), "use of closed network connection")
  941. }