load-images.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. offline_data_path=/opt/yunion/upgrade
  3. export loaded_path=${offline_data_path}/images/loaded
  4. enable_parallel=${enable_parallel:-false}
  5. if k3s --version > /dev/null 2>&1; then
  6. enable_parallel=false
  7. fi
  8. load_and_mv(){
  9. local img=$1
  10. if [ ! -d $loaded_path ]; then
  11. mkdir -p $loaded_path
  12. fi
  13. MAX_RETRY=50
  14. RETRY=0
  15. while [ $RETRY -lt $MAX_RETRY ]; do
  16. if k3s --version > /dev/null 2>&1; then
  17. xzcat $img | k3s ctr images import -
  18. else
  19. docker load -i $img
  20. fi
  21. if [ $? -eq 0 ]; then
  22. break
  23. fi
  24. RETRY=$((RETRY+1))
  25. sleep $RETRY
  26. done
  27. if [ $RETRY -eq $MAX_RETRY ]; then
  28. echo "[ERROR] load image $img failed after $MAX_RETRY retries"
  29. exit 1
  30. fi
  31. mv $img $loaded_path
  32. }
  33. export -f load_and_mv
  34. if [ ! -d $offline_data_path/images ]; then
  35. echo "no images path found under the offline data path. "
  36. exit 0
  37. fi
  38. count=$(find $offline_data_path/images -maxdepth 1 -type f -name '*tar.xz' |wc -l)
  39. if [ "$count" -eq 0 ]; then
  40. echo "no images to load. "
  41. exit 0
  42. fi
  43. if [ -x /usr/bin/parallel ] && [ "$enable_parallel" -eq "true" ]; then
  44. parallel --will-cite load_and_mv ::: $offline_data_path/images/*.tar.xz
  45. else
  46. for i in $offline_data_path/images/*.tar.xz
  47. do
  48. load_and_mv $i
  49. done
  50. fi
  51. version_file=${offline_data_path}/versions.json
  52. if ! [ -f "$version_file" ]; then
  53. echo "[ERROR] version file $version_file is empty! "
  54. exit 1
  55. fi
  56. imgs=( $(cat $version_file |jq '.dockers |to_entries[] |.key +":"+ .value' | xargs) )
  57. echo imgs ${imgs[@]}
  58. registry=$(cat $version_file | jq .registry |xargs)
  59. if [ -z "$registry" ]; then
  60. echo "[ERROR] registry is empty!"
  61. exit 1
  62. fi
  63. echo registry $registry
  64. tag_image(){
  65. local img_name_version=$1
  66. local insecure_registry="private-registry.onecloud:5000"
  67. MAX_RETRY=50
  68. RETRY=0
  69. while [ $RETRY -lt $MAX_RETRY ]; do
  70. if k3s --version > /dev/null 2>&1; then
  71. k3s ctr images ls | grep -w "$insecure_registry/$registry/$img_name_version"
  72. if [ $? -ne 0 ]; then
  73. k3s ctr images tag registry.cn-beijing.aliyuncs.com/$registry/$img_name_version \
  74. $insecure_registry/$registry/$img_name_version
  75. fi
  76. else
  77. docker tag registry.cn-beijing.aliyuncs.com/$registry/$img_name_version \
  78. $insecure_registry/$registry/$img_name_version
  79. fi
  80. if [ $? -eq 0 ]; then
  81. break
  82. fi
  83. RETRY=$((RETRY+1))
  84. sleep $RETRY
  85. done
  86. if [ $RETRY -eq $MAX_RETRY ]; then
  87. echo "[ERROR] tag image $img_name_version failed after $MAX_RETRY retries"
  88. exit 1
  89. fi
  90. }
  91. export -f tag_image
  92. for img in "${imgs[@]}"
  93. do
  94. registry=$registry tag_image $img
  95. done
  96. # rm -fv /tmp/load-images.sh