forked from eStation2/CS-Installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs_install.sh
executable file
·376 lines (312 loc) · 9.6 KB
/
cs_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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env bash
#
set -o errexit -o pipefail -o noclobber -o nounset
function success()
{
echo -e "\e[32m${1}\e[0m"
}
function info()
{
echo -e "\e[36m${1}\e[0m"
}
function warning()
{
echo -e "\e[33m${1}\e[0m"
}
function error()
{
echo -e "\e[31m${1}\e[0m"
}
if ! which realpath > /dev/null
then
function realpath()
{
if [[ "${1}" == /* ]]
then
echo "${1}"
else
echo "${PWD}/${1#./}"
fi
}
fi
function update-config-files()
{
local CONFIG_FILE=$1
local TEMPLATE_FILE=$2
local CONFIG_FILENAME="$(basename "$1")"
local MYUSER=$(id -u)
local MYGROUP=$(id -g)
if [[ -d "${CONFIG_FILE}" ]]; then
echo -e "$(warning "WARNING"): Found a directory that already exists with"
echo -e " the same name as the '$(info "${CONFIG_FILENAME}")' file."
echo " You probably ran the 'docker-compose' command manually before"
echo " running this script."
echo " Pay attention for next time."
echo -e " Removing this directory... \c"
rmdir "${CONFIG_FILE}"
success "OK!"
fi
if [[ -f "${CONFIG_FILE}" ]]; then
echo -e "$(warning "WARNING"): The default '$(info "${CONFIG_FILENAME}")' file already exists."
echo " Appending the previous configuration in '$(warning "${CONFIG_FILENAME}.old")'"
echo -e " and creating a new file from the template... \c"
echo "=====================" >> "${CONFIG_FILE}.old"
echo "Update config file to version ${CONFIG_VERSION} of `date` " >> "${CONFIG_FILE}.old"
echo "---------------------" >> "${CONFIG_FILE}.old"
cat "${CONFIG_FILE}" >> "${CONFIG_FILE}.old"
cp "${TEMPLATE_FILE}" "${CONFIG_FILE}"
echo >> "${CONFIG_FILE}"
echo "USER_ID=${USER_ID:-$MYUSER}" >> "${CONFIG_FILE}"
echo "GROUP_ID=${GROUP_ID:-$MYGROUP}" >> "${CONFIG_FILE}"
success "OK!"
else
echo -e "$(info "INFO"): Creating the default '$(info "${CONFIG_FILENAME}")' file from the template... \c"
cp "${TEMPLATE_FILE}" "${CONFIG_FILE}"
echo >> "${CONFIG_FILE}"
echo "USER_ID=${USER_ID:-$MYUSER}" >> "${CONFIG_FILE}"
echo "GROUP_ID=${GROUP_ID:-$MYGROUP}" >> "${CONFIG_FILE}"
success "OK!"
fi
}
# Base directories:
#
readonly BASE_DIR="$(realpath "$(dirname "${0}")")"
readonly BASE_FILE="$(basename "${0}")"
readonly CONFIG_DIR="${BASE_DIR}/.configs"
export IMPACT_VOLUME="${BASE_DIR}/../Impact"
#readonly IMPACT_PROJECT_NAME="$(basename "${IMPACT_VOLUME}")"
readonly IMPACT_PROJECT_NAME="$(basename "${IMPACT_VOLUME}"| tr '[:upper:]' '[:lower:]')"
# Project versions:
readonly CONFIG_VERSION="1.1.1"
# readonly SOURCE_VERSION="1.1.0"
readonly JRC_IMAGE_REGISTRY="d-prd-registry.jrc.it"
# Template files:
#
readonly DFLT_ENV_FILE="${BASE_DIR}/.env"
readonly TMPL_ENV_FILE="${BASE_DIR}/.env.template"
readonly CS_IMAGES=("climatestation/postgis:2.0"
"climatestation/web:2.0"
"climatestation/jupyterhub:latest"
"climatestation/jupyternotebook:latest")
function check-config()
{
local LATEST_VERSION=
mkdir -p "${CONFIG_DIR}"
if [[ -f "${CONFIG_DIR}/version.conf" ]]; then
LATEST_VERSION="$(cat "${CONFIG_DIR}/version.conf")"
fi
if [[ "${CONFIG_VERSION}" != "${LATEST_VERSION}" ]]; then
if [[ -z "${LATEST_VERSION}" ]]; then
echo -e "$(info "INFO"): You are running the Climate Station containers"
echo " stack on this machine for the first time."
echo " This script will now create some environmental files"
echo " needed to execute the application properly."
echo
touch "${CONFIG_DIR}/init.lock"
else
echo -e "$(info "INFO"): Since the last run of the Climate Station containers stack"
echo " on this machine, the required configurations have been changed."
echo " This script will now replace some environmental files with"
echo " the new ones needed to execute the application properly."
echo
touch "${CONFIG_DIR}/update.lock"
fi
update-config-files "${DFLT_ENV_FILE}" "${TMPL_ENV_FILE}"
echo "$(warning "WARNING"): Please, be sure to review the contents of these files"
echo " and configure them appropriately for your system."
echo -e " Don't forget to change also the '$(info "JWT_SECRET")'"
echo " value with a 32-character random string."
echo " Once you've done so, run this script again"
echo
echo " Don't forget to run with the -i option!."
echo
echo "${CONFIG_VERSION}" > "${CONFIG_DIR}/version.conf"
exit 0
elif [[ -f "${CONFIG_DIR}/init.lock" ]]; then
readonly INIT="true"
rm "${CONFIG_DIR}/init.lock"
elif [[ -f "${CONFIG_DIR}/update.lock" ]]; then
readonly UPDATE="true"
rm "${CONFIG_DIR}/update.lock"
fi
}
#export DATA_VOLUME
readonly CSTATION_COMPOSE="${BASE_DIR}/docker-compose.yml"
function mount_drive()
{
echo -n Trying to mount external drive..
wsl.exe -u root -e mkdir -p /mnt/$MOUNT > /dev/null 2>&1
wsl.exe -u root -e mount -t drvfs ${MOUNT}: /mnt/$MOUNT -o metadata,uid=$UID,gid=1000,umask=22,fmask=111 > /dev/null 2>&1
echo " Done."
echo
}
function pull_images()
{
local IMAGE_PREFIX=
[[ -n "${JRC_ENV}" ]] && IMAGE_PREFIX="${JRC_IMAGE_REGISTRY}/"
for image in ${CS_IMAGES[@]}; do
docker pull "${IMAGE_PREFIX}${image}"
done
}
function load_images()
{
echo "Trying to load images from directory $LOAD.."
files=$(shopt -s nullglob dotglob; echo "$LOAD"/*.{tar,dump})
if (( ${#files} )); then
for file in $files; do
docker load -q -i "$file"
done
echo "Done."
else
echo "Error: could not find any file!"
fi
echo
}
function fix_perms()
{
local wsl=$(which wsl.exe)
local command="sudo"
[[ -n "$wsl" ]] && command="wsl.exe -u root -e"
echo -n "Fixing filesystem permissions.."
$command \
chmod -fR u=rwX,g=rwX,o=rwX \
"${DATA_VOLUME}/static_data/log" \
"${DATA_VOLUME}/c3sf4p_jobresults" \
"${TMP_VOLUME}"
echo " Done."
echo
}
function docker-create-volume()
{
echo -e "$(info "INFO"): Creating a new Docker volume named '$(info "${1}")'... \c"
local ERRORS="$(docker volume create "${1}" 2>&1 > /dev/null)"
if [[ -z "${ERRORS}" ]]
then
success "OK!"
else
error "ERROR!"
echo "${ERRORS}"
exit 8
fi
}
# Stopping & Removing the containers:
#
function cs_up()
{
[ -n "$MOUNT" ] && mount_drive
check-config
source "${DFLT_ENV_FILE}"
if [[ -z "$(docker volume ls | awk '{ print $2 }' | grep -e "^cs-docker-postgresql12-volume$")" ]]
then
docker-create-volume "cs-docker-postgresql12-volume"
fi
if [[ -z "$(docker network ls | awk '{ print $2 }' | grep -e "^jupyterhub$")" ]]; then
docker network create "jupyterhub"
fi
[[ -n "$LOAD" ]] && load_images
[[ -n "$PULL" ]] && pull_images
[[ -n "$FIX" ]] && fix_perms
docker-compose -f "${CSTATION_COMPOSE}" up -d
echo
echo Climate Station is up.
echo
if [[ -n "$INIT" ]]; then
echo -e "$(info "INFO"): Waiting for the database containers to be ready to install updates... \c"
sleep 10
success "Ready"
docker-compose -f "${CSTATION_COMPOSE}" exec -T postgres bash /install_update_db.sh
fi
}
function cs_down()
{
docker-compose -f "${CSTATION_COMPOSE}" down
}
# Parsing command line options:
#
#
usage(){
>&2 cat << EOF
Usage: $0
[ -h | --help ]
[ -u | --user uid ] specify user id
[ -g | --group gid ] specify group id
[ -i | --init ] initialize installation
[ -j | --jrc ] pull images from JRC registry
[ -m | --mount letter ] mount removable drive in WSL
[ -p | --pull ] pull images from public registry
[ -f | --fix_perms ] fix fileystem permissions
<up|down>
EOF
}
LONGOPTS=help,init,user:,group:,jrc,mount:,pull,fix_perms,load:
OPTIONS=hiu:g:jm:pfl:
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
INIT=
USER_ID=
GROUP_ID=
PULL=
JRC_ENV=
MOUNT=
FIX=
LOAD=
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
-i|--install)
INIT=t
shift
;;
-u|--user)
USER_ID="$2"
shift 2
;;
-g|--group)
GROUP_ID="$2"
shift 2
;;
-j|--jrc)
PULL=t
JRC_ENV=t
shift
;;
-m|--mount)
MOUNT="${2%:}"
shift 2
;;
-p|--pull)
PULL=t
shift
;;
-f|--fix_perms)
FIX=t
shift
;;
-l|--load)
LOAD="$2"
shift 2
;;
--)
shift
break
;;
*)
echo -e "Unknown option: '$(warning "${1}")'"
echo -e "Run '$(info "${BASE_FILE} --help")' for more information."
exit 2
;;
esac
done
if [[ $# -gt 1 ]]; then
usage
fi
[[ ${1:-up} = "down" ]] && cs_down || cs_up