如何使用Rails 4正确设置Capistrano 3?

时间:2015-02-24 10:20:29

标签: ruby-on-rails ruby ruby-on-rails-4 nginx capistrano3

我试图为一个Rails 4应用程序安装Capistrano 3的2天,我的头发已经变成了灰色...

问题是我无法将代码部署到服务器(Ubuntu 14,nginx,在DigitalOcean上运行)。

这是我的配置: Gemfile

gem 'capistrano', '~> 3.1.0'
gem 'capistrano-rails', '~> 1.1.0'
gem 'capistrano-bundler'
gem 'unicorn'

Capfile

require 'capistrano/setup'
require 'capistrano/deploy'

 require 'capistrano/bundler'
 require 'capistrano/rails/assets'
 require 'capistrano/rails/migrations'

# Load custom tasks from `lib/capistrano/tasks' if you have any defined
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }                                                                                                                                      
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }

deploy.rb

set :application, 'project'
set :deploy_user, 'deployer'

set :scm, :git
set :repo_url, 'git@bitbucket.org:username/project.git"'

set :pty, true
set :use_sudo, false

set :keep_releases, 5
set :linked_files, %w{config/database.yml}

set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

set(:symlinks, [
  {
    source: "nginx.conf",
    link: "/etc/nginx/sites-enabled/#{fetch(:full_app_name)}"
  },
  {
    source: "unicorn_init.sh",
    link: "/etc/init.d/unicorn_#{fetch(:full_app_name)}"
  },
  {
    source: "log_rotation",
   link: "/etc/logrotate.d/#{fetch(:full_app_name)}"
  },
  {
    source: "monit",
    link: "/etc/monit/conf.d/#{fetch(:full_app_name)}.conf"
  }
])

namespace :deploy do
  before :deploy, "deploy:check_revision"
  before :deploy, "deploy:run_tests"
  #after 'deploy:symlink:shared', 'deploy:compile_assets_locally'
  after :finishing, 'deploy:cleanup'
  before 'deploy:setup_config', 'nginx:remove_default_vhost'
  after 'deploy:setup_config', 'nginx:reload'
  after 'deploy:setup_config', 'monit:restart'
  after 'deploy:publishing', 'deploy:restart'
end

config/deploy/production.rb

set :stage, :production
set :branch, "master"

set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"
set :server_name, "IP"

server 'IP', user: 'deployer', roles: %w{web app db}, primary: true
set :deploy_to, "/home/#{fetch(:deploy_user)}/apps/#{fetch(:full_app_name)}"

set :rails_env, :production
set :unicorn_worker_count, 5
set :enable_ssl, false

当我尝试设置capistrano并运行cap production deploy:setup_config时,我得到了这个输出:

/Users/adam/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.6.5/lib/bundler.rb:301: warning: Insecure world writable dir /usr/local/mysql-5.6.13-osx10.7-x86_64 in PATH, mode 040777
DEBUG [d940e414] Running /usr/bin/env [ -f /etc/nginx/sites-enabled/default ] as deployer@IP
DEBUG [d940e414] Command: [ -f /etc/nginx/sites-enabled/default ]
Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text.
deployer@188.166.53.247's password: # HERE I PUT MY PASSWORD, BUT WHEN I TYPE IT, I SEE WHAT I TYPE (THE PASSWORD IS NOT HIDDEN)
DEBUG [d940e414] Finished in 28.324 seconds with exit status 0 (successful).
INFO [cba64b02] Running /usr/bin/env sudo rm /etc/nginx/sites-enabled/default as deployer@IP
DEBUG [cba64b02] Command: /usr/bin/env sudo rm /etc/nginx/sites-enabled/default
DEBUG [cba64b02]    [sudo] password for deployer:

这里卡住了 - 什么也没发生。无论我做什么(如果我输入密码/密码) - 它都会在这里停留超过30分钟。

我可以请你帮我解决这个问题,伙计们?

非常感谢您提前,我不知道如何继续。

1 个答案:

答案 0 :(得分:0)

nginx:remove_default_vhost尝试使用deploy:setup_config执行rm之前,您配置了sudo任务。 Capistrano 3需要设置无密码的sudo,否则你会得到你描述的症状。请参阅documentation的授权部分。

deployer用户rm设置为sudo而不使用密码让事情发生变化,但在此之前,我认为值得一提:您真的希望您的部署用户能够以root身份删除任意文件而无需密码吗?我不清楚该任务的起源是什么,但恕我直言, privisioning 部署更多,而且还有其他工具比capistrano更适合配置(厨师,傀儡等)。或者只是SSH到您的服务器并将其删除。它只需要完成一次。

如果你决定让你的部署者用户在没有密码的情况下使用sudo执行rm,请记得使用visudo并且不要直接编辑/etc/sudoers