utils.go 25 KB

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