-
Notifications
You must be signed in to change notification settings - Fork 12
/
install.sh
executable file
·322 lines (280 loc) · 8.61 KB
/
install.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
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
#!/usr/bin/env bash
# CARTO 3 Self hosted installer
#
# Usage:
# install.sh <options>...
#
# Depends on:
# sed
# Github repo https://github.com/CartoDB/carto-selfhosted
#
###############################################################################
# Strict Mode
###############################################################################
# Treat unset variables and parameters other than the special parameters ‘@’ or
# ‘*’ as an error when performing parameter expansion. An 'unbound variable'
# error message will be written to the standard error, and a non-interactive
# shell will exit.
#
# This requires using parameter expansion to test for unset variables.
#
# http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
# Short form: set -u
set -o nounset
# Exit immediately if a pipeline returns non-zero.
#
# NOTE: This can cause unexpected behavior. When using `read -rd ''` with a
# heredoc, the exit status is non-zero, even though there isn't an error, and
# this setting then causes the script to exit. `read -rd ''` is synonymous with
# `read -d $'\0'`, which means `read` until it finds a `NUL` byte, but it
# reaches the end of the heredoc without finding one and exits with status `1`.
#
# More information:
#
# https://www.mail-archive.com/bug-bash@gnu.org/msg12170.html
#
# Short form: set -e
set -o errexit
# Print a helpful message if a pipeline with non-zero exit code causes the
# script to exit as described above.
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
# Allow the above trap be inherited by all functions in the script.
#
# Short form: set -E
set -o errtrace
# Return value of a pipeline is the value of the last (rightmost) command to
# exit with a non-zero status, or zero if all commands in the pipeline exit
# successfully.
set -o pipefail
# Set $IFS to only newline and tab.
#
# http://www.dwheeler.com/essays/filenames-in-shell.html
IFS=$'\n\t'
###############################################################################
# Environment
###############################################################################
# $_ME
#
# This program's basename.
_ME="$(basename "${0}")"
_DOCKER_MINIMUM_VERSION_MAJOR=20
_DOCKER_MINIMUM_VERSION_MINOR=10
_COMPOSE_MINIMUM_VERSION_MAJOR=1
_COMPOSE_MINIMUM_VERSION_MINOR=29
###############################################################################
# Help
###############################################################################
# _print_help()
#
# Usage:
# _print_help
#
# Print the program help information.
_print_help() {
cat <<-HEREDOC
install.sh prepares your environment to run the docker-compose flavor of Carto Self Hosted
Usage:
${_ME} [--ignore-checks]
${_ME} --help
Options:
--help Show this screen.
HEREDOC
}
###############################################################################
# Program Functions
###############################################################################
_valid_version() {
local _req_major
local _req_minor
local _check_major
local _check_minor
_req_major=$1
_req_minor=$2
_check_major=$3
_check_minor=$4
if [ "${_check_major}" -gt "${_req_major}" ]; then
true
return
else
if [ "${_check_major}" -lt "${_req_major}" ]; then
false
return
fi
if [ "${_check_minor}" -ge "${_req_minor}" ]; then
true
return
else
false
return
fi
fi
}
_check_docker_version() {
local docker_version_major
local docker_version_minor
docker_version_major=$(docker --version | awk '{ print $3}' | awk -F. '{ print $1 }')
docker_version_minor=$(docker --version | awk '{ print $3}' | awk -F. '{ print $2 }')
if ! _valid_version ${_DOCKER_MINIMUM_VERSION_MAJOR} ${_DOCKER_MINIMUM_VERSION_MINOR} "${docker_version_major}" "${docker_version_minor}"; then
_err "Minimum docker version is ${_DOCKER_MINIMUM_VERSION_MAJOR}.${_DOCKER_MINIMUM_VERSION_MINOR}"
exit 1
fi
}
_check_compose_version() {
# Docker Compose version v2.1.1
# docker-compose version 1.29.2, build 5becea4c
local compose_version_extracted
local compose_version_major
local compose_version_minor
compose_version_extracted=$(docker-compose --version | sed 's/^.*version\ //g' | sed 's/[\,\ ].*$//g' | sed 's/v//g')
compose_version_major=$(echo "${compose_version_extracted}" | awk -F. '{ print $1 }')
compose_version_minor=$(echo "${compose_version_extracted}" | awk -F. '{ print $2 }')
if ! _valid_version ${_COMPOSE_MINIMUM_VERSION_MAJOR} ${_COMPOSE_MINIMUM_VERSION_MINOR} "${compose_version_major}" "${compose_version_minor}"; then
_err "Minimum docker-compose version is ${_COMPOSE_MINIMUM_VERSION_MAJOR}.${_COMPOSE_MINIMUM_VERSION_MINOR}"
exit 1
fi
}
_migrate_postgres_version_var() {
sed -i -e 's/^POSTGRES_PASSWORD/POSTGRES_ADMIN_PASSWORD/g' customer.env
}
_create_env_file() {
_migrate_postgres_version_var
_info "Creating .env file..."
local version
version=$(cat VERSION)
{
cat customer.env
echo
echo "# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< CARTO VERSION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
echo
echo "CARTO_SELFHOSTED_VERSION=${version}"
echo
echo "# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo
cat env.tpl
} >.env
mkdir -p certs
cp key.json certs/key.json
_info "File .env successfully created"
_info "Script finished, run docker-compose up -d"
}
# Description Private function to check versions.
# Not thread-safe.
#
# Example
# echo "test: $(_check_min_cloud_version 1.2 1.3)"
#
#
# arg $1 string version A
# arg $2 string version B
#
# It orders asc version A and version b and if the first in the list is
# version A returns true, else return false
#
_check_min_cloud_version() {
local MINIMAL_VERSION
local PACKAGE_VERSION
MINIMAL_VERSION=$(echo "$1" | tr -d '"')
PACKAGE_VERSION=$(echo "$2" | tr -d '"')
if [[ "$(echo "$MINIMAL_VERSION $PACKAGE_VERSION" | tr ' ' '\n' | sort -V | head -n1)" != $MINIMAL_VERSION ]]; then
false
else
true
fi
}
function _run_checks() {
_info "Running command checks..."
#Docker
if ! command -v docker >/dev/null 2>&1; then
_err "docker is not installed, you can use the ./scripts/install_docker.sh helper"
exit 1
fi
_check_docker_version
#Docker Compose
if ! command -v docker-compose >/dev/null 2>&1; then
_err "docker-compose is not installed, you can use the ./scripts/install_docker-compose.sh helper"
exit 1
fi
_check_compose_version
# Files
local needed_files
needed_files=("VERSION" "customer.env" "key.json" "MIN_VERSION")
for file in ${needed_files[*]}; do
if [ ! -f ${file} ]; then
_err "Missing ${file} file"
exit 1
fi
done
}
function _run_post_checks() {
MIN_VERSION=$(cat MIN_VERSION)
PACKAGE_VERSION=$(cat .env | grep CARTO_SELFHOSTED_CUSTOMER_PACKAGE_VERSION | cut -d \= -f2)
if [ -f .env ]; then
(
# needed to remove the comments inside the .env
for line in $(cat .env | sed 's/#.*//g'); do
export $line
done
if [[ "$LOCAL_POSTGRES_SCALE" = "1" ]]; then
_warn "Using embedded databases is not for PRODUCTION use"
fi
if [[ -z "${POSTGRES_ADMIN_PASSWORD}" ]]; then
_err "There is no Postgres Admin password defined"
fi
if ! _check_min_cloud_version $MIN_VERSION $PACKAGE_VERSION; then
_err "Minimum cloud version version is $MIN_VERSION but your package was generated with $PACKAGE_VERSION. Please follow the instructions to download a new version of your customer package: https://github.com/CartoDB/carto-selfhosted/tree/master/tools#download-customer-package-tool."
fi
)
fi
}
## Log functions to avoid "echo" without context and keep style
function _info() {
printf "[INFO]: %s\n" $1
}
function _warn() {
printf "[WARN]: %s\n" $1
}
function _err() {
printf "[ERROR]: %s\n" $1
}
###############################################################################
# Main
###############################################################################
# _main()
#
# Usage:
# _main [<options>] [<arguments>]
#
# Description:
# Entry point for the program, handling basic option parsing and dispatching.
_main() {
#Start argument parsing
# Avoid complex option parsing when only one program option is expected.
if [[ $# -gt 1 ]]; then
_err "too many arguments, only one argument permitted"
_print_help
exit 1
elif [[ "${1:-}" =~ ^--ignore-checks$ ]]; then
local _IGNORE_CHECKS=true
elif [[ "${1:-}" =~ ^--help$ ]]; then
_print_help
exit 0
elif [[ $# -eq 1 ]]; then
_err "invalid argument"
_print_help
exit 1
else
local _IGNORE_CHECKS=false
fi
#End Argument parsing
if [ ${_IGNORE_CHECKS} = true ]; then
_info "Skipping command checks..."
_create_env_file
exit 0
else
_run_checks
_create_env_file
fi
_run_post_checks
}
# Call `_main` after everything has been defined.
_main "$@"