Blame view

spod-manager.sh 12.4 KB
cfccef7d   Carmine Spagnuolo   first commit
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
  #!/bin/bash
  ########################################################################
  #SPOD INSTALLATION SCRIPT###############################################
  #FIRST VERSION 29/09/2015###############################################
  ########################################################################
  ################OXWALL CONFIGURATION DATA###############################
  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
75
  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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  
  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) 
         echo "$SCRIPT_NAME is manager tool for SPOD application of ROUTE-TO-PA European project all rights are reserved. 2015"
         echo " "
         echo "list of arguments:"
         echo "-h, --help                          show commands help"
4672f1f6   isisadmin   update
94
         echo "-a, --action=ACTION                 specify an action install, uninstall, update, backup, restoredb, restoreall."
cfccef7d   Carmine Spagnuolo   first commit
95
96
97
98
99
100
101
102
         echo "-m, --mysql-username=username       specify the username of mysql (*)."
         echo "-n, --mysql-password=password       specify the password of mysql server (*)."
         echo "-o, --mysql-address=address         specify the address of mysql server (deafult is localhost)."
         echo "-p, --spod-git-username=user        specify the user name of SPOD git server (*)."
         echo "-q, --spod-git-email=email          specify the email of SPOD git server (*)."
         echo "-r, --spod-git-password=password    specify the passoword of SPOD git server (*)."
         echo "-s, --spod-git-address=address:port specify the SPOD git server (deafult is service.routetopa.eu:7480)."
         echo "-w, --spod-hostname=hostname        specify the SPOD host name (example http://spod.routetopa.eu/)."
6a03969d   Carmine Spagnuolo   update restore ac...
103
         echo "-d, --spod-database-version=db      specify the SPOD db git path located in db_dump of SPOD git core (example in backup action: oxwall_v.0.1_antares.sql, deafults is really simple SPOD and Oxwall installationi; in restore action use a local db dump)." 
4672f1f6   isisadmin   update
104
         echo "-e, --spod-www-backup=www-folder    specify the SPOD www folder path."
cfccef7d   Carmine Spagnuolo   first commit
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
         echo "-b, --backup-dir=DIR                specify the directory where store the spod backup (default is current directory)."
         echo "* the arguments is required."
         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)  
         GITHOST="$2"; shift
      ;;
      -w|--spod-hostname)  
         HOSTNAMESPOD="$2"; shift
      ;;
      -d|--spod-database-version)  
         DB="$2"; shift
      ;;
4672f1f6   isisadmin   update
142
143
144
      -e|--spod-www-backup)  
         SPODWWW="$2"; shift
      ;;
cfccef7d   Carmine Spagnuolo   first commit
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
      --) 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
241
242
          "restoredb")
                  if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] || [ -z "$DB" ];
6a03969d   Carmine Spagnuolo   update restore ac...
243
                  then
4672f1f6   isisadmin   update
244
                          echo "You must specify the mysql password and username, db dump path to restore SPOD."
6a03969d   Carmine Spagnuolo   update restore ac...
245
246
247
                          exit -2
                  fi
                  service apache2 stop
4672f1f6   isisadmin   update
248
   		mysql -u $MYSQLUSERNAME -p$MYSQLPASSWORD --host=$MYSQLHOST -e "drop database oxwall;"
6a03969d   Carmine Spagnuolo   update restore ac...
249
250
251
252
  		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
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
     	"restoreall")
                  if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] || [ -z "$SPODWWW" ];
                  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/.[^.]*
  		cp -r $SPODWWW /var/www
  		
  		cd /var/www
                  git init
                  git remote add origin http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-core.git
                  cd /var/www/ow_plugins
                  git init
                  git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-plugins.git
                  cd /var/www/ow_themes
                  git init
                  git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-themes.git	
  		service apache2 start
  		
          ;;
cfccef7d   Carmine Spagnuolo   first commit
279
  	"update")
3ce46819   isisadmin   update
280
  		if [ -z "$GITUSER" ] || [ -z "$GITEMAIL" ] || [ -z "$GITPASSWORD" ] || [ -z "$HOSTNAMESPOD" ];
cfccef7d   Carmine Spagnuolo   first commit
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  		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
299
300
301
302
303
304
  			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
eed2365c   Carmine Spagnuolo   update
305
                  	service apache2 restart
cfccef7d   Carmine Spagnuolo   first commit
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  		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
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################
  ##############################################################################################