forked from radiant-rstats/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch-rsm-jupyterhub.sh
executable file
·403 lines (371 loc) · 14.8 KB
/
launch-rsm-jupyterhub.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
## set ARG_HOME to a directory of your choosing if you do NOT
## want to to map the docker home directory to your local
## home directory
## use the command below on macOS or Linux to setup a 'launch'
## command. You can then use that command, e.g., launch ., to
## launch the container from any directory
## ln -s ~/git/docker/launch-rsm-jupyterhub.sh /usr/local/bin/launch
## to map the directory where the launch script is located to
## the docker home directory call the script_home function
script_home () {
echo "$(echo "$( cd "$(dirname "$0")" ; pwd -P )" | sed -E "s|^/([A-z]{1})/|\1:/|")"
}
function launch_usage() {
echo "Usage: $0 [-t tag (version)] [-d directory]"
echo " -t, --tag Docker image tag (version) to use"
echo " -d, --directory Base directory to use"
echo " -v, --volume Volume to mount as home directory"
echo " -s, --show Show all output generated on launch"
echo " -h, --help Print help and exit"
echo ""
echo "Example: $0 --tag 1.8.0 --directory ~/project_1"
echo ""
exit 1
}
## parse command-line arguments
while [[ "$#" > 0 ]]; do case $1 in
-t|--tag) ARG_TAG="$2"; shift;shift;;
-d|--directory) ARG_DIR="$2";shift;shift;;
-v|--volume) ARG_VOLUME="$2";shift;shift;;
-s|--show) ARG_SHOW="show";shift;shift;;
-h|--help) launch_usage;shift; shift;;
*) echo "Unknown parameter passed: $1"; echo ""; launch_usage; shift; shift;;
esac; done
## some cleanup on exit
function finish {
if [ "$ARG_HOME" != "" ]; then
echo "Removing empty files and directories ..."
find "$ARG_HOME" -empty -type d -delete
find "$ARG_HOME" -empty -type f -delete
fi
}
trap finish EXIT
## change to some other path to use as default
# ARG_HOME="~/rady"
# ARG_HOME="$(script_home)"
ARG_HOME=""
IMAGE_VERSION="latest"
NB_USER="jovyan"
CODE_WORKINGDIR="/home/${NB_USER}/git"
ID="vnijs"
LABEL="rsm-jupyterhub"
IMAGE=${ID}/${LABEL}
if [ "$ARG_TAG" != "" ]; then
IMAGE_VERSION="$ARG_TAG"
DOCKERHUB_VERSION=${IMAGE_VERSION}
else
## see https://stackoverflow.com/questions/34051747/get-environment-variable-from-docker-container
DOCKERHUB_VERSION=$(docker inspect -f '{{range $index, $value := .Config.Env}}{{println $value}} {{end}}' ${IMAGE}:${IMAGE_VERSION} | grep DOCKERHUB_VERSION)
DOCKERHUB_VERSION="${DOCKERHUB_VERSION#*=}"
fi
POSTGRES_VERSION=12
## what os is being used
ostype=`uname`
if [ "$ostype" == "Darwin" ]; then
EXT="command"
else
EXT="sh"
fi
## check if script is already running and using port 8787
curl -S localhost:8765 2>/dev/null
ret_code=$?
if [ "$ret_code" == 0 ]; then
echo "-----------------------------------------------------------------------"
echo "A launch script may already be running. To close the new session and"
echo "continue with the previous session press q + enter. To continue with"
echo "the new session and stop the previous session, press enter"
echo "-----------------------------------------------------------------------"
read contd
if [ "${contd}" == "q" ]; then
exit 1
fi
fi
## script to start Radiant, Rstudio, and JupyterLab
if [ "$ARG_SHOW" != "show" ]; then
clear
fi
has_docker=$(which docker)
if [ "${has_docker}" == "" ]; then
echo "-----------------------------------------------------------------------"
echo "Docker is not installed. Download and install Docker from"
if [[ "$ostype" == "Linux" ]]; then
echo "https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04"
elif [[ "$ostype" == "Darwin" ]]; then
echo "https://download.docker.com/mac/stable/Docker.dmg"
else
echo "https://store.docker.com/editions/community/docker-ce-desktop-windows"
fi
echo "-----------------------------------------------------------------------"
read
else
## check docker is running at all
## based on https://stackoverflow.com/questions/22009364/is-there-a-try-catch-command-in-bash
{
docker ps -q 2>/dev/null
} || {
if [[ "$ostype" == "Darwin" ]]; then
## from https://stackoverflow.com/a/48843074/1974918
# On Mac OS this would be the terminal command to launch Docker
open /Applications/Docker.app
#Wait until Docker daemon is running and has completed initialisation
while (! docker stats --no-stream 2>/dev/null); do
echo "Please wait while Docker starts up ..."
sleep 1
done
else
echo "-----------------------------------------------------------------------"
echo "Docker is not running. Please start docker on your computer"
echo "When docker has finished starting up press [ENTER] to continue"
echo "-----------------------------------------------------------------------"
read
fi
}
## kill running containers
running=$(docker ps -q)
if [ "${running}" != "" ]; then
echo "-----------------------------------------------------------------------"
echo "Stopping running containers"
echo "-----------------------------------------------------------------------"
docker stop ${running}
fi
available=$(docker images -q ${IMAGE}:${IMAGE_VERSION})
if [ "${available}" == "" ]; then
echo "-----------------------------------------------------------------------"
echo "Downloading the ${LABEL}:${IMAGE_VERSION} computing environment"
echo "-----------------------------------------------------------------------"
docker logout
docker pull ${IMAGE}:${IMAGE_VERSION}
fi
## function is not efficient by alias has scopping issues
if [[ "$ostype" == "Linux" ]]; then
HOMEDIR=~
ID=$USER
open_browser () {
xdg-open $1
}
sed_fun () {
sed -i $1 "$2"
}
MNT="-v /media:/media"
is_wsl=$(which explorer.exe)
if [[ "$is_wsl" != "" ]]; then
ostype="WSL2"
HOMEDIR="/mnt/c/Users/$USER"
if [ -d "/mnt/c" ]; then
MNT="$MNT -v /mnt/c:/mnt/c"
fi
if [ -d "/mnt/d" ]; then
MNT="$MNT -v /mnt/d:/mnt/d"
fi
fi
elif [[ "$ostype" == "Darwin" ]]; then
ostype="macOS"
HOMEDIR=~
ID=$USER
open_browser () {
open $1
}
sed_fun () {
sed -i '' -e $1 "$2"
}
MNT="-v /Volumes:/media/Volumes"
else
ostype="Windows"
HOMEDIR="C:/Users/$USERNAME"
ID=$USERNAME
open_browser () {
start $1
}
sed_fun () {
sed -i $1 "$2"
}
MNT=""
fi
if [ "$ARG_VOLUME" != "" ]; then
HOMEDIR="$ARG_VOLUME"
fi
if [ "$ARG_DIR" != "" ] || [ "$ARG_HOME" != "" ]; then
## change mapping of docker home directory to local directory if specified
if [ "${ARG_HOME}" != "" ] && [ ! -d "${ARG_HOME}" ]; then
echo "The directory ${ARG_HOME} does not yet exist."
echo "Please create the directory and restart the launch script"
sleep 5s
exit 1
fi
if [ "$ARG_DIR" != "" ]; then
if [ ! -d "${ARG_DIR}" ]; then
echo "The directory ${ARG_DIR} does not yet exist."
echo "Please create the directory and restart the launch script"
sleep 5s
exit 1
fi
ARG_HOME="$(cd "$ARG_DIR"; pwd)"
## https://unix.stackexchange.com/questions/295991/sed-error-1-not-defined-in-the-re-under-os-x
ARG_HOME="$(echo "$ARG_HOME" | sed -E "s|^/([A-z]{1})/|\1:/|")"
echo "---------------------------------------------------------------------------"
echo "Do you want to access git, ssh, and R configuration in this directory (y/n)"
echo "${ARG_HOME}"
echo "---------------------------------------------------------------------------"
read copy_config
else
copy_config="y"
fi
# setup working directory for vscode
if [ "${HOMEDIR}" != "${ARG_HOME}" ]; then
CODE_WORKINGDIR="/home/${NB_USER}"
fi
if [ "${copy_config}" == "y" ]; then
if [ -f "${HOMEDIR}/.inputrc" ] && [ ! -s "${ARG_HOME}/.inputrc" ]; then
MNT="$MNT -v ${HOMEDIR}/.inputrc:/home/$NB_USER/.inputrc"
fi
if [ -f "${HOMEDIR}/.Rprofile" ] && [ ! -s "${ARG_HOME}/.Rprofile" ]; then
MNT="$MNT -v ${HOMEDIR}/.Rprofile:/home/$NB_USER/.Rprofile"
fi
if [ -f "${HOMEDIR}/.Renviron" ] && [ ! -s "${ARG_HOME}/.Renviron" ]; then
MNT="$MNT -v ${HOMEDIR}/.Renviron:/home/$NB_USER/.Renviron"
fi
if [ -f "${HOMEDIR}/.gitconfig" ] && [ ! -s "${ARG_HOME}/.gitconfig" ]; then
MNT="$MNT -v ${HOMEDIR}/.gitconfig:/home/$NB_USER/.gitconfig"
fi
if [ -d "${HOMEDIR}/.ssh" ]; then
if [ ! -d "${ARG_HOME}/.ssh" ] || [ ! "$(ls -A $ARG_HOME/.ssh)" ]; then
MNT="$MNT -v ${HOMEDIR}/.ssh:/home/$NB_USER/.ssh"
fi
fi
fi
if [ ! -f "${ARG_HOME}/.gitignore" ]; then
## make sure no hidden files go into a git repo
touch "${ARG_HOME}/.gitignore"
echo ".*" >> "${ARG_HOME}/.gitignore"
fi
if [ -d "${HOMEDIR}/.R" ]; then
if [ ! -d "${ARG_HOME}/.R" ] || [ ! "$(ls -A $ARG_HOME/.R)" ]; then
MNT="$MNT -v ${HOMEDIR}/.R:/home/$NB_USER/.R"
fi
fi
if [ -d "${HOMEDIR}/Dropbox" ]; then
if [ ! -d "${ARG_HOME}/Dropbox" ] || [ ! "$(ls -A $ARG_HOME/Dropbox)" ]; then
MNT="$MNT -v ${HOMEDIR}/Dropbox:/home/$NB_USER/Dropbox"
sed_fun '/^Dropbox$/d' "${ARG_HOME}/.gitignore"
echo "Dropbox" >> "${ARG_HOME}/.gitignore"
fi
fi
if [ -d "${HOMEDIR}/.rstudio" ] && [ ! -d "${ARG_HOME}/.rstudio" ]; then
echo "-----------------------------------------------------------------------"
echo "Copying Rstudio and JupyterLab settings to:"
echo "${ARG_HOME}"
echo "-----------------------------------------------------------------------"
{
which rsync 2>/dev/null
HD="$(echo "$HOMEDIR" | sed -E "s|^([A-z]):|/\1|")"
AH="$(echo "$ARG_HOME" | sed -E "s|^([A-z]):|/\1|")"
rsync -a "${HD}/.rstudio" "${AH}/" --exclude sessions --exclude projects --exclude projects_settings
} ||
{
cp -r "${HOMEDIR}/.rstudio" "${ARG_HOME}/.rstudio"
rm -rf "${ARG_HOME}/.rstudio/sessions"
rm -rf "${ARG_HOME}/.rstudio/projects"
rm -rf "${ARG_HOME}/.rstudio/projects_settings"
}
fi
if [ -d "${HOMEDIR}/.rsm-msba" ] && [ ! -d "${ARG_HOME}/.rsm-msba" ]; then
{
which rsync 2>/dev/null
HD="$(echo "$HOMEDIR" | sed -E "s|^([A-z]):|/\1|")"
AH="$(echo "$ARG_HOME" | sed -E "s|^([A-z]):|/\1|")"
rsync -a "${HD}/.rsm-msba" "${AH}/" --exclude R --exclude bin --exclude lib --exclude share
} ||
{
cp -r "${HOMEDIR}/.rsm-msba" "${ARG_HOME}/.rsm-msba"
rm -rf "${ARG_HOME}/.rsm-msba/R"
rm -rf "${ARG_HOME}/.rsm-msba/bin"
rm -rf "${ARG_HOME}/.rsm-msba/lib"
rm_list=$(ls "${ARG_HOME}/.rsm-msba/share" | grep -v jupyter | grep -v code-server)
for i in ${rm_list}; do
rm -rf "${ARG_HOME}/.rsm-msba/share/${i}"
done
}
fi
SCRIPT_HOME="$(script_home)"
if [ "${SCRIPT_HOME}" != "${ARG_HOME}" ]; then
cp -p "$0" "${ARG_HOME}/launch-${LABEL}.${EXT}"
sed_fun "s+^ARG_HOME\=\".*\"+ARG_HOME\=\"\$\(script_home\)\"+" "${ARG_HOME}/launch-${LABEL}.${EXT}"
if [ "$ARG_TAG" != "" ]; then
sed_fun "s/^IMAGE_VERSION=\".*\"/IMAGE_VERSION=\"${IMAGE_VERSION}\"/" "${ARG_HOME}/launch-${LABEL}.${EXT}"
fi
fi
HOMEDIR="${ARG_HOME}"
fi
RPROF="${HOMEDIR}/.Rprofile"
touch "${RPROF}"
if ! grep -q 'radiant.report = TRUE' ${RPROF}; then
echo "Your setup does not allow report generation in Radiant."
echo "Would you like to add relevant code to .Rprofile?"
echo "Press y or n, followed by [ENTER]:"
echo ""
read allow_report
if [ "${allow_report}" == "y" ]; then
## Windows does not reliably use newlines with printf
sed_fun '/^options(radiant.maxRequestSize/d' "${RPROF}"
sed_fun '/^options(radiant.report/d' "${RPROF}"
sed_fun '/^options(radiant.shinyFiles/d' "${RPROF}"
sed_fun '/^#.*List.*specific.*directories.*you.*want.*to.*use.*with.*radiant/d' "${RPROF}"
sed_fun '/^#.*options(radiant\.sf_volumes.*=.*c(Git.*=.*"\/home\/jovyan\/git"))/d' "${RPROF}"
echo 'options(radiant.maxRequestSize = -1)' >> "${RPROF}"
echo 'options(radiant.report = TRUE)' >> "${RPROF}"
echo 'options(radiant.shinyFiles = TRUE)' >> "${RPROF}"
echo '# List specific directories you want to use with radiant' >> "${RPROF}"
echo '# options(radiant.sf_volumes = c(Git = "/home/jovyan/git"))' >> "${RPROF}"
echo '' >> "${RPROF}"
sed_fun '/^[\s]*$/d' "${RPROF}"
fi
fi
BUILD_DATE=$(docker inspect -f '{{.Created}}' ${IMAGE}:${IMAGE_VERSION})
has_volume=$(docker volume ls | awk "/pg_data/" | awk '{print $2}')
if [ "${has_volume}" == "" ]; then
docker volume create --name=pg_data
fi
echo "-----------------------------------------------------------------------"
echo "Starting the ${LABEL} computing environment on ${ostype}"
echo "Version : ${DOCKERHUB_VERSION}"
echo "Build date: ${BUILD_DATE//T*/}"
echo "Base dir. : ${HOMEDIR}"
echo "-----------------------------------------------------------------------"
{
docker run --rm -p 127.0.0.1:8888:8888 -p 127.0.0.1:8765:8765 \
-e NB_USER=0 -e NB_UID=1002 -e NB_GID=1002 \
-e CODE_WORKINGDIR=" ${CODE_WORKINGDIR}" \
-v "${HOMEDIR}":/home/${NB_USER} $MNT \
-v pg_data:/var/lib/postgresql/${POSTGRES_VERSION}/main \
${IMAGE}:${IMAGE_VERSION}
} || {
echo "-----------------------------------------------------------------------"
echo "It seems there was a problem starting the docker container. Please"
echo "report the issue and add a screenshot of any messages shown on screen."
echo "Press [ENTER] to continue"
echo "-----------------------------------------------------------------------"
read
}
## make sure abend is set correctly
## https://community.rstudio.com/t/restarting-rstudio-server-in-docker-avoid-error-message/10349/2
rstudio_abend () {
if [ -d "${HOMEDIR}/.rstudio/sessions/active" ]; then
RSTUDIO_STATE_FILES=$(find "${HOMEDIR}/.rstudio/sessions/active/*/session-persistent-state" -type f 2>/dev/null)
if [ "${RSTUDIO_STATE_FILES}" != "" ]; then
sed_fun 's/abend="1"/abend="0"/' ${RSTUDIO_STATE_FILES}
fi
fi
if [ -d "${HOMEDIR}/.rstudio/monitored/user-settings" ]; then
touch "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
sed_fun '/^alwaysSaveHistory="[0-1]"/d' "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
sed_fun '/^loadRData="[0-1]"/d' "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
sed_fun '/^saveAction=/d' "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
echo 'alwaysSaveHistory="1"' >> "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
echo 'loadRData="0"' >> "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
echo 'saveAction="0"' >> "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
sed_fun '/^$/d' "${HOMEDIR}/.rstudio/monitored/user-settings/user-settings"
fi
}
rstudio_abend
fi