sysinit.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package init
  2. import (
  3. "os"
  4. "path"
  5. "syscall"
  6. log "github.com/Sirupsen/logrus"
  7. dockerClient "github.com/fsouza/go-dockerclient"
  8. "github.com/rancherio/os/config"
  9. "github.com/rancherio/os/docker"
  10. )
  11. func importImage(client *dockerClient.Client, name, fileName string) error {
  12. file, err := os.Open(fileName)
  13. if err != nil {
  14. return err
  15. }
  16. defer file.Close()
  17. log.Debugf("Importing image for %s", fileName)
  18. repo, tag := dockerClient.ParseRepositoryTag(name)
  19. return client.ImportImage(dockerClient.ImportImageOptions{
  20. Source: "-",
  21. Repository: repo,
  22. Tag: tag,
  23. InputStream: file,
  24. })
  25. }
  26. func hasImage(name string) bool {
  27. stamp := path.Join(STATE, name)
  28. if _, err := os.Stat(stamp); os.IsNotExist(err) {
  29. return false
  30. }
  31. return true
  32. }
  33. func findImages(cfg *config.Config) ([]string, error) {
  34. log.Debugf("Looking for images at %s", config.IMAGES_PATH)
  35. result := []string{}
  36. dir, err := os.Open(config.IMAGES_PATH)
  37. if os.IsNotExist(err) {
  38. log.Debugf("Not loading images, %s does not exist")
  39. return result, nil
  40. }
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer dir.Close()
  45. files, err := dir.Readdirnames(0)
  46. if err != nil {
  47. return nil, err
  48. }
  49. for _, fileName := range files {
  50. if ok, _ := path.Match(config.IMAGES_PATTERN, fileName); ok {
  51. log.Debugf("Found %s", fileName)
  52. result = append(result, fileName)
  53. }
  54. }
  55. return result, nil
  56. }
  57. func loadImages(cfg *config.Config) error {
  58. images, err := findImages(cfg)
  59. if err != nil || len(images) == 0 {
  60. return err
  61. }
  62. client, err := docker.NewSystemClient()
  63. if err != nil {
  64. return err
  65. }
  66. for _, image := range images {
  67. if hasImage(image) {
  68. continue
  69. }
  70. inputFileName := path.Join(config.IMAGES_PATH, image)
  71. input, err := os.Open(inputFileName)
  72. if err != nil {
  73. return err
  74. }
  75. defer input.Close()
  76. log.Infof("Loading images from %s", inputFileName)
  77. err = client.LoadImage(dockerClient.LoadImageOptions{
  78. InputStream: input,
  79. })
  80. log.Infof("Done loading images from %s", inputFileName)
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. return nil
  86. }
  87. func runContainers(cfg *config.Config) error {
  88. return docker.RunServices("system-init", cfg, cfg.SystemContainers)
  89. }
  90. func tailConsole(cfg *config.Config) error {
  91. if !cfg.Console.Tail {
  92. return nil
  93. }
  94. client, err := docker.NewSystemClient()
  95. if err != nil {
  96. return err
  97. }
  98. console, ok := cfg.SystemContainers[config.CONSOLE_CONTAINER]
  99. if !ok {
  100. log.Error("Console not found")
  101. return nil
  102. }
  103. c := docker.NewContainerFromService(config.DOCKER_SYSTEM_HOST, config.CONSOLE_CONTAINER, console)
  104. if c.Err != nil {
  105. return c.Err
  106. }
  107. log.Infof("Tailing console : %s", c.Name)
  108. return client.Logs(dockerClient.LogsOptions{
  109. Container: c.Name,
  110. Stdout: true,
  111. Stderr: true,
  112. Follow: true,
  113. OutputStream: os.Stdout,
  114. ErrorStream: os.Stderr,
  115. })
  116. }
  117. func SysInit() error {
  118. cfg, err := config.LoadConfig()
  119. if err != nil {
  120. return err
  121. }
  122. initFuncs := []config.InitFunc{
  123. loadImages,
  124. runContainers,
  125. func(cfg *config.Config) error {
  126. syscall.Sync()
  127. return nil
  128. },
  129. func(cfg *config.Config) error {
  130. log.Infof("RancherOS %s started", config.VERSION)
  131. return nil
  132. },
  133. tailConsole,
  134. }
  135. return config.RunInitFuncs(cfg, initFuncs)
  136. }