Skip to content

Instantly share code, notes, and snippets.

@alexyoung
Created May 17, 2011 10:47
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alexyoung/976283 to your computer and use it in GitHub Desktop.
Deployment script
#!/usr/bin/env bash
# Configuration
SERVER='myserver'
DEPLOY_TO='/path/to/app'
EXCLUDE='*.swp .git/ db/sphinx/ tmp/ log/'
DRY_RUN=false
DEPLOY_GEM_PATH='/opt/ec/ruby/1.8.7/lib/ruby/gems/1.8'
# To improve ssh performance, consider reusing connections:
#
# Host *
# ControlMaster auto
# ControlPath /tmp/%r@%h:%p
# Map the excluded files to rsync's options
function excludes {
EXCLUDES=''
for i in $(echo $EXCLUDE | tr " " "\n")
do
EXCLUDES="$EXCLUDES --exclude $i"
done
}
# Run rsync
function upload_files {
excludes
CMD="rsync -avz $EXCLUDES"
if $DRY_RUN ; then
CMD="$CMD --dry-run"
fi
CMD="$CMD ./ $SERVER:$DEPLOY_TO"
echo $CMD
$CMD
}
# run the Rails migrations
function migrate {
ssh $SERVER "cd $DEPLOY_TO && RAILS_ENV=production rake db:migrate"
}
# Restart the web processes, easy with passenger
function restart_web_processes {
ssh $SERVER "cd $DEPLOY_TO && touch tmp/restart.txt"
}
# Run bundler, there's an extra option here for my server's environment
function bundler {
# chgrp -R deploy /opt/ec/ruby/1.8.7/lib/ruby/gems/1.8
# chmod g+w -R /opt/ec/ruby/1.8.7/lib/ruby/gems/1.8
ssh $SERVER "cd $DEPLOY_TO && GEM_PATH=$DEPLOY_GEM_PATH RAILS_ENV=production CXX=/usr/sfw/bin/g++ bundle install"
}
# Copy the app's settings files for this environment
function copy_settings {
# Remove the old ones
ssh $SERVER "rm $DEPLOY_TO/config/settings.yml"
ssh $SERVER "rm $DEPLOY_TO/config/database.yml"
ssh $SERVER "rm $DEPLOY_TO/config/sphinx.yml"
# Copy
ssh $SERVER "cp $DEPLOY_TO/config/environments/settings/test.yml $DEPLOY_TO/config/settings.yml"
ssh $SERVER "cp $DEPLOY_TO/config/environments/settings/test_database.yml $DEPLOY_TO/config/database.yml"
ssh $SERVER "cp $DEPLOY_TO/config/environments/settings/test_sphinx.yml $DEPLOY_TO/config/sphinx.yml"
}
function locales {
ssh $SERVER "cd $DEPLOY_TO && RAILS_ENV=production rake tolk:dump_all"
}
# Reusable:
function restart_sphinx {
ssh $SERVER "cd $DEPLOY_TO && RAILS_ENV=production rake ts:restart"
}
# Run deployment
upload_files
if ! $DRY_RUN ; then
copy_settings
bundler
migrate
restart_sphinx
locales
restart_web_processes
fi
@ku1ik
Copy link

ku1ik commented Dec 21, 2011

Have you considered using "git ls-files" for the list of files for rsync instead of $EXCLUDE stuff you have here?

@alexyoung
Copy link
Author

I'll give it a try, thanks

@alaa-alawi
Copy link

nice script.

I had forked it and added ControlPersist 1h to the .ssh/config, to launch a background session automatically for the first contact, and then stay up for 1 hour of inactivity before closing.

HIH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment