client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package client
  2. import (
  3. "github.com/dotcloud/docker/rcli"
  4. "github.com/dotcloud/docker/future"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "path"
  11. "path/filepath"
  12. )
  13. // Run docker in "simple mode": run a single command and return.
  14. func SimpleMode(args []string) error {
  15. var oldState *State
  16. var err error
  17. if IsTerminal(0) && os.Getenv("NORAW") == "" {
  18. oldState, err = MakeRaw(0)
  19. if err != nil {
  20. return err
  21. }
  22. defer Restore(0, oldState)
  23. }
  24. // FIXME: we want to use unix sockets here, but net.UnixConn doesn't expose
  25. // CloseWrite(), which we need to cleanly signal that stdin is closed without
  26. // closing the connection.
  27. // See http://code.google.com/p/go/issues/detail?id=3345
  28. conn, err := rcli.Call("tcp", "127.0.0.1:4242", args...)
  29. if err != nil {
  30. return err
  31. }
  32. receive_stdout := future.Go(func() error {
  33. _, err := io.Copy(os.Stdout, conn)
  34. return err
  35. })
  36. send_stdin := future.Go(func() error {
  37. _, err := io.Copy(conn, os.Stdin)
  38. if err := conn.CloseWrite(); err != nil {
  39. log.Printf("Couldn't send EOF: " + err.Error())
  40. }
  41. return err
  42. })
  43. if err := <-receive_stdout; err != nil {
  44. return err
  45. }
  46. if oldState != nil {
  47. Restore(0, oldState)
  48. }
  49. if !IsTerminal(0) {
  50. if err := <-send_stdin; err != nil {
  51. return err
  52. }
  53. }
  54. return nil
  55. }
  56. // Run docker in "interactive mode": run a bash-compatible shell capable of running docker commands.
  57. func InteractiveMode(scripts ...string) error {
  58. // Determine path of current docker binary
  59. dockerPath, err := exec.LookPath(os.Args[0])
  60. if err != nil {
  61. return err
  62. }
  63. dockerPath, err = filepath.Abs(dockerPath)
  64. if err != nil {
  65. return err
  66. }
  67. // Create a temp directory
  68. tmp, err := ioutil.TempDir("", "docker-shell")
  69. if err != nil {
  70. return err
  71. }
  72. defer os.RemoveAll(tmp)
  73. // For each command, create an alias in temp directory
  74. // FIXME: generate this list dynamically with introspection of some sort
  75. // It might make sense to merge docker and dockerd to keep that introspection
  76. // within a single binary.
  77. for _, cmd := range []string{
  78. "help",
  79. "run",
  80. "ps",
  81. "pull",
  82. "put",
  83. "rm",
  84. "kill",
  85. "wait",
  86. "stop",
  87. "start",
  88. "restart",
  89. "logs",
  90. "diff",
  91. "commit",
  92. "attach",
  93. "info",
  94. "tar",
  95. "web",
  96. "images",
  97. "docker",
  98. } {
  99. if err := os.Symlink(dockerPath, path.Join(tmp, cmd)); err != nil {
  100. return err
  101. }
  102. }
  103. // Run $SHELL with PATH set to temp directory
  104. rcfile, err := ioutil.TempFile("", "docker-shell-rc")
  105. if err != nil {
  106. return err
  107. }
  108. io.WriteString(rcfile, "enable -n help\n")
  109. os.Setenv("PATH", tmp + ":" + os.Getenv("PATH"))
  110. os.Setenv("PS1", "\\h docker> ")
  111. shell := exec.Command("/bin/bash", append([]string{"--rcfile", rcfile.Name()}, scripts...)...)
  112. shell.Stdin = os.Stdin
  113. shell.Stdout = os.Stdout
  114. shell.Stderr = os.Stderr
  115. if err := shell.Run(); err != nil {
  116. return err
  117. }
  118. return nil
  119. }