docker.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/dotcloud/docker"
  6. "github.com/dotcloud/docker/rcli"
  7. "github.com/dotcloud/docker/term"
  8. "io"
  9. "log"
  10. "os"
  11. "os/signal"
  12. "syscall"
  13. )
  14. var (
  15. GIT_COMMIT string
  16. NO_MEMORY_LIMIT string
  17. )
  18. func main() {
  19. if docker.SelfPath() == "/sbin/init" {
  20. // Running in init mode
  21. docker.SysInit()
  22. return
  23. }
  24. // FIXME: Switch d and D ? (to be more sshd like)
  25. flDaemon := flag.Bool("d", false, "Daemon mode")
  26. flDebug := flag.Bool("D", false, "Debug mode")
  27. bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge")
  28. pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID")
  29. flag.Parse()
  30. if *bridgeName != "" {
  31. docker.NetworkBridgeIface = *bridgeName
  32. } else {
  33. docker.NetworkBridgeIface = docker.DefaultNetworkBridge
  34. }
  35. if *flDebug {
  36. os.Setenv("DEBUG", "1")
  37. }
  38. docker.GIT_COMMIT = GIT_COMMIT
  39. docker.NO_MEMORY_LIMIT = NO_MEMORY_LIMIT == "1"
  40. if *flDaemon {
  41. if flag.NArg() != 0 {
  42. flag.Usage()
  43. return
  44. }
  45. if NO_MEMORY_LIMIT == "1" {
  46. log.Printf("WARNING: This version of docker has been compiled without memory limit support.")
  47. }
  48. if err := daemon(*pidfile); err != nil {
  49. log.Fatal(err)
  50. }
  51. } else {
  52. if err := runCommand(flag.Args()); err != nil {
  53. log.Fatal(err)
  54. }
  55. }
  56. }
  57. func createPidFile(pidfile string) error {
  58. if _, err := os.Stat(pidfile); err == nil {
  59. return fmt.Errorf("pid file found, ensure docker is not running or delete %s", pidfile)
  60. }
  61. file, err := os.Create(pidfile)
  62. if err != nil {
  63. return err
  64. }
  65. defer file.Close()
  66. _, err = fmt.Fprintf(file, "%d", os.Getpid())
  67. return err
  68. }
  69. func removePidFile(pidfile string) {
  70. if err := os.Remove(pidfile); err != nil {
  71. log.Printf("Error removing %s: %s", pidfile, err)
  72. }
  73. }
  74. func daemon(pidfile string) error {
  75. if err := createPidFile(pidfile); err != nil {
  76. log.Fatal(err)
  77. }
  78. defer removePidFile(pidfile)
  79. c := make(chan os.Signal, 1)
  80. signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
  81. go func() {
  82. sig := <-c
  83. log.Printf("Received signal '%v', exiting\n", sig)
  84. removePidFile(pidfile)
  85. os.Exit(0)
  86. }()
  87. service, err := docker.NewServer()
  88. if err != nil {
  89. return err
  90. }
  91. if err := http.ListenAndServe("0.0.0.0:4243", service.restEndpoint); err != nil {
  92. return err
  93. }
  94. return rcli.ListenAndServe("tcp", "127.0.0.1:4242", service)
  95. }
  96. func runCommand(args []string) error {
  97. // FIXME: we want to use unix sockets here, but net.UnixConn doesn't expose
  98. // CloseWrite(), which we need to cleanly signal that stdin is closed without
  99. // closing the connection.
  100. // See http://code.google.com/p/go/issues/detail?id=3345
  101. if conn, err := rcli.Call("tcp", "127.0.0.1:4242", args...); err == nil {
  102. options := conn.GetOptions()
  103. if options.RawTerminal &&
  104. term.IsTerminal(int(os.Stdin.Fd())) &&
  105. os.Getenv("NORAW") == "" {
  106. if oldState, err := rcli.SetRawTerminal(); err != nil {
  107. return err
  108. } else {
  109. defer rcli.RestoreTerminal(oldState)
  110. }
  111. }
  112. receiveStdout := docker.Go(func() error {
  113. _, err := io.Copy(os.Stdout, conn)
  114. return err
  115. })
  116. sendStdin := docker.Go(func() error {
  117. _, err := io.Copy(conn, os.Stdin)
  118. if err := conn.CloseWrite(); err != nil {
  119. log.Printf("Couldn't send EOF: " + err.Error())
  120. }
  121. return err
  122. })
  123. if err := <-receiveStdout; err != nil {
  124. return err
  125. }
  126. if !term.IsTerminal(int(os.Stdin.Fd())) {
  127. if err := <-sendStdin; err != nil {
  128. return err
  129. }
  130. }
  131. } else {
  132. return fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  133. }
  134. return nil
  135. }