user_docker.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package control
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "syscall"
  9. "time"
  10. "github.com/rancher/os/config"
  11. "github.com/rancher/os/pkg/compose"
  12. rosDocker "github.com/rancher/os/pkg/docker"
  13. "github.com/rancher/os/pkg/log"
  14. "github.com/rancher/os/pkg/util"
  15. "github.com/codegangsta/cli"
  16. composeClient "github.com/docker/libcompose/docker/client"
  17. "github.com/docker/libcompose/project"
  18. "golang.org/x/net/context"
  19. )
  20. const (
  21. defaultStorageContext = "console"
  22. dockerPidFile = "/var/run/docker.pid"
  23. sourceDirectory = "/engine"
  24. destDirectory = "/var/lib/rancher/engine"
  25. dockerCompletionFName = "completion"
  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 file.Name() == dockerCompletionFName {
  84. if err := os.Chmod(destFile, 0644); err != nil {
  85. return err
  86. }
  87. } else {
  88. if err := os.Chmod(destFile, 0751); err != nil {
  89. return err
  90. }
  91. }
  92. }
  93. return nil
  94. }
  95. func writeConfigCerts(cfg *config.CloudConfig) error {
  96. outDir := ServerTLSPath
  97. if err := os.MkdirAll(outDir, 0700); err != nil {
  98. return err
  99. }
  100. caCertPath := filepath.Join(outDir, CaCert)
  101. caKeyPath := filepath.Join(outDir, CaKey)
  102. serverCertPath := filepath.Join(outDir, ServerCert)
  103. serverKeyPath := filepath.Join(outDir, ServerKey)
  104. if cfg.Rancher.Docker.CACert != "" {
  105. if err := util.WriteFileAtomic(caCertPath, []byte(cfg.Rancher.Docker.CACert), 0400); err != nil {
  106. return err
  107. }
  108. if err := util.WriteFileAtomic(caKeyPath, []byte(cfg.Rancher.Docker.CAKey), 0400); err != nil {
  109. return err
  110. }
  111. }
  112. if cfg.Rancher.Docker.ServerCert != "" {
  113. if err := util.WriteFileAtomic(serverCertPath, []byte(cfg.Rancher.Docker.ServerCert), 0400); err != nil {
  114. return err
  115. }
  116. if err := util.WriteFileAtomic(serverKeyPath, []byte(cfg.Rancher.Docker.ServerKey), 0400); err != nil {
  117. return err
  118. }
  119. }
  120. return nil
  121. }
  122. func startDocker(cfg *config.CloudConfig) error {
  123. storageContext := cfg.Rancher.Docker.StorageContext
  124. if storageContext == "" {
  125. storageContext = defaultStorageContext
  126. }
  127. log.Infof("Starting Docker in context: %s", storageContext)
  128. p, err := compose.GetProject(cfg, true, false)
  129. if err != nil {
  130. return err
  131. }
  132. pid, err := waitForPid(storageContext, p)
  133. if err != nil {
  134. return err
  135. }
  136. log.Infof("%s PID %d", storageContext, pid)
  137. client, err := rosDocker.NewSystemClient()
  138. if err != nil {
  139. return err
  140. }
  141. dockerCfg := cfg.Rancher.Docker
  142. args := dockerCfg.FullArgs()
  143. log.Debugf("User Docker args: %v", args)
  144. if dockerCfg.TLS {
  145. if err := writeConfigCerts(cfg); err != nil {
  146. return err
  147. }
  148. }
  149. info, err := client.ContainerInspect(context.Background(), storageContext)
  150. if err != nil {
  151. return err
  152. }
  153. cmd := []string{"system-docker-runc", "exec", "--", info.ID, "env"}
  154. log.Info(dockerCfg.AppendEnv())
  155. cmd = append(cmd, dockerCfg.AppendEnv()...)
  156. cmd = append(cmd, dockerCommand...)
  157. cmd = append(cmd, args...)
  158. log.Infof("Running %v", cmd)
  159. return syscall.Exec("/usr/bin/system-docker-runc", cmd, os.Environ())
  160. }
  161. func waitForPid(service string, project *project.Project) (int, error) {
  162. log.Infof("Getting PID for service: %s", service)
  163. for {
  164. if pid, err := getPid(service, project); err != nil || pid == 0 {
  165. log.Infof("Waiting for %s : %d : %v", service, pid, err)
  166. time.Sleep(1 * time.Second)
  167. } else {
  168. return pid, err
  169. }
  170. }
  171. }
  172. func getPid(service string, project *project.Project) (int, error) {
  173. s, err := project.CreateService(service)
  174. if err != nil {
  175. return 0, err
  176. }
  177. containers, err := s.Containers(context.Background())
  178. if err != nil {
  179. return 0, err
  180. }
  181. if len(containers) == 0 {
  182. return 0, nil
  183. }
  184. client, err := composeClient.Create(composeClient.Options{
  185. Host: config.SystemDockerHost,
  186. })
  187. if err != nil {
  188. return 0, err
  189. }
  190. id, err := containers[0].ID()
  191. if err != nil {
  192. return 0, err
  193. }
  194. info, err := client.ContainerInspect(context.Background(), id)
  195. if err != nil || info.ID == "" {
  196. return 0, err
  197. }
  198. if info.State.Running {
  199. return info.State.Pid, nil
  200. }
  201. return 0, nil
  202. }