user_docker.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package control
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "syscall"
  8. "time"
  9. "golang.org/x/net/context"
  10. "path/filepath"
  11. "github.com/codegangsta/cli"
  12. composeClient "github.com/docker/libcompose/docker/client"
  13. "github.com/docker/libcompose/project"
  14. "github.com/rancher/os/compose"
  15. "github.com/rancher/os/config"
  16. rosDocker "github.com/rancher/os/docker"
  17. "github.com/rancher/os/log"
  18. "github.com/rancher/os/util"
  19. )
  20. const (
  21. defaultStorageContext = "console"
  22. dockerPidFile = "/var/run/docker.pid"
  23. userDocker = "user-docker"
  24. sourceDirectory = "/engine"
  25. destDirectory = "/var/lib/rancher/engine"
  26. )
  27. var (
  28. dockerCommand = []string{
  29. "ros",
  30. "docker-init",
  31. }
  32. )
  33. func userDockerAction(c *cli.Context) error {
  34. if err := copyBinaries(sourceDirectory, destDirectory); err != nil {
  35. return err
  36. }
  37. if err := syscall.Mount("/host/sys", "/sys", "", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
  38. return err
  39. }
  40. cfg := config.LoadConfig()
  41. return startDocker(cfg)
  42. }
  43. func copyBinaries(source, dest string) error {
  44. if err := os.MkdirAll(dest, 0755); err != nil {
  45. return err
  46. }
  47. files, err := ioutil.ReadDir(dest)
  48. if err != nil {
  49. return err
  50. }
  51. for _, file := range files {
  52. if err = os.RemoveAll(path.Join(dest, file.Name())); err != nil {
  53. return err
  54. }
  55. }
  56. files, err = ioutil.ReadDir(source)
  57. if err != nil {
  58. return err
  59. }
  60. for _, file := range files {
  61. sourceFile := path.Join(source, file.Name())
  62. destFile := path.Join(dest, file.Name())
  63. in, err := os.Open(sourceFile)
  64. if err != nil {
  65. return err
  66. }
  67. out, err := os.Create(destFile)
  68. if err != nil {
  69. return err
  70. }
  71. if _, err = io.Copy(out, in); err != nil {
  72. return err
  73. }
  74. if err = out.Sync(); err != nil {
  75. return err
  76. }
  77. if err = in.Close(); err != nil {
  78. return err
  79. }
  80. if err = out.Close(); err != nil {
  81. return err
  82. }
  83. if err := os.Chmod(destFile, 0751); err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. func writeConfigCerts(cfg *config.CloudConfig) error {
  90. outDir := ServerTLSPath
  91. if err := os.MkdirAll(outDir, 0700); err != nil {
  92. return err
  93. }
  94. caCertPath := filepath.Join(outDir, CaCert)
  95. caKeyPath := filepath.Join(outDir, CaKey)
  96. serverCertPath := filepath.Join(outDir, ServerCert)
  97. serverKeyPath := filepath.Join(outDir, ServerKey)
  98. if cfg.Rancher.Docker.CACert != "" {
  99. if err := util.WriteFileAtomic(caCertPath, []byte(cfg.Rancher.Docker.CACert), 0400); err != nil {
  100. return err
  101. }
  102. if err := util.WriteFileAtomic(caKeyPath, []byte(cfg.Rancher.Docker.CAKey), 0400); err != nil {
  103. return err
  104. }
  105. }
  106. if cfg.Rancher.Docker.ServerCert != "" {
  107. if err := util.WriteFileAtomic(serverCertPath, []byte(cfg.Rancher.Docker.ServerCert), 0400); err != nil {
  108. return err
  109. }
  110. if err := util.WriteFileAtomic(serverKeyPath, []byte(cfg.Rancher.Docker.ServerKey), 0400); err != nil {
  111. return err
  112. }
  113. }
  114. return nil
  115. }
  116. func startDocker(cfg *config.CloudConfig) error {
  117. storageContext := cfg.Rancher.Docker.StorageContext
  118. if storageContext == "" {
  119. storageContext = defaultStorageContext
  120. }
  121. log.Infof("Starting Docker in context: %s", storageContext)
  122. p, err := compose.GetProject(cfg, true, false)
  123. if err != nil {
  124. return err
  125. }
  126. pid, err := waitForPid(storageContext, p)
  127. if err != nil {
  128. return err
  129. }
  130. log.Infof("%s PID %d", storageContext, pid)
  131. client, err := rosDocker.NewSystemClient()
  132. if err != nil {
  133. return err
  134. }
  135. dockerCfg := cfg.Rancher.Docker
  136. args := dockerCfg.FullArgs()
  137. log.Debugf("User Docker args: %v", args)
  138. if dockerCfg.TLS {
  139. if err := writeConfigCerts(cfg); err != nil {
  140. return err
  141. }
  142. }
  143. info, err := client.ContainerInspect(context.Background(), storageContext)
  144. if err != nil {
  145. return err
  146. }
  147. cmd := []string{"docker-runc", "exec", "--", info.ID, "env"}
  148. log.Info(dockerCfg.AppendEnv())
  149. cmd = append(cmd, dockerCfg.AppendEnv()...)
  150. cmd = append(cmd, dockerCommand...)
  151. cmd = append(cmd, args...)
  152. log.Infof("Running %v", cmd)
  153. return syscall.Exec("/usr/bin/ros", cmd, os.Environ())
  154. }
  155. func waitForPid(service string, project *project.Project) (int, error) {
  156. log.Infof("Getting PID for service: %s", service)
  157. for {
  158. if pid, err := getPid(service, project); err != nil || pid == 0 {
  159. log.Infof("Waiting for %s : %d : %v", service, pid, err)
  160. time.Sleep(1 * time.Second)
  161. } else {
  162. return pid, err
  163. }
  164. }
  165. }
  166. func getPid(service string, project *project.Project) (int, error) {
  167. s, err := project.CreateService(service)
  168. if err != nil {
  169. return 0, err
  170. }
  171. containers, err := s.Containers(context.Background())
  172. if err != nil {
  173. return 0, err
  174. }
  175. if len(containers) == 0 {
  176. return 0, nil
  177. }
  178. client, err := composeClient.Create(composeClient.Options{
  179. Host: config.SystemDockerHost,
  180. })
  181. if err != nil {
  182. return 0, err
  183. }
  184. id, err := containers[0].ID()
  185. if err != nil {
  186. return 0, err
  187. }
  188. info, err := client.ContainerInspect(context.Background(), id)
  189. if err != nil || info.ID == "" {
  190. return 0, err
  191. }
  192. if info.State.Running {
  193. return info.State.Pid, nil
  194. }
  195. return 0, nil
  196. }