utils.go 23 KB

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