0

我正在使用 capistrano 在此 Railscasts 中由 Ryan Bates 描述的其他服务中安装 nginx。下面的 nginx.rb 文件是从他的源代码中复制而来的。安装过程到线时

* executing "sudo -p 'sudo password: ' add-apt-repository ppa:nginx/stable"

它会发出警告并要求我通过按 ENTER 或控制 c 来确认以继续。但是,由于这不是手动安装,我无法按 Enter 键继续。安装脚本被冻结,等待我无法手动输入的命令。有没有办法修改下面的 nginx.rb 文件来处理这种情况呢?

        triggering after callbacks for `deploy:install'
  * 2013-07-11 10:17:36 executing `nginx:install'
  * executing "sudo -p 'sudo password: ' add-apt-repository ppa:nginx/stable"
    servers: ["192.XXX.XXX.XXX"]
    [192.XXX.XXX.XXX] executing command
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] You are about to add the following PPA to your system:
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] Stable version of nginx.
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] The following are no longer updated past 1.2.7, due to PPA build restrictions:
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] * Maverick
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] The following will not be updated past 1.4.1, except for bugfixes which may have been missed:
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] * Lucid
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] * Natty
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] * Oneiric
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] More info: https://launchpad.net/~nginx/+archive/stable
 ** [out :: 192.XXX.XXX.XXX] 
 ** [out :: 192.XXX.XXX.XXX] Press [ENTER] to continue or ctrl-c to cancel adding it
 ** [out :: 192.XXX.XXX.XXX] 

nginx.rb

namespace :nginx do
  desc "Install latest stable release of nginx"
  task :install, roles: :web do
    run "#{sudo} add-apt-repository ppa:nginx/stable"
    run "#{sudo} apt-get -y update"
    run "#{sudo} apt-get -y install nginx"
  end
  after "deploy:install", "nginx:install"

  desc "Setup nginx configuration for this application"
  task :setup, roles: :web do
    template "nginx_unicorn.erb", "/tmp/nginx_conf"
    run "#{sudo} mv /tmp/nginx_conf /etc/nginx/sites-enabled/#{application}"
    run "#{sudo} rm -f /etc/nginx/sites-enabled/default"
    restart
  end
  after "deploy:setup", "nginx:setup"

  %w[start stop restart].each do |command|
    desc "#{command} nginx"
    task command, roles: :web do
      run "#{sudo} service nginx #{command}"
    end
  end
end
4

2 回答 2

2

实际上,该 railscast http://railscasts.com/episodes/337-capistrano-recipes?view=comments的评论中有解决方案,请参阅 Breno Santos Salgado 问题。

他提供的解决方案之一是

task :install, roles: :web do
  run "#{sudo} add-apt-repository ppa:nginx/stable",:pty => true do |ch, stream, data|
    if data =~ /Press.\[ENTER\].to.continue/
      #prompt, and then send the response to the remote process
      ch.send_data(Capistrano::CLI.password_prompt("Press enter to continue:") + "\n")
    else
      #use the default handler for all other text
      Capistrano::Configuration.default_io_proc.call(ch,stream,data)
    end
  end

  run "#{sudo} apt-get -y update"
  run "#{sudo} apt-get -y install nginx"
end

您也可以在那里找到一些重构的方法,选择最适合您的方法。

于 2013-07-13T16:49:15.647 回答
0

有一种更简单的方法使用echo.

卡皮斯特拉诺 2

run "echo | #{sudo} add-apt-repository ppa:nginx/stable"

卡皮斯特拉诺 3

execute 'echo | add-apt-repository ppa:nginx/stable'
于 2014-01-23T10:50:42.387 回答