#!/bin/true # REMOTE SCRIPT (can not interact with user) # turn on debug mode (use 'set +x' to disable it) # set -x # {{VAR_NAME}} - template variables ERR_CODE_NO_CHECKOUT="{{ERR_CODE_NO_CHECKOUT}}" ERR_CODE_EXEC_ERROR="{{ERR_CODE_EXEC_ERROR}}" REMOTE_SITE_DIR="{{REMOTE_SITE_DIR}}" REMOTE_WWW_DIR="{{REMOTE_WWW_DIR}}" SVN_BIND_PORT="{{SVN_BIND_PORT}}" SVN_REPO_PATH="{{SVN_REPO_PATH}}" SVN_USER="{{SVN_USER}}" SVN_PASSWORD='{{SVN_PASSWORD}}' export PATH="$HOME/bin:$PATH" SVN=$(which svn) if test -z $SVN ; then echo "> ERROR: svn command not found" 1>&2 exit 1 fi PHP=`which php` if [ -z "$PHP" ]; then echo "> WARNING: can not find PHP" 1>&2 fi COMPOSER=`which composer` if [ -z "$COMPOSER" ]; then echo "> WARNING: can not find COMPOSER" 1>&2 exit 1 fi MINIFYCSS="$REMOTE_SITE_DIR/vendor/bin/minifycss" if [ ! -e "$MINIFYCSS" ]; then echo "> WARNING: can not find MINIFYCSS ($MINIFYCSS)" 1>&2 exit 1 fi MINIFYJS="$REMOTE_SITE_DIR/vendor/bin/minifyjs" if [ ! -e "$MINIFYJS" ]; then echo "> WARNING: can not find MINIFYJS ($MINIFYJS)" 1>&2 exit 1 fi ERRORS_LIST="" SITE_PARENT_DIR=$(dirname ${REMOTE_SITE_DIR}) if [[ ! -d "${REMOTE_SITE_DIR}" && ! -f "${REMOTE_WWW_DIR}/index.php" ]] ; then exit ${ERR_CODE_NO_CHECKOUT} fi # check if where are site and www directories if [[ ! -d "${REMOTE_SITE_DIR}" ]] ; then echo "> ERROR: can not found site folder '${REMOTE_SITE_DIR}' (remove site and www/index.php to make checkout)" 1>&2 exit 1 elif [[ ! -f "${REMOTE_WWW_DIR}/index.php" ]] ; then echo "> ERROR: can not found www/index.php '${REMOTE_WWW_DIR}/index.php' (remove site and www/index.php to make checkout)" 1>&2 exit 1 fi echo "> updating sources from svn ..." CURRENT_DIR=$(pwd) cd $REMOTE_SITE_DIR echo "> running 'composer install' ..." composer install cd $CURRENT_DIR echo echo "> collenting chaged js and css files ..." cd ${REMOTE_WWW_DIR} || exit ${ERR_CODE_EXEC_ERROR} CHANGED_FILES=`svn st -u --non-interactive --ignore-externals --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" | grep '^\s*\*' | awk '{if ($3 == "") print $2; else print $3}' | grep -E '\.(css|js)$'` CHANGED_JS=`echo "$CHANGED_FILES" | grep -E '\.js$'` CHANGED_CSS=`echo "$CHANGED_FILES" | grep -E '\.css$'` echo echo "> updating site ..." cd ${REMOTE_SITE_DIR} || exit ${ERR_CODE_EXEC_ERROR} CHANGED_SITE_PHP=`svn st -u --non-interactive --ignore-externals --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" | grep '^\s*\*' | awk '{if ($3 == "") print $2; else print $3}' | grep '\.php$'` svn up --ignore-externals --accept theirs-full --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" echo echo "> updating www ..." cd ${REMOTE_WWW_DIR} || exit ${ERR_CODE_EXEC_ERROR} CHANGED_WWW_PHP=`svn st -u --non-interactive --ignore-externals --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" | grep '^\s*\*' | awk '{if ($3 == "") print $2; else print $3}' | grep '\.php$'` svn up --ignore-externals --accept theirs-full --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" if test -d "${SITE_PARENT_DIR}/.updates" ; then echo echo "> updating .updates ..." cd $SITE_PARENT_DIR/.updates || exit ${ERR_CODE_EXEC_ERROR} CHANGED_SQL=`svn st -u --non-interactive --ignore-externals --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" | grep '^\s*\*' | awk '{if ($3 == "") print $2; else print $3}' | grep '\.sql$'` svn up --ignore-externals --accept theirs-full --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" fi if test -d "${SITE_PARENT_DIR}/db" ; then echo echo "> updating db ..." cd $SITE_PARENT_DIR/db || exit ${ERR_CODE_EXEC_ERROR} CHANGED_SQL=`svn st -u --non-interactive --ignore-externals --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" | grep '^\s*\*' | awk '{if ($3 == "") print $2; else print $3}' | grep '\.sql$'` svn up --ignore-externals --accept theirs-full --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" fi echo echo "> cleaning cache..." cd ${REMOTE_SITE_DIR}/tmp/cache rm -f ./zend_cache* cd ${REMOTE_SITE_DIR}/tmp rm -f ./cache-sitemap* cd ${REMOTE_SITE_DIR}/tmp/tpl_c rm -f ./%* if test -n "$CHANGED_JS" ; then echo echo "> packing js files ..." echo "$CHANGED_JS" cd ${REMOTE_WWW_DIR} || exit ${ERR_CODE_EXEC_ERROR} for f in $(echo -e $CHANGED_JS); do ext=$(echo $f | sed 's/.*\.//') o=$(echo $f | sed "s/\.$ext$/\.pack\.$ext/") echo -n "$f -> $o - " $PHP $MINIFYJS $f > $o if [ "$?" != "0" ] ; then echo "FAILED" ERRORS_LIST=$(echo "$ERRORS_LIST\n$f") else echo "DONE" fi done; echo echo "> removing *.pack.merge.css files ..." find ${REMOTE_WWW_DIR}/js -type f -name '*.pack.merge.js' -exec rm -f {} \; fi if test -n "$CHANGED_CSS" ; then echo echo "> packing css files ..." echo "$CHANGED_CSS" cd ${REMOTE_WWW_DIR} || exit ${ERR_CODE_EXEC_ERROR} for f in $(echo -e $CHANGED_CSS); do ext=$(echo $f | sed 's/.*\.//') o=$(echo $f | sed "s/\.$ext$/\.pack\.$ext/") echo -n "$f -> $o - " $PHP $MINIFYCSS $f > $o if [ "$?" != "0" ] ; then echo "FAILED" ERRORS_LIST=$(echo "$ERRORS_LIST\n$f") else echo "DONE" fi done; echo echo "> removing *.pack.merge.css files ..." find ${REMOTE_WWW_DIR}/css -type f -name '*.pack.merge.css' -exec rm -f {} \; fi echo echo "> creating rev file ..." cd ${REMOTE_WWW_DIR} || exit ${ERR_CODE_EXEC_ERROR} SOURCE_REV=$(svn info --non-interactive --no-auth-cache --username=$SVN_USER --password="$SVN_PASSWORD" 2>&1 | grep "Last Changed Rev:" | awk '{print $4}') echo $SOURCE_REV > rev # full permission fix code # find ${REMOTE_SITE_DIR} -type d -exec chmod 755 {} \; # find ${REMOTE_SITE_DIR} -type f -name "*.php" -exec chmod 644 {} \; # find ${REMOTE_WWW_DIR} -type f -name "*.php" -exec chmod 644 {} \; if test -n "$CHANGED_SITE_PHP" ; then echo echo "> fixing permissions in site ..." cd ${REMOTE_SITE_DIR} for FILE in "$CHANGED_SITE_PHP" ; do if test -f "$FILE" ; then chmod 644 "$FILE" fi done fi if test -n "$CHANGED_WWW_PHP" ; then echo echo "> fixing permissions in www ..." cd ${REMOTE_WWW_DIR} for FILE in "$CHANGED_WWW_PHP" ; do if test -f "$FILE" ; then chmod 644 "$FILE" fi done fi if test -n "$CHANGED_SQL" ; then echo echo "> NOTE: patches that should be applied manually:" echo "========================================" for PATCH in $CHANGED_SQL ; do echo $PATCH done echo "========================================" fi echo if [ "$ERRORS_LIST" != "" ] ; then echo -e "Next files are not packed:\n$ERRORS_LIST" fi echo "> done" exit 0