Last active
August 29, 2015 14:08
-
-
Save AlexanderC/5f3ec06ff89f6d0780e7 to your computer and use it in GitHub Desktop.
DEEP(symfony2) deploy script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TRUE=1 | |
FALSE=0 | |
path=$(cd $(dirname $0); pwd -P) | |
is_sudoer=$(sudo -n uptime 2>&1 | grep "load" | wc -l | tr -d ' ') | |
api_version="v1" | |
env="prod" | |
php=`which php` | |
composer=`which composer` | |
silent=${FALSE} | |
message(){ | |
echo -e "\033[38;5;148m"$1"\033[39m" | |
} | |
error(){ | |
echo -e "\033[38;5;196m"$1"\033[39m" | |
} | |
clear_stdin(){ | |
while read -e -t 1; do : ; done | |
} | |
try_install_composer(){ | |
curl=$(which curl) | |
binaries_path=$(dirname ${curl}) | |
message "Attempt to install Composer into '"${binaries_path}"' using '"${curl}"'" | |
if [ -d ${binaries_path} ] && [ ${is_sudoer} -gt 0 ]; then | |
# try to install composer binary globally | |
${curl} -sS https://getcomposer.org/installer | sudo ${php} -- --install-dir=${binaries_path} | |
message "Moving '"${binaries_path}"/composer.phar' into '"${binaries_path}"/composer'" | |
sudo mv ${binaries_path}/composer.phar ${binaries_path}/composer | |
else | |
error "Unable to find Composer executable and attempt to install it failed" | |
exit 1 | |
fi | |
} | |
clear_cache(){ | |
# flush redis only in production environment | |
if [ ${env} == "prod" ]; then | |
if [ ${FALSE} == ${silent} ]; then | |
${php} ${path}/../app/console redis:flushall --env=${env} --no-debug | |
else | |
${php} ${path}/../app/console redis:flushall --env=${env} --no-debug --no-interaction | |
fi | |
fi | |
${php} ${path}/../app/console cache:clear --env=${env} --no-warmup --no-debug | |
} | |
update_composer(){ | |
if [ ${FALSE} == ${silent} ]; then | |
clear_stdin | |
message "Do you need composer to update? [y|n]: " | |
read update_composer | |
if [ ${update_composer} == 'y' ] || [ ${update_composer} == 'Y' ]; then | |
${composer} update --working-dir=${path}/../ --prefer-source --no-dev | |
fi | |
else | |
${composer} update --working-dir=${path}/../ --prefer-source --no-dev --no-interaction | |
fi | |
} | |
execute_migrations(){ | |
if [ ${FALSE} == ${silent} ]; then | |
${php} ${path}/../app/console doctrine:migrations:migrate --env=${env} --no-debug | |
else | |
${php} ${path}/../app/console doctrine:migrations:migrate --env=${env} --no-debug --no-interaction | |
fi | |
} | |
install_assets(){ | |
if [ ${FALSE} == ${silent} ]; then | |
clear_stdin | |
message "Do you need to install assets? [y|n]: " | |
read dump_assets | |
if [ ${dump_assets} == 'y' ] || [ ${dump_assets} == 'Y' ]; then | |
${php} ${path}/../app/console assets:install --env=${env} --no-debug | |
${php} ${path}/../app/console assetic:dump --env=${env} --no-debug | |
fi | |
else | |
${php} ${path}/../app/console assets:install --env=${env} --no-debug | |
${php} ${path}/../app/console assetic:dump --env=${env} --no-debug | |
fi | |
} | |
manage_river(){ | |
if [ ${FALSE} == ${silent} ]; then | |
clear_stdin | |
message "Do you need to assure the rivers are up to date? [y|n]: " | |
read assure_river | |
if [ ${assure_river} == 'y' ] || [ ${assure_river} == 'Y' ]; then | |
${php} ${path}/../app/console deep:core:search:assure-river --env=${env} --no-debug | |
fi | |
else | |
${php} ${path}/../app/console deep:core:search:assure-river --env=${env} --no-debug | |
fi | |
} | |
generate_docs(){ | |
if [ ${FALSE} == ${silent} ]; then | |
clear_stdin | |
message "Do you need to regenerate IO Docs? [y|n]: " | |
read generate_docs | |
if [ ${generate_docs} == 'y' ] || [ ${generate_docs} == 'Y' ]; then | |
${php} ${path}/../app/console deep:core:io-docs:dump --format=html --env=${env} --no-debug > ${path}/../web/docs/${api_version}/index.html | |
fi | |
else | |
${php} ${path}/../app/console deep:core:io-docs:dump --format=html --env=${env} --no-debug > ${path}/../web/docs/${api_version}/index.html | |
fi | |
} | |
optimize(){ | |
if [ ${env} == "prod" ]; then | |
${composer} dump-autoload --working-dir=${path}/../ --optimize | |
fi | |
} | |
assure_permissions(){ | |
server=$(ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1) | |
user=$(whoami) | |
setfacl_binary=$(which setfacl) | |
if [ -z ${server} ]; then | |
error "Unable to guess server user, using "${user}" instead..." | |
server=${user} | |
fi | |
if [ ${TRUE} == ${silent} ] && [ ${is_sudoer} -le 0 ]; then | |
error "User is not an sudoer, skipping..." | |
else | |
if [ ! -z ${setfacl_binary} ] && [ -x ${setfacl_binary} ]; then | |
sudo setfacl -R -m u:${server}:rwX -m u:${user}:rwX ${path}/../app/cache ${path}/../app/logs | |
sudo setfacl -dR -m u:${server}:rwX -m u:${user}:rwX ${path}/../app/cache ${path}/../app/logs | |
else | |
sudo chmod +a ${server}" allow delete,write,append,file_inherit,directory_inherit" ${path}/../app/cache ${path}/../app/logs | |
sudo chmod +a ${user}" allow delete,write,append,file_inherit,directory_inherit" ${path}/../app/cache ${path}/../app/logs | |
fi | |
fi | |
} | |
# read options | |
for i in "$@" | |
do | |
case ${i} in | |
-e=*|-e*|--env=*) | |
env="${i#*=}" | |
;; | |
-v=*|-v*|--version=*) | |
api_version="${i#*=}" | |
;; | |
-p=*|-p*|--php=*) | |
php="${i#*=}" | |
;; | |
-c=*|-c*|--composer=*) | |
composer="${i#*=}" | |
;; | |
-n|--no-interaction) | |
silent=${TRUE} | |
;; | |
--help) | |
space="\t" | |
message "Usage: deploy.sh -e=prod -p=/usr/bin/php -c=./composer.phar -n" | |
message "Options:" | |
message ${space}"--env (-e) To set the environment (default 'prod')" | |
message ${space}"--version (-v) To set the API version (default 'v1')" | |
message ${space}"--php (-p) To set php binary path (default 'php')" | |
message ${space}"--composer (-c) To set composer binary path (default 'composer')" | |
message ${space}"--no-interaction (-n) Do not ask any interactive question" | |
exit 0 | |
;; | |
*) | |
error "Unknown option "${i} | |
;; | |
esac | |
done | |
# assure we have php binary available | |
if [ -z ${php} ] || [ ! -x ${php} ]; then | |
error "ERROR: Unable to find PHP executable!" | |
exit 1 | |
fi | |
# assure we have composer binary available | |
if [ -z ${composer} ] || [ ! -x ${composer} ]; then | |
try_install_composer | |
fi | |
# The action starts here... | |
clear_cache | |
update_composer | |
execute_migrations | |
install_assets | |
manage_river | |
generate_docs | |
optimize | |
message "Assure right permissions..." | |
assure_permissions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment