如何通过capistrano进入生产轨道控制台?

我想通过capistrano从我的本地机器进入生产服务器上的rails控制台。 我找到了一些要点,例如https://gist.github.com/813291 ,当我进入控制台时

cap production console 

我得到以下结果

 192-168-0-100:foldername username $ cap console RAILS_ENV=production * executing `console' * executing "cd /var/www/myapp/current && rails console production" servers: ["www.example.de"] [www.example.de] executing command [www.example.de] rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '1.9.3' -c 'cd /var/www/myapp/current && rails console production' /var/www/myapp/releases/20120305102218/app/controllers/users_controller.rb:3: warning: already initialized constant VERIFY_PEER Loading production environment (Rails 3.2.1) Switch to inspect mode. 

这就是它…现在我可以输入一些文字,但没有任何反应……

有人知道如何为我的问题获得该工作或其他解决方案吗?

我为这种事情添加了自己的任务:

 namespace :rails do desc "Remote console" task :console, :roles => :app do run_interactively "bundle exec rails console #{rails_env}" end desc "Remote dbconsole" task :dbconsole, :roles => :app do run_interactively "bundle exec rails dbconsole #{rails_env}" end end def run_interactively(command) server ||= find_servers_for_task(current_task).first exec %Q(ssh #{user}@#{myproductionhost} -t '#{command}') end 

我现在说cap rails:console并获得一个控制台。

对于Capistrano 3,您可以在config/deploy添加以下行:

 namespace :rails do desc 'Open a rails console `cap [staging] rails:console [server_index default: 0]`' task :console do server = roles(:app)[ARGV[2].to_i] puts "Opening a console on: #{server.hostname}...." cmd = "ssh #{server.user}@#{server.hostname} -t 'cd #{fetch(:deploy_to)}/current && RAILS_ENV=#{fetch(:rails_env)} bundle exec rails console'" puts cmd exec cmd end end 

要打开服务器列表中的第一个服务器:

 cap [staging] rails:console 

要在服务器列表中打开第二个服务器:

 cap [staging] rails:console 1 

参考: 使用Capistrano 3打开Rails控制台

需要exec来替换当前进程,否则您将无法与rails控制台进行交互。

一个简单的Capistrano 3解决方案可能是:

 namespace :rails do desc "Run the console on a remote server." task :console do on roles(:app) do |h| execute_interactively "RAILS_ENV=#{fetch(:rails_env)} bundle exec rails console", h.user end end def execute_interactively(command, user) info "Connecting with #{user}@#{host}" cmd = "ssh #{user}@#{host} -p 22 -t 'cd #{fetch(:deploy_to)}/current && #{command}'" exec cmd end end 

然后你可以在staging上调用它说: cap staging rails:console 。 玩得开心!

我也摆弄了这种方法,但后来又避免构建我自己的交互式SSH shell客户端,只是使用这个片段,我发现它只是使用了旧的SSH。 如果您正在进行一些奇怪的SSH网关代理,这可能不合适,但是对于登录到框并执行某些操作,它就像一个魅力。

根据我的经验,capistrano不能与交互式终端很好地配合使用。

如果你必须在多个终端中执行操作,我建议使用iterm,它具有“发送到所有窗口”function,对我来说效果非常好:

http://www.iterm2.com/#/section/home

我有一个有点困难的环境,这是涌入…所以bash -lc现在不是一个真正的选择。 我的解决方案类似于@Rocco,但它更加精致。

 # run a command in the `current` directory of `deploy_to` def run_interactively(command) # select a random server to run on server = find_servers_for_task(current_task).sample # cobble together a shell environment app_env = fetch("default_environment", {}).map{|k,v| "#{k}=\"#{v}\""}.join(' ') # Import the default environment, cd to the currently deployed app, run the command command = %Q(ssh -tt -i #{ssh_options[:keys]} #{user}@#{server} "env #{app_env} bash -c 'cd #{deploy_to}/current; #{command}'") puts command exec command end namespace :rails do desc "rails console on a sidekiq worker" task :console, role: :sidekiq_normal do run_interactively "bundle exec rails console #{rails_env}" end end 

对于Capistrano 3中的Rails控制台,请参阅以下要点: https : //gist.github.com/joost/9343156