sysinit.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package init
  2. import (
  3. "os"
  4. "path"
  5. "syscall"
  6. "golang.org/x/net/context"
  7. "github.com/docker/libcompose/project/options"
  8. "github.com/rancher/os/cmd/control"
  9. "github.com/rancher/os/compose"
  10. "github.com/rancher/os/config"
  11. "github.com/rancher/os/docker"
  12. "github.com/rancher/os/log"
  13. )
  14. const (
  15. systemImagesPreloadDirectory = "/var/lib/rancher/preload/system-docker"
  16. )
  17. func hasImage(name string) bool {
  18. stamp := path.Join(state, name)
  19. if _, err := os.Stat(stamp); os.IsNotExist(err) {
  20. return false
  21. }
  22. return true
  23. }
  24. func findImages(cfg *config.CloudConfig) ([]string, error) {
  25. log.Debugf("Looking for images at %s", config.ImagesPath)
  26. result := []string{}
  27. dir, err := os.Open(config.ImagesPath)
  28. if os.IsNotExist(err) {
  29. log.Debugf("Not loading images, %s does not exist", config.ImagesPath)
  30. return result, nil
  31. }
  32. if err != nil {
  33. return nil, err
  34. }
  35. defer dir.Close()
  36. files, err := dir.Readdirnames(0)
  37. if err != nil {
  38. return nil, err
  39. }
  40. for _, fileName := range files {
  41. if ok, _ := path.Match(config.ImagesPattern, fileName); ok {
  42. log.Debugf("Found %s", fileName)
  43. result = append(result, fileName)
  44. }
  45. }
  46. return result, nil
  47. }
  48. func loadImages(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  49. images, err := findImages(cfg)
  50. if err != nil || len(images) == 0 {
  51. return cfg, err
  52. }
  53. client, err := docker.NewSystemClient()
  54. if err != nil {
  55. return cfg, err
  56. }
  57. for _, image := range images {
  58. if hasImage(image) {
  59. continue
  60. }
  61. inputFileName := path.Join(config.ImagesPath, image)
  62. input, err := os.Open(inputFileName)
  63. if err != nil {
  64. return cfg, err
  65. }
  66. defer input.Close()
  67. log.Infof("Loading images from %s", inputFileName)
  68. if _, err = client.ImageLoad(context.Background(), input, true); err != nil {
  69. return cfg, err
  70. }
  71. log.Infof("Done loading images from %s", inputFileName)
  72. }
  73. return cfg, nil
  74. }
  75. func SysInit() error {
  76. cfg := config.LoadConfig()
  77. if err := control.PreloadImages(docker.NewSystemClient, systemImagesPreloadDirectory); err != nil {
  78. log.Errorf("Failed to preload System Docker images: %v", err)
  79. }
  80. _, err := config.ChainCfgFuncs(cfg,
  81. loadImages,
  82. func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  83. p, err := compose.GetProject(cfg, false, true)
  84. if err != nil {
  85. return cfg, err
  86. }
  87. return cfg, p.Up(context.Background(), options.Up{
  88. Create: options.Create{
  89. NoRecreate: true,
  90. },
  91. Log: cfg.Rancher.Log,
  92. })
  93. },
  94. func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  95. syscall.Sync()
  96. return cfg, nil
  97. },
  98. func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  99. log.Infof("RancherOS %s started", config.Version)
  100. return cfg, nil
  101. })
  102. return err
  103. }