Last active
January 13, 2017 21:13
-
-
Save dhensby/829e1b1309ec8702fd74b3f3e45bda34 to your computer and use it in GitHub Desktop.
Simple local SilverStripe deploy script for Fedora/CentOS/RHEL
This file contains 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
#!/usr/bin/env bash | |
BU=true | |
CO=true | |
BUILD=true | |
TARGET='master' | |
DB_USER='' | |
DB_PASS='' | |
DB_HOST='' | |
DB_NAME='' | |
# Depending on your set-up you'll need to be root to deploy | |
if [[ $EUID -ne 0 ]]; then | |
echo "It appears you are trying to deploy as non-root user" | |
read -r -p "Are you sure you wish to continue? [y/N] " response | |
case $response in | |
[yY][eE][sS]|[yY]) | |
continue | |
;; | |
*) | |
echo "Aborting" | |
exit | |
;; | |
esac | |
fi | |
## Standard usage function | |
function usage { | |
echo "deploy [--no-bu] [release target] | |
--help This help | |
--no-bu Don't take a backup for this deploy | |
--no-co Don't attempt a checkout (just deploy existing files from repo) | |
--no-build Don't build dist files or dependencies | |
release target The git ref to target for this release - defaults to master" | |
} | |
function populatedbcreds { | |
ENV_CONFIG=$(cat /var/www/_ss_environment.php) | |
DB_USER=`echo "${ENV_CONFIG}" | grep 'SS_DATABASE_USERNAME' | sed -r "s/define\s*\(\s*['\"].*['\"]\s*,\s*['\"](.*)['\"]\s*\).*/\1/g"` | |
DB_PASS=`echo "${ENV_CONFIG}" | grep 'SS_DATABASE_PASSWORD' | sed -r "s/define\s*\(\s*['\"].*['\"]\s*,\s*['\"](.*)['\"]\s*\).*/\1/g"` | |
DB_HOST=`echo "${ENV_CONFIG}" | grep 'SS_DATABASE_SERVER' | sed -r "s/define\s*\(\s*['\"].*['\"]\s*,\s*['\"](.*)['\"]\s*\).*/\1/g"` | |
DB_NAME=`echo "${ENV_CONFIG}" | grep '.*define.*SS_DATABASE_NAME' | sed -r "s/define\s*\(\s*['\"].*['\"]\s*,\s*['\"](.*)['\"]\s*\).*/\1/g"` | |
} | |
function deploymentfailed { | |
>&2 echo "$1" | |
echo "== Deployment failed ==" | |
exit 1 | |
} | |
NUM_ARGS=0 | |
# Iterate over any arguments passed to the deploy script | |
for ARG in "$@"; do | |
# If the argument starts with "--" | |
if [[ "${ARG}" == --* ]]; then | |
# Trim the first two chars | |
COMMAND=${ARG:2} | |
# Should really be a switch statement | |
if [[ "${COMMAND}" == "no-bu" ]]; then | |
BU=false | |
elif [[ "${COMMAND}" == "no-co" ]]; then | |
CO=false | |
elif [[ "${COMMAND}" == "no-build" ]]; then | |
BUILD=false | |
elif [[ "${COMMAND}" == "help" ]]; then | |
usage | |
exit | |
fi | |
else | |
TARGET="${ARG}" | |
# Increment number of args, if we have more than we are allowed, then we'll error | |
((NUM_ARGS+=1)) | |
if [[ ${NUM_ARGS} > 1 ]]; then | |
>&2 echo "Only one argument allowed" | |
exit 1 | |
fi | |
fi | |
done | |
# Start the deployment process | |
echo "== Starting deployment ==" | |
# Backup the site if appropriate | |
if [ "${BU}" == true ]; then | |
echo "Backing up current site and database" | |
BU_DIR=/backups/`date -u +%F-%T`/ | |
# Ensure the backup directory exists | |
mkdir -p "${BU_DIR}" | |
populatedbcreds | |
# Backup the DB - it would be good to pull this out of the _ss_environment file | |
mysqldump -u "${DB_USER}" -h "${DB_HOST}" -p"${DB_PASS}" "${DB_NAME}" > "${BU_DIR}/site.sql" | |
if [ $? != 0 ]; then | |
deploymentfailed "Failed to backup database" | |
fi | |
# Store the path to the last backup for incremental backup purposes | |
if [ -e /backups/latest ]; then | |
LAST_BU=$(readlink -f /backups/latest) | |
DEST_PARAM="--link-dest=${LAST_BU}" | |
fi | |
# Backup with rsync using hard links to save disk space | |
rsync -q -a --delete ${DEST_PARAM} /var/www/html "${BU_DIR}" | |
# Remove the symlink to latest | |
rm -f /backups/latest | |
# Add latest symlink - good for finding last BU and would be good for roll back feature if I even implement one | |
ln -s "${BU_DIR}" /backups/latest | |
echo "Backup complete" | |
else | |
echo "Skipping backup" | |
fi | |
if [ "${CO}" == true ]; then | |
echo "Fetching '${TARGET}' from git repo" | |
cd /var/www/repo/www | |
# Fetch changes from git repo | |
git fetch --all -q | |
# Check the target exists | |
git rev-parse "origin/${TARGET}" &>/dev/null | |
# If command failed, abort deploy | |
if [[ "$?" != 0 ]]; then | |
deploymentfailed "Failed to find target '${TARGET}'" | |
fi | |
# Reset to target | |
git reset --hard HEAD &> /dev/null | |
git clean -f &> /dev/null | |
git checkout "origin/${TARGET}" &> /dev/null | |
git pull &> /dev/null | |
git reset --hard HEAD | |
echo "Changes fetched" | |
# If command failed, abort deploy | |
if [[ "$?" != 0 ]]; then | |
deploymentfailed "Failed to find target '${TARGET}'" | |
fi | |
# Reset to target | |
git reset --hard "origin/${TARGET}" | |
git clean -f | |
echo "Changes fetched" | |
else | |
echo "Skipping checkout" | |
fi | |
if [ "${BUILD}" == true ]; then | |
echo "Installing composer dependencies" | |
# Install composer deps in the repo | |
composer install -q -o --no-dev -d /var/www/repo/www/ | |
if [[ "$?" != 0 ]]; then | |
deploymentfailed "Failed to install composer dependencies" | |
fi | |
echo "Composer install complete" | |
else | |
echo "Skipping build" | |
fi | |
echo "Deploying changes to webroot" | |
# Sync files from repo to wedroot | |
rsync -a --delete --force\ | |
--exclude='/.editorconfig'\ | |
--exclude='.git/'\ | |
--exclude='/.gitattributes'\ | |
--exclude='/.gitignore'\ | |
--exclude='/assets/'\ | |
--exclude='/behat.yml'\ | |
--exclude='/build.xml'\ | |
--exclude='/composer.json'\ | |
--exclude='/composer.lock'\ | |
--exclude='/CONTRIBUTING.md'\ | |
--exclude='/debug.log'\ | |
--exclude='/silverstripe-cache/'\ | |
--exclude='/phpunit.xml.dist'\ | |
--exclude='/README.md'\ | |
--exclude='/web.config'\ | |
--exclude='/*.log'\ | |
/var/www/repo/www/ /var/www/html/ | |
if [[ "$?" != 0 ]]; then | |
deploymentfailed "Failed to deploy to webroot" | |
fi | |
# Clear caches, ensure required folders exists, set correct permissions | |
echo "Clearing caches and building DB" | |
rm -rf /var/www/html/silverstripe-cache/ | |
mkdir -p /var/www/html/silverstripe-cache/ | |
mkdir -p /var/www/html/assets/ | |
chmod +x /var/www/html/framework/sake | |
/var/www/html/framework/sake dev/build | |
if [[ "$?" != 0 ]]; then | |
deploymentfailed "Failed to build database" | |
fi | |
chown -R apache: /var/www/html/ | |
if [ `getenforce` == "Enforcing" ]; then | |
chcon -t httpd_sys_content_t /var/www/html -R | |
chcon -t httpd_sys_rw_content_t /var/www/html/assets -R | |
chcon -t httpd_sys_rw_content_t /var/www/html/silverstripe-cache -R | |
fi | |
echo "Caches cleared, DB built and code deployed" | |
echo "== Depoloyment complete ==" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment