xref: /aosp_15_r20/external/grpc-grpc/tools/dockerfile/push_testing_images.sh (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/bin/bash
2# Copyright 2016 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Builds selected testing docker images and pushes them to artifact registry.
17# NOTE: These images are not intended to be used by gRPC end users,
18# they simply provide an easily reproducible environment for running gRPC
19# tests.
20
21set -ex
22
23cd $(dirname $0)/../..
24git_root=$(pwd)
25cd -
26
27# Recognized env variables that can be used as params.
28#  LOCAL_ONLY_MODE: if set (e.g. LOCAL_ONLY_MODE=true), script will only operate locally and it won't query artifact registry and won't upload to it.
29#  CHECK_MODE: if set, the script will check that all the .current_version files are up-to-date (used by sanity tests).
30#  SKIP_UPLOAD: if set, script won't push docker images it built to artifact registry.
31#  TRANSFER_FROM_DOCKERHUB: if set, will attempt to grab docker images missing in artifact registry from dockerhub instead of building them from scratch locally.
32
33# How to configure docker before running this script for the first time:
34# Configure docker:
35# $ gcloud auth configure-docker us-docker.pkg.dev
36# Login with gcloud:
37# $ gcloud auth login
38
39# Various check that the environment is setup correctly.
40# The enviroment checks are skipped when running as a sanity check on CI.
41if [ "${CHECK_MODE}" == "" ]
42then
43  # Check that docker is installed and sudoless docker works.
44  docker run --rm -it debian:11 bash -c 'echo "sudoless docker run works!"' || \
45      (echo "Error: docker not installed or sudoless docker doesn't work?" && exit 1)
46
47  # Some of the images we build are for arm64 architecture and the easiest
48  # way of allowing them to build locally on x64 machine is to use
49  # qemu binfmt-misc hook that automatically runs arm64 binaries under
50  # an emulator.
51  # Perform a check that "qemu-user-static" with binfmt-misc hook
52  # is installed, to give an early warning (otherwise building arm64 images won't work)
53  docker run --rm -it arm64v8/debian:11 bash -c 'echo "able to run arm64 docker images with an emulator!"' || \
54      (echo "Error: can't run arm64 images under an emulator. Have you run 'sudo apt-get install qemu-user-static'?" && exit 1)
55fi
56
57ARTIFACT_REGISTRY_PREFIX=us-docker.pkg.dev/grpc-testing/testing-images-public
58
59# all dockerfile definitions we use for testing and for which we push an image to the registry
60ALL_DOCKERFILE_DIRS=(
61  tools/dockerfile/test/*
62  tools/dockerfile/grpc_artifact_*
63  tools/dockerfile/interoptest/*
64  tools/dockerfile/distribtest/*
65  third_party/rake-compiler-dock/*
66)
67
68CHECK_FAILED=""
69
70if [ "${CHECK_MODE}" != "" ]
71then
72  # Check that there are no stale .current_version files (for which the corresponding
73  # dockerfile_dir doesn't exist anymore).
74  for CURRENTVERSION_FILE in $(find tools/ third_party/rake-compiler-dock -name '*.current_version')
75  do
76    DOCKERFILE_DIR="$(echo ${CURRENTVERSION_FILE} | sed 's/.current_version$//')"
77    if [ ! -e "${DOCKERFILE_DIR}/Dockerfile" ]
78    then
79       echo "Found that ${DOCKERFILE_DIR} has '.current_version' file but there is no corresponding Dockerfile."
80       echo "Should the ${CURRENTVERSION_FILE} file be deleted?"
81       CHECK_FAILED=true
82    fi
83  done
84fi
85
86for DOCKERFILE_DIR in "${ALL_DOCKERFILE_DIRS[@]}"
87do
88  # Generate image name based on Dockerfile checksum. That works well as long
89  # as can count on dockerfiles being written in a way that changing the logical
90  # contents of the docker image always changes the SHA (e.g. using "ADD file"
91  # cmd in the dockerfile in not ok as contents of the added file will not be
92  # reflected in the SHA).
93  DOCKER_IMAGE_NAME=$(basename $DOCKERFILE_DIR)
94
95  if [ ! -e "$DOCKERFILE_DIR/Dockerfile" ]; then
96    continue
97  else
98    DOCKER_IMAGE_TAG=$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
99  fi
100
101  echo "Visiting ${DOCKERFILE_DIR}"
102
103  if [ "${LOCAL_ONLY_MODE}" == "" ]
104  then
105    # value obtained here corresponds to the "RepoDigests" from "docker image inspect", but without the need to actually pull the image
106    DOCKER_IMAGE_DIGEST_REMOTE=$(gcloud artifacts docker images describe "${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" --format=json | jq -r '.image_summary.digest')
107
108    if [ "${DOCKER_IMAGE_DIGEST_REMOTE}" != "" ]
109    then
110      # skip building the image if it already exists in the destination registry
111      echo "Docker image ${DOCKER_IMAGE_NAME} already exists in artifact registry at the right version (tag ${DOCKER_IMAGE_TAG})."
112
113      VERSION_FILE_OUT_OF_DATE=""
114      grep "^${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}@${DOCKER_IMAGE_DIGEST_REMOTE}$" ${DOCKERFILE_DIR}.current_version >/dev/null || VERSION_FILE_OUT_OF_DATE="true"
115
116      if [ "${VERSION_FILE_OUT_OF_DATE}" == "" ]
117      then
118        echo "Version file for ${DOCKER_IMAGE_NAME} is in sync with info from artifact registry."
119        continue
120      fi
121
122      if [ "${CHECK_MODE}" != "" ]
123      then
124        echo "CHECK FAILED: Version file ${DOCKERFILE_DIR}.current_version is not in sync with info from artifact registry."
125        CHECK_FAILED=true
126        continue
127      fi
128
129      # update info on what we consider to be the current version of the docker image (which will be used to run tests)
130      # we consider the sha256 image digest info from the artifact registry to be the canonical one
131      echo -n "${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}@${DOCKER_IMAGE_DIGEST_REMOTE}" >${DOCKERFILE_DIR}.current_version
132
133      continue
134    fi
135
136    if [ "${CHECK_MODE}" != "" ]
137    then
138      echo "CHECK FAILED: Docker image ${DOCKER_IMAGE_NAME} not found in artifact registry."
139      CHECK_FAILED=true
140      continue
141    fi
142
143  else
144    echo "Skipped querying artifact registry (running in local-only mode)."
145  fi
146
147  # if the .current_version file doesn't exist or it doesn't contain the right SHA checksum,
148  # it is out of date and we will need to rebuild the docker image locally.
149  LOCAL_BUILD_REQUIRED=""
150  grep "^${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}@sha256:.*$" ${DOCKERFILE_DIR}.current_version >/dev/null || LOCAL_BUILD_REQUIRED=true
151
152  # If the current version file has contains SHA checksum, but not the remote image digest,
153  # it means the locally-built image hasn't been pushed to artifact registry yet.
154  DIGEST_MISSING_IN_CURRENT_VERSION_FILE=""
155  grep "^${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}$" ${DOCKERFILE_DIR}.current_version >/dev/null && DIGEST_MISSING_IN_CURRENT_VERSION_FILE=true
156
157  if [ "${LOCAL_BUILD_REQUIRED}" == "" ]
158  then
159    echo "Dockerfile for ${DOCKER_IMAGE_NAME} hasn't changed. Will skip 'docker build'."
160    continue
161  fi
162
163  if [ "${CHECK_MODE}" != "" ] && [ "${DIGEST_MISSING_IN_CURRENT_VERSION_FILE}" != "" ]
164  then
165    echo "CHECK FAILED: Dockerfile for ${DOCKER_IMAGE_NAME} has changed and was built locally, but looks like it hasn't been pushed."
166    CHECK_FAILED=true
167    continue
168  fi
169
170  if [ "${CHECK_MODE}" != "" ]
171  then
172    echo "CHECK FAILED: Dockerfile for ${DOCKER_IMAGE_NAME} has changed, but the ${DOCKERFILE_DIR}.current_version is not up to date."
173    CHECK_FAILED=true
174    continue
175  fi
176
177  if [ "${TRANSFER_FROM_DOCKERHUB}" == "" ]
178  then
179    echo "Running 'docker build' for ${DOCKER_IMAGE_NAME}"
180    echo "=========="
181    docker build -t ${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} ${DOCKERFILE_DIR}
182    echo "=========="
183  else
184    # TRANSFER_FROM_DOCKERHUB is a temporary feature that pulls the corresponding image from dockerhub instead
185    # of building it from scratch locally. This should simplify the dockerhub -> artifact registry migration.
186    # TODO(jtattermusch): remove this feature in Q1 2023.
187    DOCKERHUB_ORGANIZATION=grpctesting
188    # pull image from dockerhub
189    docker pull ${DOCKERHUB_ORGANIZATION}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}
190    # add the artifact registry tag
191    docker tag ${DOCKERHUB_ORGANIZATION}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} ${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}
192  fi
193
194  # After building the docker image locally, we don't know the image's RepoDigest (which is distinct from image's "Id" digest) yet
195  # so we can only update the .current_version file with the image tag (which will be enough for running tests under docker locally).
196  # The .current_version file will be updated with both tag and SHA256 repo digest later, once we actually push it.
197  # See b/278226801 for context.
198  echo -n "${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" >${DOCKERFILE_DIR}.current_version
199
200  if [ "${SKIP_UPLOAD}" == "" ] && [ "${LOCAL_ONLY_MODE}" == "" ]
201  then
202    docker push ${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}
203
204    # After successful push, the image's RepoDigest info will become available in "docker image inspect",
205    # so we update the .current_version file with the repo digest.
206    DOCKER_IMAGE_DIGEST_REMOTE=$(docker image inspect "${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" | jq -e -r ".[0].RepoDigests[] | select(contains(\"${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}@\"))" | sed 's/^.*@sha256:/sha256:/')
207    echo -n "${ARTIFACT_REGISTRY_PREFIX}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}@${DOCKER_IMAGE_DIGEST_REMOTE}" >${DOCKERFILE_DIR}.current_version
208  fi
209done
210
211if [ "${CHECK_MODE}" != "" ]
212then
213    # Check that dockerimage_current_versions.bzl is up to date.
214    CHECK_MODE="${CHECK_MODE}" tools/bazelify_tests/generate_dockerimage_current_versions_bzl.sh || CHECK_FAILED=true
215else
216    # Regenerate dockerimage_current_versions.bzl
217    tools/bazelify_tests/generate_dockerimage_current_versions_bzl.sh
218fi
219
220if [ "${CHECK_FAILED}" != "" ]
221then
222  echo "ERROR: Some checks have failed."
223  exit 1
224fi
225
226echo "All done."
227