Permalink
Browse files

Updated Cap recipes and deploy.rb example.

  • Loading branch information...
1 parent 9aa1802 commit 554a25b792a239a330520f55309a07f2173b8a2f @seven1m seven1m committed Oct 8, 2010
Showing with 34 additions and 189 deletions.
  1. +1 −0 Capfile
  2. +6 −13 config/deploy.rb.example
  3. +12 −7 lib/recipes/deploy.rb
  4. +15 −169 lib/recipes/install.rb
View
@@ -4,3 +4,4 @@ Dir['plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
Dir['lib/recipes/*.rb'].each { |recipe| load(recipe) }
load 'config/deploy'
load 'lib/cap'
+default_run_options[:pty] = true
View
@@ -1,13 +1,3 @@
-# copy this file to config/deploy.rb and adjust, then on the server:
-# # sudo adduser deploy
-# add user "deploy" to /etc/sudoers
-# copy your ssh public key (~/.ssh/id_rsa.pub) to the remote server (/home/deploy/.ssh/authorized_keys)
-# finally, on your local machine:
-# # cap deploy:install
-# # cap deploy:setup
-# # cap deploy:cold
-# See http://www.capify.org/getting-started/rails for more help
-
# point to your server
set :host, 'beonebody.org'
@@ -20,9 +10,6 @@ set :repository, "git://github.com/seven1m/onebody.git"
set :branch, 'stable' # remove this line if you want bleeding edge or you're pointing to your own repo/fork
set :scm, :git
-# port to run OneBody Setup Mode on
-set :setup_port, 7999
-
set :application, 'onebody'
set :user, 'deploy'
set :runner, user
@@ -31,3 +18,9 @@ set :deploy_via, :fast_remote_cache
set :deploy_to, "/var/www/apps/#{application}"
set :copy_exclude, %w(test .git)
set :use_sudo, false
+
+# for RVM
+$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
+require "rvm/capistrano"
+set :rvm_ruby_string, 'ree@onebody'
+set :rvm_type, :user
View
@@ -1,9 +1,10 @@
namespace :deploy do
- task :before_setup do
+ task :chown_deploy_dir do
sudo "mkdir -p #{deploy_to}"
sudo "chown #{user}:#{user} #{deploy_to}"
end
+ before 'deploy:setup', 'deploy:chown_deploy_dir'
namespace :shared do
@@ -25,14 +26,17 @@
end
task :create_database do
- mysql_root_password = HighLine.new.ask('MySQL ROOT password: ') { |q| q.echo = false }
- run "mysql -uroot -p#{mysql_root_password} -e \"create database onebody; grant all on onebody.* to onebody@localhost identified by '#{get_db_password}'\""
- yml = render_erb_template(File.dirname(__FILE__) + '/templates/database.yml')
- put yml, "#{shared_path}/config/database.yml"
+ unless ENV['SKIP_DB_SETUP']
+ mysql_root_password = HighLine.new.ask('MySQL ROOT password: ') { |q| q.echo = false }
+ p = mysql_root_password.empty? ? '' : "-p#{mysql_root_password}"
+ run "mysql -uroot #{p} -e \"create database if not exists onebody; grant all on onebody.* to onebody@localhost identified by '#{get_db_password}';\""
+ yml = render_erb_template(File.dirname(__FILE__) + '/templates/database.yml')
+ put yml, "#{shared_path}/config/database.yml"
+ end
end
after 'deploy:setup', 'deploy:create_database'
- task :after_update_code do
+ task :update_links_and_plugins do
rb = render_erb_template(File.dirname(__FILE__) + '/templates/links.rb')
put rb, "#{release_path}/config/initializers/links.rb"
commands = [
@@ -41,10 +45,11 @@
"rm -rf #{release_path}/public/assets && ln -s #{shared_path}/public/assets #{release_path}/public/assets",
"cd #{shared_path}/plugins; if [ \"$(ls -A)\" ]; then rsync -a * #{release_path}/plugins/; fi",
"cd #{shared_path}/initializers; if [ \"$(ls -A)\" ]; then rsync -a * #{release_path}/config/initializers/; fi",
- "cd #{release_path} && whenever -w RAILS_ENV=production"
+ "cd #{release_path}; if [[ `which whenever` != '' ]]; then whenever -w RAILS_ENV=production; fi"
]
run commands.join('; ')
end
+ after 'deploy:update_code', 'deploy:update_links_and_plugins'
task :copy_ssh_key do
run "mkdir -p ~/.ssh"
View
@@ -1,195 +1,41 @@
require 'open-uri'
namespace :deploy do
+
namespace :install do
- desc 'Install all required server software on Ubuntu'
+ desc 'Deploy the newest release, install Rails, gem dependencies, and migrate the database.'
task :default do
- prerequisites
- ruby
- rubygems
- rails
- passenger
- dependencies
- mysql
- postfix
- end
-
- desc 'Install Ruby/OneBody prerequisites'
- task :prerequisites do
- sudo 'aptitude update'
- sudo 'aptitude install -y build-essential imagemagick apache2 apache2-dev apache2-mpm-worker apache2-threaded-dev git-core rsync libxml2-dev libxslt-dev libcurl4-gnutls-dev'
- end
-
- desc 'Install Ruby'
- task :ruby, :roles => :web do
- sudo 'aptitude update'
- sudo 'aptitude install -y ruby1.8 ruby1.8-dev'
- end
-
- desc 'Install RubyGems'
- task :rubygems, :roles => :web do
- sudo 'aptitude update'
- sudo 'aptitude install -y libgems-ruby1.8'
- sudo 'ln -sf /usr/bin/ruby1.8 /usr/bin/ruby'
- run 'cd /tmp && wget -nv http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz && tar xzf rubygems-1.3.5.tgz'
- run 'cd /tmp/rubygems-1.3.5 && sudo ruby setup.rb'
- if run_and_return('gem1.8 --version') =~ /1\.2\.0/
- sudo 'ln -sf /usr/bin/gem1.8 /usr/bin/gem'
- end
- sudo 'gem update --system'
+ update
+ find_and_execute_task('deploy:install:rails')
+ find_and_execute_task('deploy:install:dependencies')
+ migrate
+ restart
end
desc 'Install/Update Rails'
task :rails do
rails_version = File.read(File.dirname(__FILE__) + '/../../config/environment.rb').match(/RAILS_GEM_VERSION = '(.+?)'/)[1]
unless run_and_return('gem list rails') =~ Regexp.new(rails_version)
- sudo "gem install -v=#{rails_version} rails --no-rdoc --no-ri"
+ run "gem install -v=#{rails_version} rails --no-rdoc --no-ri"
end
end
- desc 'Install Passenger (set APACHE_CONFIG=true to enable module in Apache)'
- task :passenger, :roles => :web do
- gem_name = nil
- send(:sudo, 'gem install passenger --no-rdoc --no-ri') do |channel, stream, data|
- logger.info data, channel[:host]
- if data =~ /select which gem to install/i
- if selection = data.split(/\n/).select { |l| l =~ /\(ruby\)$/ }.first and
- selection =~ /^\s*(\d+)\./
- logger.info "Selecting #{$1}...", channel[:host]
- channel.send_data "#{$1}\n"
- end
- end
- if data =~ /successfully installed (passenger-[\d\.]+)/i
- gem_name = $1
- end
- end
- gems_path = nil
- send(:run, 'gem which rake') do |channel, stream, data|
- data =~ /^(\/.+?)\/rake.+/
- gems_path = $1
- end
- passenger_path = "#{gems_path}/#{gem_name}"
- run "cd #{passenger_path} && sudo rake clean apache2"
- if ENV['APACHE_CONFIG']
- run "echo 'LoadModule passenger_module #{passenger_path}/ext/apache2/mod_passenger.so' | sudo tee /etc/apache2/mods-available/passenger.load"
- run "echo '<IfModule passenger_module>\\n PassengerRoot #{passenger_path}\\n PassengerRuby /usr/bin/ruby\\n</IfModule>' | sudo tee /etc/apache2/mods-available/passenger.conf"
- sudo "a2enmod passenger"
- sudo "/etc/init.d/apache2 force-reload"
- end
- end
-
- desc 'Install MySQL'
- task :mysql, :roles => :db do
- sudo 'aptitude update'
- sudo 'aptitude install -y mysql-server libmysql-ruby1.8'
- password = HighLine.new.ask('Password for MySQL root user: ') { |q| q.echo = false }
- run "mysqladmin -uroot password \"#{password}\""
- end
-
- desc 'Install Postfix'
- task :postfix, :roles => :web do
- sudo 'aptitude update'
- sudo 'aptitude install -y postfix'
- end
-
- # Configure iptables firewall (assumes iptables already installed)
- # use at your own risk (check templates/iptables.txt before you use this)
- task :firewall, :roles => :web do
- rules = render_erb_template(File.dirname(__FILE__) + '/templates/iptables.txt')
- put rules, "/tmp/iptables.up.rules"
- sudo "mv /tmp/iptables.up.rules /etc/"
- sudo "ruby -e \"d=File.read('/etc/network/interfaces'); exit if d =~ /iptables/; d.gsub!(/(iface lo inet loopback)(\\n)/, '\\1\\2pre-up iptables-restore < /etc/iptables.up.rules\\2'); File.open('/etc/network/interfaces', 'w') { |f| f.write(d) }\""
- puts 'Restart the server for the config to take effect.'
- end
-
desc 'Install gem dependencies'
task :dependencies, :roles => :web do
- gems = File.read(File.dirname(__FILE__) + '/../../config/environment.rb').scan(/config\.gem ["']([a-z0-9_\-]+)["'](.*)/i)
- github_gems = gems.select { |g| g[1] =~ /gems\.github\.com/ }
- gems -= github_gems
- sudo "gem install --no-rdoc --no-ri #{gems.map { |g| g[0] }.join(' ')}"
- sudo "gem install --no-rdoc --no-ri #{github_gems.map { |g| g[0] }.join(' ')} -s http://gems.github.com"
+ run "cd #{release_path}; gem install mysql && rake gems:install"
end
- # Ruby Enterprise Edition Recipes
- # # # # # # # # # # # # # # # # #
-
- namespace :ree do
-
- desc 'Install all required server software on Ubuntu, with Ruby Enterprise Edition'
- task :default do
- find_and_execute_task('deploy:install:prerequisites')
- ruby
- rails
- dependencies
- find_and_execute_task('deploy:install:mysql')
- find_and_execute_task('deploy:install:postfix')
- end
-
- desc 'Install Ruby Enterprise Edition'
- task :ruby, :roles => :web do
- sudo 'aptitude install -y libreadline5-dev libmysqlclient-dev'
- url = open('http://www.rubyenterpriseedition.com/download.html').read.match(%r{http://rubyforge\.org/frs/download\.php/[^"]+}).to_s
- filename = url.split('/').last
- run "cd /tmp && wget -nv #{url} && tar xzf #{filename}"
- directory = filename.sub(/\.tar\.gz/, '')
- run "cd /tmp/#{directory} && sudo ruby installer.rb -a /opt/ruby-enterprise"
- end
-
- desc 'Install Passenger (set APACHE_CONFIG=true to enable module in Apache)'
- task :passenger, :roles => :web do
- gem_name = nil
- send(:sudo, '/opt/ruby-enterprise/bin/gem install passenger --no-rdoc --no-ri') do |channel, stream, data|
- logger.info data, channel[:host]
- if data =~ /select which gem to install/i
- if selection = data.split(/\n/).select { |l| l =~ /\(ruby\)$/ }.first and
- selection =~ /^\s*(\d+)\./
- logger.info "Selecting #{$1}...", channel[:host]
- channel.send_data "#{$1}\n"
- end
- end
- if data =~ /successfully installed (passenger-[\d\.]+)/i
- gem_name = $1
- end
- end
- gems_path = '/opt/ruby-enterprise/lib/ruby/gems/1.8/gems'
- passenger_path = "#{gems_path}/#{gem_name}"
- run "cd #{passenger_path} && sudo rake clean apache2"
- if ENV['APACHE_CONFIG']
- run "echo 'LoadModule passenger_module #{passenger_path}/ext/apache2/mod_passenger.so' | sudo tee /etc/apache2/mods-available/passenger.load"
- run "echo '<IfModule passenger_module>\\n PassengerRoot #{passenger_path}\\n PassengerRuby /opt/ruby-enterprise/bin/ruby\\n</IfModule>' | sudo tee /etc/apache2/mods-available/passenger.conf"
- sudo "a2enmod passenger"
- sudo "/etc/init.d/apache2 force-reload"
- end
- end
-
- desc 'Install/Update Rails in Ruby Enterprise Edition'
- task :rails do
- rails_version = File.read(File.dirname(__FILE__) + '/../../config/environment.rb').match(/RAILS_GEM_VERSION = '(.+?)'/)[1]
- unless run_and_return('/opt/ruby-enterprise/bin/gem list rails') =~ Regexp.new(rails_version)
- sudo "/opt/ruby-enterprise/bin/gem install -v=#{rails_version} rails --no-rdoc --no-ri"
- end
- end
+ end
- desc 'Install gem dependencies in Ruby Enterprise Edition'
- task :dependencies do
- gems = File.read(File.dirname(__FILE__) + '/../../config/environment.rb').scan(/config\.gem ["']([a-z0-9_\-]+)["'](.*)/i)
- github_gems = gems.select { |g| g[1] =~ /gems\.github\.com/ }
- gems -= github_gems
- sudo "/opt/ruby-enterprise/bin/gem install --no-rdoc --no-ri #{gems.map { |g| g[0] }.join(' ')}"
- sudo "/opt/ruby-enterprise/bin/gem install --no-rdoc --no-ri #{github_gems.map { |g| g[0] }.join(' ')} -s http://gems.github.com"
- end
+ namespace :upgrade do
+ desc 'Deploy the newest release, upgrade Rails, gem dependencies, and migrate the database.'
+ task :default do
+ # for the timebeing, this does nothing different than the deploy:install recipe
end
+ after 'deploy:upgrade', 'deploy:install'
end
- task :cold do
- update
- find_and_execute_task('deploy:install:rails')
- find_and_execute_task('deploy:install:dependencies')
- migrate
- restart
- end
end

0 comments on commit 554a25b

Please sign in to comment.