preload.sh 959 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. set -e
  3. BASE=${1:-${PRELOAD_DIR}}
  4. BASE=${BASE:-/mnt/preload}
  5. should_load() {
  6. file=${1}
  7. if [[ ${file} =~ \.done$ ]]; then echo false
  8. elif [ -f ${file} ]; then
  9. if [[ ${file} -nt ${file}.done ]]; then echo true
  10. else echo false
  11. fi
  12. else echo false
  13. fi
  14. }
  15. if [ -d ${BASE} ]; then
  16. echo Preloading docker images from ${BASE}...
  17. for file in $(ls ${BASE}); do
  18. path=${BASE}/${file}
  19. loading=$(should_load ${path})
  20. if [ ${loading} == "true" ]; then
  21. CAT="cat ${path}"
  22. if [[ ${file} =~ \.t?gz$ ]]; then CAT="${CAT} | gunzip"; fi
  23. if [[ ${file} =~ \.t?xz$ ]]; then CAT="${CAT} | unxz"; fi
  24. CAT="${CAT} | docker load"
  25. echo loading from ${path}
  26. eval ${CAT} || :
  27. touch ${path}.done || :
  28. fi
  29. done
  30. echo Done.
  31. else
  32. echo Can not preload images from ${BASE}: not a dir or does not exist.
  33. fi