【问题标题】:How to enter rails console on production via capistrano?如何通过 capistrano 进入 Rails 控制台进行生产?
【发布时间】:2012-03-23 01:37:45
【问题描述】:

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

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

有人知道如何完成这项工作或解决我的问题吗?

【问题讨论】:

标签: ruby-on-rails console capistrano


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        我有一个有点困难的环境,那就是涌入......所以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
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            一个简单的 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
            

            然后您可以在暂存时将其称为:cap staging rails:console。玩得开心!

            【讨论】:

            • 问题是当您在服务器列表中有多个服务器时,此任务^^^ 将尝试在所有服务器中打开控制台。 ://
            【解决方案7】:

            对于 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 
            

            参考:Opening a Rails console with Capistrano 3

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

            【讨论】:

              【解决方案8】:

              对于 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
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2010-12-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-02-15
                • 1970-01-01
                • 2016-04-03
                相关资源
                最近更新 更多