utils.go 26 KB

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