Last active
September 30, 2015 00:08
-
-
Save stojg/1690547 to your computer and use it in GitHub Desktop.
Capistrano recipe for SS
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
# This is an example of how I use capistrano in a sweet symphony to deploy a SilverStripe site. | |
# | |
# If you haven't installed capistrano yet, now would be a good time to do it. | |
# | |
# sudo gem install capistrano | |
# | |
# Have a look at http://cheat.errtheblog.com/s/capistrano/ for some tricks. | |
# Otherwise http://theadmin.org/articles/capistrano-variables/ shows most vars | |
# And here is the original deploy.rb https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy.rb | |
# Before deploying, create a non priviliged deploy user to use for deploys | |
# sudo /usr/sbin/useradd deploy | |
# add your public key to the /home/deploy/.ssh/authorized_keys | |
# sudo /usr/sbin/addgroup sites | |
# Set default group for deploy to be sites | |
# sudo usermod -g sites deploy | |
# Change the group that apache will run as | |
# sudo vim /etc/apache2/envvars | |
# export APACHE_RUN_GROUP=sites | |
# sudo chown www-data:sites /var/www/ | |
# sudo chmod 2775 /var/www/ | |
# Then run cap:setup and cap:check | |
# --------------------------------------------------------------- | |
# Application settings | |
# --------------------------------------------------------------- | |
# This what the base folder of the docroot would be, see :deploy_to below | |
set :application, "banana.com" | |
# This contains a list of folders that should be shared between releases and is | |
# outside the code repository | |
set :shared_children, %w(assets) | |
# The remote server with dns name and roles | |
server 'web1.se', :app, :web, :db | |
# Where on the remote server should we deploy | |
set :deploy_to, "/var/www/#{application}" | |
# This is how many releases that will be kept on the server | |
set :keep_releases, 4 | |
# --------------------------------------------------------------- | |
# User settings | |
# --------------------------------------------------------------- | |
# Which user to ssh in and running the command as | |
set :user, "deploy" | |
set :runner, "deploy" | |
# Remember that the :user needs to be in this group, otherwise *nix will not allow chown | |
set :webserver_group, "sites" | |
# sudo isn't required for my deployments. | |
set :use_sudo, false | |
# --------------------------------------------------------------- | |
# SCM settings | |
# --------------------------------------------------------------- | |
set :repository, "stig@github.com:/var/git/banana.com.git" | |
set :scm, "git" | |
set :branch, "master" | |
set :scm_verbose, false | |
set :deploy_via, :remote_cache | |
# Disable the usage of submodules | |
# set :git_enable_submodules, 1 | |
# --------------------------------------------------------------- | |
# Database settings, used for backups and restores | |
# --------------------------------------------------------------- | |
set :db_user, 'user' | |
set :db_password, 'password' | |
set :db_schema, 'schema' | |
# --------------------------------------------------------------- | |
# SSH | |
# --------------------------------------------------------------- | |
# Forwarding agents are awesome! | |
ssh_options[:forward_agent] = true | |
# Must be set for the password prompt from git to work | |
default_run_options[:pty] = true | |
ssh_options[:port] = 22 | |
# --------------------------------------------------------------- | |
# The real overridden silverstipe-o-fantastic-recipie | |
# --------------------------------------------------------------- | |
namespace :deploy do | |
# Overriden to support rollbacks and importing the old database, but to be | |
# honest, I'm not sure this works or should be used at all. | |
task :update_code, :except => { :no_release => true } do | |
on_rollback { | |
run "rm -rf #{release_path}; true" | |
} | |
strategy.deploy! | |
finalize_update | |
end | |
task :create_symlink, :except => { :no_release => true } do | |
on_rollback do | |
if previous_release | |
run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true" | |
else | |
logger.important "no previous release to rollback to, rollback of symlink skipped" | |
end | |
end | |
logger.important "SYMLINKINK!" | |
run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}" | |
end | |
# Symlink all the 'shared_children into the newly relasesed folder | |
task :finalize_update, :except => { :no_release => true } do | |
shared_children.map do |d| | |
run "ln -s #{shared_path}/#{d.split('/').last} #{latest_release}/#{d}" | |
end | |
end | |
# The migrate task takes care of certain Silverstripe things that needs to happen | |
# 1) SCP Upload the ./config/_ss_environment.php to the release folder | |
# 2) Create a silverstripe-cache in the release folder | |
# 3) Set 2775 permissions on all folder | |
# 4) Set 664 permissions on all files beside sapphire/sake | |
# 5) Change the owner of everything to the 'webserver_group' | |
task :silverstripe do | |
top.upload "./config/_ss_environment.php", "#{latest_release}/_ss_environment.php", :via => :scp | |
# Add the cache folder inside this release so we don't need to worry about | |
# the cache being weird. | |
run "mkdir #{latest_release}/silverstripe-cache" | |
# Set the execute permissions on sapphire/sake | |
run "chmod a+x #{latest_release}/sapphire/sake" | |
# Run the mighty dev/build | |
run "#{latest_release}/sapphire/sake dev/build" | |
# Set permissions for directories | |
run "find #{latest_release} -type d -exec chmod 2775 {} \\;" | |
# Set permissions for files | |
run "find #{latest_release} -type f -not -name sake -exec chmod 664 {} \\;" | |
# Set the group owner to the webserver group | |
run "chown -RP :#{webserver_group} #{latest_release}" | |
end | |
# Nope, we don't want to restart any services on the server | |
task :restart do | |
#logger.debug "the restart task doesnt make sense in this kind of deploys" | |
end | |
# Backup the database | |
namespace :db do | |
desc <<-DESC | |
Dump the database to the root directory | |
DESC | |
task :backup, :except => { :no_release => true } do | |
logger.info "Backing up database" | |
run "mysqldump -u#{db_user} -p#{db_password} #{db_schema} | gzip > #{previous_release}/dump.sql.gz" | |
run "chmod 600 #{previous_release}/dump.sql.gz" | |
end | |
end | |
end | |
# Before symlink, backup the database | |
before "deploy:migrate", "deploy:db:backup" | |
# Before the switching the current symlink, do the silverstripe specifics | |
before "deploy:symlink", "deploy:silverstripe" | |
# Automatically remove old releases | |
after "deploy:update", "deploy:cleanup" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment