utils.go 23 KB

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