You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.8 KiB

  1. #!/bin/bash
  2. OPTION=$1
  3. CURRENT_DIR=$(cd $(dirname $0); pwd)
  4. TEMP_DIR="${CURRENT_DIR}/temp"
  5. IMAGE_TAR_FILE="${CURRENT_DIR}/container-images.tar.gz"
  6. IMAGE_DIR="${CURRENT_DIR}/container-images"
  7. IMAGE_LIST="${IMAGE_DIR}/container-images.txt"
  8. RETRY_COUNT=5
  9. function create_container_image_tar() {
  10. set -e
  11. IMAGES=$(kubectl describe pods --all-namespaces | grep " Image:" | awk '{print $2}' | sort | uniq)
  12. # NOTE: etcd and pause cannot be seen as pods.
  13. # The pause image is used for --pod-infra-container-image option of kubelet.
  14. EXT_IMAGES=$(kubectl cluster-info dump | egrep "quay.io/coreos/etcd:|k8s.gcr.io/pause:" | sed s@\"@@g)
  15. IMAGES="${IMAGES} ${EXT_IMAGES}"
  16. rm -f ${IMAGE_TAR_FILE}
  17. rm -rf ${IMAGE_DIR}
  18. mkdir ${IMAGE_DIR}
  19. cd ${IMAGE_DIR}
  20. sudo docker pull registry:latest
  21. sudo docker save -o registry-latest.tar registry:latest
  22. for image in ${IMAGES}
  23. do
  24. FILE_NAME="$(echo ${image} | sed s@"/"@"-"@g | sed s/":"/"-"/g)".tar
  25. set +e
  26. for step in $(seq 1 ${RETRY_COUNT})
  27. do
  28. sudo docker pull ${image}
  29. if [ $? -eq 0 ]; then
  30. break
  31. fi
  32. echo "Failed to pull ${image} at step ${step}"
  33. if [ ${step} -eq ${RETRY_COUNT} ]; then
  34. exit 1
  35. fi
  36. done
  37. set -e
  38. sudo docker save -o ${FILE_NAME} ${image}
  39. # NOTE: Here removes the following repo parts from each image
  40. # so that these parts will be replaced with Kubespray.
  41. # - kube_image_repo: "k8s.gcr.io"
  42. # - gcr_image_repo: "gcr.io"
  43. # - docker_image_repo: "docker.io"
  44. # - quay_image_repo: "quay.io"
  45. FIRST_PART=$(echo ${image} | awk -F"/" '{print $1}')
  46. if [ "${FIRST_PART}" = "k8s.gcr.io" ] ||
  47. [ "${FIRST_PART}" = "gcr.io" ] ||
  48. [ "${FIRST_PART}" = "docker.io" ] ||
  49. [ "${FIRST_PART}" = "quay.io" ]; then
  50. image=$(echo ${image} | sed s@"${FIRST_PART}/"@@)
  51. fi
  52. echo "${FILE_NAME} ${image}" >> ${IMAGE_LIST}
  53. done
  54. cd ..
  55. sudo chown ${USER} ${IMAGE_DIR}/*
  56. tar -zcvf ${IMAGE_TAR_FILE} ./container-images
  57. rm -rf ${IMAGE_DIR}
  58. echo ""
  59. echo "${IMAGE_TAR_FILE} is created to contain your container images."
  60. echo "Please keep this file and bring it to your offline environment."
  61. }
  62. function register_container_images() {
  63. if [ ! -f ${IMAGE_TAR_FILE} ]; then
  64. echo "${IMAGE_TAR_FILE} should exist."
  65. exit 1
  66. fi
  67. if [ ! -d ${TEMP_DIR} ]; then
  68. mkdir ${TEMP_DIR}
  69. fi
  70. # To avoid "http: server gave http response to https client" error.
  71. LOCALHOST_NAME=$(hostname)
  72. if [ -d /etc/docker/ ]; then
  73. set -e
  74. # Ubuntu18.04, RHEL7/CentOS7
  75. cp ${CURRENT_DIR}/docker-daemon.json ${TEMP_DIR}/docker-daemon.json
  76. sed -i s@"HOSTNAME"@"${LOCALHOST_NAME}"@ ${TEMP_DIR}/docker-daemon.json
  77. sudo cp ${TEMP_DIR}/docker-daemon.json /etc/docker/daemon.json
  78. elif [ -d /etc/containers/ ]; then
  79. set -e
  80. # RHEL8/CentOS8
  81. cp ${CURRENT_DIR}/registries.conf ${TEMP_DIR}/registries.conf
  82. sed -i s@"HOSTNAME"@"${LOCALHOST_NAME}"@ ${TEMP_DIR}/registries.conf
  83. sudo cp ${TEMP_DIR}/registries.conf /etc/containers/registries.conf
  84. else
  85. echo "docker package(docker-ce, etc.) should be installed"
  86. exit 1
  87. fi
  88. tar -zxvf ${IMAGE_TAR_FILE}
  89. sudo docker load -i ${IMAGE_DIR}/registry-latest.tar
  90. sudo docker run --restart=always -d -p 5000:5000 --name registry registry:latest
  91. set +e
  92. set -e
  93. while read -r line; do
  94. file_name=$(echo ${line} | awk '{print $1}')
  95. org_image=$(echo ${line} | awk '{print $2}')
  96. new_image="${LOCALHOST_NAME}:5000/${org_image}"
  97. image_id=$(tar -tf ${IMAGE_DIR}/${file_name} | grep "\.json" | grep -v manifest.json | sed s/"\.json"//)
  98. sudo docker load -i ${IMAGE_DIR}/${file_name}
  99. sudo docker tag ${image_id} ${new_image}
  100. sudo docker push ${new_image}
  101. done <<< "$(cat ${IMAGE_LIST})"
  102. echo "Succeeded to register container images to local registry."
  103. echo "Please specify ${LOCALHOST_NAME}:5000 for the following options in your inventry:"
  104. echo "- kube_image_repo"
  105. echo "- gcr_image_repo"
  106. echo "- docker_image_repo"
  107. echo "- quay_image_repo"
  108. }
  109. if [ "${OPTION}" == "create" ]; then
  110. create_container_image_tar
  111. elif [ "${OPTION}" == "register" ]; then
  112. register_container_images
  113. else
  114. echo "This script has two features:"
  115. echo "(1) Get container images from an environment which is deployed online."
  116. echo "(2) Deploy local container registry and register the container images to the registry."
  117. echo ""
  118. echo "Step(1) should be done online site as a preparation, then we bring"
  119. echo "the gotten images to the target offline environment."
  120. echo "Then we will run step(2) for registering the images to local registry."
  121. echo ""
  122. echo "${IMAGE_TAR_FILE} is created to contain your container images."
  123. echo "Please keep this file and bring it to your offline environment."
  124. echo ""
  125. echo "Step(1) can be operated with:"
  126. echo " $ ./manage-offline-container-images.sh create"
  127. echo ""
  128. echo "Step(2) can be operated with:"
  129. echo " $ ./manage-offline-container-images.sh register"
  130. echo ""
  131. echo "Please specify 'create' or 'register'."
  132. echo ""
  133. exit 1
  134. fi