user_docker.go 4.9 KB

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