Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Last active January 28, 2017 10:29
Show Gist options
  • Save ajaxray/ee1f7b925ecd9c6304e75ff50e3f1413 to your computer and use it in GitHub Desktop.
Save ajaxray/ee1f7b925ecd9c6304e75ff50e3f1413 to your computer and use it in GitHub Desktop.
A Bash script for resetting environment of Symfony2 app during development.
#!/usr/bin/env bash
# Author : Anis Uddin Ahmad <anis.prorgrammer@gmail.com>
# http://ajaxray.com
# Usages (from application base dir)
# sh bin/reload.sh => quick reset (default env is prod)
# sh bin/reload.sh dev => quick reset dev
# sh bin/reload.sh prod full => full reset prod (including assets, supervirord etc.)
# Paths and Settings
SUPERVISOR_CONFIG='/etc/supervisor/supervisord.conf'
SUPERVISOR_APP_PROGRAMS='app/config/supervisord_programs.conf'
SF_CONSOLE='app/console'
APP_ENV=${1:-prod}
RELOAD_TYPE=${2:-quick}
SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )
# ========== Display utils =====================
fg_red=`tput setaf 1`
fg_green=`tput setaf 2`
reset=`tput sgr0`
bell=`tput bel`
# ========== Functions ==================
reload_supervisor() {
echo "Restarting supervisord..."
if [ ! -f ${SUPERVISOR_CONFIG} ]; then
echo "ERROR: ${fg_red}Supervisor config (${SUPERVISOR_CONFIG}) is not found! Aborting...${reset}"
echo "Note: If supervisord is inatalled, set the config path to \$SUPERVISOR_CONFIG of this script"
return 1
fi
# Check if my config in included in supervisor config
# See - http://stackoverflow.com/a/4749368
if grep -Fq ${SUPERVISOR_APP_PROGRAMS} ${SUPERVISOR_CONFIG}
then
supervisorctl -c ${SUPERVISOR_CONFIG} shutdown > /dev/null
supervisord -c ${SUPERVISOR_CONFIG}
else
echo -e "ERROR: ${fg_red}${SUPERVISOR_APP_PROGRAMS} is not included in supervisor config!${reset}"
echo "It should be added in [include] section of ${SUPERVISOR_CONFIG} file"
fi
}
check_schema() {
echo "Checking if doctrine schema up-to-date..."
local STATUS=`php ${SF_CONSOLE} doctrine:schema:update --dump-sql`
if [[ ${STATUS} != *"Nothing to update"* ]]; then
if [ 'full' = ${RELOAD_TYPE} ]; then
echo "${fg_red}You have changes in schema!${reset}"
update_schema
else
echo "WARNING: ${fg_red}You have changes in schema! remember to update.${reset}"
fi
fi
}
update_schema() {
read -p "Want to update now?(Yes/No/Preview) : " update
if [[ "Yes, yes" =~ "${update}" ]]; then
php ${SF_CONSOLE} doctrine:schema:update --force
elif [[ "Preview, preview" =~ "${update}" ]]; then
php ${SF_CONSOLE} doctrine:schema:update --dump-sql
update_schema
else
echo "WARNING: ${fg_red}Skipping! But do not forget to update it!${reset}"
fi
}
clear_cache() {
echo "Cleaning cache..."
if [ 'full' = ${RELOAD_TYPE} ]; then
rm -rf app/cache/${APP_ENV} app/logs/${APP_ENV}.log
else
php app/console cache:clear --env=${APP_ENV} > /dev/null
fi
}
prepare_dirs() {
echo "Preparing directories..."
chmod -R 0777 app/cache app/logs/
chmod -R 0777 web/reports web/uploads
}
prepare_assets() {
echo "Installing and dumping assets..."
php app/console assets:install --symlink --relative --env=${APP_ENV} > /dev/null
php app/console assetic:dump --env=${APP_ENV} > /dev/null
}
# ========= Starting execution ==============
# Comment out things you do not need
# Check if running script from correct directory before running commands
cd "${SCRIPTPATH}/.."
if [ -f "${SF_CONSOLE}" ]; then
check_schema
clear_cache
if [ 'full' = ${RELOAD_TYPE} ]; then
reload_supervisor
prepare_assets
fi
prepare_dirs
else # app/console not found at place
echo -e "ERROR: ${fg_red}This script should run from base dir of Symfony2 project!${reset}"
echo -e "${bell}${SF_CONSOLE} not found from ${fg_red}`pwd` ${reset}"
exit
fi
echo "Done!"
# Hints
# Reading from properties file - http://stackoverflow.com/a/38096496
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment