Blame view

spod-manager.sh 14.6 KB
cfccef7d   Carmine Spagnuolo   first commit
1
  #!/bin/bash
879b0066   isisadmin   licence update
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
  
  ###############################################################################
  #The MIT License (MIT)#########################################################
  ###############################################################################
  #Copyright (c) 2015 ROUTE-TO-PA CONSORTIUM#####################################
  ###############################################################################
  #Permission is hereby granted, free of charge, to any person obtaining a copy##
  #of this software and associated documentation files (the "Software"), to deal#
  #in the Software without restriction, including without limitation the rights##
  #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell#####
  #copies of the Software, and to permit persons to whom the Software is#########
  #furnished to do so, subject to the following conditions:######################
  ###############################################################################
  #The above copyright notice and this permission notice shall be included in####
  #all copies or substantial portions of the Software.###########################
  ###############################################################################
  #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR####
  #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,######
  #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE###
  #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER########
  #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,#
  #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN#####
  #THE SOFTWARE.#################################################################
  ###############################################################################
  ###############################################################################
  #SPOD INSTALLATION SCRIPT######################################################
  #FIRST VERSION 29/09/2015######################################################
  ###############################################################################
  ################OXWALL CONFIGURATION DATA######################################
cfccef7d   Carmine Spagnuolo   first commit
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
  CONFIG0="
  <?php
  
  define('OW_URL_HOME', '"
  CONFIG1="');
  
  define('OW_DB_HOST', 'localhost');
  define('OW_DB_PORT', null);
  define('OW_DB_USER', 'root');
  define('OW_DB_PASSWORD', 'is15rdc');
  define('OW_DB_NAME', 'oxwall');
  
  define('OW_DB_PREFIX', 'ow_');
  
  define('OW_DIR_USERFILES', OW_DIR_ROOT.'ow_userfiles'.DS);
  define('OW_DIR_STATIC', OW_DIR_ROOT.'ow_static'.DS);
  define('OW_URL_STATIC', OW_URL_HOME.'ow_static/');
  define('OW_URL_USERFILES', OW_URL_HOME.'ow_userfiles/');
  define('OW_DIR_PLUGINFILES', OW_DIR_ROOT.'ow_pluginfiles/');
  
  define('OW_PASSWORD_SALT', 'UzYBYBE6yWA4EhaD');
  
  define('OW_DIR_CORE', OW_DIR_ROOT.'ow_core'.DS);
  define('OW_DIR_INC', OW_DIR_ROOT.'ow_includes'.DS);
  define('OW_DIR_LIB', OW_DIR_ROOT.'ow_libraries'.DS);
  define('OW_DIR_UTIL', OW_DIR_ROOT.'ow_utilities'.DS);
  define('OW_DIR_PLUGIN', OW_DIR_ROOT.'ow_plugins'.DS);
  define('OW_DIR_THEME', OW_DIR_ROOT.'ow_themes'.DS);
  define('OW_DIR_SYSTEM_PLUGIN', OW_DIR_ROOT.'ow_system_plugins'.DS);
  define('OW_DIR_SMARTY', OW_DIR_ROOT.'ow_smarty'.DS);
  
  define('OW_USE_CLOUDFILES', false);
  
  if ( defined('OW_CRON') )
  {
      define('OW_DEBUG_MODE', false);
      define('OW_DEV_MODE', false);
      define('OW_PROFILER_ENABLE', false);
  }
  else
  {"
  CONFIG2="
      define('OW_DEBUG_MODE', false);
      define('OW_DEV_MODE', true);
      define('OW_PROFILER_ENABLE', false);
  }"
  CONFIG3="
      define('OW_DEBUG_MODE', false);
      define('OW_DEV_MODE', false);
      define('OW_PROFILER_ENABLE', false);
  }"
  #############################################################################
  #Checking if it is root access
  if [ "$EUID" -ne 0 ];
  then 	
  	echo "Please run as root";
    	exit -1;
  fi
  #############################################################################
  #Checking Apche installation on local machine
  APACHE=$(which apache2)
  if [ -z "$APACHE" ]; then
     echo "Apache server is required."
     exit -2
  fi
  #############################################################################
  SCRIPT_NAME="spod-manager.sh"
  #Arguments
4672f1f6   isisadmin   update
99
  OPTIONS=$(getopt -o ha:b:m:n:o:p:q:r:s:w:d:e: -l help,action:,backup-dir:,mysql-username:,mysql-password:,mysql-address:,spod-git-username:,spod-git-email:,spod-git-password:,spod-git-address:,spod-hostname:,spod-database-version:spod-www-backup: -- "$@")
cfccef7d   Carmine Spagnuolo   first commit
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  
  if [ $? -ne 0 ]; then
    echo "error in parameters."
    exit -1
  fi
  
  eval set -- $OPTIONS
  MYSQLHOST="127.0.0.1"
  GITSPOD="service.routetopa.eu:7480"
  BACKUPDIR=$(pwd)
  DB="db_dump/oxwall1709.sql"
  while true; do
    case "$1" in
      -h|--help) 
0abbaeb8   isisadmin   update help
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
          echo "$SCRIPT_NAME   is package for the installation, update, backup and restore of a Social Platform for Open Data (SPOD) application.
  SPOD enables social interactions among open data users and between open data users and government data. http://spod.routetopa.eu"
          echo " "
          echo "list of arguments:"
          echo "-h, --help                          show commands help."
          echo "-a, --action=ACTION                 define the action [install, uninstall, update, backup, restoredb, restoreall] (*)."
          echo "-m, --mysql-username=username       mysql server username (*)."
          echo "-n, --mysql-password=password       mysql server password  (*)."
          echo "-o, --mysql-address=address         mysql server hostname or ip address (deafult is localhost)."
          echo "-p, --spod-git-username=user        SPOD Git server user name (*)."
          echo "-q, --spod-git-email=email          SPOD Git server user email  (*)."
          echo "-r, --spod-git-password=password    SPOD Git server password  (*)."
          echo "-s, --spod-git-address=address:port SPOD Git server hostname or address (deafult is http://service.routetopa.eu:7480)."
          echo "-w, --spod-hostname=hostname        SPOD hostname (example http://spod.routetopa.eu/)."
          echo "-e, --spod-www-backup=www-folder    SPOD backup folder path (defauld is current directory)."
          echo "-d, --spod-database-version=db      SPOD db name. For installation, it is the name of a db file on the spod-core/db_dump folder (example oxwall_v.0.1_antares.sql).  For restoredb and restoreall, it is the db file absolute path)."
          echo " -b, --backup-dir=DIR                SPOD backup directory (default is current directory)."
          echo "* the arguments is required."
         
cfccef7d   Carmine Spagnuolo   first commit
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
         exit 0;
       ;;
      -a|--action)  
         ACTION="$2"; shift
      ;;
      -b|--backup-dir)  
         BACKUPDIR="$2"; shift
      ;;
      -m|--mysql-username)  
         MYSQLUSERNAME="$2"; shift
      ;;
      -n|--mysql-password)  
         MYSQLPASSWORD="$2"; shift
      ;;
      -o|--mysql-address)  
         MYSQLHOST="$2"; shift
      ;;
      -p|--spod-git-username)  
         GITUSER="$2"; shift
      ;;
      -q|--spod-git-email)  
         GITEMAIL="$2"; shift
      ;;
      -r|--spod-git-password)  
         GITPASSWORD="$2"; shift
      ;;
      -s|--spod-git-address)  
956367c9   isisadmin   bug fix
160
         GITSPOD="$2"; shift
cfccef7d   Carmine Spagnuolo   first commit
161
162
163
164
165
166
167
      ;;
      -w|--spod-hostname)  
         HOSTNAMESPOD="$2"; shift
      ;;
      -d|--spod-database-version)  
         DB="$2"; shift
      ;;
4672f1f6   isisadmin   update
168
169
170
      -e|--spod-www-backup)  
         SPODWWW="$2"; shift
      ;;
cfccef7d   Carmine Spagnuolo   first commit
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
      --) shift; break
      ;;
      *)         
  	echo "unknown option: $1" ; exit 1 ;;
    esac
    shift
  done
  
  #############################################################################
  #############################################################################
  #############################################################################
  #############################################################################
  #############################################################################
  #Script Actions
  if [ -z "$ACTION" ];
  then
      echo "You must specify a valid action.";
      exit -2;
  fi
  if [ $# -ne 0 ]; then
    echo "unknown option(s): $@";
    exit 1;
  fi
  
  echo "SPOD MANAGER Action: $ACTION ..."
  
    case "$ACTION" in
  	"install")
  		if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] || [ -z "$GITUSER" ] || [ -z "$GITEMAIL" ] || [ -z "$GITPASSWORD" ] [ -z "$HOSTNAMESPOD" ];
                  then
                          echo "You must specify the mysql password - username; git user - email - password; SPOD hostname in order to unistall SPOD."
                          exit -2
                  fi
  	  	git config --global user.name "$GITUSER"
          	git config --global user.email "$GITEMAIL"
  		cd /var/www
  		git init
  		git remote add origin http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-core.git
  		git pull origin master
  		mkdir /var/www/ow_plugins
  		cd /var/www/ow_plugins
  		git init
  		git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-plugins.git
  		git pull origin master
  		mkdir /var/www/ow_themes
  		cd /var/www/ow_themes
  		git init
  		git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-themes.git
  		git pull origin master
  		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST -e "create database if not exists oxwall;"
  		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST --database=oxwall < /var/www/db_dump/$DB
  		chown www-data:www-data -R /var/www
  		rm -f /var/www/ow_includes/config.php
  		echo $CONFIG0$HOSTNAMESPOD$CONFIG1$CONFIG2 >> /var/www/ow_includes/config.php
  		service apache2 restart
  		wget -q -O /dev/null http://localhost/
  		rm -f /var/www/ow_includes/config.php
  		echo $CONFIG0$HOSTNAMESPOD$CONFIG1$CONFIG3 >> /var/www/ow_includes/config.php
  		CRONCHECK=$(crontab -l);
  		if [[ $CRONCHECK == *"http://localhost/ow_cron/run.php"* ]];
  		then
          		echo "Cron job already exists";
  		else
          		crontab -l | { cat; echo "* * * * * wget -q -O /dev/null http://localhost/ow_cron/run.php"; } | crontab -
  		fi
  	;;
  	"uninstall")
  		if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] ;
  		then
  			echo "You must specify the mysql password and username to unistall SPOD."
  			echo " sudo ./spod-manager.sh -a uninstall -m user -n password"
  			exit -2
  		fi
  		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST -e "drop database oxwall;"
  		rm -rf /var/www/*
  		rm -rf /var/www/.[^.]*
  		crontab -l | sed '/http:\/\/localhost\/ow_cron\/run\.php/d' | crontab -
  	;;
  	"backup")
  		if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] ;
                  then
                          echo "You must specify the mysql password and username to unistall SPOD."
                          exit -2
                  fi	
  		service apache2 stop
  
  		DATEDIR=$(date +%Y-%m-%d-%N)
  		IP=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
  
  		mkdir -p $BACKUPDIR/$DATEDIR
  		mysqldump -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST oxwall | gzip > $BACKUPDIR/$DATEDIR/db_backup_spod.sql.gz
  
  		mkdir $BACKUPDIR/$DATEDIR/var
  		cp -r /var/www $BACKUPDIR/$DATEDIR/var
  		service apache2 start
  	;;
4672f1f6   isisadmin   update
267
268
          "restoredb")
                  if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] || [ -z "$DB" ];
6a03969d   Carmine Spagnuolo   update restore ac...
269
                  then
4672f1f6   isisadmin   update
270
                          echo "You must specify the mysql password and username, db dump path to restore SPOD."
6a03969d   Carmine Spagnuolo   update restore ac...
271
272
273
                          exit -2
                  fi
                  service apache2 stop
4672f1f6   isisadmin   update
274
   		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST -e "drop database oxwall;"
6a03969d   Carmine Spagnuolo   update restore ac...
275
276
277
278
  		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST -e "create database if not exists oxwall;"
  		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST --database=oxwall < $DB
                  service apache2 start
          ;;
4672f1f6   isisadmin   update
279
     	"restoreall")
956367c9   isisadmin   bug fix
280
                  if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] || [ -z "$SPODWWW" ] || [ -z "$GITUSER" ] || [ -z "$GITPASSWORD" ] || [ -z "$GITEMAIL" ] || [ -z "$GITSPOD" ];
4672f1f6   isisadmin   update
281
282
283
284
285
286
287
288
289
290
                  then
                          echo "You must specify the mysql password and username, SPOD backup dir and DB dump path to restore SPOD."
                          exit -2
                  fi
                  service apache2 stop
   		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST -e "drop database oxwall;"
                  mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST -e "create database if not exists oxwall;"
                  mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST --database=oxwall < $DB
                  rm -rf /var/www/*
                  rm -rf /var/www/.[^.]*
919a8e72   isisadmin   bug fix
291
  		cp -r $SPODWWW /var
4672f1f6   isisadmin   update
292
293
  		
  		cd /var/www
29dbd016   isisadmin   bug fix
294
  		git remote rm origin
4672f1f6   isisadmin   update
295
296
297
                  git init
                  git remote add origin http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-core.git
                  cd /var/www/ow_plugins
29dbd016   isisadmin   bug fix
298
299
                  git remote rm origin
  		git init
4672f1f6   isisadmin   update
300
301
                  git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-plugins.git
                  cd /var/www/ow_themes
29dbd016   isisadmin   bug fix
302
  		git remote rm origin
4672f1f6   isisadmin   update
303
304
                  git init
                  git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-themes.git	
cc01ff7a   isisadmin   bug fix
305
306
  		
  		chown www-data:www-data -R /var/www
4672f1f6   isisadmin   update
307
308
309
  		service apache2 start
  		
          ;;
cfccef7d   Carmine Spagnuolo   first commit
310
  	"update")
3ce46819   isisadmin   update
311
  		if [ -z "$GITUSER" ] || [ -z "$GITEMAIL" ] || [ -z "$GITPASSWORD" ] || [ -z "$HOSTNAMESPOD" ];
cfccef7d   Carmine Spagnuolo   first commit
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  		then
  			echo "You must specify the SPOD git credentials."
  			exit -2
  		fi
  		git config --global user.name "$GITUSER"
  		git config --global user.email "$GITEMAIL"
  		if [ -x /var/www/.git ] && [ -x /var/www/ow_plugins ] && [ -x /var/www/ow_themes ]; then
  	 		cd /var/www
  			git pull origin master
  
  			cd /var/www/ow_plugins
  			git pull origin master
  
  			cd /var/www/ow_themes
  			git pull origin master
  
  			service apache2 restart
  			wget -q -O /dev/null http://localhost/ow_cron/run.php
d641c4f5   Carmine Spagnuolo   update clean memory
330
331
332
333
334
335
  			rm -f /var/www/ow_includes/config.php
                  	echo $CONFIG0$HOSTNAMESPOD$CONFIG1$CONFIG2 >> /var/www/ow_includes/config.php
                  	service apache2 restart
                 		wget -q -O /dev/null http://localhost/
                  	rm -f /var/www/ow_includes/config.php
                  	echo $CONFIG0$HOSTNAMESPOD$CONFIG1$CONFIG3 >> /var/www/ow_includes/config.php
cc01ff7a   isisadmin   bug fix
336
  			chown www-data:www-data -R /var/www
eed2365c   Carmine Spagnuolo   update
337
                  	service apache2 restart
cfccef7d   Carmine Spagnuolo   first commit
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  		else
  			echo "Your SPOD installation is corrupt, please uninstall and istall it again. Your database will be erased."
  			exit -2
  		fi
  	;;
  	*)
  		echo "No valid action found.";
  		exit -1;
  	;;
  esac
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
879b0066   isisadmin   licence update
354
  ##############################################################################################