Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
1413 richard 1
#!/bin/sh
2
 
3
#########################################################
4
#
5
# Installation script for SURFmap.
6
#
7
# Copyright (C) 2013 INVEA-TECH a.s.
8
# Author(s):    Rick Hofstede   <r.j.hofstede@utwente.nl>
9
#               Pavel Celeda    <celeda@invea-tech.com>
10
#
11
# LICENSE TERMS - 3-clause BSD license
12
#
13
# $Id: install.sh 626 2014-06-22 07:48:21Z rickhofstede $
14
#
15
#########################################################
16
 
17
SURFMAP_VER=3.3
18
SURFMAP_REL=SURFmap_v${SURFMAP_VER}.tar.gz
19
TMP_DIR=SURFmap
20
GEO_DB=GeoLiteCity.dat.gz
21
GEOv6_DB=GeoLiteCityv6.dat.gz
22
 
23
err () {
24
    printf "ERROR: ${*}\n"
25
    exit 1
26
}
27
 
28
err_line () {
29
    echo "-----"
30
    printf "ERROR: ${*}\n"
31
    exit 1
32
}
33
 
34
FRONTEND_ONLY=0             # Install/update frontend only
35
BACKEND_ONLY=0              # Install/update backend only
36
NFSEN_CONF_OVERWRITE=""     # Overwrite path to nfsen.conf (i.e., don't determine path automatically)
37
 
38
# http://wiki.bash-hackers.org/howto/getopts_tutorial
39
while getopts ":fbn:" opt; do
40
    case $opt in
41
        f)
42
            FRONTEND_ONLY=1
43
            ;;
44
        b)
45
            BACKEND_ONLY=1
46
            ;;
47
        n)
48
            NFSEN_CONF_OVERWRITE=$OPTARG
49
            ;;
50
        \?)
51
            err "Invalid option: -$OPTARG"
52
            exit 1
53
            ;;
54
        :)
55
            err "Option -$OPTARG requires an argument. Exiting..."
56
            exit 1
57
            ;;
58
    esac
59
done
60
 
61
if [ $FRONTEND_ONLY = 1 -a $BACKEND_ONLY = 1 ]; then
62
    err "You have specified two excluding options (-f and -b)."
63
fi
64
 
65
# Determine (based on combination of input parameters) whether frontend has to be installed
66
if [ $FRONTEND_ONLY = 1 ] || [ $FRONTEND_ONLY = 0 -a $BACKEND_ONLY = 0 ]; then
67
    INSTALL_FRONTEND=1
68
else
69
    INSTALL_FRONTEND=0
70
fi
71
 
72
# Determine (based on combination of input parameters) whether backend has to be installed
73
if [ $BACKEND_ONLY = 1 ] || [ $FRONTEND_ONLY = 0 -a $BACKEND_ONLY = 0 ]; then
74
    INSTALL_BACKEND=1
75
else
76
    INSTALL_BACKEND=0
77
fi
78
 
79
echo "SURFmap installation script"
80
echo "---------------------------"
81
 
82
# Discover NfSen configuration
83
NFSEN_VARFILE=/tmp/nfsen-tmp.conf
84
if [ ! -n "$(ps axo command | grep [n]fsend | grep -v nfsend-comm)" ]; then
85
    err "NfSen - nfsend not running; cannot detect nfsen.conf location. Exiting..."
86
fi
87
 
88
NFSEN_LIBEXECDIR=$(cat $(ps axo command= | grep -vE "(nfsend-comm|grep)" | grep -Eo "[^ ]+nfsend") | grep libexec | cut -d'"' -f2 | head -n 1)
89
 
90
if [ -z "${NFSEN_CONF_OVERWRITE}" ]; then
91
    NFSEN_CONF=$(cat ${NFSEN_LIBEXECDIR}/NfConf.pm | grep \/nfsen.conf | cut -d'"' -f2)
92
else
93
    NFSEN_CONF=$NFSEN_CONF_OVERWRITE
94
fi
95
 
96
# Parse nfsen.conf file
97
cat ${NFSEN_CONF} | grep -v \# | egrep '\$BASEDIR|\$BINDIR|\$LIBEXECDIR|\$HTMLDIR|\$FRONTEND_PLUGINDIR|\$BACKEND_PLUGINDIR|\$WWWGROUP|\$WWWUSER|\$USER' | tr -d ';' | tr -d ' ' | cut -c2- | sed 's,/",",g' > ${NFSEN_VARFILE}
98
. ${NFSEN_VARFILE}
99
rm -rf ${NFSEN_VARFILE}
100
 
101
SURFMAP_CONF=${FRONTEND_PLUGINDIR}/SURFmap/config.php
102
 
103
# Check permissions to install SURFmap - you must be ${USER} or root
104
if [ "$(id -u)" != "$(id -u ${USER})" ] && [ "$(id -u)" != "0" ]; then
105
    err "You do not have sufficient permissions to install SURFmap on this machine!"
106
fi
107
 
108
if [ "$(id -u)" = "$(id -u ${USER})" ]; then
109
    WWWUSER=${USER}     # we are installing as normal user
110
fi
111
 
112
# Download files from Web
113
if [ $(uname) = "FreeBSD" -o $(uname) = "OpenBSD" ]; then
114
    RETRIEVE_TOOL="fetch"
115
else
116
    RETRIEVE_TOOL="wget"
117
fi
118
 
119
 
120
# Move one directory-level up, if necessary, to be consistent with the case in which the tar-ball is unpacked (else-clause)
121
if [ -f install.sh ]; then
122
    cd ..
123
fi
124
 
125
# Check PHP dependencies
126
PHP_JSON=$(php -m | grep 'json' 2> /dev/null)
127
PHP_MBSTRING=$(php -m 2> /dev/null | grep 'mbstring')
128
PHP_PDOSQLITE=$(php -m 2> /dev/null | grep 'pdo_sqlite$') # The dollar-sign ($) makes sure that 'pdo_sqlite2' is not accepted
129
PHP_SOCKETS=$(php -m 2> /dev/null | grep '^sockets$')
130
PHP_XML=$(php -m 2> /dev/null | grep '^xml$')
131
 
132
if [ "$PHP_JSON" != "json" ]; then
133
    err "The PHP 'JSON' module is missing.\nDon't forget to restart your Web server after installing the package."
134
elif [ "$PHP_MBSTRING" != "mbstring" ]; then
135
    err "The PHP 'mbstring' module is missing.\nDon't forget to restart your Web server after installing the package."
136
elif [ "$PHP_PDOSQLITE" != "pdo_sqlite" ]; then
137
    err "The PHP PDO SQLite v3 module is missing.\nDon't forget to restart your Web server after installing the package."
138
elif [ "$PHP_SOCKETS" != "sockets" ]; then
139
    err "The PHP 'sockets' module is missing.\nDon't forget to restart your Web server after installing the package."
140
elif [ "$PHP_XML" != "xml" ]; then
141
    err "The PHP 'xml' module is missing.\nDon't forget to restart your Web server after installing the package."
142
fi
143
###################################################################################################
144
# Backup old SURFmap installation
145
SURFMAP_BACKUPDIR_FRONTEND=${FRONTEND_PLUGINDIR}/SURFmap-$(date +%s)
146
SURFMAP_BACKUPDIR_BACKEND=${BACKEND_PLUGINDIR}/SURFmap-$(date +%s)
147
if [ $INSTALL_FRONTEND = 1 -a -d ${FRONTEND_PLUGINDIR}/SURFmap ]; then
148
    echo "Backing up existing SURFmap (frontend) installation to ${SURFMAP_BACKUPDIR_FRONTEND}"
149
    mv ${FRONTEND_PLUGINDIR}/SURFmap ${SURFMAP_BACKUPDIR_FRONTEND}
150
fi
151
if [ $INSTALL_BACKEND = 1 -a -d ${BACKEND_PLUGINDIR}/SURFmap ]; then
152
    echo "Backing up existing SURFmap (backend) installation to ${SURFMAP_BACKUPDIR_BACKEND}"
153
    mv ${BACKEND_PLUGINDIR}/SURFmap ${SURFMAP_BACKUPDIR_BACKEND}
154
fi
155
 
156
# Install backend and frontend plugin files
157
echo "Installing SURFmap ${SURFMAP_VER} to ${FRONTEND_PLUGINDIR}/SURFmap"
158
if [ $INSTALL_FRONTEND = 1 ]; then
159
    cp -r ./${TMP_DIR}/frontend/* ${FRONTEND_PLUGINDIR}
160
fi
161
if [ $INSTALL_BACKEND = 1 ]; then
162
    cp -r ./${TMP_DIR}/backend/* ${BACKEND_PLUGINDIR}
163
fi
164
##################################################################################################################################################
165
# Deleting temporary files
166
# rm -rf ${TMP_DIR}
1418 richard 167
# rm -rf ${GEO_DB}
168
# rm -rf ${GEOv6_DB}
1413 richard 169
 
170
# Check whether an old SURFmap version was found and ask whether frontend configuration and data structures should be retained
171
if [ $INSTALL_FRONTEND = 1 -a -d ${SURFMAP_BACKUPDIR_FRONTEND} ]; then
172
    OLD_VER=$(cat ${SURFMAP_BACKUPDIR_FRONTEND}/version.php | grep -m1 \$version | awk '{print $3}' |  cut -d"\"" -f2)
173
    if [ ${OLD_VER} = ${SURFMAP_VER} ]; then
174
        while true; do
175
            read -p "Do you wish to keep the frontend configuration and data structures from your previous installation [y,n] (default: y)? " input
176
            case $input in
177
                [Nn]* ) break;;
178
                * )     echo "Copying backend configuration and data structures from previous installation..."
179
                        cp ${SURFMAP_BACKUPDIR_FRONTEND}/config.php ${FRONTEND_PLUGINDIR}/SURFmap/;
180
                        cp ${SURFMAP_BACKUPDIR_FRONTEND}/db/* ${FRONTEND_PLUGINDIR}/SURFmap/db/;
181
                        break;;
182
            esac
183
        done
184
    fi
185
fi
186
 
187
# Set permissions - owner and group
188
echo "Setting plugin file permissions - user \"${USER}\" and group \"${WWWGROUP}\""
189
if [ $INSTALL_FRONTEND = 1 ]; then
190
    chown -R ${USER}:${WWWGROUP} ${FRONTEND_PLUGINDIR}/SURFmap*
191
    chmod -R g+w ${FRONTEND_PLUGINDIR}/SURFmap/db
192
fi
193
if [ $INSTALL_BACKEND = 1 ]; then
194
    chown -R ${USER}:${WWWGROUP} ${BACKEND_PLUGINDIR}/SURFmap*
195
fi
196
 
197
# Update plugin configuration file - config.php. We use ',' as sed delimiter instead of escaping all '/' to '\/'.
198
echo "Updating plugin configuration file ${SURFMAP_CONF}"
199
 
200
# Check for proxy and update config.php accordingly
201
PROXY=$(env | grep -i http_proxy | awk '{ START=index($0,"=")+1; print substr($0,START) }')
202
if [ "$PROXY" != "" ]; then
203
    echo "HTTP proxy detected"
204
    sed -i.tmp "s,config\['use_proxy'\] = 0,config\['use_proxy'\] = 1,g" ${SURFMAP_CONF}
205
 
206
    PROXY_IP_PORT=$(echo ${PROXY} | awk '{ FROM=index($0,"//")+2; print substr($0,FROM) }')
207
    PROXY_IP=$(echo ${PROXY_IP_PORT} | awk '{ TO=index($0,":")-1; print substr($0,0,TO) }')
208
    PROXY_PORT=$(echo ${PROXY_IP_PORT} | awk '{ FROM=index($0,":")+1; print substr($0,FROM) }')
209
 
210
    OLD_PROXY_IP=$(grep "$config\['proxy_ip'\]" ${SURFMAP_CONF} | cut -d'"' -f2)
211
    OLD_PROXY_PORT=$(grep "$config\['proxy_port'\]" ${SURFMAP_CONF} | awk '{ FROM=index($0,"=")+2; TO=index($0,";"); print substr($0,FROM,TO-FROM) }')
212
 
213
    sed -i.tmp "s,${OLD_PROXY_IP},${PROXY_IP},g" ${SURFMAP_CONF}
214
    sed -i.tmp "s,${OLD_PROXY_PORT},${PROXY_PORT},g" ${SURFMAP_CONF}
215
fi
216
 
217
# Get my location information
218
cd ${FRONTEND_PLUGINDIR}/SURFmap/setup
219
MY_LOC=$(php retrievelocation.php | grep config_data | cut -d'>' -f2 | cut -d'<' -f1)
220
echo "Geocoding plugin location - ${MY_LOC}"
221
 
222
cd - > /dev/null
223
 
224
# Fill my location in plugin configuration file
225
if [ "${MY_LOC}" != "(UNKNOWN),(UNKNOWN),(UNKNOWN),(UNKNOWN),(UNKNOWN)" ]; then
226
    OLDENTRY=$(grep internal_domains ${SURFMAP_CONF} | cut -d'"' -f 6)
227
    sed -i.tmp "s/${OLDENTRY}/$(echo ${MY_LOC} | cut -d',' -f1)/g" ${SURFMAP_CONF}
228
 
229
    OLDENTRY=$(grep internal_domains ${SURFMAP_CONF} | cut -d'"' -f 10)
230
    NEWENTRY=$(echo ${MY_LOC} | cut -d',' -f2)
231
    if [ "${NEWENTRY}" = "(UNKNOWN)" ]; then
232
        NEWENTRY=""
233
    fi
234
    sed -i.tmp "s/${OLDENTRY}/${NEWENTRY}/g" ${SURFMAP_CONF}
235
 
236
    OLDENTRY=$(grep internal_domains ${SURFMAP_CONF} | cut -d'"' -f 14)
237
    NEWENTRY=$(echo ${MY_LOC} | cut -d',' -f3)
238
    if [ "${NEWENTRY}" = "(UNKNOWN)" ]; then
239
        NEWENTRY=""
240
    fi
241
    sed -i.tmp "s/${OLDENTRY}/${NEWENTRY}/g" ${SURFMAP_CONF}
242
 
243
    OLDENTRY=$(grep "$config\['map_center'\]" ${SURFMAP_CONF} | cut -d'"' -f2)
244
    NEWENTRY=$(echo ${MY_LOC} | cut -d',' -f4,5)
245
    if [ "${NEWENTRY}" != "(UNKNOWN),(UNKNOWN)" ]; then
246
        sed -i.tmp "s/${OLDENTRY}/${NEWENTRY}/g" ${SURFMAP_CONF}
247
    fi
248
fi
249
 
250
# Enable plugin
251
OLDENTRY=$(grep \@plugins ${NFSEN_CONF})
252
 
253
if grep "SURFmap" ${NFSEN_CONF} > /dev/null; then
254
    echo "Found 'SURFmap' in ${NFSEN_CONF}; assuming it is already configured"
255
else
256
    echo "Updating NfSen configuration file ${NFSEN_CONF}"
257
 
258
    # Check whether we are running Linux of BSD (BSD sed does not support inserting new lines (\n))
259
    if [ $(uname) = "Linux" ]; then
260
        sed -i.tmp "/SURFmap/d" ${NFSEN_CONF}
261
        sed -i.tmp "s/${OLDENTRY}/${OLDENTRY}\n    \[ 'live', 'SURFmap' ],/g" ${NFSEN_CONF}
262
    else # Something else (we assume *BSD)
263
        sed -i.tmp "s/${OLDENTRY}/${OLDENTRY}\ \[ 'live', 'SURFmap' ],/g" ${NFSEN_CONF}
264
    fi
265
fi
266
 
267
# Check whether an old SURFmap version was found and ask whether the backup of that version should be removed
268
if [ -d ${SURFMAP_BACKUPDIR_FRONTEND} -a -d ${SURFMAP_BACKUPDIR_BACKEND} ]; then
269
    while true; do
270
        read -p "Do you wish to remove the backup of your previous SURFmap installation [y,n] (default: n)? " input
271
        case $input in
272
            [Yy]* ) echo "Removing backup of previous installation..."
273
                    rm -rf ${SURFMAP_BACKUPDIR_FRONTEND} ${SURFMAP_BACKUPDIR_BACKEND}; 
274
                    break;;
275
            * )     break;;
276
        esac
277
    done
278
fi
279
 
280
echo "-----"
281
echo "Please restart/reload NfSen to finish installation (e.g., sudo ${BINDIR}/nfsen reload)"