spod-manager.sh 9.87 KB
#!/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
OPTIONS=$(getopt -o ha:b:m:n:o:p:q:r:s:w:d: -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: -- "$@")

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"
       echo "-a, --action=ACTION                 specify an action install, uninstall, update and backup."
       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/)."
       echo "-d, --spod-database-version=db      specify the SPOD db git path located in db_dump of SPOD git core (example oxwall_v.0.1_antares.sql, deafults is really simple SPOD and Oxwall installation)." 
       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
    ;;
    --) 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
	;;
	"update")
		if [ -z "$GITUSER" ] || [ -z "$GITEMAIL" ] || [ -z "$GITPASSWORD" ] ;
		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
		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
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################