rails项目部署服务器死了几次

时间:2014-07-06 17:07:17

标签: ruby-on-rails ruby capistrano unicorn

我使用capistrano 3.0部署rails项目

ror + nginx + unicorn

部署非常糟糕。

但服务器在独角兽重启期间死亡。

1~2秒死亡服务器..

我不知道我要做什么..

这是我的deploy.rb源..

# -*- encoding : utf-8 -*-
# config valid only for Capistrano 3.1
lock '3.1.0'

require 'unicorn'

#set :whenever_command, "bundle exec whenever"
#require "whenever/capistrano"

set :application, 'projectname'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

# Default deploy_to directory is /var/www/my_app
# set :deploy_to, '/var/www/my_app'

# Default value for :scm is :git
 set :scm, :git

set :repo_url, 'git@github.com:gitURL'
# Default value for :format is :pretty
set :branch,  'master'
set :deploy_via, :remote_cache
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :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}

# Default value for default_env is {}
set :default_env, { path: "/root/.rbenv/versions/2.1.0/lib/ruby/gems:/root/.rbenv/versions/2.1.0/bin:$PATH" }
#set :current_deploy_path, "DEPLOY_PATH"

# Default value for keep_releases is 5
# set :keep_releases, 5


set :ssh_options, { :forward_agent => true}
set :user, "user"
set :password, "passwod"


namespace :whenever do
  task :start do 
    on roles(:app) do
      execute("cd #{fetch :current_deploy_path}")
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, 'exec whenever --update-crontab'
        end
      end
    end
  end
end

namespace :deploy do

  desc 'Restart application'
  task :restart do
    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

  after :restart, :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, 'cache:clear'
      # end
    end
  end

 before 'deploy:assets:precompile', :link_assets
 task :link_assets do
    on roles(:app), :roles => :app, :except => { :no_release => true } do
      execute("ln -fs #{shared_path}/database.yml #{release_path}/config/database.yml")
    end
  end


#before 'deploy:publishing', :kill_unicorns

  task :kill_unicorns do
    on roles(:app), in: :sequence, wait: 3 do
      execute("if [ -f #{fetch :current_deploy_path}/tmp/pids/unicorn.pid ]; then kill -s QUIT `cat #{fetch :current_deploy_path}/tmp/pids/unicorn.pid`; fi")
    end
  end

  task :make_unicorn do
    on roles(:app), in: :sequence, wait: 3 do
      execute :mkdir, '-p', "#{fetch :current_deploy_path}/tmp/pids"
      execute("cd #{fetch :current_deploy_path}")
      execute("if [ -f #{shared_path}/unicorn.pid ]; then kill -s QUIT `cat #{shared_path}/unicorn.pid`; fi")
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, 'exec unicorn -D -c config/unicorn.rb -E production'
        end
      end
      execute("cp #{fetch :current_deploy_path}/tmp/pids/unicorn.pid #{shared_path}/unicorn.pid")
    end
 end


#  desc "Update the crontab file"
#  task :update_crontab do #, :roles => :db do
#    on roles(:app), in: :sequence, wait: 3 do
#      execute("cd #{release_path} && bundle exec whenever --update-crontab #{application}")
#    end
#  end

 # after 'deploy:publishing', :kill_unicorns
  after 'deploy:publishing', :make_unicorn
  after 'deploy:finishing', 'whenever:start'

end

这是我的deploy / production.rb

set :domain, "14.63.165.216"
set :rails_env, "production"
set :current_deploy_path, "/geuinea_pig/priday/current"
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
server '14.63.165.216', user: 'root', roles: %w{web app db}, primary: true#, my_property: :my_value



set :deploy_to, '/geuinea_pig/priday'
set :ssh_options, {
    user: 'root', # overrides user setting above
    #     keys: %w(/home/user_name/.ssh/id_rsa),
    forward_agent: false,
    #auth_methods: %w(publickey password),
    auth_methods: %w(password),
    password: 'dPffhdPffh1!'
}

这是在capistrano日志中部署日志

INFO [193203be] Running /usr/bin/env if [ -f /path/shared/unicorn.pid ]; then kill -s QUIT `cat /path/shared/unicorn.pid`; fi on 14.63.165.216
DEBUG [193203be] Command: if [ -f /path/shared/unicorn.pid ]; then kill -s QUIT `cat /path/shared/unicorn.pid`; fi
#---------------------------------
#THIS TIME DIED SERVER 2~3 second 
#---------------------------------
INFO [193203be] Finished in 0.227 seconds with exit status 0 (successful).
DEBUG [05a0c865] Running /usr/bin/env if test ! -d /path/releases/20140706164800; then echo "Directory does not exist '/path/releases/20140706164800'" 1>&2; false; fi on 14.63.165.216
DEBUG [05a0c865] Command: if test ! -d /path/releases/20140706164800; then echo "Directory does not exist '/path/releases/20140706164800'" 1>&2; false; fi
DEBUG [05a0c865] Finished in 0.229 seconds with exit status 0 (successful).
INFO [3cf3c052] Running /usr/bin/env bundle exec unicorn -D -c config/unicorn.rb -E production on 14.63.165.216
DEBUG [3cf3c052] Command: cd /path/releases/20140706164800 && ( PATH=/root/.rbenv/versions/2.1.0/lib/ruby/gems:/root/.rbenv/versions/2.1.0/bin:$PATH RAILS_ENV=production /usr/bin/env bundle exec unicorn -D -c config/unicorn.rb -E production )
INFO [3cf3c052] Finished in 1.061 seconds with exit status 0 (successful).
INFO [07718210] Running /usr/bin/env cp /path/current/tmp/pids/unicorn.pid /path/shared/unicorn.pid on 14.63.165.216
DEBUG [07718210] Command: cp /path/current/tmp/pids/unicorn.pid /path/shared/unicorn.pid
INFO [07718210] Finished in 0.197 seconds with exit status 0 (successful).

1 个答案:

答案 0 :(得分:0)

如果你不熟悉Capistrano,我会建议你开始慢一点?我发现它具有挑战性。事实上,我认为你超越了我。有很多小问题可能出错,日志/输出可能有点压倒性,我尝试了一些更简单的上限任务。我在我的部署用户的主目录下创建了一个奇怪的目录,然后运行它:

task :dir_exists do
  ask(:dir_to_test, "$home")
  on roles(:all) do |host|
    if test("[ -d #{fetch(:dir_to_test)} ]")
      info "Phew, it's ok, the directory exists!"
    else
      info "Directory #{fetch(:dir_to_test)} does not exist"
    end
  end
end

如果运行,至少我知道我正在与服务器通信并且可以运行。我起初遇到的大问题主要与身份验证有关。我没有得到整个ssh-add的东西。即使是昨天我也不得不在我的数据库上运行一些新的命令,我花了大约两个小时来弄清楚我的语法是完整的废话。

无论如何,你可以更具体地了解出了什么问题,提供的帮助就越多。顺便问一下,你真的有一个名为" / geuinea_pig / friday / current"的顶级目录吗?如果所有这些都是capistrano,可能会保存这些文件并开始更简单?我这样说是因为我的部署更简单,也许你不需要这一切?此外,在本地计算机上没有反击执行(你有反引号cat /path/shared/unicorn.pid反引号)?

无论如何,祝你好运!