|
6 | 6 | # which is available at https://www.eclipse.org/legal/epl-2.0/
|
7 | 7 | #
|
8 | 8 | # SPDX-License-Identifier: EPL-2.0
|
9 |
| -# |
10 |
| -# Updates plugin runner images to point a registry defined by environment |
11 |
| -# variables |
12 |
| -# CHE_SIDECAR_CONTAINERS_REGISTRY_URL |
13 |
| -# CHE_SIDECAR_CONTAINERS_REGISTRY_ORGANIZATION |
14 |
| -# CHE_SIDECAR_CONTAINERS_REGISTRY_TAG |
15 |
| -# |
16 |
| -# By default, this script will operate on the `/var/www/html/v3` directory. |
17 |
| -# This can be overridden by the environment variable $METAS_DIR |
18 |
| -# |
19 |
| -# Will execute any arguments on completion (`exec $@`) |
20 | 9 |
|
21 | 10 | set -e
|
22 | 11 |
|
23 |
| -REGISTRY=${CHE_SIDECAR_CONTAINERS_REGISTRY_URL} |
24 |
| -ORGANIZATION=${CHE_SIDECAR_CONTAINERS_REGISTRY_ORGANIZATION} |
25 |
| -TAG=${CHE_SIDECAR_CONTAINERS_REGISTRY_TAG} |
26 |
| - |
27 |
| -DEFAULT_METAS_DIR="/var/www/html/v3" |
28 |
| -METAS_DIR="${METAS_DIR:-${DEFAULT_METAS_DIR}}" |
29 |
| - |
30 |
| -# Regex used to break an image reference into groups: |
31 |
| -# \1 - Whitespace and (optional) quotation preceding image reference |
32 |
| -# \2 - Registry portion of image, e.g. (quay.io)/che-incubator/che-code:tag |
33 |
| -# \3 - Organization portion of image, e.g. quay.io/(eclipse)/che-code:tag |
34 |
| -# \4 - Image name portion of image, e.g. quay.io/che-incubator/(che-code):tag |
35 |
| -# \5 - Optional image digest identifier (empty for tags), e.g. quay.io/che-incubator/che-code(@sha256):digest |
36 |
| -# \6 - Tag of image or digest, e.g. quay.io/che-incubator/che-code:(tag) |
37 |
| -# \7 - Optional quotation following image reference |
38 |
| -IMAGE_REGEX="([[:space:]>-]*[\r]?[[:space:]]*[\"']?)([._:a-zA-Z0-9-]*)/([._a-zA-Z0-9-]*)/([._a-zA-Z0-9-]*)(@sha256)?:([._a-zA-Z0-9-]*)([\"']?)" |
39 |
| - |
40 | 12 |
|
41 | 13 | function run_main() {
|
42 | 14 |
|
43 |
| - extract_and_use_related_images_env_variables_with_image_digest_info |
44 |
| - |
45 |
| - update_container_image_references |
46 |
| - |
47 | 15 | # Add current (arbitrary) user to /etc/passwd and /etc/group
|
48 | 16 | if ! whoami &> /dev/null; then
|
49 | 17 | if [ -w /etc/passwd ]; then
|
@@ -74,99 +42,6 @@ function run_main() {
|
74 | 42 |
|
75 | 43 | }
|
76 | 44 |
|
77 |
| -function extract_and_use_related_images_env_variables_with_image_digest_info() { |
78 |
| - # Extract and use env variables with image digest information. |
79 |
| - # Env variable name format: |
80 |
| - # RELATED_IMAGES_(Image_name)_(Image_label)_(Encoded_base32_image_tag) |
81 |
| - # Where are: |
82 |
| - # "Image_name" - image name. Not valid chars for env variable name replaced to '_'. |
83 |
| - # "Image_label" - image target, for example 'plugin_registry_image'. |
84 |
| - # "Encoded_base32_image_tag_" - original image tag encoded to base32, to avoid invalid for env name chars. base32 alphabet has only |
85 |
| - # one invalid character for env name: '='. That's why it was replaced to '_'. |
86 |
| - # INFO: "=" for base32 it is pad character. If encoded string contains this char(s), then it is always located at the end of the string. |
87 |
| - # Env value it is image with digest to use. |
88 |
| - # Example env variable: |
89 |
| - # RELATED_IMAGE_che_sidecar_clang_plugin_registry_image_HAWTQM3BMRRDGYIK="quay.io/eclipse/che-sidecar-clang@sha256:1c217f34ca69108fdd1ab844c0bcf960edff92519677bde4f8a5f4841b104745" |
90 |
| - if env | grep -q ".*plugin_registry_image.*"; then |
91 |
| - declare -A imageMap |
92 |
| - readarray -t ENV_IMAGES < <(env | grep ".*plugin_registry_image.*") |
93 |
| - for imageEnv in "${ENV_IMAGES[@]}"; do |
94 |
| - tagOrDigest=$(echo "${imageEnv}" | sed -e 's;.*registry_image_\(.*\)=.*;\1;' | tr _ = | base32 -d) |
95 |
| - if [[ ${tagOrDigest} == *"@"* ]]; then |
96 |
| - # Well, image was "freezed", because it already has got digest, so do nothing. |
97 |
| - continue |
98 |
| - fi |
99 |
| - imageWithDigest=${imageEnv#*=}; |
100 |
| - if [[ -n "${tagOrDigest}" ]]; then |
101 |
| - imageToReplace="${imageWithDigest%@*}:${tagOrDigest}" |
102 |
| - else |
103 |
| - imageToReplace="${imageWithDigest%@*}" |
104 |
| - fi |
105 |
| - digest="@${imageWithDigest#*@}" |
106 |
| - imageMap["${imageToReplace}"]="${digest}" |
107 |
| - done |
108 |
| - |
109 |
| - echo "--------------------------Digest map--------------------------" |
110 |
| - for KEY in "${!imageMap[@]}"; do |
111 |
| - echo "Key: $KEY Value: ${imageMap[${KEY}]}" |
112 |
| - done |
113 |
| - echo "--------------------------------------------------------------" |
114 |
| - |
115 |
| - readarray -t metas < <(find "${METAS_DIR}" -name 'meta.yaml' -o -name 'devfile.yaml') |
116 |
| - for meta in "${metas[@]}"; do |
117 |
| - readarray -t images < <(grep "image:" "${meta}" | sed -r "s;.*image:[[:space:]]*'?\"?([._:a-zA-Z0-9-]*/?[._a-zA-Z0-9-]*/[._a-zA-Z0-9-]*(@sha256)?:?[._a-zA-Z0-9-]*)'?\"?[[:space:]]*;\1;") |
118 |
| - for image in "${images[@]}"; do |
119 |
| - separators="${image//[^\/]}" |
120 |
| - # Warning, keep in mind: image without registry name is it possible case. It's mean, that image comes from private registry, where is we have organization name, but no registry name... |
121 |
| - digest="${imageMap[${image}]}" |
122 |
| - |
123 |
| - if [[ -z "${digest}" ]] && [ "${#separators}" == "1" ]; then |
124 |
| - imageWithDefaultRegistry="docker.io/${image}" |
125 |
| - digest="${imageMap[${imageWithDefaultRegistry}]}" |
126 |
| - fi |
127 |
| - |
128 |
| - if [[ -n "${digest}" ]]; then |
129 |
| - if [[ ${image} == *":"* ]]; then |
130 |
| - imageWithoutTag="${image%:*}" |
131 |
| - tag="${image#*:}" |
132 |
| - else |
133 |
| - imageWithoutTag=${image} |
134 |
| - tag="" |
135 |
| - fi |
136 |
| - |
137 |
| - REGEX="([[:space:]]*\"?'?)(${imageWithoutTag}):?(${tag})(\"?'?)" |
138 |
| - sed -i -E "s|image:${REGEX}|image:\1\2${digest}\4|" "$meta" |
139 |
| - fi |
140 |
| - done |
141 |
| - done |
142 |
| - fi |
143 |
| -} |
144 |
| - |
145 |
| -function update_container_image_references() { |
146 |
| - |
147 |
| - # We can't use the `-d` option for readarray because |
148 |
| - # registry.centos.org/centos/httpd-24-centos7 ships with Bash 4.2 |
149 |
| - # The below command will fail if any path contains whitespace |
150 |
| - readarray -t metas < <(find "${METAS_DIR}" -name 'meta.yaml' -o -name 'devfile.yaml') |
151 |
| - for meta in "${metas[@]}"; do |
152 |
| - echo "Checking meta $meta" |
153 |
| - # Need to update each field separately in case they are not defined. |
154 |
| - # Defaults don't work because registry and tags may be different. |
155 |
| - if [ -n "$REGISTRY" ]; then |
156 |
| - echo " Updating image registry to $REGISTRY" |
157 |
| - < "$meta" tr '\n' '\r' | sed -E "s|image:$IMAGE_REGEX|image:\1${REGISTRY}/\3/\4\5:\6\7|g" | tr '\r' '\n' > "$meta.tmp" && cat "$meta.tmp" > "$meta" && rm "$meta.tmp" |
158 |
| - fi |
159 |
| - if [ -n "$ORGANIZATION" ]; then |
160 |
| - echo " Updating image organization to $ORGANIZATION" |
161 |
| - < "$meta" tr '\n' '\r' | sed -E "s|image:$IMAGE_REGEX|image:\1\2/${ORGANIZATION}/\4\5:\6\7|g" | tr '\r' '\n' > "$meta.tmp" && cat "$meta.tmp" > "$meta" && rm "$meta.tmp" |
162 |
| - fi |
163 |
| - if [ -n "$TAG" ]; then |
164 |
| - echo " Updating image tag to $TAG" |
165 |
| - < "$meta" tr '\n' '\r' | sed -E "s|image:$IMAGE_REGEX|image:\1\2/\3/\4:${TAG}\7|g" | tr '\r' '\n' > "$meta.tmp" && cat "$meta.tmp" > "$meta" && rm "$meta.tmp" |
166 |
| - fi |
167 |
| - done |
168 |
| -} |
169 |
| - |
170 | 45 | # do not execute the main function in unit tests
|
171 | 46 | if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
|
172 | 47 | then
|
|
0 commit comments