-
Notifications
You must be signed in to change notification settings - Fork 5
/
install.sh
executable file
·366 lines (309 loc) · 8.68 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
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
#!/bin/bash
# Make script agnostic of direct or `bash -c` calls.
if [[ "$0" != *install.sh ]]
then
set -- "$0" "$@"
fi
################
## Configuration
################
# Default Octave Docker image to be used.
OCTAVE_VERSION="9.3.0"
OCTAVE_IMAGE="docker.io/gnuoctave/octave"
# Choose default container tool in this order.
CONTAINER_TOOL_HIERARCHY="docker, podman, singularity"
#############
## Path setup
#############
# https://freedesktop.org/wiki/Specifications/menu-spec/
# Version 1.1 20 August 2016
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
BIN_DIR=$HOME/bin
APP_DIR=$XDG_DATA_HOME/applications
ICON_DIR=$XDG_DATA_HOME/icons/hicolor/128x128/apps
# Ensure directories to exist.
mkdir -p $BIN_DIR
mkdir -p $APP_DIR
mkdir -p $ICON_DIR
function usage()
{
if [ -n "$1" ]
then
echo -e "\nError: $1"
fi
echo -e "\nUsage: install.sh with options:\n"
echo -e "\t-d --debug verbose debug output"
echo -e "\t-f --force overwrite previous installations"
echo -e "\t-h --help show this help"
echo -e "\t-t --container-tool one of $CONTAINER_TOOL_HIERARCHY"
echo -e "\t-u --uninstall only uninstall previous setups\n"
exit 1
}
###################################
## Command-line argument processing
###################################
DEBUG=false
FORCE=false
QUIET_FLAG="--quiet"
UNINSTALL_ONLY=false
while [ "$1" != "" ]
do
case $1 in
-d|--debug)
DEBUG=true
QUIET_FLAG=""
shift
;;
-f|--force)
FORCE=true
shift
;;
-t|--container-tool)
CONTAINER_TOOL="$2"
shift
shift
;;
-u|--uninstall)
UNINSTALL_ONLY=true
shift
;;
-h|--help)
usage
;;
*)
usage "invalid input '$1'."
;;
esac
done
# Debug output function.
function DEBUG_MSG()
{
if $DEBUG
then
echo -e "\nDEBUG: $1\n"
fi
}
################################
## Detect previous installations
################################
PREV_INSTALL=""
INSTALLED_FILES=" \
$BIN_DIR/mkoctfile
$BIN_DIR/octave
$BIN_DIR/octave-config
$BIN_DIR/octave-cli
$BIN_DIR/octave-docker-entrypoint.sh
$APP_DIR/octave-docker.desktop
$ICON_DIR/octave-logo-128.png"
for f in $INSTALLED_FILES
do
if [ -f "$f" ]
then
PREV_INSTALL+=" $f "
fi
done
if [ -n "$PREV_INSTALL" ]
then
# Report previous installation.
if $FORCE
then
echo -e "\nFound previous installation (will be overwritten):\n"
else
echo -e "\nFound previous installation:\n"
fi
for f in $PREV_INSTALL
do
echo " $f"
done
echo " "
# Ask user for confirmation (regard -f --force).
if ! $FORCE
then
while true; do
read -p "Enter [c] to cancel and [d] to delete previous installations. " yn
case $yn in
[Cc]*)
echo -e "\nInstallation canceled.\n"
exit 1
;;
[Dd]*)
break
;;
*)
echo "Please answer [c] or [d]."
;;
esac
done
fi
for f in $PREV_INSTALL
do
rm -f "$f"
done
fi
# Finish here if only uninstall was requested.
if $UNINSTALL_ONLY
then
echo -e "\nUninstall finished.\n"
echo -e " - Please delete Docker/Podman images (docker rmi ...)" \
" or any singularity SIF-files in ${BIN_DIR} manually.\n"
exit 0
fi
#####################################
## Container tool detection and setup
#####################################
if [ -z $CONTAINER_TOOL ]
then
for t in $(echo $CONTAINER_TOOL_HIERARCHY | tr "," "\n")
do
# Use first existing tool
if type "$t" &> /dev/null
then
CONTAINER_TOOL=$t
break
fi
done
if [ -z $CONTAINER_TOOL ]
then
usage "No container tool ($CONTAINER_TOOL_HIERARCHY) could be detected." \
"Please install one of them."
fi
else
if ! type "$CONTAINER_TOOL" &> /dev/null
then
echo -e "\nError: '$CONTAINER_TOOL' cannot be found,"\
"please choose another container tool."
usage
fi
fi
DEBUG_MSG "use '$CONTAINER_TOOL' as container tool."
# Setup for the container tool.
case $CONTAINER_TOOL in
"docker" | "podman")
PULL_CMD="$CONTAINER_TOOL pull $QUIET_FLAG $OCTAVE_IMAGE:$OCTAVE_VERSION"
RUN_CMD="$CONTAINER_TOOL run \$DOCKER_INTERACTIVE \\
--rm \\
--network=host \\
--env=\"DISPLAY\" \\
--env=\"HOME=\$HOME\" \\
--env=\"XDG_RUNTIME_DIR=\$XDG_RUNTIME_DIR\" \\
--volume=\"\$HOME:\$HOME:rw\" \\
--volume=\"\$OCTAVE_CONF_DIR_HOST:\$OCTAVE_CONF_DIR:rw\" \\
--volume=\"/dev:/dev:rw\" \\
--volume=\"/run/user:/run/user:rw\" \\
--volume=\"$BIN_DIR/octave-docker-entrypoint.sh:/entrypoint.sh:ro\" \\
--entrypoint=\"/entrypoint.sh\" \\
--workdir=\"\$HOME\" \\
$OCTAVE_IMAGE:\$OCTAVE_VERSION"
;;
"singularity")
SIF_FILE="$BIN_DIR/octave_${OCTAVE_VERSION}.sif"
PULL_CMD="$CONTAINER_TOOL pull --disable-cache $SIF_FILE \
${OCTAVE_IMAGE/docker.io/docker:/}:${OCTAVE_VERSION}"
R_CMD="--bind /run/user,\$OCTAVE_CONF_DIR_HOST:\$OCTAVE_CONF_DIR $SIF_FILE"
RUN_CMD="$CONTAINER_TOOL exec $R_CMD"
;;
*)
echo -e "\nError: invalid container tool '$CONTAINER_TOOL'."
usage
esac
###################
## New installation
###################
# Get images
echo -e "\nPull '$OCTAVE_IMAGE:$OCTAVE_VERSION' image with '$CONTAINER_TOOL'...\n"
bash -c "$PULL_CMD"
# Install start scripts.
function get_docker_entrypoint()
{
echo "#!/bin/bash
# User is resolved during installation.
# Entrypoint is executed inside running container.
groupadd -g $(id -g) $(id -gn) 2> /dev/null || groupmod -n $(id -gn) ubuntu
useradd -g $(id -g) -u $(id -u) -G sudo $(id -un) 2> /dev/null || usermod -l $(id -un) ubuntu
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
CMD=\${1##*/}
shift
sudo -E -u $(id -un) \${CMD} \$@
"
}
function get_Octave_start_script()
{
echo "#!/bin/bash
OCTAVE_VERSION=\"\${OCTAVE_VERSION:-\"$OCTAVE_VERSION\"}\"
DOCKER_INTERACTIVE=\"-it\"
for arg in \"\$@\"
do
if [[ \"\$arg\" == \"--gui\" ]]
then
DOCKER_INTERACTIVE=\"\"
fi
# Old Octave qt4 builds
if [[ \"\$arg\" == \"--force-gui\" ]]
then
DOCKER_INTERACTIVE=\"--env=QT_GRAPHICSSYSTEM=native\"
fi
done
## Avoid collisions with different 'OCTAVE_VERSION's
XDG_CONFIG_HOME=\"\${XDG_CONFIG_HOME:-\$HOME/.config}\"
OCTAVE_CONF_DIR=\"\$XDG_CONFIG_HOME/octave\"
OCTAVE_CONF_DIR_HOST=\"\$OCTAVE_CONF_DIR/\$OCTAVE_VERSION\"
mkdir -p \"\$OCTAVE_CONF_DIR_HOST\"
$RUN_CMD \"\${0##*/}\" \"\$@\"
"
}
get_docker_entrypoint > $BIN_DIR/octave-docker-entrypoint.sh
get_Octave_start_script > $BIN_DIR/octave
chmod u+x $BIN_DIR/octave-docker-entrypoint.sh
chmod u+x $BIN_DIR/octave
ln -sf $BIN_DIR/octave $BIN_DIR/mkoctfile
ln -sf $BIN_DIR/octave $BIN_DIR/octave-config
ln -sf $BIN_DIR/octave $BIN_DIR/octave-cli
# Install desktop file icon.
ICON_URL="https://raw.githubusercontent.com/gnu-octave/docker/main/assets"
WGET_FLAGS="--directory-prefix=$ICON_DIR $QUIET_FLAG"
if [ ! -f "$ICON_DIR/octave-logo-128.png" ]
then
wget $WGET_FLAGS $ICON_URL/octave-logo-128.png
fi
# Install desktop file.
function get_Octave_desktop_file()
{
echo "#!/usr/bin/env xdg-open
[Desktop Entry]
Categories=Education;Science;Math;
Comment=Interactive programming environment for numerical computations
Comment[ca]=Entorn de programació interactiva per a càlculs numèrics
Comment[de]=Interaktive Programmierumgebung für numerische Berechnungen
Comment[es]=Entorno de programación interactiva para cálculos numéricos
Comment[fr]=Environnement de programmation interactif pour le calcul numérique
Comment[it]=Ambiente di programmazione interattivo per il calcolo numerico
Comment[ja]=数値計算のための対話的なプログラミング環境
Comment[nl]=Interactieve programmeeromgeving voor numerieke berekeningen
Comment[pt]=Ambiente de programação interativo para computação numérica
Comment[zh]=数值计算交互式编程环境
Exec=$BIN_DIR/octave --gui -q %f
GenericName=GNU Octave
Icon=$ICON_DIR/octave-logo-128.png
MimeType=text/x-octave;text/x-matlab;
Name=GNU Octave
StartupNotify=true
Terminal=false
Type=Application
StartupWMClass=octave-gui
Keywords=science;math;matrix;numerical computation;plotting;"
}
TMP_DESKTOP_FILE="$(mktemp -d)/octave-docker.desktop"
get_Octave_desktop_file > ${TMP_DESKTOP_FILE}
if $DEBUG
then
desktop-file-validate ${TMP_DESKTOP_FILE}
fi
desktop-file-install \
--dir=$APP_DIR \
--delete-original \
--rebuild-mime-info-cache \
${TMP_DESKTOP_FILE}
echo -e "\nInstallation was successful."
echo -e "\n Run Octave with:\n\n\t$BIN_DIR/octave --gui"
echo -e "\n Desktop launcher has been created.\n\n"