Skip to content

Instantly share code, notes, and snippets.

@prashanthrajagopal
Forked from jrochkind/gist:4260448
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prashanthrajagopal/a18dab03363a2b5d5c50 to your computer and use it in GitHub Desktop.
Save prashanthrajagopal/a18dab03363a2b5d5c50 to your computer and use it in GitHub Desktop.
A cap 'stage' for dynamic deploy of feature branch demos
####
#### Forked from https://gist.github.com/jrochkind/4260448
####
# require 'rvm/capistrano'
require 'fileutils'
# An experiment for a capistrano 'stage' that can actually be used to
# deploy multiple feature branches at once.
# Intended to be installed config/deploy/feature_demo , when using cap
# multi-stage support: add 'feature_demo' to 'stages' in `config/deploy.rb` too.
# Run from a git feature branch `cap feature_demo deploy` to deploy to
# single machine we have all our feature demos on.
# Name of feature branch will be taken from current git checkout.
# Will be deployed to /opt/feature_demos/name_of_feature, from
# git branch name_of_feature, AND an httpd conf will be created telling
# passenger to deploy that app, woo.
#
# cap feature_demo deploy
#
# You WILL need to manually restart apache if this is the first time
# you've deployed that feature demo.
#
# If you want to deploy a different branch than your current checkout:
#
# cap feature_demo deploy -S branch=some_branch
#
# If you want to reference the deployed feature by something other than
# branch name:
#
# cap feature_demo deploy -S feature=something_else
#
# feature name is used for directory path of checkout, Apache
# subUri, etc. shouldn't have spaces, should be a nice token.
#
#
# If you are done with a feature demo and want to wipe it from disk etc,
# cap feature_demo deploy:cleanup_feature_demo
#
# (Have to use `feature` or `branch` args matching how you created it)
# not using any of our special roles for cronjobs, for a feature
# demo we just want a basic app deploy.
server "demo.server.tld", :app, :web
unless exists? :branch
set :branch do
branch = `git symbolic-ref -q HEAD`.sub(%r{^refs/heads/}, '').chomp
raise CommandError.new("feature_demo cap stage only works if you have a checked out feature branch!") if branch == "master"
branch
end
end
unless exists? :feature
set(:feature) { fetch :branch }
end
set :deploy_to, "/opt/feature_demos/#{fetch(:feature)}"
# I can never get capistrano sudo to work anyway, all places
# we're going to write to just need to be writeable by the user
set :use_sudo, false
# We have a 'demo' Rails environment defined, which has config settings
# very production-like, but uses non-production database and other external
# APIs.
set :rails_env, "staging"
# No reason to keep old releases in our feature_demo checkouts
set :keep_releases, 1
set(:apache_conf_dir, "/etc/apache2/sites-available") unless exists? :apache_conf_dir
unless exists? :apache_conf_file
set(:apache_conf_file_path) { File.join(apache_conf_dir, "#{fetch(:feature)}") }
end
namespace :deploy do
task :passenger_apache_conf, :roles => [:web] do
apache_conf = <<-EOS
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot #{fetch(:deploy_to)}/public
<Directory #{fetch(:deploy_to)}>
RailsEnv #{fetch(:rails_env)}
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
EOS
put apache_conf, apache_conf_file_path
end
task :passenger_symlink, :roles => [:web] do
run "sudo a2ensite #{fetch(:feature)}"
run "sudo service apache2 reload"
end
end
namespace :db do
task :create_yml, :roles => [:web] do
database_yml_path = "#{fetch(:shared_path)}/config/database.yml"
database_yml = <<-EOS
#{fetch(:rails_env)}:
adapter: mysql2
encoding: utf8
reconnect: true
database: #{fetch(:feature)}
pool: 20
username: root
password: password
host : localhost
EOS
put database_yml, database_yml_path
end
task :apply_dump, :roles => [:db] do
dump = ENV[:dump_file] || '/home/app/dev_dump.sql.gz'
execute "mysql -uroot -ppassword -e 'create database #{fetch(:feature)}'"
execute "zcat #{fetch(:dump)} | mysql -uroot -ppassword #{fetch(:feature)}"
end
end
before 'deploy:migrate', 'db:create_yml'
after 'db:create_yml', 'db:apply_dump'
before "deploy:restart", "deploy:passenger_symlink", "deploy:passenger_apache_conf"
# Want to clean up a feature branch?
# cap deploy:cleanup_feature_demo
namespace :deploy do
task :cleanup_feature_demo, :roles => [:web] do
FileUtils.rm_f fetch(:deploy_to)
execute "sudo a2dissite #{feature}"
execute "mysql -uroot -ppassword -e 'drop database #{fetch(:feature)}'"
FileUtils.rm_f fetch(:apache_conf_file_path)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment