preload.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package control
  2. import (
  3. "compress/gzip"
  4. "context"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "regexp"
  11. "strings"
  12. "github.com/codegangsta/cli"
  13. dockerClient "github.com/docker/engine-api/client"
  14. "github.com/docker/engine-api/types"
  15. "github.com/rancher/os/config"
  16. "github.com/rancher/os/docker"
  17. "github.com/rancher/os/log"
  18. )
  19. const (
  20. userImagesPreloadDirectory = "/var/lib/rancher/preload/docker"
  21. )
  22. func preloadImagesAction(c *cli.Context) error {
  23. err := PreloadImages(docker.NewDefaultClient, userImagesPreloadDirectory)
  24. if err != nil {
  25. log.Errorf("Failed to preload user images: %v", err)
  26. }
  27. return err
  28. }
  29. func shouldLoad(file string) bool {
  30. if strings.HasSuffix(file, ".done") {
  31. return false
  32. }
  33. if _, err := os.Stat(fmt.Sprintf("%s.done", file)); err == nil {
  34. return false
  35. }
  36. return true
  37. }
  38. func PreloadImages(clientFactory func() (dockerClient.APIClient, error), imagesDir string) error {
  39. var client dockerClient.APIClient
  40. clientInitialized := false
  41. if _, err := os.Stat(imagesDir); os.IsNotExist(err) {
  42. if err = os.MkdirAll(imagesDir, 0755); err != nil {
  43. return err
  44. }
  45. } else if err != nil {
  46. return err
  47. }
  48. files, err := ioutil.ReadDir(imagesDir)
  49. if err != nil {
  50. return err
  51. }
  52. for _, file := range files {
  53. filename := path.Join(imagesDir, file.Name())
  54. if !shouldLoad(filename) {
  55. log.Infof("Skipping to preload the file: %s", filename)
  56. continue
  57. }
  58. image, err := os.Open(filename)
  59. if err != nil {
  60. return err
  61. }
  62. defer image.Close()
  63. var imageReader io.Reader
  64. imageReader = image
  65. match, err := regexp.MatchString(".t?gz$", file.Name())
  66. if err != nil {
  67. return err
  68. }
  69. if match {
  70. imageReader, err = gzip.NewReader(image)
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. if !clientInitialized {
  76. client, err = clientFactory()
  77. if err != nil {
  78. return err
  79. }
  80. clientInitialized = true
  81. }
  82. var imageLoadResponse types.ImageLoadResponse
  83. if imageLoadResponse, err = client.ImageLoad(context.Background(), imageReader, false); err != nil {
  84. return err
  85. }
  86. cfg := config.LoadConfig()
  87. if cfg.Rancher.PreloadWait {
  88. if _, err := ioutil.ReadAll(imageLoadResponse.Body); err != nil {
  89. return err
  90. }
  91. }
  92. log.Infof("Finished to load image %s", filename)
  93. log.Infof("Creating done stamp file for image %s", filename)
  94. doneStamp, err := os.Create(fmt.Sprintf("%s.done", filename))
  95. if err != nil {
  96. return err
  97. }
  98. defer doneStamp.Close()
  99. log.Infof("Finished to created the done stamp file for image %s", filename)
  100. }
  101. return nil
  102. }