Skip to content

Commit

Permalink
deploy ready?
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit Riessen committed Jan 7, 2011
1 parent e671d4d commit 1c1c6b0
Show file tree
Hide file tree
Showing 19 changed files with 523 additions and 58 deletions.
4 changes: 4 additions & 0 deletions Capfile
@@ -0,0 +1,4 @@
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }

load 'config/deploy' # remove this line to skip loading any of the default tasks
24 changes: 3 additions & 21 deletions Gemfile
Expand Up @@ -3,40 +3,22 @@ source 'http://rubygems.org'

gem 'rails', '3.0.3'

gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'haml'
gem 'haml-rails'
gem 'hpricot'
gem 'ruby_parser'
gem 'devise'

gem 'json'
# Use unicorn as the web server
gem 'unicorn'

# Deploy with Capistrano
gem 'eventmachine'
gem 'thin'
gem 'capistrano'
gem 'capistrano-ext'

gem "state_machine"
gem 'uuidtools'

gem "prawn", "= 0.11.1.pre"

gem "rest-client" ## active resource is a pile of warm shiet.
gem 'mysql2'

gem 'mysql'
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19'

# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'

gem 'heroku'
group :test do
gem 'rspec', "= 1.3.0"
gem 'mocha'
Expand Down
29 changes: 10 additions & 19 deletions Gemfile.lock
Expand Up @@ -40,32 +40,24 @@ GEM
net-ssh-gateway (>= 1.0.0)
capistrano-ext (1.2.1)
capistrano (>= 1.0.0)
configuration (1.2.0)
daemons (1.1.0)
devise (1.1.5)
bcrypt-ruby (~> 2.1.2)
warden (~> 1.0.2)
erubis (2.6.6)
abstract (>= 1.0.0)
eventmachine (0.12.10)
factory_girl (1.3.2)
haml (3.0.24)
haml-rails (0.3.4)
actionpack (~> 3.0)
activesupport (~> 3.0)
haml (~> 3.0)
railties (~> 3.0)
heroku (1.16.2)
json_pure (>= 1.2.0, < 1.5.0)
launchy (~> 0.3.2)
rest-client (>= 1.4.0, < 1.7.0)
highline (1.6.1)
hpricot (0.8.3)
i18n (0.5.0)
json (1.4.6)
json_pure (1.4.6)
kgio (2.0.0)
launchy (0.3.7)
configuration (>= 0.0.5)
rake (>= 0.8.1)
looksee (0.2.1)
mail (2.2.12)
activesupport (>= 2.3.6)
Expand All @@ -75,7 +67,7 @@ GEM
mime-types (1.16)
mocha (0.9.10)
rake
mysql (2.8.1)
mysql2 (0.2.6)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
net-sftp (2.0.5)
Expand Down Expand Up @@ -117,15 +109,15 @@ GEM
sexp_processor (~> 3.0)
sexp_processor (3.0.5)
shoulda (2.11.3)
sqlite3-ruby (1.3.2)
state_machine (0.9.4)
thin (1.2.7)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
unicorn (3.1.0)
kgio (~> 2.0.0)
rack
uuidtools (2.1.1)
warden (1.0.3)
rack (>= 1.0.0)
Expand All @@ -138,24 +130,23 @@ DEPENDENCIES
capistrano
capistrano-ext
devise
eventmachine
factory_girl
haml
haml-rails
heroku
hpricot
json
looksee
mocha
mysql
mysql2
prawn (= 0.11.1.pre)
rails (= 3.0.3)
rest-client
rspec (= 1.3.0)
rspec-rails (= 1.3.2)
ruby_parser
shoulda
sqlite3-ruby
state_machine
unicorn
thin
uuidtools
wirble
18 changes: 1 addition & 17 deletions app/controllers/page_elements_controller.rb
Expand Up @@ -28,7 +28,7 @@ def create
## NOTE: that the params[:page_id] is the page number in this case.
## NOTE: params[:publication_id] is the publication UUID in this case.
publication = Publication.find_by_uuid(params[:publication_id])
page_element_klazz = class_for_isa(params[:isa])
page_element_klazz = PageElement.class_for_isa(params[:isa])
data = {
:id_str => params[:idStr],
:x => params[:x], :y => params[:y],
Expand All @@ -47,22 +47,6 @@ def create

protected

# don't take 'isa' on face value, check whether we have a corresponding class
# for the isa value.
def class_for_isa(isa_str)
case isa_str
when "Facebook" then FacebookElement
when "Tweet" then TwitterElement
when "Flickr" then FlickrElement
when "ImageTE" then ImageElement
when "TextTE" then TextElement
when "FbLikeTE" then FbLikeElement
when "TwitterFeedTE" then TwitterFeedElement
else
"UnknownClass#{params[:isa]}"
end
end

def check_page_element(params, &block)
## NOTE: that the params[:page_id] is the page number in this case.
## NOTE: params[:publication_id] is the publication UUID in this case.
Expand Down
17 changes: 17 additions & 0 deletions app/models/page_element.rb
Expand Up @@ -22,6 +22,23 @@ def extract_data_from_params(params)
def params_to_data(params)
extract_data_from_params(params).to_json
end

# don't take 'isa' on face value, check whether we have a corresponding class
# for the isa value.
def class_for_isa(isa_str)
case isa_str
when "Facebook" then FacebookElement
when "Tweet" then TwitterElement
when "Flickr" then FlickrElement
when "ImageTE" then ImageElement
when "TextTE" then TextElement
when "FbLikeTE" then FbLikeElement
when "DiggButtonTE" then DiggButtonElement
when "TwitterFeedTE" then TwitterFeedElement
else
"UnknownClass#{params[:isa]}"
end
end
end

def extra_data
Expand Down
1 change: 0 additions & 1 deletion app/views/publications/show.html.haml
Expand Up @@ -7,4 +7,3 @@
%div{ :id => dom_id(page), :class => "page_a4 shadow" }
- page.page_elements.each do |page_element|
= render :partial => "page_element", :locals => { :page_element => page_element }
%fb:like{ :ref => @publication.uuid }
159 changes: 159 additions & 0 deletions config/deploy.rb
@@ -0,0 +1,159 @@
set :stages, %w(staging production prodbackup beta)
set :default_stage, "staging"
require 'capistrano/ext/multistage'

require File.dirname(__FILE__) + '/../lib/deploy_helpers'
class << self
include DeployHelpers::Bindings
include DeployHelpers::Utils
end

set :application, "website"
set :deploy_to, "/var/app/#{application}"
set :user, "deploy"
set :local_rails_root, File.expand_path(File.join(File.dirname(__FILE__), ".."))

set :scm, :git
set :repository, "gitosis@dev.2monki.es:#{ENV['GITREPO'] || application}.git"
set :branch, ENV["GITBRANCH"] || "master"
set :git_shallow_clone, 3
set :git_enable_submodules, 1 ## note, this does not work for deep modules only toplevel

set :rvm_ruby_version, "ruby-1.9.2-head"

namespace :deploy do
{ ### After hooks
"migrate" => "restart",
"symlink" => "setup_paths",
"setup_paths" => [ "run_remote_scripts", "generate_remote_files"],
}.each do |after_task, before_tasks|
[before_tasks].flatten.each do |before_task|
after "deploy:#{after_task}", "deploy:#{before_task}"
end
end

{ ### Before hooks
"symlink" => "setup_diff",
"set_passenger_version" => "setup_nginx_passenger_module",
"restart" => ["show_diffs", "update_superglue",
"set_passenger_version", "bundle_install"],
}.each do |before_task, after_tasks|
[after_tasks].flatten.each do |after_task|
before "deploy:#{before_task}", "deploy:#{after_task}"
end
end

desc "update the superglue and make sure it's still holding things together"
task :update_superglue do
run "cd ~/superglue && git pull"
end

desc "install gems with bundle"
task :bundle_install do
run_with_rvm(rvm_ruby_version, current_path) { "bundle install" }
end

desc "Need to use rvm to call rake, i.e. migrate won't work otherwise."
task :migrate, :roles => :db do
run_with_rvm(rvm_ruby_version, current_path) do
"RAILS_ENV=production rake db:migrate"
end
end

desc "create various paths and replace symlinks"
task :setup_paths do
[
# system folder is only used for maintenance, therefore whip it's ass
[ "public/system", "#{shared_path}/system/maintenance"],

# various yaml configurations
["config/apis.yml",
"#{shared_path}/system/config/apis.yml-#{application}"],
["config/database.yml",
"#{shared_path}/system/config/database.yml-#{application}"],

# redirect the log symlink for log
["log", "#{shared_path}/#{application}/log"],
].each do |dest, src|
dest = "#{current_path}/#{dest}"
run "rm -fr #{dest} && ln -snf #{src} #{dest}"
end
end

desc "Generate files on the remote server"
task :generate_remote_files do
{
"nginx.conf" => "#{current_path}/config",
"monitrc" => "#{current_path}/config",
}.each do |configfile, remote_path|
remote_path = "#{remote_path}/#{configfile}"
local_path = "config/remote_files/#{configfile}.erb"
local_file = File.join(local_rails_root, local_path)
erb = ERB.new(File.open(local_file).read)
perm_val = ("%o" % File.stat(local_file).mode)[3..-1]
binding_method = "generate_binding_for_#{configfile.gsub(/[.]/,'_')}"

roles[:app].each do |server_def|
put(erb.result( self.send(binding_method,
generate_default_opts(server_def,current_path))),
remote_path, :only => { :host_ip => server_def.options[:host_ip] })

run("chmod #{perm_val} #{remote_path}",
:only => {:host_ip => server_def.options[:host_ip]})

@diff[server_def.host].
merge!({ configfile =>
capture("diff -C 1 #{current_path}/#{local_path} " +
"#{remote_path} || echo",
:only => {:host_ip => server_def.options[:host_ip]})})
end
end
end

desc "Do stuff on the server to which we have just deployed"
task :run_remote_scripts do
[
## TODO add some!
].each do |basename|
binding_method = "generate_binding_for_#{basename.gsub(/[.]/,'_')}"
local_path = File.join(local_rails_root, "config", "remote_scripts",
"#{basename}.erb")
remote_path = "#{current_path}/tmp/#{basename}"
erb = ERB.new(File.open(local_path).read)

roles[:app].each do |server_def|
put(erb.result(self.send(binding_method,
generate_default_opts(server_def,current_path))),
remote_path, :only => { :host_ip => server_def.options[:host_ip] })
run("ruby #{remote_path}", :only => {:host_ip => server_def.options[:host_ip]})
end
end
end

# Restart passenger on deploy
desc "Restarting passenger with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end

# show all the diffs we did. this makes the deploy a little more understandable if
# we have the differs at the end and all the "deploy stuff".
task :show_diffs do
puts " All went well, just about to restart but first the differs"
puts " --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<"
(@diff||{}).each do |server, diffs|
puts " ## Server: #{server}"
diffs.each do |filename, diffstr|
puts " @@@@@@@@@@@@@@@@@@@ File: #{filename} @ #{server}"
puts diffstr
end
end
puts " -->8-->8-->8-->8-->8-->8-->8-->8-->8-->8-->8-->8-->8-->8-->8"
end
task(:setup_diff) { @diff = Hash.new { |h,k| h[k] = Hash.new } }

[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
end

0 comments on commit 1c1c6b0

Please sign in to comment.