utils.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/sha1"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "index/suffixarray"
  11. "io"
  12. "io/ioutil"
  13. "net/http"
  14. "os"
  15. "os/exec"
  16. "path/filepath"
  17. "regexp"
  18. "runtime"
  19. "strconv"
  20. "strings"
  21. "sync"
  22. "time"
  23. )
  24. var (
  25. IAMSTATIC bool // whether or not Docker itself was compiled statically via ./hack/make.sh binary
  26. INITSHA1 string // sha1sum of separate static dockerinit, if Docker itself was compiled dynamically via ./hack/make.sh dynbinary
  27. INITPATH string // custom location to search for a valid dockerinit binary (available for packagers as a last resort escape hatch)
  28. )
  29. // A common interface to access the Fatal method of
  30. // both testing.B and testing.T.
  31. type Fataler interface {
  32. Fatal(args ...interface{})
  33. }
  34. // Go is a basic promise implementation: it wraps calls a function in a goroutine,
  35. // and returns a channel which will later return the function's return value.
  36. func Go(f func() error) chan error {
  37. ch := make(chan error)
  38. go func() {
  39. ch <- f()
  40. }()
  41. return ch
  42. }
  43. // Request a given URL and return an io.Reader
  44. func Download(url string) (*http.Response, error) {
  45. var resp *http.Response
  46. var err error
  47. if resp, err = http.Get(url); err != nil {
  48. return nil, err
  49. }
  50. if resp.StatusCode >= 400 {
  51. return nil, errors.New("Got HTTP status code >= 400: " + resp.Status)
  52. }
  53. return resp, nil
  54. }
  55. func logf(level string, format string, a ...interface{}) {
  56. // Retrieve the stack infos
  57. _, file, line, ok := runtime.Caller(2)
  58. if !ok {
  59. file = "<unknown>"
  60. line = -1
  61. } else {
  62. file = file[strings.LastIndex(file, "/")+1:]
  63. }
  64. fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] %s:%d %s\n", level, file, line, format), a...)
  65. }
  66. // Debug function, if the debug flag is set, then display. Do nothing otherwise
  67. // If Docker is in damon mode, also send the debug info on the socket
  68. func Debugf(format string, a ...interface{}) {
  69. if os.Getenv("DEBUG") != "" {
  70. logf("debug", format, a...)
  71. }
  72. }
  73. func Errorf(format string, a ...interface{}) {
  74. logf("error", format, a...)
  75. }
  76. // HumanDuration returns a human-readable approximation of a duration
  77. // (eg. "About a minute", "4 hours ago", etc.)
  78. func HumanDuration(d time.Duration) string {
  79. if seconds := int(d.Seconds()); seconds < 1 {
  80. return "Less than a second"
  81. } else if seconds < 60 {
  82. return fmt.Sprintf("%d seconds", seconds)
  83. } else if minutes := int(d.Minutes()); minutes == 1 {
  84. return "About a minute"
  85. } else if minutes < 60 {
  86. return fmt.Sprintf("%d minutes", minutes)
  87. } else if hours := int(d.Hours()); hours == 1 {
  88. return "About an hour"
  89. } else if hours < 48 {
  90. return fmt.Sprintf("%d hours", hours)
  91. } else if hours < 24*7*2 {
  92. return fmt.Sprintf("%d days", hours/24)
  93. } else if hours < 24*30*3 {
  94. return fmt.Sprintf("%d weeks", hours/24/7)
  95. } else if hours < 24*365*2 {
  96. return fmt.Sprintf("%d months", hours/24/30)
  97. }
  98. return fmt.Sprintf("%f years", d.Hours()/24/365)
  99. }
  100. // HumanSize returns a human-readable approximation of a size
  101. // using SI standard (eg. "44kB", "17MB")
  102. func HumanSize(size int64) string {
  103. i := 0
  104. var sizef float64
  105. sizef = float64(size)
  106. units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
  107. for sizef >= 1000.0 {
  108. sizef = sizef / 1000.0
  109. i++
  110. }
  111. return fmt.Sprintf("%.4g %s", sizef, units[i])
  112. }
  113. // Parses a human-readable string representing an amount of RAM
  114. // in bytes, kibibytes, mebibytes or gibibytes, and returns the
  115. // number of bytes, or -1 if the string is unparseable.
  116. // Units are case-insensitive, and the 'b' suffix is optional.
  117. func RAMInBytes(size string) (bytes int64, err error) {
  118. re, error := regexp.Compile("^(\\d+)([kKmMgG])?[bB]?$")
  119. if error != nil {
  120. return -1, error
  121. }
  122. matches := re.FindStringSubmatch(size)
  123. if len(matches) != 3 {
  124. return -1, fmt.Errorf("Invalid size: '%s'", size)
  125. }
  126. memLimit, error := strconv.ParseInt(matches[1], 10, 0)
  127. if error != nil {
  128. return -1, error
  129. }
  130. unit := strings.ToLower(matches[2])
  131. if unit == "k" {
  132. memLimit *= 1024
  133. } else if unit == "m" {
  134. memLimit *= 1024 * 1024
  135. } else if unit == "g" {
  136. memLimit *= 1024 * 1024 * 1024
  137. }
  138. return memLimit, nil
  139. }
  140. func Trunc(s string, maxlen int) string {
  141. if len(s) <= maxlen {
  142. return s
  143. }
  144. return s[:maxlen]
  145. }
  146. // Figure out the absolute path of our own binary (if it's still around).
  147. func SelfPath() string {
  148. path, err := exec.LookPath(os.Args[0])
  149. if err != nil {
  150. if os.IsNotExist(err) {
  151. return ""
  152. }
  153. if execErr, ok := err.(*exec.Error); ok && os.IsNotExist(execErr.Err) {
  154. return ""
  155. }
  156. panic(err)
  157. }
  158. path, err = filepath.Abs(path)
  159. if err != nil {
  160. if os.IsNotExist(err) {
  161. return ""
  162. }
  163. panic(err)
  164. }
  165. return path
  166. }
  167. func dockerInitSha1(target string) string {
  168. f, err := os.Open(target)
  169. if err != nil {
  170. return ""
  171. }
  172. defer f.Close()
  173. h := sha1.New()
  174. _, err = io.Copy(h, f)
  175. if err != nil {
  176. return ""
  177. }
  178. return hex.EncodeToString(h.Sum(nil))
  179. }
  180. func isValidDockerInitPath(target string, selfPath string) bool { // target and selfPath should be absolute (InitPath and SelfPath already do this)
  181. if target == "" {
  182. return false
  183. }
  184. if IAMSTATIC {
  185. if selfPath == "" {
  186. return false
  187. }
  188. if target == selfPath {
  189. return true
  190. }
  191. targetFileInfo, err := os.Lstat(target)
  192. if err != nil {
  193. return false
  194. }
  195. selfPathFileInfo, err := os.Lstat(selfPath)
  196. if err != nil {
  197. return false
  198. }
  199. return os.SameFile(targetFileInfo, selfPathFileInfo)
  200. }
  201. return INITSHA1 != "" && dockerInitSha1(target) == INITSHA1
  202. }
  203. // Figure out the path of our dockerinit (which may be SelfPath())
  204. func DockerInitPath(localCopy string) string {
  205. selfPath := SelfPath()
  206. if isValidDockerInitPath(selfPath, selfPath) {
  207. // if we're valid, don't bother checking anything else
  208. return selfPath
  209. }
  210. var possibleInits = []string{
  211. localCopy,
  212. INITPATH,
  213. filepath.Join(filepath.Dir(selfPath), "dockerinit"),
  214. // FHS 3.0 Draft: "/usr/libexec includes internal binaries that are not intended to be executed directly by users or shell scripts. Applications may use a single subdirectory under /usr/libexec."
  215. // http://www.linuxbase.org/betaspecs/fhs/fhs.html#usrlibexec
  216. "/usr/libexec/docker/dockerinit",
  217. "/usr/local/libexec/docker/dockerinit",
  218. // FHS 2.3: "/usr/lib includes object files, libraries, and internal binaries that are not intended to be executed directly by users or shell scripts."
  219. // http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#USRLIBLIBRARIESFORPROGRAMMINGANDPA
  220. "/usr/lib/docker/dockerinit",
  221. "/usr/local/lib/docker/dockerinit",
  222. }
  223. for _, dockerInit := range possibleInits {
  224. if dockerInit == "" {
  225. continue
  226. }
  227. path, err := exec.LookPath(dockerInit)
  228. if err == nil {
  229. path, err = filepath.Abs(path)
  230. if err != nil {
  231. // LookPath already validated that this file exists and is executable (following symlinks), so how could Abs fail?
  232. panic(err)
  233. }
  234. if isValidDockerInitPath(path, selfPath) {
  235. return path
  236. }
  237. }
  238. }
  239. return ""
  240. }
  241. type NopWriter struct{}
  242. func (*NopWriter) Write(buf []byte) (int, error) {
  243. return len(buf), nil
  244. }
  245. type nopWriteCloser struct {
  246. io.Writer
  247. }
  248. func (w *nopWriteCloser) Close() error { return nil }
  249. func NopWriteCloser(w io.Writer) io.WriteCloser {
  250. return &nopWriteCloser{w}
  251. }
  252. type bufReader struct {
  253. sync.Mutex
  254. buf *bytes.Buffer
  255. reader io.Reader
  256. err error
  257. wait sync.Cond
  258. }
  259. func NewBufReader(r io.Reader) *bufReader {
  260. reader := &bufReader{
  261. buf: &bytes.Buffer{},
  262. reader: r,
  263. }
  264. reader.wait.L = &reader.Mutex
  265. go reader.drain()
  266. return reader
  267. }
  268. func (r *bufReader) drain() {
  269. buf := make([]byte, 1024)
  270. for {
  271. n, err := r.reader.Read(buf)
  272. r.Lock()
  273. if err != nil {
  274. r.err = err
  275. } else {
  276. r.buf.Write(buf[0:n])
  277. }
  278. r.wait.Signal()
  279. r.Unlock()
  280. if err != nil {
  281. break
  282. }
  283. }
  284. }
  285. func (r *bufReader) Read(p []byte) (n int, err error) {
  286. r.Lock()
  287. defer r.Unlock()
  288. for {
  289. n, err = r.buf.Read(p)
  290. if n > 0 {
  291. return n, err
  292. }
  293. if r.err != nil {
  294. return 0, r.err
  295. }
  296. r.wait.Wait()
  297. }
  298. }
  299. func (r *bufReader) Close() error {
  300. closer, ok := r.reader.(io.ReadCloser)
  301. if !ok {
  302. return nil
  303. }
  304. return closer.Close()
  305. }
  306. type WriteBroadcaster struct {
  307. sync.Mutex
  308. buf *bytes.Buffer
  309. writers map[StreamWriter]bool
  310. }
  311. type StreamWriter struct {
  312. wc io.WriteCloser
  313. stream string
  314. }
  315. func (w *WriteBroadcaster) AddWriter(writer io.WriteCloser, stream string) {
  316. w.Lock()
  317. sw := StreamWriter{wc: writer, stream: stream}
  318. w.writers[sw] = true
  319. w.Unlock()
  320. }
  321. type JSONLog struct {
  322. Log string `json:"log,omitempty"`
  323. Stream string `json:"stream,omitempty"`
  324. Created time.Time `json:"time"`
  325. }
  326. func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
  327. w.Lock()
  328. defer w.Unlock()
  329. w.buf.Write(p)
  330. for sw := range w.writers {
  331. lp := p
  332. if sw.stream != "" {
  333. lp = nil
  334. for {
  335. line, err := w.buf.ReadString('\n')
  336. if err != nil {
  337. w.buf.Write([]byte(line))
  338. break
  339. }
  340. b, err := json.Marshal(&JSONLog{Log: line, Stream: sw.stream, Created: time.Now().UTC()})
  341. if err != nil {
  342. // On error, evict the writer
  343. delete(w.writers, sw)
  344. continue
  345. }
  346. lp = append(lp, b...)
  347. lp = append(lp, '\n')
  348. }
  349. }
  350. if n, err := sw.wc.Write(lp); err != nil || n != len(lp) {
  351. // On error, evict the writer
  352. delete(w.writers, sw)
  353. }
  354. }
  355. return len(p), nil
  356. }
  357. func (w *WriteBroadcaster) CloseWriters() error {
  358. w.Lock()
  359. defer w.Unlock()
  360. for sw := range w.writers {
  361. sw.wc.Close()
  362. }
  363. w.writers = make(map[StreamWriter]bool)
  364. return nil
  365. }
  366. func NewWriteBroadcaster() *WriteBroadcaster {
  367. return &WriteBroadcaster{writers: make(map[StreamWriter]bool), buf: bytes.NewBuffer(nil)}
  368. }
  369. func GetTotalUsedFds() int {
  370. if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
  371. Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
  372. } else {
  373. return len(fds)
  374. }
  375. return -1
  376. }
  377. // TruncIndex allows the retrieval of string identifiers by any of their unique prefixes.
  378. // This is used to retrieve image and container IDs by more convenient shorthand prefixes.
  379. type TruncIndex struct {
  380. index *suffixarray.Index
  381. ids map[string]bool
  382. bytes []byte
  383. }
  384. func NewTruncIndex() *TruncIndex {
  385. return &TruncIndex{
  386. index: suffixarray.New([]byte{' '}),
  387. ids: make(map[string]bool),
  388. bytes: []byte{' '},
  389. }
  390. }
  391. func (idx *TruncIndex) Add(id string) error {
  392. if strings.Contains(id, " ") {
  393. return fmt.Errorf("Illegal character: ' '")
  394. }
  395. if _, exists := idx.ids[id]; exists {
  396. return fmt.Errorf("Id already exists: %s", id)
  397. }
  398. idx.ids[id] = true
  399. idx.bytes = append(idx.bytes, []byte(id+" ")...)
  400. idx.index = suffixarray.New(idx.bytes)
  401. return nil
  402. }
  403. func (idx *TruncIndex) Delete(id string) error {
  404. if _, exists := idx.ids[id]; !exists {
  405. return fmt.Errorf("No such id: %s", id)
  406. }
  407. before, after, err := idx.lookup(id)
  408. if err != nil {
  409. return err
  410. }
  411. delete(idx.ids, id)
  412. idx.bytes = append(idx.bytes[:before], idx.bytes[after:]...)
  413. idx.index = suffixarray.New(idx.bytes)
  414. return nil
  415. }
  416. func (idx *TruncIndex) lookup(s string) (int, int, error) {
  417. offsets := idx.index.Lookup([]byte(" "+s), -1)
  418. //log.Printf("lookup(%s): %v (index bytes: '%s')\n", s, offsets, idx.index.Bytes())
  419. if offsets == nil || len(offsets) == 0 || len(offsets) > 1 {
  420. return -1, -1, fmt.Errorf("No such id: %s", s)
  421. }
  422. offsetBefore := offsets[0] + 1
  423. offsetAfter := offsetBefore + strings.Index(string(idx.bytes[offsetBefore:]), " ")
  424. return offsetBefore, offsetAfter, nil
  425. }
  426. func (idx *TruncIndex) Get(s string) (string, error) {
  427. before, after, err := idx.lookup(s)
  428. //log.Printf("Get(%s) bytes=|%s| before=|%d| after=|%d|\n", s, idx.bytes, before, after)
  429. if err != nil {
  430. return "", err
  431. }
  432. return string(idx.bytes[before:after]), err
  433. }
  434. // TruncateID returns a shorthand version of a string identifier for convenience.
  435. // A collision with other shorthands is very unlikely, but possible.
  436. // In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
  437. // will need to use a langer prefix, or the full-length Id.
  438. func TruncateID(id string) string {
  439. shortLen := 12
  440. if len(id) < shortLen {
  441. shortLen = len(id)
  442. }
  443. return id[:shortLen]
  444. }
  445. // Code c/c from io.Copy() modified to handle escape sequence
  446. func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) {
  447. buf := make([]byte, 32*1024)
  448. for {
  449. nr, er := src.Read(buf)
  450. if nr > 0 {
  451. // ---- Docker addition
  452. // char 16 is C-p
  453. if nr == 1 && buf[0] == 16 {
  454. nr, er = src.Read(buf)
  455. // char 17 is C-q
  456. if nr == 1 && buf[0] == 17 {
  457. if err := src.Close(); err != nil {
  458. return 0, err
  459. }
  460. return 0, nil
  461. }
  462. }
  463. // ---- End of docker
  464. nw, ew := dst.Write(buf[0:nr])
  465. if nw > 0 {
  466. written += int64(nw)
  467. }
  468. if ew != nil {
  469. err = ew
  470. break
  471. }
  472. if nr != nw {
  473. err = io.ErrShortWrite
  474. break
  475. }
  476. }
  477. if er == io.EOF {
  478. break
  479. }
  480. if er != nil {
  481. err = er
  482. break
  483. }
  484. }
  485. return written, err
  486. }
  487. func HashData(src io.Reader) (string, error) {
  488. h := sha256.New()
  489. if _, err := io.Copy(h, src); err != nil {
  490. return "", err
  491. }
  492. return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
  493. }
  494. type KernelVersionInfo struct {
  495. Kernel int
  496. Major int
  497. Minor int
  498. Flavor string
  499. }
  500. func (k *KernelVersionInfo) String() string {
  501. flavor := ""
  502. if len(k.Flavor) > 0 {
  503. flavor = fmt.Sprintf("-%s", k.Flavor)
  504. }
  505. return fmt.Sprintf("%d.%d.%d%s", k.Kernel, k.Major, k.Minor, flavor)
  506. }
  507. // Compare two KernelVersionInfo struct.
  508. // Returns -1 if a < b, = if a == b, 1 it a > b
  509. func CompareKernelVersion(a, b *KernelVersionInfo) int {
  510. if a.Kernel < b.Kernel {
  511. return -1
  512. } else if a.Kernel > b.Kernel {
  513. return 1
  514. }
  515. if a.Major < b.Major {
  516. return -1
  517. } else if a.Major > b.Major {
  518. return 1
  519. }
  520. if a.Minor < b.Minor {
  521. return -1
  522. } else if a.Minor > b.Minor {
  523. return 1
  524. }
  525. return 0
  526. }
  527. func FindCgroupMountpoint(cgroupType string) (string, error) {
  528. output, err := ioutil.ReadFile("/proc/mounts")
  529. if err != nil {
  530. return "", err
  531. }
  532. // /proc/mounts has 6 fields per line, one mount per line, e.g.
  533. // cgroup /sys/fs/cgroup/devices cgroup rw,relatime,devices 0 0
  534. for _, line := range strings.Split(string(output), "\n") {
  535. parts := strings.Split(line, " ")
  536. if len(parts) == 6 && parts[2] == "cgroup" {
  537. for _, opt := range strings.Split(parts[3], ",") {
  538. if opt == cgroupType {
  539. return parts[1], nil
  540. }
  541. }
  542. }
  543. }
  544. return "", fmt.Errorf("cgroup mountpoint not found for %s", cgroupType)
  545. }
  546. func GetKernelVersion() (*KernelVersionInfo, error) {
  547. var (
  548. err error
  549. )
  550. uts, err := uname()
  551. if err != nil {
  552. return nil, err
  553. }
  554. release := make([]byte, len(uts.Release))
  555. i := 0
  556. for _, c := range uts.Release {
  557. release[i] = byte(c)
  558. i++
  559. }
  560. // Remove the \x00 from the release for Atoi to parse correctly
  561. release = release[:bytes.IndexByte(release, 0)]
  562. return ParseRelease(string(release))
  563. }
  564. func ParseRelease(release string) (*KernelVersionInfo, error) {
  565. var (
  566. flavor string
  567. kernel, major, minor int
  568. err error
  569. )
  570. tmp := strings.SplitN(release, "-", 2)
  571. tmp2 := strings.Split(tmp[0], ".")
  572. if len(tmp2) > 0 {
  573. kernel, err = strconv.Atoi(tmp2[0])
  574. if err != nil {
  575. return nil, err
  576. }
  577. }
  578. if len(tmp2) > 1 {
  579. major, err = strconv.Atoi(tmp2[1])
  580. if err != nil {
  581. return nil, err
  582. }
  583. }
  584. if len(tmp2) > 2 {
  585. // Removes "+" because git kernels might set it
  586. minorUnparsed := strings.Trim(tmp2[2], "+")
  587. minor, err = strconv.Atoi(minorUnparsed)
  588. if err != nil {
  589. return nil, err
  590. }
  591. }
  592. if len(tmp) == 2 {
  593. flavor = tmp[1]
  594. } else {
  595. flavor = ""
  596. }
  597. return &KernelVersionInfo{
  598. Kernel: kernel,
  599. Major: major,
  600. Minor: minor,
  601. Flavor: flavor,
  602. }, nil
  603. }
  604. // FIXME: this is deprecated by CopyWithTar in archive.go
  605. func CopyDirectory(source, dest string) error {
  606. if output, err := exec.Command("cp", "-ra", source, dest).CombinedOutput(); err != nil {
  607. return fmt.Errorf("Error copy: %s (%s)", err, output)
  608. }
  609. return nil
  610. }
  611. type NopFlusher struct{}
  612. func (f *NopFlusher) Flush() {}
  613. type WriteFlusher struct {
  614. sync.Mutex
  615. w io.Writer
  616. flusher http.Flusher
  617. }
  618. func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
  619. wf.Lock()
  620. defer wf.Unlock()
  621. n, err = wf.w.Write(b)
  622. wf.flusher.Flush()
  623. return n, err
  624. }
  625. // Flush the stream immediately.
  626. func (wf *WriteFlusher) Flush() {
  627. wf.Lock()
  628. defer wf.Unlock()
  629. wf.flusher.Flush()
  630. }
  631. func NewWriteFlusher(w io.Writer) *WriteFlusher {
  632. var flusher http.Flusher
  633. if f, ok := w.(http.Flusher); ok {
  634. flusher = f
  635. } else {
  636. flusher = &NopFlusher{}
  637. }
  638. return &WriteFlusher{w: w, flusher: flusher}
  639. }
  640. func NewHTTPRequestError(msg string, res *http.Response) error {
  641. return &JSONError{
  642. Message: msg,
  643. Code: res.StatusCode,
  644. }
  645. }
  646. func IsURL(str string) bool {
  647. return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
  648. }
  649. func IsGIT(str string) bool {
  650. return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/")
  651. }
  652. // GetResolvConf opens and read the content of /etc/resolv.conf.
  653. // It returns it as byte slice.
  654. func GetResolvConf() ([]byte, error) {
  655. resolv, err := ioutil.ReadFile("/etc/resolv.conf")
  656. if err != nil {
  657. Errorf("Error openning resolv.conf: %s", err)
  658. return nil, err
  659. }
  660. return resolv, nil
  661. }
  662. // CheckLocalDns looks into the /etc/resolv.conf,
  663. // it returns true if there is a local nameserver or if there is no nameserver.
  664. func CheckLocalDns(resolvConf []byte) bool {
  665. var parsedResolvConf = StripComments(resolvConf, []byte("#"))
  666. if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
  667. return true
  668. }
  669. for _, ip := range [][]byte{
  670. []byte("127.0.0.1"),
  671. []byte("127.0.1.1"),
  672. } {
  673. if bytes.Contains(parsedResolvConf, ip) {
  674. return true
  675. }
  676. }
  677. return false
  678. }
  679. // StripComments parses input into lines and strips away comments.
  680. func StripComments(input []byte, commentMarker []byte) []byte {
  681. lines := bytes.Split(input, []byte("\n"))
  682. var output []byte
  683. for _, currentLine := range lines {
  684. var commentIndex = bytes.Index(currentLine, commentMarker)
  685. if commentIndex == -1 {
  686. output = append(output, currentLine...)
  687. } else {
  688. output = append(output, currentLine[:commentIndex]...)
  689. }
  690. output = append(output, []byte("\n")...)
  691. }
  692. return output
  693. }
  694. // GetNameserversAsCIDR returns nameservers (if any) listed in
  695. // /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
  696. // This function's output is intended for net.ParseCIDR
  697. func GetNameserversAsCIDR(resolvConf []byte) []string {
  698. var parsedResolvConf = StripComments(resolvConf, []byte("#"))
  699. nameservers := []string{}
  700. re := regexp.MustCompile(`^\s*nameserver\s*(([0-9]+\.){3}([0-9]+))\s*$`)
  701. for _, line := range bytes.Split(parsedResolvConf, []byte("\n")) {
  702. var ns = re.FindSubmatch(line)
  703. if len(ns) > 0 {
  704. nameservers = append(nameservers, string(ns[1])+"/32")
  705. }
  706. }
  707. return nameservers
  708. }
  709. // FIXME: Change this not to receive default value as parameter
  710. func ParseHost(defaultHost string, defaultPort int, defaultUnix, addr string) (string, error) {
  711. var (
  712. proto string
  713. host string
  714. port int
  715. )
  716. switch {
  717. case strings.HasPrefix(addr, "unix://"):
  718. proto = "unix"
  719. addr = strings.TrimPrefix(addr, "unix://")
  720. if addr == "" {
  721. addr = defaultUnix
  722. }
  723. case strings.HasPrefix(addr, "tcp://"):
  724. proto = "tcp"
  725. addr = strings.TrimPrefix(addr, "tcp://")
  726. default:
  727. if strings.Contains(addr, "://") {
  728. return "", fmt.Errorf("Invalid bind address protocol: %s", addr)
  729. }
  730. proto = "tcp"
  731. }
  732. if proto != "unix" && strings.Contains(addr, ":") {
  733. hostParts := strings.Split(addr, ":")
  734. if len(hostParts) != 2 {
  735. return "", fmt.Errorf("Invalid bind address format: %s", addr)
  736. }
  737. if hostParts[0] != "" {
  738. host = hostParts[0]
  739. } else {
  740. host = defaultHost
  741. }
  742. if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
  743. port = p
  744. } else {
  745. port = defaultPort
  746. }
  747. } else {
  748. host = addr
  749. port = defaultPort
  750. }
  751. if proto == "unix" {
  752. return fmt.Sprintf("%s://%s", proto, host), nil
  753. }
  754. return fmt.Sprintf("%s://%s:%d", proto, host, port), nil
  755. }
  756. func GetReleaseVersion() string {
  757. resp, err := http.Get("http://get.docker.io/latest")
  758. if err != nil {
  759. return ""
  760. }
  761. defer resp.Body.Close()
  762. if resp.ContentLength > 24 || resp.StatusCode != 200 {
  763. return ""
  764. }
  765. body, err := ioutil.ReadAll(resp.Body)
  766. if err != nil {
  767. return ""
  768. }
  769. return strings.TrimSpace(string(body))
  770. }
  771. // Get a repos name and returns the right reposName + tag
  772. // The tag can be confusing because of a port in a repository name.
  773. // Ex: localhost.localdomain:5000/samalba/hipache:latest
  774. func ParseRepositoryTag(repos string) (string, string) {
  775. n := strings.LastIndex(repos, ":")
  776. if n < 0 {
  777. return repos, ""
  778. }
  779. if tag := repos[n+1:]; !strings.Contains(tag, "/") {
  780. return repos[:n], tag
  781. }
  782. return repos, ""
  783. }
  784. type User struct {
  785. Uid string // user id
  786. Gid string // primary group id
  787. Username string
  788. Name string
  789. HomeDir string
  790. }
  791. // UserLookup check if the given username or uid is present in /etc/passwd
  792. // and returns the user struct.
  793. // If the username is not found, an error is returned.
  794. func UserLookup(uid string) (*User, error) {
  795. file, err := ioutil.ReadFile("/etc/passwd")
  796. if err != nil {
  797. return nil, err
  798. }
  799. for _, line := range strings.Split(string(file), "\n") {
  800. data := strings.Split(line, ":")
  801. if len(data) > 5 && (data[0] == uid || data[2] == uid) {
  802. return &User{
  803. Uid: data[2],
  804. Gid: data[3],
  805. Username: data[0],
  806. Name: data[4],
  807. HomeDir: data[5],
  808. }, nil
  809. }
  810. }
  811. return nil, fmt.Errorf("User not found in /etc/passwd")
  812. }
  813. type DependencyGraph struct {
  814. nodes map[string]*DependencyNode
  815. }
  816. type DependencyNode struct {
  817. id string
  818. deps map[*DependencyNode]bool
  819. }
  820. func NewDependencyGraph() DependencyGraph {
  821. return DependencyGraph{
  822. nodes: map[string]*DependencyNode{},
  823. }
  824. }
  825. func (graph *DependencyGraph) addNode(node *DependencyNode) string {
  826. if graph.nodes[node.id] == nil {
  827. graph.nodes[node.id] = node
  828. }
  829. return node.id
  830. }
  831. func (graph *DependencyGraph) NewNode(id string) string {
  832. if graph.nodes[id] != nil {
  833. return id
  834. }
  835. nd := &DependencyNode{
  836. id: id,
  837. deps: map[*DependencyNode]bool{},
  838. }
  839. graph.addNode(nd)
  840. return id
  841. }
  842. func (graph *DependencyGraph) AddDependency(node, to string) error {
  843. if graph.nodes[node] == nil {
  844. return fmt.Errorf("Node %s does not belong to this graph", node)
  845. }
  846. if graph.nodes[to] == nil {
  847. return fmt.Errorf("Node %s does not belong to this graph", to)
  848. }
  849. if node == to {
  850. return fmt.Errorf("Dependency loops are forbidden!")
  851. }
  852. graph.nodes[node].addDependency(graph.nodes[to])
  853. return nil
  854. }
  855. func (node *DependencyNode) addDependency(to *DependencyNode) bool {
  856. node.deps[to] = true
  857. return node.deps[to]
  858. }
  859. func (node *DependencyNode) Degree() int {
  860. return len(node.deps)
  861. }
  862. // The magic happens here ::
  863. func (graph *DependencyGraph) GenerateTraversalMap() ([][]string, error) {
  864. Debugf("Generating traversal map. Nodes: %d", len(graph.nodes))
  865. result := [][]string{}
  866. processed := map[*DependencyNode]bool{}
  867. // As long as we haven't processed all nodes...
  868. for len(processed) < len(graph.nodes) {
  869. // Use a temporary buffer for processed nodes, otherwise
  870. // nodes that depend on each other could end up in the same round.
  871. tmpProcessed := []*DependencyNode{}
  872. for _, node := range graph.nodes {
  873. // If the node has more dependencies than what we have cleared,
  874. // it won't be valid for this round.
  875. if node.Degree() > len(processed) {
  876. continue
  877. }
  878. // If it's already processed, get to the next one
  879. if processed[node] {
  880. continue
  881. }
  882. // It's not been processed yet and has 0 deps. Add it!
  883. // (this is a shortcut for what we're doing below)
  884. if node.Degree() == 0 {
  885. tmpProcessed = append(tmpProcessed, node)
  886. continue
  887. }
  888. // If at least one dep hasn't been processed yet, we can't
  889. // add it.
  890. ok := true
  891. for dep := range node.deps {
  892. if !processed[dep] {
  893. ok = false
  894. break
  895. }
  896. }
  897. // All deps have already been processed. Add it!
  898. if ok {
  899. tmpProcessed = append(tmpProcessed, node)
  900. }
  901. }
  902. Debugf("Round %d: found %d available nodes", len(result), len(tmpProcessed))
  903. // If no progress has been made this round,
  904. // that means we have circular dependencies.
  905. if len(tmpProcessed) == 0 {
  906. return nil, fmt.Errorf("Could not find a solution to this dependency graph")
  907. }
  908. round := []string{}
  909. for _, nd := range tmpProcessed {
  910. round = append(round, nd.id)
  911. processed[nd] = true
  912. }
  913. result = append(result, round)
  914. }
  915. return result, nil
  916. }
  917. // An StatusError reports an unsuccessful exit by a command.
  918. type StatusError struct {
  919. Status string
  920. StatusCode int
  921. }
  922. func (e *StatusError) Error() string {
  923. return fmt.Sprintf("Status: %s, Code: %d", e.Status, e.StatusCode)
  924. }
  925. func quote(word string, buf *bytes.Buffer) {
  926. // Bail out early for "simple" strings
  927. if word != "" && !strings.ContainsAny(word, "\\'\"`${[|&;<>()~*?! \t\n") {
  928. buf.WriteString(word)
  929. return
  930. }
  931. buf.WriteString("'")
  932. for i := 0; i < len(word); i++ {
  933. b := word[i]
  934. if b == '\'' {
  935. // Replace literal ' with a close ', a \', and a open '
  936. buf.WriteString("'\\''")
  937. } else {
  938. buf.WriteByte(b)
  939. }
  940. }
  941. buf.WriteString("'")
  942. }
  943. // Take a list of strings and escape them so they will be handled right
  944. // when passed as arguments to an program via a shell
  945. func ShellQuoteArguments(args []string) string {
  946. var buf bytes.Buffer
  947. for i, arg := range args {
  948. if i != 0 {
  949. buf.WriteByte(' ')
  950. }
  951. quote(arg, &buf)
  952. }
  953. return buf.String()
  954. }
  955. func IsClosedError(err error) bool {
  956. /* This comparison is ugly, but unfortunately, net.go doesn't export errClosing.
  957. * See:
  958. * http://golang.org/src/pkg/net/net.go
  959. * https://code.google.com/p/go/issues/detail?id=4337
  960. * https://groups.google.com/forum/#!msg/golang-nuts/0_aaCvBmOcM/SptmDyX1XJMJ
  961. */
  962. return strings.HasSuffix(err.Error(), "use of closed network connection")
  963. }
  964. func PartParser(template, data string) (map[string]string, error) {
  965. // ip:public:private
  966. var (
  967. templateParts = strings.Split(template, ":")
  968. parts = strings.Split(data, ":")
  969. out = make(map[string]string, len(templateParts))
  970. )
  971. if len(parts) != len(templateParts) {
  972. return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template)
  973. }
  974. for i, t := range templateParts {
  975. value := ""
  976. if len(parts) > i {
  977. value = parts[i]
  978. }
  979. out[t] = value
  980. }
  981. return out, nil
  982. }
  983. var globalTestID string
  984. // TestDirectory creates a new temporary directory and returns its path.
  985. // The contents of directory at path `templateDir` is copied into the
  986. // new directory.
  987. func TestDirectory(templateDir string) (dir string, err error) {
  988. if globalTestID == "" {
  989. globalTestID = RandomString()[:4]
  990. }
  991. prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
  992. if prefix == "" {
  993. prefix = "docker-test-"
  994. }
  995. dir, err = ioutil.TempDir("", prefix)
  996. if err = os.Remove(dir); err != nil {
  997. return
  998. }
  999. if templateDir != "" {
  1000. if err = CopyDirectory(templateDir, dir); err != nil {
  1001. return
  1002. }
  1003. }
  1004. return
  1005. }
  1006. // GetCallerName introspects the call stack and returns the name of the
  1007. // function `depth` levels down in the stack.
  1008. func GetCallerName(depth int) string {
  1009. // Use the caller function name as a prefix.
  1010. // This helps trace temp directories back to their test.
  1011. pc, _, _, _ := runtime.Caller(depth + 1)
  1012. callerLongName := runtime.FuncForPC(pc).Name()
  1013. parts := strings.Split(callerLongName, ".")
  1014. callerShortName := parts[len(parts)-1]
  1015. return callerShortName
  1016. }
  1017. func CopyFile(src, dst string) (int64, error) {
  1018. if src == dst {
  1019. return 0, nil
  1020. }
  1021. sf, err := os.Open(src)
  1022. if err != nil {
  1023. return 0, err
  1024. }
  1025. defer sf.Close()
  1026. if err := os.Remove(dst); err != nil && !os.IsNotExist(err) {
  1027. return 0, err
  1028. }
  1029. df, err := os.Create(dst)
  1030. if err != nil {
  1031. return 0, err
  1032. }
  1033. defer df.Close()
  1034. return io.Copy(df, sf)
  1035. }
  1036. // Returns the relative path to the cgroup docker is running in.
  1037. func GetThisCgroup(cgroupType string) (string, error) {
  1038. output, err := ioutil.ReadFile("/proc/self/cgroup")
  1039. if err != nil {
  1040. return "", err
  1041. }
  1042. for _, line := range strings.Split(string(output), "\n") {
  1043. parts := strings.Split(line, ":")
  1044. // any type used by docker should work
  1045. if parts[1] == cgroupType {
  1046. return parts[2], nil
  1047. }
  1048. }
  1049. return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", cgroupType)
  1050. }
  1051. // Returns a list of pids for the given container.
  1052. func GetPidsForContainer(id string) ([]int, error) {
  1053. pids := []int{}
  1054. // memory is chosen randomly, any cgroup used by docker works
  1055. cgroupType := "memory"
  1056. cgroupRoot, err := FindCgroupMountpoint(cgroupType)
  1057. if err != nil {
  1058. return pids, err
  1059. }
  1060. cgroupThis, err := GetThisCgroup(cgroupType)
  1061. if err != nil {
  1062. return pids, err
  1063. }
  1064. filename := filepath.Join(cgroupRoot, cgroupThis, id, "tasks")
  1065. if _, err := os.Stat(filename); os.IsNotExist(err) {
  1066. // With more recent lxc versions use, cgroup will be in lxc/
  1067. filename = filepath.Join(cgroupRoot, cgroupThis, "lxc", id, "tasks")
  1068. }
  1069. output, err := ioutil.ReadFile(filename)
  1070. if err != nil {
  1071. return pids, err
  1072. }
  1073. for _, p := range strings.Split(string(output), "\n") {
  1074. if len(p) == 0 {
  1075. continue
  1076. }
  1077. pid, err := strconv.Atoi(p)
  1078. if err != nil {
  1079. return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
  1080. }
  1081. pids = append(pids, pid)
  1082. }
  1083. return pids, nil
  1084. }