| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright The containerd Authors. |
| 4 |
|
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
|
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
set -euo pipefail |
| 18 |
|
| 19 |
CONTEXT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/" |
| 20 |
REPO="${CONTEXT}../../" |
| 21 |
|
| 22 |
source "${REPO}/script/util/utils.sh" |
| 23 |
|
| 24 |
GOBASE_VERSION=$(go_base_version "${REPO}/Dockerfile") |
| 25 |
|
| 26 |
# TODO: get the following versions from go.mod once we add them. |
| 27 |
PROTOC_VERSION=3.17.3 |
| 28 |
GOGO_VERSION=v1.3.2 |
| 29 |
|
| 30 |
COMMAND="${1}" |
| 31 |
if [ "${COMMAND}" != "update" ] && [ "${COMMAND}" != "validate" ] ; then |
| 32 |
echo "either of \"update\" or \"validate\" must be specified" |
| 33 |
exit 1 |
| 34 |
fi |
| 35 |
|
| 36 |
TMP_CONTEXT=$(mktemp -d) |
| 37 |
GENERATED_FILES=$(mktemp -d) |
| 38 |
function cleanup { |
| 39 |
local ORG_EXIT_CODE="${1}" |
| 40 |
rm -rf "${GENERATED_FILES}" || true |
| 41 |
rm -rf "${TMP_CONTEXT}" || true |
| 42 |
exit "${ORG_EXIT_CODE}" |
| 43 |
} |
| 44 |
trap 'cleanup "$?"' EXIT SIGHUP SIGINT SIGQUIT SIGTERM |
| 45 |
|
| 46 |
cat <<EOF > "${TMP_CONTEXT}/Dockerfile" |
| 47 |
FROM golang:${GOBASE_VERSION} AS golang-base |
| 48 |
|
| 49 |
ARG PROTOC_VERSION=${PROTOC_VERSION} |
| 50 |
ARG GOGO_VERSION=${GOGO_VERSION} |
| 51 |
ARG TARGETOS TARGETARCH |
| 52 |
|
| 53 |
RUN apt-get update && apt-get --no-install-recommends install -y unzip && \ |
| 54 |
PARCH=\$(echo \${TARGETARCH} | sed -e 's/amd64/x86_64/' -e 's/arm64/aarch_64/') && \ |
| 55 |
wget -O /tmp/protoc.zip \ |
| 56 |
https://github.com/google/protobuf/releases/download/v\${PROTOC_VERSION}/protoc-\${PROTOC_VERSION}-\${TARGETOS}-\${PARCH}.zip && \ |
| 57 |
unzip /tmp/protoc.zip -d /usr/local && \ |
| 58 |
go install github.com/gogo/protobuf/protoc-gen-gogo@\$GOGO_VERSION |
| 59 |
|
| 60 |
WORKDIR /go/src/github.com/containerd/stargz-snapshotter |
| 61 |
|
| 62 |
FROM golang-base AS generate |
| 63 |
COPY . . |
| 64 |
RUN git add -A && \ |
| 65 |
go generate -v ./... && \ |
| 66 |
mkdir /generated-files && \ |
| 67 |
git ls-files -m --others -- **/*.pb.go | tar -cf - --files-from - | tar -C /generated-files -xf - |
| 68 |
|
| 69 |
FROM scratch AS update |
| 70 |
COPY --from=generate /generated-files / |
| 71 |
|
| 72 |
FROM golang-base AS validate |
| 73 |
COPY . . |
| 74 |
RUN git add -A && \ |
| 75 |
go generate -v ./... && \ |
| 76 |
DIFFS=\$(git status --porcelain -- **/*.pb.go 2>/dev/null) ; \ |
| 77 |
if [ "\${DIFFS}" ]; then \ |
| 78 |
echo "Unexpected generated files; Please run \"make generate\"" && \ |
| 79 |
git diff && \ |
| 80 |
echo \${DIFFS} && \ |
| 81 |
exit 1 ; \ |
| 82 |
else \ |
| 83 |
echo OK ; \ |
| 84 |
fi |
| 85 |
EOF |
| 86 |
|
| 87 |
case ${COMMAND} in |
| 88 |
update) |
| 89 |
DOCKER_BUILDKIT=1 docker build --progress=plain --target update -f "${TMP_CONTEXT}/Dockerfile" -o - "${REPO}" | tar -C "${GENERATED_FILES}/" -x |
| 90 |
if ! [ -z "$(ls -A ${GENERATED_FILES}/)" ]; then cp -R "${GENERATED_FILES}/"* "${REPO}" ; fi |
| 91 |
;; |
| 92 |
validate) |
| 93 |
DOCKER_BUILDKIT=1 docker build --progress=plain --target validate -f "${TMP_CONTEXT}/Dockerfile" "${REPO}" |
| 94 |
;; |
| 95 |
esac |