-
Notifications
You must be signed in to change notification settings - Fork 7
/
install.sh
executable file
·282 lines (235 loc) · 7.44 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
#!/bin/bash
OS=""
PACKAGE_SYSTEM=""
UNAME=`uname`
DATE_STRING=`date +"%Y-%m-%d_%H%M%S"`
LOG_FILE="/tmp/MLC_install_log_$DATE_STRING.log"
FILE_LOG_LEVEL=7
CONSOLE_LOG_LEVEL=5
MLC_PYTHON_DIR=/opt/mlc-python-2.7.11
UNINSTALL=0
INSTALLED=0
PACKAGE_PATH=""
MATLAB_DIR=""
PYTHON_ENG_DIR="extern/engines/python"
function usage() {
log_message 5 "MLC Installer. Available options:"
log_message 5 " -h: Show this help"
log_message 5 " -u: uninstall MLC"
log_message 5 " -p <file>: MLC's [deb|RPM] file. Mandatory!"
log_message 5 " -d <dir>: Absolute path where MATLAB is installed in the system. Mandatory!"
}
# Check if the OS where this script is running is a Linux like flavor
function check_os() {
if [ x"$UNAME" = "xLinux" ]; then
return 0
fi
log_message 1 "ERROR: The present OS is not a Linux like system. Aborting installation."
exit 1
}
function get_linux_flavor() {
# grep return 0 if any line was selected
if egrep -q -i "centos|rhel|fedora" /etc/os-release; then
PACKAGE_SYSTEM="RPM"
elif egrep -q -i "ubuntu|debian" /etc/os-release; then
# TODO: Find out how to identify another RPM OSs like Debian or Mint
PACKAGE_SYSTEM="DEB"
fi
if [ x"$PACKAGE_SYSTEM" = "x" ]; then
log_message 1 "Could not identify Linux package system. Aborting instalation."
exit 1
else
log_message 5 "$PACKAGE_SYSTEM Package System detected."
fi
}
# LOG LEVELS:
# 0: EMERGENCY
# 1: CRITICAL
# 2: ERROR
# 3: WARNING
# 4: NOTICE
# 5: INFO
# 6: DEBUG
# 7: TRACE
function log_message() {
LOG_LEVEL=$1
LOG_MESSAGE=$2
if [ $LOG_LEVEL -le $FILE_LOG_LEVEL ]; then
echo -e "$LOG_MESSAGE" >> $LOG_FILE
fi
if [ $LOG_LEVEL -le $CONSOLE_LOG_LEVEL ]; then
echo -e "$LOG_MESSAGE"
fi
}
function check_run_as_root() {
case `id` in
uid=0*) ;;
*)
log_message 1 "ERROR: Must be root to run script (use -h for help). Aborting installation"
exit 1
;;
esac
}
function parse_args() {
log_message 7 "Checking command line arguments"
# Needed to make getopts work
local OPTIND opt
OPTSPEC=":hup:d:"
while getopts $OPTSPEC opt; do
case "${opt}" in
h)
usage
exit 0
;;
u)
UNINSTALL=1
;;
p)
if [ -z "$OPTARG" ]; then
log_message 1 "option '-p' requires an argument"
usage
exit 1
fi
PACKAGE_PATH=$OPTARG
;;
d)
if [ -z "$OPTARG" ]; then
log_message 1 "option '-d' requires an argument"
usage
exit 1
fi
MATLAB_DIR=$OPTARG
;;
\?)
log_message 1 "ERROR: Invalid option received"
usage
exit 1
;;
esac
done
if [ $UNINSTALL -eq 1 ]; then
return
fi
if [ x"$PACKAGE_PATH" = "x" ]; then
log_message 1 "Package path must be supplied."
usage
exit 1
fi
if [ x"$MATLAB_DIR" = "x" ]; then
log_message 1 "MATLAB dir must be supplied."
usage
exit 1
fi
}
function check_previous_installation() {
log_message 5 "Searching for previous version of MLC"
if [ -d $MLC_PYTHON_DIR ]; then
log_message 5 "Previous installation of MLC detected"
INSTALLED=1
fi
}
# For the moment, there will be always only a version of mlc.
# This version always will be installed in /opt/mlc-python-2.7.11
function uninstall() {
log_message 5 "Preparing for uninstall"
if [ $PACKAGE_SYSTEM = "DEB" ]; then
MLC_PYTHON=`dpkg -l | grep mlc-python | awk '{print $2}'`
MLC_VERSION=`dpkg -l | grep mlc-python | awk '{print $3}'`
elif [ $PACKAGE_SYSTEM = "RPM" ]; then
MLC_PYTHON="mlc-python"
RPM_PACKAGE=`rpm -qa | grep $MLC_PYTHON`
MLC_VERSION=`yum info $RPM_PACKAGE | grep -i "version" | awk -F ':' '{print $2}' | sed 's/ //g'`
fi
if [ x"$MLC_PYTHON" != "x" ]; then
log_message 5 "Version $MLC_VERSION of MLC found. Proceed to uninstall."
# Redirect stderr to stdout and the output to /dev/null to avoid annoying warnings
if [ $PACKAGE_SYSTEM = "DEB" ]; then
dpkg -r $MLC_PYTHON > /dev/null 2>&1
elif [ $PACKAGE_SYSTEM = "RPM" ]; then
rpm -e "$MLC_PYTHON"
fi
else
log_message 3 "No version of MLC was found on the database. Aborting uninstalling."
exit 1
fi
if [ -d $MLC_PYTHON_DIR ]; then
log_message 5 "MLC python root dir wasn't removed after the package removal. Do you want to remove it? y[n]"
DEFAULT="y"
while true; do
read -p "" RESPONSE
RESPONSE=${RESPONSE:=$DEFAULT}
case $RESPONSE in
[yn]*) break;;
*) log_message 4 "Please answer y or n";;
esac
done
if [ $RESPONSE = y ]; then
log_message 5 "Proceed to remove MLC python root dir"
rm -rf $MLC_PYTHON_DIR
fi
fi
log_message 5 "MLC Python was succesfully uninstalled."
}
function install_mlc() {
# Check if the package exists
if [ ! -f $PACKAGE_PATH ]; then
log_message 1 "Package path doesn't exists. Aborting installation."
exit 1
fi
# Check if MATLAB dir exists
if [ ! -d $MATLAB_DIR ]; then
log_message 1 "MATLAB dir doesn't exists. Aborting installation."
fi
# Check that the python engine is installed
if [ ! -d "$MATLAB_DIR/$PYTHON_ENG_DIR" ]; then
log_message 1 "MATLAB version isn't shipped with Python Engine module. Check that your MATLAB version is superior to version 2014a"
fi
log_message 5 "Proceed to install MLC Python."
if [ $PACKAGE_SYSTEM = "DEB" ]; then
dpkg -i $PACKAGE_PATH
elif [ $PACKAGE_SYSTEM = "RPM" ]; then
rpm -ivh $PACKAGE_PATH
fi
if [ $? -ne 0 ]; then
log_message 1 "An error ocurred while installing MLC Package. Aborting installation"
exit 1
fi
# Move to Python engine directory to install the Engine module. It doesn't work in another way
cd "$MATLAB_DIR/$PYTHON_ENG_DIR"
#Remove the build directory if it exists
rm -rf "$MATLAB_DIR/$PYTHON_ENG_DIR/build"
$MLC_PYTHON_DIR/bin/mlc_python "$MATLAB_DIR/$PYTHON_ENG_DIR/setup.py" build > /dev/null 2>&1
if [ $? -ne 0 ]; then
log_message 1 "An error ocurred while building MATLAB Python Package. Aborting installation"
UNINSTALL=1
uninstall
exit 1
fi
$MLC_PYTHON_DIR/bin/mlc_python "$MATLAB_DIR/$PYTHON_ENG_DIR/setup.py" install > /dev/null 2>&1
if [ $? -ne 0 ]; then
log_message 1 "An error ocurred while installing MATLAB Python Package. Aborting installation"
UNINSTALL=1
uninstall
exit 1
fi
log_message 5 "MLC Python succesfully installed"
}
function main() {
log_message 4 "############################"
log_message 4 "# MLC Installer #"
log_message 4 "############################"
log_message 4 "Saving log to $LOG_FILE"
check_os
get_linux_flavor
if [ $# -ne 1 -o "$1" != "-h" ]; then
check_run_as_root
fi
parse_args $*
check_previous_installation
if [ $UNINSTALL -eq 1 ]; then
uninstall
else
install_mlc
fi
}
main $*