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.

172 lines
5.5 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:|registry.k8s.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: "registry.k8s.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}" = "registry.k8s.io" ] ||
  47. [ "${FIRST_PART}" = "gcr.io" ] ||
  48. [ "${FIRST_PART}" = "docker.io" ] ||
  49. [ "${FIRST_PART}" = "quay.io" ] ||
  50. [ "${FIRST_PART}" = "${PRIVATE_REGISTRY}" ]; then
  51. image=$(echo ${image} | sed s@"${FIRST_PART}/"@@)
  52. fi
  53. echo "${FILE_NAME} ${image}" >> ${IMAGE_LIST}
  54. done
  55. cd ..
  56. sudo chown ${USER} ${IMAGE_DIR}/*
  57. tar -zcvf ${IMAGE_TAR_FILE} ./container-images
  58. rm -rf ${IMAGE_DIR}
  59. echo ""
  60. echo "${IMAGE_TAR_FILE} is created to contain your container images."
  61. echo "Please keep this file and bring it to your offline environment."
  62. }
  63. function register_container_images() {
  64. if [ ! -f ${IMAGE_TAR_FILE} ]; then
  65. echo "${IMAGE_TAR_FILE} should exist."
  66. exit 1
  67. fi
  68. if [ ! -d ${TEMP_DIR} ]; then
  69. mkdir ${TEMP_DIR}
  70. fi
  71. # To avoid "http: server gave http response to https client" error.
  72. LOCALHOST_NAME=$(hostname)
  73. if [ -d /etc/docker/ ]; then
  74. set -e
  75. # Ubuntu18.04, RHEL7/CentOS7
  76. cp ${CURRENT_DIR}/docker-daemon.json ${TEMP_DIR}/docker-daemon.json
  77. sed -i s@"HOSTNAME"@"${LOCALHOST_NAME}"@ ${TEMP_DIR}/docker-daemon.json
  78. sudo cp ${TEMP_DIR}/docker-daemon.json /etc/docker/daemon.json
  79. elif [ -d /etc/containers/ ]; then
  80. set -e
  81. # RHEL8/CentOS8
  82. cp ${CURRENT_DIR}/registries.conf ${TEMP_DIR}/registries.conf
  83. sed -i s@"HOSTNAME"@"${LOCALHOST_NAME}"@ ${TEMP_DIR}/registries.conf
  84. sudo cp ${TEMP_DIR}/registries.conf /etc/containers/registries.conf
  85. else
  86. echo "docker package(docker-ce, etc.) should be installed"
  87. exit 1
  88. fi
  89. tar -zxvf ${IMAGE_TAR_FILE}
  90. sudo docker load -i ${IMAGE_DIR}/registry-latest.tar
  91. set +e
  92. sudo docker container inspect registry >/dev/null 2>&1
  93. if [ $? -ne 0 ]; then
  94. sudo docker run --restart=always -d -p 5000:5000 --name registry registry:latest
  95. fi
  96. set -e
  97. while read -r line; do
  98. file_name=$(echo ${line} | awk '{print $1}')
  99. raw_image=$(echo ${line} | awk '{print $2}')
  100. new_image="${LOCALHOST_NAME}:5000/${raw_image}"
  101. org_image=$(sudo docker load -i ${IMAGE_DIR}/${file_name} | head -n1 | awk '{print $3}')
  102. image_id=$(sudo docker image inspect ${org_image} | grep "\"Id\":" | awk -F: '{print $3}'| sed s/'\",'//)
  103. if [ -z "${file_name}" ]; then
  104. echo "Failed to get file_name for line ${line}"
  105. exit 1
  106. fi
  107. if [ -z "${raw_image}" ]; then
  108. echo "Failed to get raw_image for line ${line}"
  109. exit 1
  110. fi
  111. if [ -z "${org_image}" ]; then
  112. echo "Failed to get org_image for line ${line}"
  113. exit 1
  114. fi
  115. if [ -z "${image_id}" ]; then
  116. echo "Failed to get image_id for file ${file_name}"
  117. exit 1
  118. fi
  119. sudo docker load -i ${IMAGE_DIR}/${file_name}
  120. sudo docker tag ${image_id} ${new_image}
  121. sudo docker push ${new_image}
  122. done <<< "$(cat ${IMAGE_LIST})"
  123. echo "Succeeded to register container images to local registry."
  124. echo "Please specify ${LOCALHOST_NAME}:5000 for the following options in your inventry:"
  125. echo "- kube_image_repo"
  126. echo "- gcr_image_repo"
  127. echo "- docker_image_repo"
  128. echo "- quay_image_repo"
  129. }
  130. if [ "${OPTION}" == "create" ]; then
  131. create_container_image_tar
  132. elif [ "${OPTION}" == "register" ]; then
  133. register_container_images
  134. else
  135. echo "This script has two features:"
  136. echo "(1) Get container images from an environment which is deployed online."
  137. echo "(2) Deploy local container registry and register the container images to the registry."
  138. echo ""
  139. echo "Step(1) should be done online site as a preparation, then we bring"
  140. echo "the gotten images to the target offline environment. if images are from"
  141. echo "a private registry, you need to set PRIVATE_REGISTRY environment variable."
  142. echo "Then we will run step(2) for registering the images to local registry."
  143. echo ""
  144. echo "${IMAGE_TAR_FILE} is created to contain your container images."
  145. echo "Please keep this file and bring it to your offline environment."
  146. echo ""
  147. echo "Step(1) can be operated with:"
  148. echo " $ ./manage-offline-container-images.sh create"
  149. echo ""
  150. echo "Step(2) can be operated with:"
  151. echo " $ ./manage-offline-container-images.sh register"
  152. echo ""
  153. echo "Please specify 'create' or 'register'."
  154. echo ""
  155. exit 1
  156. fi