Skip to content

Instantly share code, notes, and snippets.

@irjudson
Forked from pol/gist:158438
Created August 6, 2009 22:27
Show Gist options
  • Save irjudson/163598 to your computer and use it in GitHub Desktop.
Save irjudson/163598 to your computer and use it in GitHub Desktop.
# Dev/Apps Capistrano Deployment Script
# Copyright (c) 2009 Montana State University
#
# Version: 0.2
#
# FILE: deploy.mysql.rb
# This deployment file will allow a user to deploy their application
# code to dev.msu.montana.edu
#
# PREREQUISITES
# - capistrano gem installed (sudo gem install capistrano)
# - application has been capified (capify /path/to/app)
# - application revisions are stored in a subversion repository
#
# INITIALIZATION
# First make sure that the script below contains appropriate
# configuration settings. The 'application' variable should be the
# username that is used on dev and apps as well as the directory that
# the application is allowed to use.
#
# Secondly, make sure that the 'repository' setting is correct. This
# will have a sensible default, but it may not be accurate for your
# application.
#
# Example
# If my application was called 'msu', then the application
# variable setting should look like:
#
# set :application, "msu"
#
# This means that all of the following is true:
# - the app will be installed to '/home/msu'
# - the database name to be used is 'msu'
# - the demo user name is 'msudemo' (optional)
# - the server user that the app runs as is named 'msu'
#
# Generally this file will be entirely set up in such a way that you
# will not have to edit it at all.
#
# DEPLOYMENT
# The first step of deployment is setting up. This is done by running
# 'cap <server> setup'. Continuing the previous example, if I wanted
# to set up the development server, I would run:
#
# $ cap dev setup
#
# If I wanted to run the production setup, I would run:
#
# $ cap apps setup
#
# Once the setup is complete, you can deploy with much the same
# pattern. The command in this case is 'cap <server> deploy'. To
# deploy to the development server, I would run:
#
# $ cap dev deploy
#
require 'erb'
set :application, "neuromorpho"
set :repository, "https://forge.montana.edu/svn/#{application}/trunk"
set :apps_database, "#{application}_production"
set :dev_database, "#{application}_development"
set :demo_database, "#{application}_demo"
# Common Settings
desc "Common Settings"
task :common_settings do
set :deploy_to, "/home/#{application}/rails/#{application}"
set :shared_dir, "shared"
set :user, "#{application}"
set :use_sudo, false
role :app, domain
role :web, domain
# server domain, :app, :web
role :db, domain, :primary => true
# default_run_options[:pty] = true
end
# Development Server (dev.msu.montana.edu)
desc "Deploy to dev.msu.montana.edu"
task :dev do
set :user, "#{application}"
set :domain, "dev.msu.montana.edu"
set :database, dev_database
common_settings
end
# Production Server (apps.montana.edu)
desc "Deploy to apps.montana.edu"
task :apps do
set :user, "#{application}"
set :domain, "apps.montana.edu"
set :database, apps_database
common_settings
end
# Demo Instance on the Production Server (apps.montana.edu)
desc "Deploy a demo to apps.montana.edu"
task :demo do
set :orig_user, application
set :user, "#{application}demo"
set :domain, "apps.montana.edu"
set :database, demo_database
common_settings
set :repository, "https://forge.montana.edu/svn/neurosys/prototypes/#{orig_application}/trunk"
end
# SVN Settings
desc "Prompt the user for the SVN settings"
task :svn_settings do
scm_username_prompt = "SVN username for checkout (leave blank for public): "
set :temp_scm_username, Proc.new { Capistrano::CLI.password_prompt(scm_username_prompt) }
set :scm_username, "'#{temp_scm_username}'" unless temp_scm_username.empty?
scm_password_prompt = "SVN password for checkout (leave blank for public): "
set :temp_scm_password, Proc.new { Capistrano::CLI.password_prompt(scm_password_prompt) }
set :scm_password, "'#{temp_scm_password}'" unless temp_scm_password.empty?
set :checkout, "export"
end
before :deploy, "svn_settings"
# Passenger Tasks
namespace :passenger do
desc "Restart Passenger Webserver"
task :restart do
run "touch #{current_path}/tmp/restart.txt"
end
desc "Create Symlink For App"
task :add_symlink do
run "ln -nfs /home/#{application}/rails/#{application}/current /home/#{application}/rails/app"
end
end
after :deploy, "passenger:add_symlink"
deploy.task :restart, :roles => :app do
"passenger:restart"
end
# Database Tasks
namespace :db do
desc "Create database.yml in shared/config"
task :default do
db_password_prompt = "Server Database password for #{user}: "
set :db_password, Proc.new { Capistrano::CLI.password_prompt(db_password_prompt) }
run "mkdir -p #{deploy_to}/#{shared_dir}/config"
database_yml = <<-DB
login: &login
adapter: mysql
username: #{user}
password: #{db_password}
host: snarl.msu.montana.edu
sslca: /etc/mysql/ssl/ca-cert.pem
sslkey: /etc/mysql/ssl/client-key.pem
sslcert: /etc/mysql/ssl/client-cert.pem
test:
<<: *login
database: #{user}_test
production:
<<: *login
database: #{database}
DB
put ERB.new(database_yml).result(binding), "#{deploy_to}/#{shared_dir}/config/database.yml"
end
desc "Link in the production database.yml"
task :symlink do
run "ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml"
end
end
before "deploy:setup", :db
after "deploy:update_code", "db:symlink"
# Assets Tasks
desc "Create the assets directory for uploaded files"
namespace :assets do
task :default do
run "mkdir -p #{deploy_to}/#{shared_dir}/public/assets"
run "cp -al /home/#{orig_user}/rails/#{orig_user}/shared/public/assets/0000 #{deploy_to}/#{shared_dir}/public/assets/"
end
task :symlink do
run "ln -nfs #{deploy_to}/#{shared_dir}/public/assets #{release_path}/public/assets"
end
end
#before "deploy:setup", :assets
#after "deploy:update_code", "assets:symlink"
# Demo Synching Tasks
desc "Sync the production database to the demo database"
namespace :demo_prep do
task :prep_directories do
run "mkdir -p #{deploy_to}/#{shared_dir}/public/assets"
run "mkdir -p #{deploy_to}/#{shared_dir}/db/data"
run "/usr/bin/mysqldump -u #{orig_user} -p#{db_password} #{apps_database} > #{deploy_to}/#{shared_dir}/system/#{apps_database}.sql"
run "ln -nfs #{deploy_to}/#{shared_dir}/db/data #{release_path}/db/data"
end
task :load_production_sql do
run "/usr/bin/mysql -u #{orig_application} -p#{db_password} #{database} < #{deploy_to}/#{shared_dir}/system/#{apps_database}.sql"
end
task :load_dumped_yaml do
run("cd #{deploy_to}/current; /usr/bin/rake db:load RAILS_ENV=production")
end
task :obfuscate_production do
run("cd #{deploy_to}/current; /usr/bin/rake db:dump OBF='users.first_name, users.last_name, users.email, teams.name' RAILS_ENV='production'")
end
task :load_production_data do
load_production_sql
obfuscate_production
load_dumped_yaml
end
end
# These should in all liklihood be done manually
# after "deploy:setup", 'demo:prep_directories'
# after "deploy:setup", 'demo:load_production_data'
# Common Rake tasks
namespace :rake do
namespace :db do
task :reset do
run("cd #{deploy_to}/current; /usr/bin/rake db:reset RAILS_ENV=production")
end
task :reset do
run("cd #{deploy_to}/current; /usr/bin/rake db:reset RAILS_ENV=production")
end
task :migrate do
run("cd #{deploy_to}/current; /usr/bin/rake db:migrate RAILS_ENV=production")
end
task :load_fixtures do
run("cd #{deploy_to}/current; /usr/bin/rake db:fixtures:load RAILS_ENV=production")
end
task :create do
run("cd #{deploy_to}/current; /usr/bin/rake db:create RAILS_ENV=production")
end
end
task :install_gems do
run("cd #{deploy_to}/current; /usr/bin/rake gems:install")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment