如何通过capistrano进入生产环境的rails控制台?

23

我想通过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.

就是这样...现在我可以输入一些文本,但是什么也不会发生...

有没有人有办法让它工作或者另一种解决我的问题的方法?


1
也许这可以帮到你:https://dev59.com/gG855IYBdhLWcg3wbztl - Awea
9个回答

11

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

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并获得一个控制台。

5
对于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控制台交互。


3
一个简单的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。祝玩得愉快!

问题在于当服务器列表中有多个服务器时,此任务 ^^^ 将尝试在所有服务器上打开控制台。 :/ - Pablo Cantero

1
对于 Capistrano > 3.5 和 rbenv。适用于2021年的工作。
namespace :rails do
  desc "Open the rails console on one of the remote servers"
  task :console do |current_task|
    on roles(:app) do |server|
      server ||= find_servers_for_task(current_task).first
      exec %Q[ssh -l #{server.user||fetch(:user)} #{server.hostname} -p #{server.port || 22} -t 'export PATH="$HOME/.rbenv/bin:$PATH"; eval "$(rbenv init -)"; cd #{release_path}; bin/rails console -e production']
    end
  end
end

0

0

这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Alberto Anderick Jr

0
在我的经验中,Capistrano 并不适合与交互式终端一起使用。
如果你需要在多个终端执行命令,我建议使用 iTerm,它有一个“发送到所有窗口”的功能,对我来说非常好用:

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


0

我也曾尝试过那种方法,但最后决定避免构建自己的交互式SSH shell客户端,而是采用了this snippet,它只是使用了好老的SSH。如果你的SSH网关代理出了问题,这可能不太适合你,但是对于登录到一个盒子并执行一些操作,它的效果就像魔法一样。


0

我有一个有点困难的环境,它是influx...所以现在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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接