forked from cybertec-postgresql/cybertec_migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator
executable file
Β·330 lines (268 loc) Β· 9.9 KB
/
migrator
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2022 CYBERTEC PostgreSQL International GmbH <[email protected]>
set -e
shopt -s nullglob
function usage() {
print "$(cat <<EOF
$(highlight "up") Start Migrator in background
$(highlight "down") Stop Migrator
$(highlight "logs") Print logs
$(highlight "version") Display current version
$(highlight "versions") Display all sanctioned versions
$(highlight "update") Update version info from remote source
$(highlight "update") --archive <archive-file> Update version info from archive
$(highlight "upgrade") Upgrade to newest version
$(highlight "upgrade") --archive <archive-file> Upgrade to version contained in archive
$(highlight "upgrade") <version> Upgrade to specific version
$(highlight "configure") Generate settings file
$(highlight "help") Display this help text
EOF
)"
}
function dependency_barrier {
command -v docker &>/dev/null || \
error "$(highlight "docker") not installed"
command -v docker-compose &>/dev/null || \
error "$(highlight "docker-compose") not installed"
command -v git &>/dev/null || \
error "$(highlight "git") not installed"
}
# Color codes
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
WHITE='\033[1;37m'
LIGHT_GREY='\033[1;37m'
# No color
NC='\033[0m'
function print {
printf "${1}\n"
}
function highlight {
echo "${LIGHT_GREY}${1}${NC}"
}
function error {
>&2 printf "[${RED}ERROR${NC}] ${1}\n"
return ${2-1}
}
function warn {
printf "[${YELLOW}WARN${NC}] ${1}\n"
}
function info {
printf "[${WHITE}INFO${NC}] ${1}\n"
}
function ok {
printf "[${GREEN}OK${NC}] ${1}\n"
}
function generate_environment_file {
password="$(generate_random)"
version="$(git describe --exact-match --tags HEAD)"
cat <<EOF > "${1}"
# ββ User configurable ββ
EXTERNAL_HTTP_PORT=80
VERSION=${version}
# ββ Internal β ββ
CORE_DB_PASSWORD="${password}"
EOF
chmod 600 "${1}"
}
function generate_random {
local PASSWORD_CHARSET='A-Za-z0-9!&()*+,-./:;<=>?@{|}~'
local PASSWORD_LENGTH=32
LC_ALL=C tr -dc "${PASSWORD_CHARSET}" </dev/urandom | head -c "${PASSWORD_LENGTH}"
}
# Temporary directory
TEMP_TEMPLATE='migrator.XXXXXX'
# Directory name in archive
ARCHIVE_DIR='./cybertec_migrator'
# Environment configuration files
ENV_FILE='./.env'
# Directory where we save container images provided from offline installation packages
IMAGE_PATH='images'
# This file exists only if the directory was provided by on offline installation package
VERSION_FILE='.version'
function most_recent_tag {
sanctioned_tags | head -n 1
}
function sanctioned_tags {
git tag --sort=-v:refname
}
function get_hostname {
# BSD `hostname` doesn't understand the `-I` flag
(hostname --all-ip-addresses 2> /dev/null || hostname) | awk '{ print $1 }'
}
# TODO: replace this with sed -I
function replace_in_place {
target=$(mktemp)
sed -e "${1}" "${2}" > "${target}"
mv "${target}" "${2}"
}
function version_barrier {
if ! git tag | grep -q "^${1}$"; then
error "Unknown version $(highlight "${1}")"
fi;
}
function print_url {
ip_addr="$(get_hostname)"
if [ ! -z "${ip_addr}" ]; then
port=":$(print_env 'EXTERNAL_HTTP_PORT')"
if [ "${port}" = ':80' ]; then
port=''
fi;
ok "Started on $(highlight "'http://${ip_addr}${port}'")"
fi;
}
function print_env {
env -i "${BASH}" -c "set -a; . "${ENV_FILE}"; set +a; echo \${${1}};"
}
# TODO Switch to .meta-inf file which contains more information. For now we
# remain on the .version file for backwards compatiblity.
function installed_from_archive() {
[ -f "${VERSION_FILE}" ] && return
false
}
dependency_barrier
# Change working directory to directory that contains script
cd "${0%/*}"
catch_all_exit_code=1
case "${1}" in
'up')
docker-compose up -d
print_url
;;
'down')
docker-compose down
;;
'logs')
if [ ! -z "$(docker-compose top)" ]; then
docker-compose logs --timestamps --tail='all'
else
error "Logs not avaliable while not running ($(highlight "'${0} up'") to start)"
fi;
;;
'configure')
if [ ! -f "${ENV_FILE}" ]; then
generate_environment_file "${ENV_FILE}"
ok 'Generated environment file'
next_command="install"
installed_from_archive && next_command="${next_command} --archive <archive_file>"
info "Run $(highlight "'${0} ${next_command}'") to complete setup"
else
warn 'Environment file already exists'
info "Please modify environment file $(highlight "'${ENV_FILE}'") manually"
fi;
;;
'update')
# No archive provided β Fetch from origin
if [ -z "${2}" ]; then
info 'Updating release information'
git fetch || error 'Could not fetch versions'
new_version=$(most_recent_tag)
info "Recommended version: "$(highlight "${new_version}")""
if [ -f "${ENV_FILE}" ] && [ ! "${new_version}" = $(print_env 'VERSION') ]; then
info "Run $(highlight "'${0} upgrade'") to upgrade to version ${new_version}"
fi;
# Archive provided β Use `.git` folder contained within archive as origin
elif [ "${2}" = '--archive' ] && [ -f "${3}" ]; then
info "Updating release information from archive '${3}'"
temp_dir="$(mktemp -t "${TEMP_TEMPLATE}" -d)"
trap 'rm -rf -- "${temp_dir}"' EXIT
extract_error=$(tar -xf "${3}" --strip-components=2 --directory "${temp_dir}" "${ARCHIVE_DIR}/${VERSION_FILE}" "${ARCHIVE_DIR}/.git" 2>&1 > /dev/null) || \
error "Failed to extract update information\n${extract_error}"
archive_version=$(<"${temp_dir}/${VERSION_FILE}")
git fetch --quiet --tags "${temp_dir}/.git" 1>/dev/null
info "Updated release information"
if [ -f "${ENV_FILE}" ] && [ ! "${archive_version}" = $(print_env 'VERSION') ]; then
info "Run $(highlight "'${0} upgrade --archive ${3}'") to upgrade to version ${archive_version}"
fi;
else
error "Invalid path $(highlight "${3}")"
fi;
;;
'install')
;&
'upgrade')
# Ensure that the environment file exists
if [ ! -f "${ENV_FILE}" ]; then
generate_environment_file "${ENV_FILE}"
fi;
# No parameters provided β Fetch most recent images from online registry
if [ -z "${2}" ]; then
version="$(most_recent_tag)"
if [ -z "${version}" ]; then
error "No versions available (run $(highlight "'${0} update'") to fetch versions)"
fi;
info "Pulling images for $(highlight "${version}")"
if pull_error=$(VERSION="${version}" docker-compose pull 2>&1 > /dev/null); then
ok "Pulled $(highlight "${version}")"
elif installed_from_archive; then
# || true to prevent exit with the error message so we can provide a suggestion
error "Failed to pull container images\n${pull_error}\n" || true
info "Migrator was extracted from archive file: run $(highlight "'${0} ${1} --archive <archive_file>'") to proceed with upgrade"
exit 2
else
error "Failed to pull container images\n${pull_error}"
fi
# Archive provided β Import images from archive
elif [ "${2}" = '--archive' ] && [ -f "${3}" ]; then
# TODO move reading version incl. version check to a function
info "Reading version information from archive file '${3}'"
version=$(tar -xf "${3}" "${ARCHIVE_DIR}/${VERSION_FILE}" --to-stdout 2>&1) || \
error "Failed to extract archive file\n$version"
version_barrier "${version}" || (
info "Run $(highlight "'${0} update --archive ${3}'") before attempting upgrade"
exit 1
)
info "Upgrading to version $version"
info "Extracting archive file '${3}'"
temp_dir="$(mktemp -t "${TEMP_TEMPLATE}" -d)"
trap 'rm -rf -- "${temp_dir}"' EXIT
extraction_error=$(tar -xf "${3}" --strip-components=2 --directory "${temp_dir}" 2>&1 > /dev/null) || \
error "Failed to extract archive file\n$extraction_error"
archive_image_path="${temp_dir}/${IMAGE_PATH}"
[ -d "${archive_image_path}" ] || error "Archive file corrupted - '${IMAGE_PATH}' directory with container images missing"
# Import images *before* moving them to local image directory
info 'Loading container images'
for image in ${archive_image_path}/*.tar; do
docker load < "${image}"
done
info 'Container images loaded'
# Save loaded images from the archive file.
mkdir -p "${IMAGE_PATH}"
mv ${archive_image_path}/*.tar "${IMAGE_PATH}"
info 'Archived container images'
# Explicit version provided but no archive
elif [ ! -z "${2}" ] && [ -z "${3}" ]; then
version_barrier "${2}"
version="${2}"
info "Pulling $(highlight "${version}")"
VERSION="${version}" docker-compose pull
ok "Pulled $(highlight "${version}")"
else
error 'Unexpected usage'
fi
git checkout --quiet "${version}"
replace_in_place "s/^VERSION=.*/VERSION=${version}/" "${ENV_FILE}"
info "Upgraded to $(highlight "${version}")"
info "Run $(highlight "'${0} up'") to switch to new version"
warn 'Switching will abort running migrations'
;;
'versions')
sanctioned_tags
;;
'version')
if [ -f "${ENV_FILE}" ]; then
print_env 'VERSION'
else
error "Initial configuration outstanding (run $(highlight "'${0} configure'"))"
fi;
;;
'help')
catch_all_exit_code='0'
;&
*)
usage
exit "${catch_all_exit_code}"
;;
esac