How to enter rails console on production via capis

2019-03-14 15:09发布

I want to enter the rails console on production server from my local machine via capistrano. I found some gists, e.g. https://gist.github.com/813291 and when I enter console via

cap production console 

I get the following result

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.

and thats it... Now I can enter some text, but nothing happens...

Has anybody an idea how to get that work or another solution for my problem?

7条回答
做自己的国王
2楼-- · 2019-03-14 15:50

In my experience, capistrano isn't built to work very well with interactive terminals.

If you have to execute things in multiple terminals, I'd suggest iterm, which has a "send to all windows" function that works very well for me:

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

查看更多
太酷不给撩
3楼-- · 2019-03-14 15:51

I have a somewhat difficult environment, which is influx ... So bash -lc isn't really an option right now. My solution is similar to @Rocco, but it's a bit more refined.

# 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
查看更多
Juvenile、少年°
4楼-- · 2019-03-14 16:05

For a Rails console in Capistrano 3 see this gist: https://gist.github.com/joost/9343156

查看更多
【Aperson】
5楼-- · 2019-03-14 16:06

I have fiddled with that approach as well, but then resorted to avoiding building my own interactive SSH shell client and just went with this snippet I found that simply uses good old SSH. This might not be suitable if you have some weird SSH gateway proxying going on, but for logging into a box and performing some operations, it works like a charm.

查看更多
Evening l夕情丶
6楼-- · 2019-03-14 16:13

For Capistrano 3 you can add these lines in your 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

To open the first server in the servers list:

cap [staging] rails:console 

To open the second server in the servers list:

cap [staging] rails:console 1 

Reference: Opening a Rails console with Capistrano 3

exec is needed to replace the current process, otherwise you will not be able to interact with the rails console.

查看更多
相关推荐>>
7楼-- · 2019-03-14 16:15

I've added my own tasks for this kind of thing:

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

I now say cap rails:console and get a console.

查看更多
登录 后发表回答