diff --git a/Capfile b/Capfile index 447fe75..e5361f2 100644 --- a/Capfile +++ b/Capfile @@ -1,4 +1,4 @@ load 'deploy' # Uncomment if you are using Rails' asset pipeline -load 'deploy/assets' +#load 'deploy/assets' load 'config/deploy' # remove this line to skip loading any of the default tasks diff --git a/Gemfile b/Gemfile index 94812f4..92b2d98 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ -#source 'https://rubygems.org' -source 'http://ruby.taobao.org' +source 'https://rubygems.org' +#source 'http://ruby.taobao.org' gem 'rails', '3.2.13' @@ -46,7 +46,11 @@ gem 'formtastic-bootstrap' gem 'unicorn' # Deploy with Capistrano -gem 'capistrano' +group :development do + gem 'rvm-capistrano' + gem 'capistrano-rbenv' + gem 'capistrano-unicorn', require: false +end # To use debugger # gem 'debugger' diff --git a/Gemfile.lock b/Gemfile.lock index 33e1c16..b892ff9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,5 +1,5 @@ GEM - remote: http://ruby.taobao.org/ + remote: https://rubygems.org/ specs: actionmailer (3.2.13) actionpack (= 3.2.13) @@ -36,6 +36,13 @@ GEM net-sftp (>= 2.0.0) net-ssh (>= 2.0.14) net-ssh-gateway (>= 1.1.0) + capistrano-platform-resources (0.1.3) + capistrano (< 3) + capistrano-rbenv (1.0.5) + capistrano (< 3) + capistrano-platform-resources (>= 0.1.3) + capistrano-unicorn (0.1.7) + capistrano coffee-rails (3.2.2) coffee-script (>= 2.2.0) railties (~> 3.2.0) @@ -108,6 +115,8 @@ GEM rdoc (3.12.2) json (~> 1.4) ref (1.0.4) + rvm-capistrano (1.3.0) + capistrano (>= 2.0.0) sass (3.2.8) sass-rails (3.2.6) railties (~> 3.2.0) @@ -146,7 +155,8 @@ PLATFORMS ruby DEPENDENCIES - capistrano + capistrano-rbenv + capistrano-unicorn coffee-rails (~> 3.2.1) commonjs formtastic @@ -157,6 +167,7 @@ DEPENDENCIES libv8 (~> 3.11.8) mysql2 rails (= 3.2.13) + rvm-capistrano sass-rails (~> 3.2.3) simple_form therubyracer diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..9249f1e --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb \ No newline at end of file diff --git a/config/database.example.yml b/config/database.example.yml index 348e564..faa43ba 100644 --- a/config/database.example.yml +++ b/config/database.example.yml @@ -38,5 +38,5 @@ production: database: blog_production pool: 5 username: root - password: + password: password socket: /var/run/mysqld/mysqld.sock diff --git a/config/deploy.rb b/config/deploy.rb index 9f38025..b0e617a 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -1,52 +1,68 @@ require "bundler/capistrano" +require 'capistrano-rbenv' +require 'capistrano-unicorn' -server "42.121.122.34", :web, :app, :db, primary: true +server "198.199.86.209", :web, :app, :db, primary: true set :application, "blog" -set :user, "root" +set :user, "ruby" set :deploy_to, "/home/#{user}/apps/#{application}" -set :deploy_via, :remote_cache +set :deploy_via, :copy#remote_cache set :use_sudo, false +set :unicorn_binary, 'unicorn_rails' -set :scm, "git" +#set :scm, "git" #set :repository, "git@github.com:jerry134/#{application}.git" -set :repository, "https://github.com/jerry134/#{application}.git" -set :branch, "master" +set :repository, '.'#"https://github.com/jerry134/#{application}.git" +#set :branch, "master" default_run_options[:pty] = true ssh_options[:forward_agent] = true after "deploy", "deploy:cleanup" # keep only the last 5 releases +after 'deploy:restart', 'unicorn:reload' +after 'deploy:restart', 'unicorn:restart' namespace :deploy do - %w[start stop restart].each do |command| - desc "#{command} unicorn server" - task command, roles: :app, except: {no_release: true} do - run "/etc/init.d/unicorn_#{application} #{command}" - end - end + #%w[start stop restart].each do |command| + #desc "#{command} unicorn server" + #task command, roles: :app, except: {no_release: true} do + #run "/etc/init.d/unicorn_#{application} #{command}" + #end + #end - task :setup_config, roles: :app do - sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}" - sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}" - run "mkdir -p #{shared_path}/config" - put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml" - puts "Now edit the config files in #{shared_path}." - end - after "deploy:setup", "deploy:setup_config" + task :start do + run "unicorn_rails -c #{release_path}/config/unicorn.rb -E production -D" + end + task :stop do + run "ps aux | grep unicorn | grep -v grep | awk '{print $2}' | xargs kill -s 9" + end + task :restart do + run "ps aux | grep unicorn | grep -v grep | awk '{print $2}' | xargs kill -s 9" + run "unicorn_rails -c #{release_path}/config/unicorn.rb -E production -D" + end + + #task :setup_config, roles: :app do + #sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}" + #sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}" + #run "mkdir -p #{shared_path}/config" + #put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml" + #puts "Now edit the config files in #{shared_path}." + #end + #after "deploy:setup", "deploy:setup_config" task :symlink_config, roles: :app do run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" end after "deploy:finalize_update", "deploy:symlink_config" - desc "Make sure local git is in sync with remote." - task :check_revision, roles: :web do - unless `git rev-parse HEAD` == `git rev-parse origin/master` - puts "WARNING: HEAD is not the same as origin/master" - puts "Run `git push` to sync changes." - exit - end - end - before "deploy", "deploy:check_revision" + #desc "Make sure local git is in sync with remote." + #task :check_revision, roles: :web do + #unless `git rev-parse HEAD` == `git rev-parse origin/master` + #puts "WARNING: HEAD is not the same as origin/master" + #puts "Run `git push` to sync changes." + #exit + #end + #end + #before "deploy", "deploy:check_revision" end diff --git a/config/deploy_rvm.rb b/config/deploy_rvm.rb new file mode 100644 index 0000000..4b9ba82 --- /dev/null +++ b/config/deploy_rvm.rb @@ -0,0 +1,55 @@ +require "bundler/capistrano" +require 'rvm/capistrano' +set :rvm_ruby_string, :local + +server "192.34.61.70", :web, :app, :db, primary: true + +set :application, "blog" +set :user, "ken" +set :deploy_to, "/home/#{user}/apps/#{application}" +set :deploy_via, :copy +set :use_sudo, false + +#set :scm, "git" +#set :repository, "git@github.com:jerry134/#{application}.git" +set :repository, '.'#"https://github.com/jerry134/#{application}.git" +set :branch, "master" + +default_run_options[:pty] = true +ssh_options[:forward_agent] = true + +after "deploy", "deploy:cleanup" # keep only the last 5 releases +before 'deploy:setup', 'rvm:create_gemset' + +namespace :deploy do + %w[start stop restart].each do |command| + desc "#{command} unicorn server" + task command, roles: :app, except: {no_release: true} do + run "/etc/init.d/unicorn_#{application} #{command}" + end + end + + task :setup_config, roles: :app do + sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}" + sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}" + run "mkdir -p #{shared_path}/config" + put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml" + puts "Now edit the config files in #{shared_path}." + end + after "deploy:setup", "deploy:setup_config" + + task :symlink_config, roles: :app do + run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" + end + after "deploy:finalize_update", "deploy:symlink_config" + + desc "Make sure local git is in sync with remote." + task :check_revision, roles: :web do + unless `git rev-parse HEAD` == `git rev-parse origin/master` + puts "WARNING: HEAD is not the same as origin/master" + puts "Run `git push` to sync changes." + exit + end + end + before "deploy", "deploy:check_revision" +end diff --git a/config/unicorn.rb b/config/unicorn.rb index 1bf8f70..2e210b7 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -1,10 +1,45 @@ -root = "/home/root/apps/blog/current" -working_directory root -pid "#{root}/tmp/pids/unicorn.pid" -stderr_path "#{root}/log/unicorn.log" -stdout_path "#{root}/log/unicorn.log" +#root = "/home/ruby/apps/blog/current" +#pid "#{root}/tmp/pids/unicorn.pid" -listen "/tmp/unicorn.blog.sock" -worker_processes 2 -timeout 30 +#listen "/tmp/unicorn.blog.sock" +app_path = "/home/ruby/apps/blog/current" + +# Set unicorn options +worker_processes 1 +preload_app true +timeout 180 +listen 9000 #"127.0.0.1:9000" + +# Spawn unicorn master worker for user apps (group: apps) +user 'ruby', 'ruby' + +# Fill path to your app +working_directory app_path + +# Should be 'production' by default, otherwise use other env +rails_env = ENV['RAILS_ENV'] || 'production' + +# Log everything to one file +stderr_path "log/unicorn.log" +stdout_path "log/unicorn.log" + +# Set master PID location +#pid "#{app_path}/tmp/pids/unicorn.pid" + +before_fork do |server, worker| + ActiveRecord::Base.connection.disconnect! + + old_pid = "#{server.config[:pid]}.oldbin" + if File.exists?(old_pid) && server.pid != old_pid + begin + Process.kill("QUIT", File.read(old_pid).to_i) + rescue Errno::ENOENT, Errno::ESRCH + # someone else did our job for us + end + end +end + +after_fork do |server, worker| + ActiveRecord::Base.establish_connection +end diff --git a/config/unicorn/unicorn.rb b/config/unicorn/unicorn.rb new file mode 100644 index 0000000..e334c9d --- /dev/null +++ b/config/unicorn/unicorn.rb @@ -0,0 +1,45 @@ +#root = "/home/ruby/apps/blog/current" +#pid "#{root}/tmp/pids/unicorn.pid" + +#listen "/tmp/unicorn.blog.sock" + +app_path = "/home/ruby/apps/blog/current" + +# Set unicorn options +worker_processes 1 +preload_app true +timeout 180 +listen 9000 #"127.0.0.1:9000" + +# Spawn unicorn master worker for user apps (group: apps) +user 'ruby', 'ruby' + +# Fill path to your app +working_directory app_path + +# Should be 'production' by default, otherwise use other env +rails_env = ENV['RAILS_ENV'] || 'production' + +# Log everything to one file +stderr_path "log/unicorn.log" +stdout_path "log/unicorn.log" + +# Set master PID location +pid "#{app_path}/tmp/pids/unicorn.pid" + +before_fork do |server, worker| + ActiveRecord::Base.connection.disconnect! + + old_pid = "#{server.config[:pid]}.oldbin" + if File.exists?(old_pid) && server.pid != old_pid + begin + Process.kill("QUIT", File.read(old_pid).to_i) + rescue Errno::ENOENT, Errno::ESRCH + # someone else did our job for us + end + end +end + +after_fork do |server, worker| + ActiveRecord::Base.establish_connection +end