spod-manager.sh 14.6 KB
#!/bin/bash

###############################################################################
#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######################################
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
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: -- "$@")

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 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."
       
       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)  
       GITSPOD="$2"; shift
    ;;
    -w|--spod-hostname)  
       HOSTNAMESPOD="$2"; shift
    ;;
    -d|--spod-database-version)  
       DB="$2"; shift
    ;;
    -e|--spod-www-backup)  
       SPODWWW="$2"; shift
    ;;
    --) 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
	;;
        "restoredb")
                if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] || [ -z "$DB" ];
                then
                        echo "You must specify the mysql password and username, 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
                service apache2 start
        ;;
   	"restoreall")
                if [ -z "$MYSQLUSERNAME" ] || [ -z "$MYSQLPASSWORD" ] || [ -z "$SPODWWW" ] || [ -z "$GITUSER" ] || [ -z "$GITPASSWORD" ] || [ -z "$GITEMAIL" ] || [ -z "$GITSPOD" ];
                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
		
		cd /var/www
		git remote rm origin
                git init
                git remote add origin http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-core.git
                cd /var/www/ow_plugins
                git remote rm origin
		git init
                git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-plugins.git
                cd /var/www/ow_themes
		git remote rm origin
                git init
                git remote add origin  http://$GITUSER:$GITPASSWORD@$GITSPOD/isislab/spod-themes.git	
		
		chown www-data:www-data -R /var/www
		service apache2 start
		
        ;;
	"update")
		if [ -z "$GITUSER" ] || [ -z "$GITEMAIL" ] || [ -z "$GITPASSWORD" ] || [ -z "$HOSTNAMESPOD" ];
		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
			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
			chown www-data:www-data -R /var/www
                	service apache2 restart
		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
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################