utils.go 24 KB

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