sysinit.go 2.4 KB

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