main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "os/exec"
  9. "os/signal"
  10. "path/filepath"
  11. "strconv"
  12. "github.com/dotcloud/docker/pkg/libcontainer"
  13. "github.com/dotcloud/docker/pkg/libcontainer/cgroups/fs"
  14. "github.com/dotcloud/docker/pkg/libcontainer/namespaces"
  15. )
  16. var (
  17. dataPath = os.Getenv("data_path")
  18. console = os.Getenv("console")
  19. rawPipeFd = os.Getenv("pipe")
  20. )
  21. func main() {
  22. if len(os.Args) < 2 {
  23. log.Fatalf("invalid number of arguments %d", len(os.Args))
  24. }
  25. container, err := loadContainer()
  26. if err != nil {
  27. log.Fatalf("unable to load container: %s", err)
  28. }
  29. switch os.Args[1] {
  30. case "exec": // this is executed outside of the namespace in the cwd
  31. var nspid, exitCode int
  32. if nspid, err = readPid(); err != nil && !os.IsNotExist(err) {
  33. log.Fatalf("unable to read pid: %s", err)
  34. }
  35. if nspid > 0 {
  36. exitCode, err = namespaces.ExecIn(container, nspid, os.Args[2:])
  37. } else {
  38. term := namespaces.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty)
  39. exitCode, err = startContainer(container, term, dataPath, os.Args[2:])
  40. }
  41. if err != nil {
  42. log.Fatalf("failed to exec: %s", err)
  43. }
  44. os.Exit(exitCode)
  45. case "init": // this is executed inside of the namespace to setup the container
  46. // by default our current dir is always our rootfs
  47. rootfs, err := os.Getwd()
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. pipeFd, err := strconv.Atoi(rawPipeFd)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. syncPipe, err := namespaces.NewSyncPipeFromFd(0, uintptr(pipeFd))
  56. if err != nil {
  57. log.Fatalf("unable to create sync pipe: %s", err)
  58. }
  59. if err := namespaces.Init(container, rootfs, console, syncPipe, os.Args[2:]); err != nil {
  60. log.Fatalf("unable to initialize for container: %s", err)
  61. }
  62. case "stats":
  63. // returns the stats of the current container.
  64. stats, err := getContainerStats(container)
  65. if err != nil {
  66. log.Printf("Failed to get stats - %v\n", err)
  67. os.Exit(1)
  68. }
  69. fmt.Printf("Stats:\n%v\n", stats)
  70. os.Exit(0)
  71. case "spec":
  72. // returns the spec of the current container.
  73. spec, err := getContainerSpec(container)
  74. if err != nil {
  75. log.Printf("Failed to get spec - %v\n", err)
  76. os.Exit(1)
  77. }
  78. fmt.Printf("Spec:\n%v\n", spec)
  79. os.Exit(0)
  80. default:
  81. log.Fatalf("command not supported for nsinit %s", os.Args[0])
  82. }
  83. }
  84. func loadContainer() (*libcontainer.Container, error) {
  85. f, err := os.Open(filepath.Join(dataPath, "container.json"))
  86. if err != nil {
  87. return nil, err
  88. }
  89. defer f.Close()
  90. var container *libcontainer.Container
  91. if err := json.NewDecoder(f).Decode(&container); err != nil {
  92. return nil, err
  93. }
  94. return container, nil
  95. }
  96. func readPid() (int, error) {
  97. data, err := ioutil.ReadFile(filepath.Join(dataPath, "pid"))
  98. if err != nil {
  99. return -1, err
  100. }
  101. pid, err := strconv.Atoi(string(data))
  102. if err != nil {
  103. return -1, err
  104. }
  105. return pid, nil
  106. }
  107. // startContainer starts the container. Returns the exit status or -1 and an
  108. // error.
  109. //
  110. // Signals sent to the current process will be forwarded to container.
  111. func startContainer(container *libcontainer.Container, term namespaces.Terminal, dataPath string, args []string) (int, error) {
  112. var (
  113. cmd *exec.Cmd
  114. sigc = make(chan os.Signal, 10)
  115. )
  116. signal.Notify(sigc)
  117. createCommand := func(container *libcontainer.Container, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
  118. cmd = namespaces.DefaultCreateCommand(container, console, rootfs, dataPath, init, pipe, args)
  119. return cmd
  120. }
  121. startCallback := func() {
  122. go func() {
  123. for sig := range sigc {
  124. cmd.Process.Signal(sig)
  125. }
  126. }()
  127. }
  128. return namespaces.Exec(container, term, "", dataPath, args, createCommand, startCallback)
  129. }
  130. // returns the container stats in json format.
  131. func getContainerStats(container *libcontainer.Container) (string, error) {
  132. stats, err := fs.GetStats(container.Cgroups)
  133. if err != nil {
  134. return "", err
  135. }
  136. out, err := json.MarshalIndent(stats, "", "\t")
  137. if err != nil {
  138. return "", err
  139. }
  140. return string(out), nil
  141. }
  142. // returns the container spec in json format.
  143. func getContainerSpec(container *libcontainer.Container) (string, error) {
  144. spec, err := json.MarshalIndent(container, "", "\t")
  145. if err != nil {
  146. return "", err
  147. }
  148. return string(spec), nil
  149. }