Debian Codegraph @

script/util/utils.sh

raw
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 # Preparing creds for provided user and password for private registry(only for testing purpose)
18 # See also: https://docs.docker.com/registry/deploying/
19 function prepare_creds {
20 local OUTPUT="${1}"
21 local REGISTRY_HOST="${2}"
22 local USER="${3}"
23 local PASS="${4}"
24 mkdir "${OUTPUT}/auth" "${OUTPUT}/certs"
25 openssl req -subj "/C=JP/ST=Remote/L=Snapshotter/O=TestEnv/OU=Integration/CN=${REGISTRY_HOST}" \
26 -addext "subjectAltName = DNS:${REGISTRY_HOST}" \
27 -newkey rsa:2048 -nodes -keyout "${OUTPUT}/certs/domain.key" \
28 -x509 -days 365 -out "${OUTPUT}/certs/domain.crt"
29 htpasswd -Bbn "${USER}" "${PASS}" > "${OUTPUT}/auth/htpasswd"
30 }
31
32 # Check if all snapshots logged in the specified file are prepared as remote snapshots.
33 # Whether a snapshot is prepared as a remote snapshot must be logged with the key
34 # "remote-snapshot-prepared" in JSON-formatted log.
35 # See also /snapshot/snapshot.go in this repo.
36 LOG_REMOTE_SNAPSHOT="remote-snapshot-prepared"
37 function check_remote_snapshots {
38 local LOG_FILE="${1}"
39 local REMOTE=0
40 local LOCAL=0
41
42 REMOTE=$(jq -r 'select(."'"${LOG_REMOTE_SNAPSHOT}"'" == "true")' "${LOG_FILE}" | wc -l)
43 LOCAL=$(jq -r 'select(."'"${LOG_REMOTE_SNAPSHOT}"'" == "false")' "${LOG_FILE}" | wc -l)
44 if [[ ${LOCAL} -gt 0 ]] ; then
45 echo "some local snapshots creation have been reported (local:${LOCAL},remote:${REMOTE})"
46 return 1
47 elif [[ ${REMOTE} -gt 0 ]] ; then
48 echo "all layers have been reported as remote snapshots (local:${LOCAL},remote:${REMOTE})"
49 return 0
50 else
51 echo "no log for checking remote snapshot was provided; Is the log-level = debug?"
52 return 1
53 fi
54 }
55
56 # Get version from ARG directive in the specified Dockerfile.
57 function get_version_from_arg {
58 local DOCKERFILE="${1}"
59 local ARGNAME="${2}"
60 cat "${DOCKERFILE}" | grep "${ARGNAME}=" | head -1 | sed -E 's/ARG +'"${ARGNAME}"'=v?([^ ]+).*/\1/g' | tr -d '\n'
61 }
62
63 # Get version of golang base image from the specified Dockerfile.
64 function go_base_version {
65 local DOCKERFILE="${1}"
66 cat "${DOCKERFILE}" | grep -E 'FROM\s+golang:' | head -1 | sed -E 's/FROM +[^:]*:([^ ]+).*/\1/g' | tr -d '\n'
67 }