utils.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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) (resp *http.Response, err error) {
  45. if resp, err = http.Get(url); err != nil {
  46. return nil, err
  47. }
  48. if resp.StatusCode >= 400 {
  49. return nil, fmt.Errorf("Got HTTP status code >= 400: %s", resp.Status)
  50. }
  51. return resp, nil
  52. }
  53. func logf(level string, format string, a ...interface{}) {
  54. // Retrieve the stack infos
  55. _, file, line, ok := runtime.Caller(2)
  56. if !ok {
  57. file = "<unknown>"
  58. line = -1
  59. } else {
  60. file = file[strings.LastIndex(file, "/")+1:]
  61. }
  62. fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] %s:%d %s\n", level, file, line, format), a...)
  63. }
  64. // Debug function, if the debug flag is set, then display. Do nothing otherwise
  65. // If Docker is in damon mode, also send the debug info on the socket
  66. func Debugf(format string, a ...interface{}) {
  67. if os.Getenv("DEBUG") != "" {
  68. logf("debug", format, a...)
  69. }
  70. }
  71. func Errorf(format string, a ...interface{}) {
  72. logf("error", format, a...)
  73. }
  74. // HumanDuration returns a human-readable approximation of a duration
  75. // (eg. "About a minute", "4 hours ago", etc.)
  76. func HumanDuration(d time.Duration) string {
  77. if seconds := int(d.Seconds()); seconds < 1 {
  78. return "Less than a second"
  79. } else if seconds < 60 {
  80. return fmt.Sprintf("%d seconds", seconds)
  81. } else if minutes := int(d.Minutes()); minutes == 1 {
  82. return "About a minute"
  83. } else if minutes < 60 {
  84. return fmt.Sprintf("%d minutes", minutes)
  85. } else if hours := int(d.Hours()); hours == 1 {
  86. return "About an hour"
  87. } else if hours < 48 {
  88. return fmt.Sprintf("%d hours", hours)
  89. } else if hours < 24*7*2 {
  90. return fmt.Sprintf("%d days", hours/24)
  91. } else if hours < 24*30*3 {
  92. return fmt.Sprintf("%d weeks", hours/24/7)
  93. } else if hours < 24*365*2 {
  94. return fmt.Sprintf("%d months", hours/24/30)
  95. }
  96. return fmt.Sprintf("%f years", d.Hours()/24/365)
  97. }
  98. // HumanSize returns a human-readable approximation of a size
  99. // using SI standard (eg. "44kB", "17MB")
  100. func HumanSize(size int64) string {
  101. i := 0
  102. var sizef float64
  103. sizef = float64(size)
  104. units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
  105. for sizef >= 1000.0 {
  106. sizef = sizef / 1000.0
  107. i++
  108. }
  109. return fmt.Sprintf("%.4g %s", sizef, units[i])
  110. }
  111. // Parses a human-readable string representing an amount of RAM
  112. // in bytes, kibibytes, mebibytes or gibibytes, and returns the
  113. // number of bytes, or -1 if the string is unparseable.
  114. // Units are case-insensitive, and the 'b' suffix is optional.
  115. func RAMInBytes(size string) (bytes int64, err error) {
  116. re, error := regexp.Compile("^(\\d+)([kKmMgG])?[bB]?$")
  117. if error != nil {
  118. return -1, error
  119. }
  120. matches := re.FindStringSubmatch(size)
  121. if len(matches) != 3 {
  122. return -1, fmt.Errorf("Invalid size: '%s'", size)
  123. }
  124. memLimit, error := strconv.ParseInt(matches[1], 10, 0)
  125. if error != nil {
  126. return -1, error
  127. }
  128. unit := strings.ToLower(matches[2])
  129. if unit == "k" {
  130. memLimit *= 1024
  131. } else if unit == "m" {
  132. memLimit *= 1024 * 1024
  133. } else if unit == "g" {
  134. memLimit *= 1024 * 1024 * 1024
  135. }
  136. return memLimit, nil
  137. }
  138. func Trunc(s string, maxlen int) string {
  139. if len(s) <= maxlen {
  140. return s
  141. }
  142. return s[:maxlen]
  143. }
  144. // Figure out the absolute path of our own binary (if it's still around).
  145. func SelfPath() string {
  146. path, err := exec.LookPath(os.Args[0])
  147. if err != nil {
  148. if os.IsNotExist(err) {
  149. return ""
  150. }
  151. if execErr, ok := err.(*exec.Error); ok && os.IsNotExist(execErr.Err) {
  152. return ""
  153. }
  154. panic(err)
  155. }
  156. path, err = filepath.Abs(path)
  157. if err != nil {
  158. if os.IsNotExist(err) {
  159. return ""
  160. }
  161. panic(err)
  162. }
  163. return path
  164. }
  165. func dockerInitSha1(target string) string {
  166. f, err := os.Open(target)
  167. if err != nil {
  168. return ""
  169. }
  170. defer f.Close()
  171. h := sha1.New()
  172. _, err = io.Copy(h, f)
  173. if err != nil {
  174. return ""
  175. }
  176. return hex.EncodeToString(h.Sum(nil))
  177. }
  178. func isValidDockerInitPath(target string, selfPath string) bool { // target and selfPath should be absolute (InitPath and SelfPath already do this)
  179. if target == "" {
  180. return false
  181. }
  182. if IAMSTATIC {
  183. if selfPath == "" {
  184. return false
  185. }
  186. if target == selfPath {
  187. return true
  188. }
  189. targetFileInfo, err := os.Lstat(target)
  190. if err != nil {
  191. return false
  192. }
  193. selfPathFileInfo, err := os.Lstat(selfPath)
  194. if err != nil {
  195. return false
  196. }
  197. return os.SameFile(targetFileInfo, selfPathFileInfo)
  198. }
  199. return INITSHA1 != "" && dockerInitSha1(target) == INITSHA1
  200. }
  201. // Figure out the path of our dockerinit (which may be SelfPath())
  202. func DockerInitPath(localCopy string) string {
  203. selfPath := SelfPath()
  204. if isValidDockerInitPath(selfPath, selfPath) {
  205. // if we're valid, don't bother checking anything else
  206. return selfPath
  207. }
  208. var possibleInits = []string{
  209. localCopy,
  210. INITPATH,
  211. filepath.Join(filepath.Dir(selfPath), "dockerinit"),
  212. // 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."
  213. // http://www.linuxbase.org/betaspecs/fhs/fhs.html#usrlibexec
  214. "/usr/libexec/docker/dockerinit",
  215. "/usr/local/libexec/docker/dockerinit",
  216. // 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."
  217. // http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#USRLIBLIBRARIESFORPROGRAMMINGANDPA
  218. "/usr/lib/docker/dockerinit",
  219. "/usr/local/lib/docker/dockerinit",
  220. }
  221. for _, dockerInit := range possibleInits {
  222. if dockerInit == "" {
  223. continue
  224. }
  225. path, err := exec.LookPath(dockerInit)
  226. if err == nil {
  227. path, err = filepath.Abs(path)
  228. if err != nil {
  229. // LookPath already validated that this file exists and is executable (following symlinks), so how could Abs fail?
  230. panic(err)
  231. }
  232. if isValidDockerInitPath(path, selfPath) {
  233. return path
  234. }
  235. }
  236. }
  237. return ""
  238. }
  239. type NopWriter struct{}
  240. func (*NopWriter) Write(buf []byte) (int, error) {
  241. return len(buf), nil
  242. }
  243. type nopWriteCloser struct {
  244. io.Writer
  245. }
  246. func (w *nopWriteCloser) Close() error { return nil }
  247. func NopWriteCloser(w io.Writer) io.WriteCloser {
  248. return &nopWriteCloser{w}
  249. }
  250. type bufReader struct {
  251. sync.Mutex
  252. buf *bytes.Buffer
  253. reader io.Reader
  254. err error
  255. wait sync.Cond
  256. }
  257. func NewBufReader(r io.Reader) *bufReader {
  258. reader := &bufReader{
  259. buf: &bytes.Buffer{},
  260. reader: r,
  261. }
  262. reader.wait.L = &reader.Mutex
  263. go reader.drain()
  264. return reader
  265. }
  266. func (r *bufReader) drain() {
  267. buf := make([]byte, 1024)
  268. for {
  269. n, err := r.reader.Read(buf)
  270. r.Lock()
  271. if err != nil {
  272. r.err = err
  273. } else {
  274. r.buf.Write(buf[0:n])
  275. }
  276. r.wait.Signal()
  277. r.Unlock()
  278. if err != nil {
  279. break
  280. }
  281. }
  282. }
  283. func (r *bufReader) Read(p []byte) (n int, err error) {
  284. r.Lock()
  285. defer r.Unlock()
  286. for {
  287. n, err = r.buf.Read(p)
  288. if n > 0 {
  289. return n, err
  290. }
  291. if r.err != nil {
  292. return 0, r.err
  293. }
  294. r.wait.Wait()
  295. }
  296. }
  297. func (r *bufReader) Close() error {
  298. closer, ok := r.reader.(io.ReadCloser)
  299. if !ok {
  300. return nil
  301. }
  302. return closer.Close()
  303. }
  304. type WriteBroadcaster struct {
  305. sync.Mutex
  306. buf *bytes.Buffer
  307. writers map[StreamWriter]bool
  308. }
  309. type StreamWriter struct {
  310. wc io.WriteCloser
  311. stream string
  312. }
  313. func (w *WriteBroadcaster) AddWriter(writer io.WriteCloser, stream string) {
  314. w.Lock()
  315. sw := StreamWriter{wc: writer, stream: stream}
  316. w.writers[sw] = true
  317. w.Unlock()
  318. }
  319. type JSONLog struct {
  320. Log string `json:"log,omitempty"`
  321. Stream string `json:"stream,omitempty"`
  322. Created time.Time `json:"time"`
  323. }
  324. func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
  325. w.Lock()
  326. defer w.Unlock()
  327. w.buf.Write(p)
  328. for sw := range w.writers {
  329. lp := p
  330. if sw.stream != "" {
  331. lp = nil
  332. for {
  333. line, err := w.buf.ReadString('\n')
  334. if err != nil {
  335. w.buf.Write([]byte(line))
  336. break
  337. }
  338. b, err := json.Marshal(&JSONLog{Log: line, Stream: sw.stream, Created: time.Now().UTC()})
  339. if err != nil {
  340. // On error, evict the writer
  341. delete(w.writers, sw)
  342. continue
  343. }
  344. lp = append(lp, b...)
  345. lp = append(lp, '\n')
  346. }
  347. }
  348. if n, err := sw.wc.Write(lp); err != nil || n != len(lp) {
  349. // On error, evict the writer
  350. delete(w.writers, sw)
  351. }
  352. }
  353. return len(p), nil
  354. }
  355. func (w *WriteBroadcaster) CloseWriters() error {
  356. w.Lock()
  357. defer w.Unlock()
  358. for sw := range w.writers {
  359. sw.wc.Close()
  360. }
  361. w.writers = make(map[StreamWriter]bool)
  362. return nil
  363. }
  364. func NewWriteBroadcaster() *WriteBroadcaster {
  365. return &WriteBroadcaster{writers: make(map[StreamWriter]bool), buf: bytes.NewBuffer(nil)}
  366. }
  367. func GetTotalUsedFds() int {
  368. if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
  369. Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
  370. } else {
  371. return len(fds)
  372. }
  373. return -1
  374. }
  375. // TruncIndex allows the retrieval of string identifiers by any of their unique prefixes.
  376. // This is used to retrieve image and container IDs by more convenient shorthand prefixes.
  377. type TruncIndex struct {
  378. sync.RWMutex
  379. index *suffixarray.Index
  380. ids map[string]bool
  381. bytes []byte
  382. }
  383. func NewTruncIndex() *TruncIndex {
  384. return &TruncIndex{
  385. index: suffixarray.New([]byte{' '}),
  386. ids: make(map[string]bool),
  387. bytes: []byte{' '},
  388. }
  389. }
  390. func (idx *TruncIndex) Add(id string) error {
  391. idx.Lock()
  392. defer idx.Unlock()
  393. if strings.Contains(id, " ") {
  394. return fmt.Errorf("Illegal character: ' '")
  395. }
  396. if _, exists := idx.ids[id]; exists {
  397. return fmt.Errorf("Id already exists: %s", id)
  398. }
  399. idx.ids[id] = true
  400. idx.bytes = append(idx.bytes, []byte(id+" ")...)
  401. idx.index = suffixarray.New(idx.bytes)
  402. return nil
  403. }
  404. func (idx *TruncIndex) Delete(id string) error {
  405. idx.Lock()
  406. defer idx.Unlock()
  407. if _, exists := idx.ids[id]; !exists {
  408. return fmt.Errorf("No such id: %s", id)
  409. }
  410. before, after, err := idx.lookup(id)
  411. if err != nil {
  412. return err
  413. }
  414. delete(idx.ids, id)
  415. idx.bytes = append(idx.bytes[:before], idx.bytes[after:]...)
  416. idx.index = suffixarray.New(idx.bytes)
  417. return nil
  418. }
  419. func (idx *TruncIndex) lookup(s string) (int, int, error) {
  420. offsets := idx.index.Lookup([]byte(" "+s), -1)
  421. //log.Printf("lookup(%s): %v (index bytes: '%s')\n", s, offsets, idx.index.Bytes())
  422. if offsets == nil || len(offsets) == 0 || len(offsets) > 1 {
  423. return -1, -1, fmt.Errorf("No such id: %s", s)
  424. }
  425. offsetBefore := offsets[0] + 1
  426. offsetAfter := offsetBefore + strings.Index(string(idx.bytes[offsetBefore:]), " ")
  427. return offsetBefore, offsetAfter, nil
  428. }
  429. func (idx *TruncIndex) Get(s string) (string, error) {
  430. idx.RLock()
  431. defer idx.RUnlock()
  432. before, after, err := idx.lookup(s)
  433. //log.Printf("Get(%s) bytes=|%s| before=|%d| after=|%d|\n", s, idx.bytes, before, after)
  434. if err != nil {
  435. return "", err
  436. }
  437. return string(idx.bytes[before:after]), err
  438. }
  439. // TruncateID returns a shorthand version of a string identifier for convenience.
  440. // A collision with other shorthands is very unlikely, but possible.
  441. // In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
  442. // will need to use a langer prefix, or the full-length Id.
  443. func TruncateID(id string) string {
  444. shortLen := 12
  445. if len(id) < shortLen {
  446. shortLen = len(id)
  447. }
  448. return id[:shortLen]
  449. }
  450. // Code c/c from io.Copy() modified to handle escape sequence
  451. func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) {
  452. buf := make([]byte, 32*1024)
  453. for {
  454. nr, er := src.Read(buf)
  455. if nr > 0 {
  456. // ---- Docker addition
  457. // char 16 is C-p
  458. if nr == 1 && buf[0] == 16 {
  459. nr, er = src.Read(buf)
  460. // char 17 is C-q
  461. if nr == 1 && buf[0] == 17 {
  462. if err := src.Close(); err != nil {
  463. return 0, err
  464. }
  465. return 0, nil
  466. }
  467. }
  468. // ---- End of docker
  469. nw, ew := dst.Write(buf[0:nr])
  470. if nw > 0 {
  471. written += int64(nw)
  472. }
  473. if ew != nil {
  474. err = ew
  475. break
  476. }
  477. if nr != nw {
  478. err = io.ErrShortWrite
  479. break
  480. }
  481. }
  482. if er == io.EOF {
  483. break
  484. }
  485. if er != nil {
  486. err = er
  487. break
  488. }
  489. }
  490. return written, err
  491. }
  492. func HashData(src io.Reader) (string, error) {
  493. h := sha256.New()
  494. if _, err := io.Copy(h, src); err != nil {
  495. return "", err
  496. }
  497. return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
  498. }
  499. type KernelVersionInfo struct {
  500. Kernel int
  501. Major int
  502. Minor int
  503. }
  504. func (k *KernelVersionInfo) String() string {
  505. return fmt.Sprintf("%d.%d.%d", k.Kernel, k.Major, k.Minor)
  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 GetKernelVersion() (*KernelVersionInfo, error) {
  528. var (
  529. err error
  530. )
  531. uts, err := uname()
  532. if err != nil {
  533. return nil, err
  534. }
  535. release := make([]byte, len(uts.Release))
  536. i := 0
  537. for _, c := range uts.Release {
  538. release[i] = byte(c)
  539. i++
  540. }
  541. // Remove the \x00 from the release for Atoi to parse correctly
  542. release = release[:bytes.IndexByte(release, 0)]
  543. return ParseRelease(string(release))
  544. }
  545. func ParseRelease(release string) (*KernelVersionInfo, error) {
  546. var (
  547. parts [3]int
  548. err error
  549. )
  550. re := regexp.MustCompile(`^([0-9]+)\.([0-9]+)\.([0-9]+)`)
  551. subs := re.FindStringSubmatch(release)
  552. if len(subs) < 4 {
  553. return nil, errors.New("Can't parse kernel version " + release)
  554. }
  555. for i := 0; i < 3; i++ {
  556. parts[i], err = strconv.Atoi(subs[i+1])
  557. if err != nil {
  558. return nil, err
  559. }
  560. }
  561. return &KernelVersionInfo{
  562. Kernel: parts[0],
  563. Major: parts[1],
  564. Minor: parts[2],
  565. }, nil
  566. }
  567. // FIXME: this is deprecated by CopyWithTar in archive.go
  568. func CopyDirectory(source, dest string) error {
  569. if output, err := exec.Command("cp", "-ra", source, dest).CombinedOutput(); err != nil {
  570. return fmt.Errorf("Error copy: %s (%s)", err, output)
  571. }
  572. return nil
  573. }
  574. type NopFlusher struct{}
  575. func (f *NopFlusher) Flush() {}
  576. type WriteFlusher struct {
  577. sync.Mutex
  578. w io.Writer
  579. flusher http.Flusher
  580. }
  581. func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
  582. wf.Lock()
  583. defer wf.Unlock()
  584. n, err = wf.w.Write(b)
  585. wf.flusher.Flush()
  586. return n, err
  587. }
  588. // Flush the stream immediately.
  589. func (wf *WriteFlusher) Flush() {
  590. wf.Lock()
  591. defer wf.Unlock()
  592. wf.flusher.Flush()
  593. }
  594. func NewWriteFlusher(w io.Writer) *WriteFlusher {
  595. var flusher http.Flusher
  596. if f, ok := w.(http.Flusher); ok {
  597. flusher = f
  598. } else {
  599. flusher = &NopFlusher{}
  600. }
  601. return &WriteFlusher{w: w, flusher: flusher}
  602. }
  603. func NewHTTPRequestError(msg string, res *http.Response) error {
  604. return &JSONError{
  605. Message: msg,
  606. Code: res.StatusCode,
  607. }
  608. }
  609. func IsURL(str string) bool {
  610. return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
  611. }
  612. func IsGIT(str string) bool {
  613. return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/")
  614. }
  615. // GetResolvConf opens and read the content of /etc/resolv.conf.
  616. // It returns it as byte slice.
  617. func GetResolvConf() ([]byte, error) {
  618. resolv, err := ioutil.ReadFile("/etc/resolv.conf")
  619. if err != nil {
  620. Errorf("Error openning resolv.conf: %s", err)
  621. return nil, err
  622. }
  623. return resolv, nil
  624. }
  625. // CheckLocalDns looks into the /etc/resolv.conf,
  626. // it returns true if there is a local nameserver or if there is no nameserver.
  627. func CheckLocalDns(resolvConf []byte) bool {
  628. var parsedResolvConf = StripComments(resolvConf, []byte("#"))
  629. if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
  630. return true
  631. }
  632. for _, ip := range [][]byte{
  633. []byte("127.0.0.1"),
  634. []byte("127.0.1.1"),
  635. } {
  636. if bytes.Contains(parsedResolvConf, ip) {
  637. return true
  638. }
  639. }
  640. return false
  641. }
  642. // StripComments parses input into lines and strips away comments.
  643. func StripComments(input []byte, commentMarker []byte) []byte {
  644. lines := bytes.Split(input, []byte("\n"))
  645. var output []byte
  646. for _, currentLine := range lines {
  647. var commentIndex = bytes.Index(currentLine, commentMarker)
  648. if commentIndex == -1 {
  649. output = append(output, currentLine...)
  650. } else {
  651. output = append(output, currentLine[:commentIndex]...)
  652. }
  653. output = append(output, []byte("\n")...)
  654. }
  655. return output
  656. }
  657. // GetNameserversAsCIDR returns nameservers (if any) listed in
  658. // /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
  659. // This function's output is intended for net.ParseCIDR
  660. func GetNameserversAsCIDR(resolvConf []byte) []string {
  661. var parsedResolvConf = StripComments(resolvConf, []byte("#"))
  662. nameservers := []string{}
  663. re := regexp.MustCompile(`^\s*nameserver\s*(([0-9]+\.){3}([0-9]+))\s*$`)
  664. for _, line := range bytes.Split(parsedResolvConf, []byte("\n")) {
  665. var ns = re.FindSubmatch(line)
  666. if len(ns) > 0 {
  667. nameservers = append(nameservers, string(ns[1])+"/32")
  668. }
  669. }
  670. return nameservers
  671. }
  672. // FIXME: Change this not to receive default value as parameter
  673. func ParseHost(defaultHost string, defaultPort int, defaultUnix, addr string) (string, error) {
  674. var (
  675. proto string
  676. host string
  677. port int
  678. )
  679. addr = strings.TrimSpace(addr)
  680. switch {
  681. case strings.HasPrefix(addr, "unix://"):
  682. proto = "unix"
  683. addr = strings.TrimPrefix(addr, "unix://")
  684. if addr == "" {
  685. addr = defaultUnix
  686. }
  687. case strings.HasPrefix(addr, "tcp://"):
  688. proto = "tcp"
  689. addr = strings.TrimPrefix(addr, "tcp://")
  690. case addr == "":
  691. proto = "unix"
  692. addr = defaultUnix
  693. default:
  694. if strings.Contains(addr, "://") {
  695. return "", fmt.Errorf("Invalid bind address protocol: %s", addr)
  696. }
  697. proto = "tcp"
  698. }
  699. if proto != "unix" && strings.Contains(addr, ":") {
  700. hostParts := strings.Split(addr, ":")
  701. if len(hostParts) != 2 {
  702. return "", fmt.Errorf("Invalid bind address format: %s", addr)
  703. }
  704. if hostParts[0] != "" {
  705. host = hostParts[0]
  706. } else {
  707. host = defaultHost
  708. }
  709. if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
  710. port = p
  711. } else {
  712. port = defaultPort
  713. }
  714. } else {
  715. host = addr
  716. port = defaultPort
  717. }
  718. if proto == "unix" {
  719. return fmt.Sprintf("%s://%s", proto, host), nil
  720. }
  721. return fmt.Sprintf("%s://%s:%d", proto, host, port), nil
  722. }
  723. func GetReleaseVersion() string {
  724. resp, err := http.Get("https://get.docker.io/latest")
  725. if err != nil {
  726. return ""
  727. }
  728. defer resp.Body.Close()
  729. if resp.ContentLength > 24 || resp.StatusCode != 200 {
  730. return ""
  731. }
  732. body, err := ioutil.ReadAll(resp.Body)
  733. if err != nil {
  734. return ""
  735. }
  736. return strings.TrimSpace(string(body))
  737. }
  738. // Get a repos name and returns the right reposName + tag
  739. // The tag can be confusing because of a port in a repository name.
  740. // Ex: localhost.localdomain:5000/samalba/hipache:latest
  741. func ParseRepositoryTag(repos string) (string, string) {
  742. n := strings.LastIndex(repos, ":")
  743. if n < 0 {
  744. return repos, ""
  745. }
  746. if tag := repos[n+1:]; !strings.Contains(tag, "/") {
  747. return repos[:n], tag
  748. }
  749. return repos, ""
  750. }
  751. type User struct {
  752. Uid string // user id
  753. Gid string // primary group id
  754. Username string
  755. Name string
  756. HomeDir string
  757. }
  758. // UserLookup check if the given username or uid is present in /etc/passwd
  759. // and returns the user struct.
  760. // If the username is not found, an error is returned.
  761. func UserLookup(uid string) (*User, error) {
  762. file, err := ioutil.ReadFile("/etc/passwd")
  763. if err != nil {
  764. return nil, err
  765. }
  766. for _, line := range strings.Split(string(file), "\n") {
  767. data := strings.Split(line, ":")
  768. if len(data) > 5 && (data[0] == uid || data[2] == uid) {
  769. return &User{
  770. Uid: data[2],
  771. Gid: data[3],
  772. Username: data[0],
  773. Name: data[4],
  774. HomeDir: data[5],
  775. }, nil
  776. }
  777. }
  778. return nil, fmt.Errorf("User not found in /etc/passwd")
  779. }
  780. // An StatusError reports an unsuccessful exit by a command.
  781. type StatusError struct {
  782. Status string
  783. StatusCode int
  784. }
  785. func (e *StatusError) Error() string {
  786. return fmt.Sprintf("Status: %s, Code: %d", e.Status, e.StatusCode)
  787. }
  788. func quote(word string, buf *bytes.Buffer) {
  789. // Bail out early for "simple" strings
  790. if word != "" && !strings.ContainsAny(word, "\\'\"`${[|&;<>()~*?! \t\n") {
  791. buf.WriteString(word)
  792. return
  793. }
  794. buf.WriteString("'")
  795. for i := 0; i < len(word); i++ {
  796. b := word[i]
  797. if b == '\'' {
  798. // Replace literal ' with a close ', a \', and a open '
  799. buf.WriteString("'\\''")
  800. } else {
  801. buf.WriteByte(b)
  802. }
  803. }
  804. buf.WriteString("'")
  805. }
  806. // Take a list of strings and escape them so they will be handled right
  807. // when passed as arguments to an program via a shell
  808. func ShellQuoteArguments(args []string) string {
  809. var buf bytes.Buffer
  810. for i, arg := range args {
  811. if i != 0 {
  812. buf.WriteByte(' ')
  813. }
  814. quote(arg, &buf)
  815. }
  816. return buf.String()
  817. }
  818. func IsClosedError(err error) bool {
  819. /* This comparison is ugly, but unfortunately, net.go doesn't export errClosing.
  820. * See:
  821. * http://golang.org/src/pkg/net/net.go
  822. * https://code.google.com/p/go/issues/detail?id=4337
  823. * https://groups.google.com/forum/#!msg/golang-nuts/0_aaCvBmOcM/SptmDyX1XJMJ
  824. */
  825. return strings.HasSuffix(err.Error(), "use of closed network connection")
  826. }
  827. func PartParser(template, data string) (map[string]string, error) {
  828. // ip:public:private
  829. var (
  830. templateParts = strings.Split(template, ":")
  831. parts = strings.Split(data, ":")
  832. out = make(map[string]string, len(templateParts))
  833. )
  834. if len(parts) != len(templateParts) {
  835. return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template)
  836. }
  837. for i, t := range templateParts {
  838. value := ""
  839. if len(parts) > i {
  840. value = parts[i]
  841. }
  842. out[t] = value
  843. }
  844. return out, nil
  845. }
  846. var globalTestID string
  847. // TestDirectory creates a new temporary directory and returns its path.
  848. // The contents of directory at path `templateDir` is copied into the
  849. // new directory.
  850. func TestDirectory(templateDir string) (dir string, err error) {
  851. if globalTestID == "" {
  852. globalTestID = RandomString()[:4]
  853. }
  854. prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
  855. if prefix == "" {
  856. prefix = "docker-test-"
  857. }
  858. dir, err = ioutil.TempDir("", prefix)
  859. if err = os.Remove(dir); err != nil {
  860. return
  861. }
  862. if templateDir != "" {
  863. if err = CopyDirectory(templateDir, dir); err != nil {
  864. return
  865. }
  866. }
  867. return
  868. }
  869. // GetCallerName introspects the call stack and returns the name of the
  870. // function `depth` levels down in the stack.
  871. func GetCallerName(depth int) string {
  872. // Use the caller function name as a prefix.
  873. // This helps trace temp directories back to their test.
  874. pc, _, _, _ := runtime.Caller(depth + 1)
  875. callerLongName := runtime.FuncForPC(pc).Name()
  876. parts := strings.Split(callerLongName, ".")
  877. callerShortName := parts[len(parts)-1]
  878. return callerShortName
  879. }
  880. func CopyFile(src, dst string) (int64, error) {
  881. if src == dst {
  882. return 0, nil
  883. }
  884. sf, err := os.Open(src)
  885. if err != nil {
  886. return 0, err
  887. }
  888. defer sf.Close()
  889. if err := os.Remove(dst); err != nil && !os.IsNotExist(err) {
  890. return 0, err
  891. }
  892. df, err := os.Create(dst)
  893. if err != nil {
  894. return 0, err
  895. }
  896. defer df.Close()
  897. return io.Copy(df, sf)
  898. }
  899. type readCloserWrapper struct {
  900. io.Reader
  901. closer func() error
  902. }
  903. func (r *readCloserWrapper) Close() error {
  904. return r.closer()
  905. }
  906. func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser {
  907. return &readCloserWrapper{
  908. Reader: r,
  909. closer: closer,
  910. }
  911. }