2013-07-11 22 views
0

我正在使用capistrano安裝nginx以及其他服務,如Ryan Bates在此Railscasts中所述。下面的nginx.rb文件是從他的源代碼中複製的。當安裝過程到達線如何按'輸入以繼續'與capistrano的自動安裝

* 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 

回答

2

其實,有意見中正確的解決方案爲railscast http://railscasts.com/episodes/337-capistrano-recipes?view=comments,看到佈雷諾桑托斯薩爾加多問題。

一個由他提供的解決方案是

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 

你可以找到一些重構的方式來做到這一點有太多,選擇什麼適合你的最好。

+0

謝謝,我會嘗試了這一點,並接受你的答案,如果它的工作原理 – Leahcim

+0

@biomancer任何想法如何與使用該capistrano 3? – Mohamad

+0

@Mohamad我對capistrano 3沒有經驗,對不起。 – biomancer

0

使用echo有一個更簡單的方法。

Capistrano的2

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

Capistrano的3

execute 'echo | add-apt-repository ppa:nginx/stable'