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.

170 lines
5.4 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. set +e
  91. sudo docker container inspect registry >/dev/null 2>&1
  92. if [ $? -ne 0 ]; then
  93. sudo docker run --restart=always -d -p 5000:5000 --name registry registry:latest
  94. fi
  95. set -e
  96. while read -r line; do
  97. file_name=$(echo ${line} | awk '{print $1}')
  98. raw_image=$(echo ${line} | awk '{print $2}')
  99. new_image="${LOCALHOST_NAME}:5000/${raw_image}"
  100. org_image=$(sudo docker load -i ${IMAGE_DIR}/${file_name} | head -n1 | awk '{print $3}')
  101. image_id=$(sudo docker image inspect ${org_image} | grep "\"Id\":" | awk -F: '{print $3}'| sed s/'\",'//)
  102. if [ -z "${file_name}" ]; then
  103. echo "Failed to get file_name for line ${line}"
  104. exit 1
  105. fi
  106. if [ -z "${raw_image}" ]; then
  107. echo "Failed to get raw_image for line ${line}"
  108. exit 1
  109. fi
  110. if [ -z "${org_image}" ]; then
  111. echo "Failed to get org_image for line ${line}"
  112. exit 1
  113. fi
  114. if [ -z "${image_id}" ]; then
  115. echo "Failed to get image_id for file ${file_name}"
  116. exit 1
  117. fi
  118. sudo docker load -i ${IMAGE_DIR}/${file_name}
  119. sudo docker tag ${image_id} ${new_image}
  120. sudo docker push ${new_image}
  121. done <<< "$(cat ${IMAGE_LIST})"
  122. echo "Succeeded to register container images to local registry."
  123. echo "Please specify ${LOCALHOST_NAME}:5000 for the following options in your inventry:"
  124. echo "- kube_image_repo"
  125. echo "- gcr_image_repo"
  126. echo "- docker_image_repo"
  127. echo "- quay_image_repo"
  128. }
  129. if [ "${OPTION}" == "create" ]; then
  130. create_container_image_tar
  131. elif [ "${OPTION}" == "register" ]; then
  132. register_container_images
  133. else
  134. echo "This script has two features:"
  135. echo "(1) Get container images from an environment which is deployed online."
  136. echo "(2) Deploy local container registry and register the container images to the registry."
  137. echo ""
  138. echo "Step(1) should be done online site as a preparation, then we bring"
  139. echo "the gotten images to the target offline environment."
  140. echo "Then we will run step(2) for registering the images to local registry."
  141. echo ""
  142. echo "${IMAGE_TAR_FILE} is created to contain your container images."
  143. echo "Please keep this file and bring it to your offline environment."
  144. echo ""
  145. echo "Step(1) can be operated with:"
  146. echo " $ ./manage-offline-container-images.sh create"
  147. echo ""
  148. echo "Step(2) can be operated with:"
  149. echo " $ ./manage-offline-container-images.sh register"
  150. echo ""
  151. echo "Please specify 'create' or 'register'."
  152. echo ""
  153. exit 1
  154. fi