-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_feature_tracker_db.sh
executable file
·219 lines (175 loc) · 5.87 KB
/
update_feature_tracker_db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
#
# Update the release components table with image identifiers and product
# versions for a given release version.
#
# This script is idempotent, provided the inputs (especially the image digests)
# do not change.
#
# Requirements:
#
# 1. crane - https://github.com/google/go-containerregistry/tree/main/cmd/crane
# 2. psql - Postgres command line client.
#
# Environment:
#
# The following variables are read from the environment:
#
# PGPASSWORD
# PGHOST
# PGUSER
# PGDATABASE
# DB_SCHEMA
#
set -euo pipefail
#set -x
PRODUCT_CODE_NAMES=(
airflow
druid
hadoop
hbase
hive
kafka
nifi
spark-k8s
superset
trino
zookeeper
)
product_image_digest() {
# Return the digest for the given image name.
#
# Example output: sha256:822d427031bf93055c51f4dc3bcaa468867ce83f59de6824af279df4cce2d066
#
# Params:
#
# IMAGE_NAME - Example: docker.stackable.tech/stackable/zookeeper:3.8.3-stackable23.11.0
#
IMAGE_NAME="$1"
IMAGE_DIGEST=$(crane digest "${IMAGE_NAME}")
if [ -z "$IMAGE_DIGEST" ]; then
echo "Image ${IMAGE_NAME} not found in registry"
exit 1
fi
echo "${IMAGE_DIGEST}"
}
ensure_release_version() {
# Ensure the release version is present in the DB.
#
# Params:
#
# RELEASE_NAME - Example: 2023-11
# RELEASE_VERSION - Example: 23.11.0
# -AtR "": In this case this basically means "just output the content of the selected column"
# -c: Execute the provided command and exit
RELEASE_ID=$(psql -qAtR "" -c "SELECT id FROM $DB_SCHEMA.releases WHERE name = '$RELEASE_NAME'")
if [ -z "$RELEASE_ID" ]; then
echo "Release $RELEASE_NAME not found in database"
exit 1
fi
# Create release version if it does not exist
psql -q -c "INSERT INTO $DB_SCHEMA.release_versions (release_id, version, created_at) VALUES ($RELEASE_ID, '$RELEASE_VERSION', now()) ON CONFLICT DO NOTHING"
}
ensure_product_version() {
# Ensure that a given product version exists in the DB.
# Returns the product version id.
#
# Params:
#
# PRODUCT_CODE_NAME - See the PRODUCT_CODE_NAMES array for valid values.
# PRODUCT_VERSION - Example: 2.6.1
#
# Return:
#
# The id of the product version.
#
PRODUCT_CODE_NAME="$1"
PRODUCT_VERSION="$2"
PRODUCT_ID=$(psql -qAtR "" -c "SELECT id FROM $DB_SCHEMA.products WHERE code_name = '$PRODUCT_CODE_NAME'")
if [ -z "$PRODUCT_ID" ]; then
echo >&2 "Product $PRODUCT_CODE_NAME not found in database"
exit 1
fi
PRODUCT_VERSION_ID=$(psql -qAtR "" -c "SELECT id FROM $DB_SCHEMA.product_versions WHERE product_id = $PRODUCT_ID AND version = '$PRODUCT_VERSION'")
if [ -z "$PRODUCT_VERSION_ID" ]; then
echo >&2 "Product version $PRODUCT_VERSION not found in database, creating it now"
psql -qAtR "" -c "INSERT INTO $DB_SCHEMA.product_versions (product_id, version) VALUES ($PRODUCT_ID, '$PRODUCT_VERSION')"
fi
PRODUCT_VERSION_ID=$(psql -qAtR "" -c "SELECT id FROM $DB_SCHEMA.product_versions WHERE product_id = $PRODUCT_ID AND version = '$PRODUCT_VERSION'")
echo "${PRODUCT_VERSION_ID}"
}
update_release_components() {
# Update the release components table for the given parameters.
#
# Params:
#
# RELEASE_VERSION - Example: 23.11.0
# PRODUCT_VERSION_ID - Example: 38. See ensure_product_version() above.
# REPOSITORY_NAME - Example: docker.stackable.tech/stackable/zookeeper
# IMAGE_DIGEST - Example: sha256:822d427031bf93055c51f4dc3bcaa468867ce83f59de6824af279df4cce2d066
local RELEASE_VERSION="$1"
local PRODUCT_VERSION_ID="$2"
local REPOSITORY_NAME="$3"
local IMAGE_DIGEST="$4"
REGISTRY=$(echo "${REPOSITORY_NAME}" | sed 's/\/.*//')
PURL="pkg:docker/${REPOSITORY_NAME}@${IMAGE_DIGEST}?repository_url=$REGISTRY"
psql -q -c "INSERT INTO $DB_SCHEMA.release_components (product_version_id, release_version, purl) VALUES ($PRODUCT_VERSION_ID, '$RELEASE_VERSION', '$PURL') ON CONFLICT DO NOTHING"
}
usage() {
cat <<EOF
Usage: $0 <release> <version>
Options:
release - release name (ex. 2023-11)"
version - release version (ex. 23.11.0)
The following environment variables are required for the connection to the Postgres DB:
PGPASSWORD
PGHOST
PGUSER
PGDATABASE
DB_SCHEMA
EOF
}
main() {
# Update the release components for the given release (version).
#
# Params
#
# RELEASE_NAME - Example: 2023-11
# RELEASE_VERSION - Example: 23.11.0
#
local RELEASE_NAME="${1:-}"
local RELEASE_VERSION="${2:-}"
local REGISTRY=docker.stackable.tech # REGISTRY=oci.stackable.tech
local REGISTRY_PATH=stackable # REGISTRY_PATH=sdp
if [ -z "$RELEASE_NAME" ]; then
echo "Release name cannot be empty."
usage
exit 1
fi
if [ -z "$RELEASE_VERSION" ]; then
echo "Release version cannot be empty."
usage
exit 1
fi
echo "Update release components for release $RELEASE_NAME and version $RELEASE_VERSION"
ensure_release_version "${RELEASE_NAME}" "${RELEASE_VERSION}"
for PRODUCT_CODE_NAME in "${PRODUCT_CODE_NAMES[@]}"; do
REPOSITORY_NAME="${REGISTRY}/${REGISTRY_PATH}/${PRODUCT_CODE_NAME}"
IMAGES=$(crane ls "${REPOSITORY_NAME}" | grep "stackable${RELEASE_VERSION}" | xargs -I '{}' echo "${REPOSITORY_NAME}":'{}')
# Linter complains about usless echo in $(echo $IMAGES)
# but the "echo" is necessary to split IMAGES into multiple lines.
# Otherwise IMAGE_NAME contains a long string with all images separated by
# newline.
#
# shellcheck disable=SC2116
for IMAGE_NAME in $(echo "${IMAGES}"); do
PRODUCT_VERSION=$(echo "${IMAGE_NAME}" | sed 's/^.*://' | sed 's/-.*//')
PRODUCT_VERSION_ID=$(ensure_product_version "${PRODUCT_CODE_NAME}" "${PRODUCT_VERSION}")
PRODUCT_IMAGE_DIGEST=$(product_image_digest "${IMAGE_NAME}")
echo "Updating compnents for image $IMAGE_NAME"
update_release_components "${RELEASE_VERSION}" "${PRODUCT_VERSION_ID}" "${REPOSITORY_NAME}" "${PRODUCT_IMAGE_DIGEST}"
done
done
echo "Done."
}
main "$@"