preload.go 2.2 KB

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