-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_images
executable file
·241 lines (219 loc) · 8.22 KB
/
manage_images
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/bash
BASE_DIR=$(dirname "${0}")
LANG=en_EN
# Get script name
SCRIPT=$(basename ${0})
# Supported distributions and PostgreSQL versions
SUPPORTEDDISTROS="centos7 rockylinux8 ubuntu18.04 ubuntu20.04 opensuseleap15.5"
SUPPORTEDPGVERS="9.3 9.4 9.5 9.6 10 11 12 13 14 15 16"
# Ignored combinations, will exit without errors so the CI does not fail
IGNOREDCOMBINATIONS="centos7|16"
# Use remote images by default
LOCAL="--pull"
# Parameters for the image
DATE=$(date +%Y%m%dT%H%M%s%N)
# Use podman by default as container engine
CENGINE='podman'
# Default registry
REGISTRY='docker.io'
# Default registry userspace, not configurable for now
NAMESPACE='tdsfdw'
help() {
echo ""
echo "Script to create tds_fdw PostgreSQL container images for CI"
echo ""
echo "Syntax: "
echo ""
echo "${SCRIPT} <ARGUMENTS>"
echo ""
echo "Mandatory arguments:"
echo ""
echo " --action=<create|update|updates_report|promote>"
echo " * create: will create images from scratch. Output tag will be '<PG_VERSION>-experimental'."
echo " * updates_report: will create a report of the images requiring upgrades"
echo " * update: will try to update the base image, and PostgreSQL software for all images."
echo " Output tag will be '<PG_VERSION>-experimental'."
echo " * promote: will promote image(s) by removing tagging then as 'PG_VERSION' without the"
echo " '-experimental'."
echo " * publish: will push current stable version to a registry"
echo ""
echo "Mandatory arguments when --action=publish"
echo ""
echo " --username: Username to be used for the registry"
echo " --password: Password to be used for the registry"
echo ""
echo "Optional arguments:"
echo ""
echo " --local If present, try to use local images"
echo " --versions=<PG_VERSIONS> A comma separated list of versions"
echo " (supported: $(echo ${SUPPORTEDPGVERS}|sed -e 's/ /, /g'))"
echo " --distributions=<DISTRIBUTIONS> A comma separated list of distributions"
echo " (supported: $(echo ${SUPPORTEDDISTROS}|sed -e 's/ /, /g'))"
echo " --docker Use docker instead of podman"
echo " --registry=<REGISTRY> Specify an image registry. If absent, docker.io"
echo " will be used by default"
echo " --errorcodes If present, it will return the following exit codes:"
echo " -1001 if there are updates for current image"
echo " -1002 if there are updates for the parent image"
echo " -1003 if there are updates for both current and parent images"
echo ""
exit 1
}
print_incorrect_syntax() {
echo "Incorrect syntax (use -h for help)"
}
print_unknown_version() {
echo "Unknown version ${1} (use -h for help)"
}
print_unknown_distro() {
echo "Unknown distribution ${1} (use -h for help)"
}
check_updates() {
local IMAGE_NAME="${1}"
local DISTRO="${2}"
local VERSION="${3}"
local CONTNAME="tdsfdw-${DISTRO}-postgresql${VERSION}"
${CENGINE} run -i -u root --name ${CONTNAME} ${IMAGE_NAME} /opt/check_updates.sh
local UPDATES=${?}
${CENGINE} rm ${CONTNAME} > /dev/null
return "${UPDATES}"
}
update_image() {
local IMAGE_NAME="${1}"
local DISTRO="${2}"
local VERSION="${3/-testing}"
local FULL="${4}"
local LOCAL="${5}"
if [ "FULL" != "TRUE" ]; then
NOCACHE='--no-cache'
fi
cat ${DISTRO}/Dockerfile | sed -e "s/<!POSTRESQL_VER!>/${3}/g" > ${DISTRO}/Dockerfile.tmp
local BASE_IMAGE=$(gawk 'match($0, /^FROM (.*)$/, a) {print a[1]}' ${DISTRO}/Dockerfile.tmp)
${CENGINE} pull ${BASE_IMAGE}
${CENGINE} build ${LOCAL} --build-arg POSTGRESQL_VER=${VERSION} --build-arg DATE=${DATE} ${NOCACHE} -t ${IMAGE_NAME_EXP} -f ${DISTRO}/Dockerfile.tmp ${DISTRO}/
rm -f ${DISTRO}/Dockerfile.tmp
}
ARGS=$(getopt -o h --long help,local,action:,versions:,distributions:,docker,registry:,username:,password:,errorcodes -n "${SCRIPT}" -- "$@")
if [ $? -ne 0 ];
then
print_incorrect_syntax
exit 1
fi
eval set -- "${ARGS}"
# extract options and their arguments into variables
while true ; do
case "${1}" in
-h|--help) help; exit 1 ;;
--action) ACTION="${2}"; shift 2 ;;
--local) LOCAL=""; shift 1 ;;
--versions) VERSIONS="${2}"; shift 2;;
--distributions) DISTROS="${2}"; shift 2;;
--docker) CENGINE='docker'; shift 1;;
--registry) REGISTRY="${2}"; shift 2;;
--username) USERNAME="${2}"; shift 2;;
--password) PASSWORD="${2}"; shift 2;;
--errorcodes) ERRORCODES='TRUE'; shift 1;;
--) shift ; break ;;
*) print_incorrect_syntax; exit 1 ;;
esac
done
# Check actions
case "${ACTION}" in
create) ;;
updates_report) ;;
update) ;;
promote) ;;
publish) ;;
*) print_incorrect_syntax; exit 1;:
esac
# Check Versions
if [ -z "${VERSIONS}" ]; then
AVERSIONS="${SUPPORTEDPGVERS}"
else
AVERSIONS=""
for VERSION in $(echo ${VERSIONS}|tr ',' ' '); do
VFOUND=0
for SVERSION in ${SUPPORTEDPGVERS}; do
if [ "${VERSION}" == "${SVERSION}" ]; then
AVERSIONS="${AVERSIONS} ${VERSION}"
VFOUND=1
fi
done
if [ $VFOUND -eq 0 ]; then
print_unknown_version "${VERSION}"
exit 1
fi
done
fi
# Check Distributios
if [ -z "${DISTROS}" ]; then
ADISTROS="${SUPPORTEDDISTROS}"
else
ADISTROS=""
for DISTRO in $(echo ${DISTROS}|tr ',' ' '); do
DFOUND=0
for SDISTRO in ${SUPPORTEDDISTROS}; do
if [ "${DISTRO}" == "${SDISTRO}" ]; then
ADISTROS="${ADISTROS} ${DISTRO}"
DFOUND=1
fi
done
if [ ${DFOUND} -eq 0 ]; then
print_unknown_distro "${DISTRO}"
exit 1
fi
done
fi
for IGNOREDCOMBINATION in ${IGNOREDCOMBINATIONS}; do
IGNOREDDISTRO=$(echo ${IGNOREDCOMBINATION}|cut -d'|' -f1)
IGNOREDVER=$(echo ${IGNOREDCOMBINATION}|cut -d'|' -f2)
if [ "${IGNOREDDISTRO}" == "${DISTRO}" ] && [ "${IGNOREDVER}" == "${VERSION}" ]; then
echo "${IGNOREDDISTRO} with PostgreSQL ${IGNOREDVER} is not available"
exit 0
fi
done
for DISTRO in ${ADISTROS}; do
for VERSION in ${AVERSIONS}; do
IMAGE_NAME="${REGISTRY}/${NAMESPACE}/${DISTRO}-postgresql:${VERSION}"
IMAGE_NAME_EXP="${IMAGE_NAME}-experimental"
if [ "${ACTION}" == "create" ]; then
echo "================================================================================="
echo " Creating ${IMAGE_NAME_EXP}..."
echo "================================================================================="
update_image ${IMAGE_NAME_EXP} ${DISTRO} ${VERSION} TRUE
elif [ "${ACTION}" == "updates_report" ]; then
echo "${DISTRO}|${VERSION}|$(check_updates ${IMAGE_NAME_EXP} ${DISTRO} ${VERSION})"
elif [ "${ACTION}" == "update" ]; then
echo "================================================================================="
echo " Trying to update image ${IMAGE_NAME_EXP}..."
echo "================================================================================="
check_updates ${IMAGE_NAME_EXP} ${DISTRO} ${VERSION}
UPDATES=${?}
if [ "${UPDATES}" == "0" ]; then
echo "INFO: No updates available"
else
if [ "${UPDATES}" == "2" -o "${UPDATES}" == "3" ]; then
echo "INFO: Will try to get an updated base image"
elif [ "${UPDATES}" == "1" -o "${UPDATES}" == "3" ]; then
echo "INFO: Update will include packages from the parent image"
fi
if [ "${ERRORCODES}" == "TRUE" ]; then
exit -100${UPDATES}
fi
update_image ${IMAGE_NAME_EXP} ${DISTRO} ${VERSION} ${UPDATE_BASE} ${LOCAL}
fi
elif [ "${ACTION}" == "promote" ]; then
echo "================================================================================="
echo " Promoting ${IMAGE_NAME_EXP} as:"
echo " ${IMAGE_NAME}"
echo "================================================================================="
${CENGINE} tag ${IMAGE_NAME_EXP} ${IMAGE_NAME}
elif [ "${ACTION}" == "publish" ]; then
echo "================================================================================="
echo " Publishing ${IMAGE_NAME}..."
echo "================================================================================="
${CENGINE} login --username "${USERNAME}" --password "${PASSWORD}" ${REGISTRY}
${CENGINE} push ${IMAGE_NAME}
fi
done
done