在Capistrano处理输入提示的正确方法?

时间:2013-03-03 12:15:12

标签: shell automation capistrano apt-get

处理由我通过Capistrano运行的命令触发的输入提示的正确方法是什么?

一个例子是我使用iptables-persistent安装的aptitude包。尽管有--no-gui标志,但仍然会出现提示,要求我确认我希望如何配置。

有没有办法通过命令行传递参数来避免这样的提示?

1 个答案:

答案 0 :(得分:0)

我发现并且能够从以下方法实现这个非常有用的handle_command_with_input方法:

https://github.com/nesquena/cap-recipes/blob/master/lib/cap_recipes/tasks/utilities.rb

def handle_command_with_input(local_run_method, shell_command, input_query, response=nil)
send(local_run_method, shell_command, {:pty => true}) do |channel, stream, data|
  if data =~ input_query
    if response
      logger.info "#{data} #{"*"*(rand(10)+5)}", channel[:host]
      channel.send_data "#{response}\n"
    else
      logger.info data, channel[:host]
      response = ::Capistrano::CLI.password_prompt "#{data}"
      channel.send_data "#{response}\n"
    end
  else
    logger.info data, channel[:host]
  end
end
end

这些代码都不是我的。 Gracias a Nesquena。