Skip to content

Instantly share code, notes, and snippets.

@danielalvarenga
Last active August 29, 2015 14:07
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 danielalvarenga/82e29cee3d5feb758d22 to your computer and use it in GitHub Desktop.
Save danielalvarenga/82e29cee3d5feb758d22 to your computer and use it in GitHub Desktop.
Configuração de projeto rails (Spree Commerce) para deploy utilizando Capistrando e Unicorn
# Database config shared in Capistrano 3: .../shared/config/database.yml
# MySQL. Versions 5.0+ are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: 5
host: 127.0.0.1
socket: /var/run/mysqld/mysqld.sock
homol:
<<: *default
database: db_homol
username: root
password: my_pass
# .../config/deploy.rb
# config valid only for Capistrano 3.1
lock '3.2.1'
set :application, 'spree_story'
set :repo_url, 'git@github.com:brasilct/spree_story.git'
# Default value for :scm is :git
set :scm, :git
# Default value for :format is :pretty
set :format, :pretty
# Default value for :log_level is :debug
set :log_level, :debug
set :ssh_options, {
verbose: :debug
}
# Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/spree}
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
set :default_shell, '/bin/bash --login'
set :use_sudo, false
namespace :deploy do
desc 'Restart application'
task :restart do
invoke 'unicorn:restart'
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
task :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
within release_path do
execute :rake, 'tmp:cache:clear'
end
end
end
after :restart, :clear_cache
after :restart, :cleanup
end
namespace :spree do
desc 'Add Spree User Admin'
task :auth do
on roles(:web) do
# Here we can do anything such as:
within release_path do
execute :rake, 'spree_auth:admin:create'
end
end
end
end
namespace :task do
desc 'Execute the specific command. Ex. call: cap staging "task:invoke[db:migrate]"'
task :invoke, :command do |task, args|
on roles(:app) do
within release_path do
execute :rake, args[:command]
end
end
end
end
# Staging de Capistrango: .../config/deploy/homol.rb
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary server in each group
# is considered to be the first unless any hosts have the primary
# property set. Don't declare `role :all`, it's a meta role.
# role :app, %w{ubuntu@123.123.123.123}
# role :web, %w{ubuntu@123.123.123.123}
# role :db, %w{ubuntu@123.123.123.123}
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server definition into the
# server list. The second argument is a, or duck-types, Hash and is
# used to set extended properties on the server.
# server '123.123.123.123', user: 'ubuntu', roles: %w{web app db}, my_property: :my_value
# Custom SSH Options
# ==================
# You may pass any option but keep in mind that net/ssh understands a
# limited set of options, consult[net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start).
#
# Global options
# --------------
# set :ssh_options, {
# keys: %w(/home/rlisowski/.ssh/id_rsa),
# forward_agent: false,
# auth_methods: %w(password)
# }
#
# And/or per server (overrides global)
# ------------------------------------
default_environment = :homol
app_name = 'spree_story'
host = '123.123.123.123'
host_user = 'my_user'
host_publickey_path = %w(~/.ssh/publickey)
# Default value for keep_releases is 5
set :keep_releases, 3
set :stage, default_environment
server host,
user: host_user,
roles: %w{web app db},
ssh_options: {
user: host_user, # overrides user setting above
keys: host_publickey_path,
forward_agent: false,
auth_methods: %w(publickey password),
# password: ''
}
# Default deploy_to directory is /var/www/my_app
set :deploy_to, "/var/www/#{app_name}"
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :branch, default_environment
# Rails specific options
set :rails_env, default_environment # If the environment differs from the stage name
set :migration_role, :db # Defaults to 'db'
set :conditionally_migrate, false # Defaults to false
set :assets_roles, [:web, :app] # Defaults to [:web]
# set :assets_prefix, 'prepackaged-assets' # Defaults to 'assets' this should match config.assets.prefix in your rails config/application.rb
# set :normalize_asset_timestamps, %{public/images public/javascripts public/stylesheets} # If you need to touch public/images, public/javascripts and public/stylesheets on each deploy
# RVM
set :rvm_type, :user
set :rvm_ruby_version, "2.1.2@#{app_name}"
set :rvm_custom_path, '/home/ubuntu/.rvm'
set :unicorn_rack_env, default_environment
set :unicorn_env, default_environment
set :unicorn_config_path, "config/unicorn/#{default_environment}.rb"
# Config de Unicorn: .../config/unicorn/homol.rb
app_name = "spree_story"
shared_path = "/var/www/#{app_name}/shared"
host_user = 'my_user'
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete documentation.
worker_processes 4
# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/tmp/unicorn.#{app_name}.socket", :backlog => 64
# Preload our app for more speed
preload_app true
# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory "/var/www/#{app_name}/current"
# feel free to point this anywhere accessible on the filesystem
user host_user, host_user
# By default, the Unicorn logger will write to stderr.
# Additionally, some applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"
pid "#{shared_path}/tmp/pids/unicorn.pid"
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
end
# The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
#
# This allows a new master process to incrementally
# phase out the old master process with SIGTTOU to avoid a
# thundering herd (especially in the "preload_app false" case)
# when doing a transparent upgrade. The last worker spawned
# will then kill off the old master process with a SIGQUIT.
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
after_fork do |server, worker|
# the following is *required* for Rails + "preload_app true"
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
# if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment